Path: redb/redb.Tests.Integration/Tests/Base/FilterFieldPathTestsBase.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/FilterFieldPathTestsBase.cs ---- using System.Linq.Expressions; using redb.Core; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// The field path of a filter is a chain of fields of the queried object. A method call inside the path is refused with /// one message, and so is an array or list element by index on the free providers: the parser used to drop the part /// before them, so Items[0].Name == "top" filtered by the field Name of the object itself and returned it. /// redb Pro reads the element (ElementIndexFilterTestsBase has the full set). /// The paths that are supported keep answering: a field, a field of a nested class, Nullable Value and HasValue, /// Length and Count, a dictionary entry by key (Contains over an array of simple values and Any over an array of list /// items have their own classes: ArrayTestsBase, ListTestsBase). /// /// One object per test, read by its id: Name "top", Rank 1; Items[0] and Parts[0] have Name "inner", Rank 7. /// public abstract class FilterFieldPathTestsBase { protected readonly IRedbService Redb; protected FilterFieldPathTestsBase(IRedbService redb) => Redb = redb; private async Task> SeedAsync() { await Redb.SyncSchemeAsync(); var obj = new RedbObject { name = "fp-" + Guid.NewGuid().ToString("N")[..8], Props = new FieldPathProps { Name = "top", Rank = 1, Items = [new FieldPathItem { Name = "inner", Rank = 7 }], Parts = [new FieldPathItem { Name = "inner", Rank = 7 }], ByKey = new Dictionary { ["k"] = new() { Name = "entry", Rank = 5 } }, Main = new FieldPathItem { Name = "main", Rank = 3, Score = 4 }, Scores = new Dictionary { ["a"] = 1, ["n"] = null } } }; var id = await Redb.SaveAsync(obj); return Redb.Query().WhereRedb(o => o.Id == id); } /// redb Pro reads an element by index in a filter; the free providers refuse it. protected virtual bool ReadsElementsByIndex => false; public static IEnumerable ElementPaths() { // Each of these compared the top-level field of the same name and returned the object. The free providers // refuse them; redb Pro reads the element, whose Name is "inner", Rank 7 and Score null. yield return ["array-index", (Expression>)(p => p.Items![0].Name == "top"), "Items", false]; yield return ["list-index", (Expression>)(p => p.Parts![0].Rank == 1), "Parts", false]; yield return ["array-index-null", (Expression>)(p => p.Items![0].Score == null), "Items", true]; yield return ["array-index-string-method", (Expression>)(p => p.Items![0].Name.StartsWith("to")), "Items", false]; yield return ["array-index-element-field", (Expression>)(p => p.Items![0].Name == "inner"), "Items", true]; } [Theory] [MemberData(nameof(ElementPaths))] public async Task ElementByIndex_IsReadInPro_AndRefusedInFree( string kind, Expression> predicate, string named, bool matchesInPro) { var q = await SeedAsync(); if (ReadsElementsByIndex) { (await q.Where(predicate).ToListAsync()).Should().HaveCount(matchesInPro ? 1 : 0, $"case {kind}"); return; } var act = () => q.Where(predicate).ToListAsync(); var ex = (await act.Should().ThrowAsync($"case {kind}")).Which; ex.Message.Should().Contain(named).And.Contain("Contains(").And.Contain("Any(").And.Contain("dictionary"); } public static IEnumerable RefusedPaths() { yield return ["first", (Expression>)(p => p.Items!.First().Name == "top"), "First()"]; // Not a field of the queried object under the dictionary or the string method: refused with the same // NotSupportedException and message (these three threw ArgumentException with wordings of their own). yield return ["contains-key-not-on-a-field", (Expression>)(p => (p.ByKey ?? new Dictionary()).ContainsKey("k")), "ByKey"]; yield return ["dictionary-indexer-not-on-a-field", (Expression>)(p => (p.ByKey ?? new Dictionary())["k"].Name == "entry"), "ByKey"]; yield return ["string-method-not-on-a-field", (Expression>)(p => (p.Main ?? new FieldPathItem()).Name.StartsWith("ma")), "Main"]; yield return ["dictionary-entry-not-on-a-field", (Expression>)(p => (p.ByKey ?? new Dictionary())["k"] != null), "ByKey"]; } [Theory] [MemberData(nameof(RefusedPaths))] public async Task ElementByIndexOrMethodCallInThePath_IsRefused_NamingThePathAndTheWayOut( string kind, Expression> predicate, string named) { var q = await SeedAsync(); var act = () => q.Where(predicate).ToListAsync(); var ex = (await act.Should().ThrowAsync($"case {kind}")).Which; ex.Message.Should().Contain(named).And.Contain("Contains(").And.Contain("Any(").And.Contain("dictionary"); } public static IEnumerable SupportedPaths() { yield return ["field", (Expression>)(p => p.Name == "top"), true]; yield return ["field-miss", (Expression>)(p => p.Name == "inner"), false]; yield return ["nested-class", (Expression>)(p => p.Main!.Name == "main"), true]; yield return ["nested-class-miss", (Expression>)(p => p.Main!.Name == "top"), false]; yield return ["nullable-value", (Expression>)(p => p.Main!.Score!.Value == 4), true]; yield return ["nullable-has-value", (Expression>)(p => p.Main!.Score.HasValue), true]; yield return ["string-length", (Expression>)(p => p.Name.Length == 3), true]; // A field of a dictionary entry without a value (the entry's Score is null). yield return ["dictionary-entry-field-null", (Expression>)(p => p.ByKey!["k"].Score == null), true]; yield return ["dictionary-entry-field-not-null", (Expression>)(p => p.ByKey!["k"].Score != null), false]; yield return ["array-length", (Expression>)(p => p.Items!.Length == 1), true]; yield return ["list-count", (Expression>)(p => p.Parts!.Count == 1), true]; yield return ["dictionary-entry", (Expression>)(p => p.ByKey!["k"].Name == "entry"), true]; yield return ["dictionary-entry-miss", (Expression>)(p => p.ByKey!["k"].Name == "top"), false]; yield return ["coalesce-then-string-function", (Expression>)(p => (p.Name ?? "").ToLower() == "top"), true]; // Nullable Value and HasValue over a dictionary entry: the path is the entry, not a member chain. yield return ["dictionary-nullable-value", (Expression>)(p => p.Scores!["a"].Value == 1), true]; yield return ["dictionary-nullable-value-miss", (Expression>)(p => p.Scores!["a"].Value == 2), false]; yield return ["dictionary-nullable-has-value", (Expression>)(p => p.Scores!["a"].HasValue), true]; yield return ["dictionary-nullable-has-value-null", (Expression>)(p => p.Scores!["n"].HasValue), false]; yield return ["dictionary-nullable-not-null-null", (Expression>)(p => p.Scores!["n"] != null), false]; yield return ["dictionary-nullable-is-null-null", (Expression>)(p => p.Scores!["n"] == null), true]; yield return ["dictionary-nullable-not-has-value-null", (Expression>)(p => !p.Scores!["n"].HasValue), true]; } public static IEnumerable RefusedOrderingPaths() { // Each of these sorted by the top-level field of the same name, or sent an empty path. The key keeps its own // type, as in an application (a boxed key is a conversion, refused for another reason). yield return ["array-index", (Func, Task>)(q => q.OrderBy(p => p.Items![0].Name).ToListAsync()), "Items"]; yield return ["dictionary-entry-field", (Func, Task>)(q => q.OrderBy(p => p.ByKey!["k"].Name).ToListAsync()), "ByKey"]; yield return ["dictionary-nullable-value", (Func, Task>)(q => q.OrderBy(p => p.Scores!["a"]!.Value).ToListAsync()), "Scores"]; } [Theory] [MemberData(nameof(RefusedOrderingPaths))] public async Task OrderBy_ThroughAnEntryOrAnElement_IsRefused_NamingThePath( string kind, Func, Task> sort, string named) { var q = await SeedAsync(); var act = () => sort(q); var ex = (await act.Should().ThrowAsync($"case {kind}")).Which; ex.Message.Should().Contain(named); } [Theory] [MemberData(nameof(SupportedPaths))] public async Task SupportedPath_KeepsAnswering(string kind, Expression> predicate, bool matches) { var q = await SeedAsync(); var rows = await q.Where(predicate).ToListAsync(); rows.Should().HaveCount(matches ? 1 : 0, $"case {kind}"); } }