Path: redb/redb.Tests.Integration/Tests/Base/ElementIndexFilterTestsBase.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/ElementIndexFilterTestsBase.cs ---- using System.Linq.Expressions; using redb.Core; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// A filter of redb Pro reads an element of an array or a list by its index: the element itself (Levels[0] == 5) /// or one field of an element of a class (Items[0].Name == v). An element past the end is no value, as a missing /// dictionary key or a missing field is: == and != leave the object out, == null takes it. The /// expected answers are written out per case (LINQ to objects would throw on an index past the end). /// /// Four objects, Marker o0..o3; the object's own Name and Rank differ from every element's: /// o0: Levels [5, 1], Tags [a, b], Items/Parts [(x0, 1, null), (y0, 2, 3)], Statuses [Admin, User]; /// o1: Levels [1, 5], Tags [b, a], Items/Parts [(y1, 2, 4), (x1, 1, null)], Statuses [User]; /// o2: Levels [5], Tags [a], Items/Parts [(x2, 3, 7)], Statuses []; /// o3: no arrays. /// public abstract class ElementIndexFilterTestsBase { protected readonly IRedbService Redb; protected ElementIndexFilterTestsBase(IRedbService redb) => Redb = redb; private async Task<(string Tag, RedbList List)> SeedAsync() { await Redb.SyncSchemeAsync(); var tag = "ei-" + Guid.NewGuid().ToString("N")[..8]; var list = await Redb.ListProvider.SaveListAsync(RedbList.Create(tag, tag)); var items = await Redb.ListProvider.AddItemsAsync(list, ["Admin", "User"]); var admin = items.First(i => i.Value == "Admin"); var user = items.First(i => i.Value == "User"); static ElementIndexItem[] Elements(params (string Name, int Rank, int? Score)[] e) => e.Select(x => new ElementIndexItem { Name = x.Name, Rank = x.Rank, Score = x.Score }).ToArray(); var props = new[] { new ElementIndexProps { Levels = [5, 1], Tags = ["a", "b"], Items = Elements(("x0", 1, null), ("y0", 2, 3)), Statuses = [admin, user] }, new ElementIndexProps { Levels = [1, 5], Tags = ["b", "a"], Items = Elements(("y1", 2, 4), ("x1", 1, null)), Statuses = [user] }, new ElementIndexProps { Levels = [5], Tags = ["a"], Items = Elements(("x2", 3, 7)), Statuses = [] }, new ElementIndexProps() }; for (var i = 0; i < props.Length; i++) { props[i].Marker = tag; props[i].Name = "own"; props[i].Rank = 9; props[i].Parts = props[i].Items?.Select(e => new ElementIndexItem { Name = e.Name, Rank = e.Rank, Score = e.Score }).ToList(); await Redb.SaveAsync(new RedbObject { name = $"o{i}", Props = props[i] }); } return (tag, list); } private async Task NamesAsync(string tag, Expression> filter) => (await Redb.Query().Where(p => p.Marker == tag).Where(filter).ToListAsync()) .Select(o => o.name!).OrderBy(n => n).ToArray(); private async Task CleanupAsync(RedbList list) { var items = await Redb.ListProvider.GetListItemsAsync(list.Id); await Redb.Context.Bulk.BulkDeleteValuesByListItemIdsAsync(items.Select(i => i.Id).ToList()); await Redb.ListProvider.DeleteListAsync(list.Id); } public static IEnumerable ReadCases() { static object[] Case(string kind, Expression> filter, params string[] names) => [kind, filter, names]; // The element itself: an array and a list of simple values. yield return Case("array-first", p => p.Levels![0] == 5, "o0", "o2"); yield return Case("array-second", p => p.Levels![1] == 5, "o1"); yield return Case("array-range", p => p.Levels![0] > 1, "o0", "o2"); yield return Case("list-first", p => p.Tags![0] == "a", "o0", "o2"); yield return Case("list-second-not-equal", p => p.Tags![1] != "a", "o0"); // A field of an element of a class: an array and a list. yield return Case("array-field", p => p.Items![0].Name == "x0", "o0"); yield return Case("array-field-second", p => p.Items![1].Rank == 1, "o1"); yield return Case("array-field-range", p => p.Items![0].Rank > 1, "o1", "o2"); yield return Case("list-field", p => p.Parts![0].Name == "y1", "o1"); yield return Case("list-field-second", p => p.Parts![1].Name == "y0", "o0"); yield return Case("field-string-function", p => p.Items![0].Name.StartsWith("x"), "o0", "o2"); yield return Case("field-nullable-value", p => p.Items![0].Score!.Value == 7, "o2"); yield return Case("field-and-own-field", p => p.Items![0].Rank == 1 && p.Rank == 9, "o0"); yield return Case("two-elements", p => p.Items![0].Rank == 2 && p.Items![1].Rank == 1, "o1"); // A missing value: a null field of an element, an element past the end. One rule for both, as for a missing // dictionary key: no value matches neither == nor !=, and == null takes it. yield return Case("field-null", p => p.Items![0].Score == null, "o0", "o3"); yield return Case("field-not-null", p => p.Items![0].Score != null, "o1", "o2"); yield return Case("past-end-equal", p => p.Items![5].Name == "x0"); yield return Case("past-end-not-equal", p => p.Items![5].Name != "x0"); yield return Case("past-end-null", p => p.Items![5].Name == null, "o0", "o1", "o2", "o3"); yield return Case("second-missing-not-equal", p => p.Items![1].Name != "x1", "o0"); yield return Case("second-missing-null", p => p.Items![1].Name == null, "o2", "o3"); yield return Case("element-past-end-null", p => p.Tags![2] == null, "o0", "o1", "o2", "o3"); yield return Case("element-missing-null", p => p.Tags![1] == null, "o2", "o3"); yield return Case("class-element-present", p => p.Items![1] != null, "o0", "o1"); yield return Case("class-element-missing", p => p.Items![1] == null, "o2", "o3"); yield return Case("element-missing-not-equal", p => p.Levels![1] != 5, "o0"); } [Theory] [MemberData(nameof(ReadCases))] public async Task ElementByIndex_IsRead(string kind, Expression> filter, string[] expected) { var (tag, list) = await SeedAsync(); try { (await NamesAsync(tag, filter)).Should().Equal(expected.OrderBy(n => n), $"case {kind}"); } finally { await CleanupAsync(list); } } /// /// The same null rule when the element field is the only Props condition (the objects picked by id): SQL Server Pro /// without the planner read such a filter from the entries that have a value under them and answered no object. /// [Fact] public async Task NullCheck_OnTheOnlyPropsCondition_TakesTheObjectsWithoutAValue() { var (tag, list) = await SeedAsync(); try { var ids = (await Redb.Query().Where(p => p.Marker == tag).ToListAsync()).Select(o => o.id).ToList(); async Task Names(Expression> filter) => (await Redb.Query().WhereInRedb(o => o.Id, ids).Where(filter).ToListAsync()) .Select(o => o.name!).OrderBy(n => n).ToArray(); (await Names(p => p.Items![1].Name == null)).Should().Equal("o2", "o3"); (await Names(p => p.Items![0].Score == null)).Should().Equal("o0", "o3"); (await Names(p => p.Items![1].Name != "x1")).Should().Equal("o0"); (await Names(p => p.Items![1].Name == "x1")).Should().Equal("o1"); } finally { await CleanupAsync(list); } } [Fact] public async Task Index_FromAVariable_IsRead() { var (tag, list) = await SeedAsync(); try { var second = 1; (await NamesAsync(tag, p => p.Items![second].Rank == 1)).Should().Equal("o1"); (await NamesAsync(tag, p => p.Levels![second] == 5)).Should().Equal("o1"); } finally { await CleanupAsync(list); } } public static IEnumerable RefusedCases() { static object[] Case(string kind, Expression> filter) => [kind, filter]; // An element that is a list item is not read by index: Any(s => s.Value == v) asks for it in any position. yield return Case("list-item-element", p => p.Statuses![0].Value == "Admin"); yield return Case("negative-index", p => p.Items![-1].Name == "x0"); yield return Case("index-from-the-object", p => p.Items![p.Rank].Name == "x0"); yield return Case("nested-class-in-element", p => p.Items![0].Inner!.Name == "x"); yield return Case("array-in-element", p => p.Items![0].Marks![0] == 1); yield return Case("first", p => p.Items!.First().Name == "x0"); yield return Case("element-at", p => p.Items!.ElementAt(0).Name == "x0"); } [Theory] [MemberData(nameof(RefusedCases))] public async Task ElementPathItCannotRead_IsRefused(string kind, Expression> filter) { await Redb.SyncSchemeAsync(); var act = () => Redb.Query().Where(filter).ToListAsync(); var ex = (await act.Should().ThrowAsync($"case {kind}")).Which; ex.Message.Should().StartWith("A query cannot read", $"case {kind}"); } [Fact] public async Task SortByAnElement_IsRefused() { await Redb.SyncSchemeAsync(); var act = () => Redb.Query().OrderBy(p => p.Items![0].Rank).ToListAsync(); await act.Should().ThrowAsync(); } }