Path: redb/redb.Tests.Integration/Tests/Base/BugRegressionTestsBase.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/BugRegressionTestsBase.cs ---- using redb.Core; using redb.Core.Models.Users; using redb.Core.Query.Aggregation; using redb.Tests.Integration.Helpers; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// Regression suite for bugs surfaced by redb.Examples cases /// E110, E111, E142, E150 and E200. /// /// All tests are written so that fixing the corresponding bug makes the /// test pass; a failing test is a real regression that has to be fixed /// either in core (parser / projection extractor) or in the provider /// (PVT SQL / Pro inline SQL). /// /// Targets: /// * E110 — WhereHasAncestor over a single scheme tree; /// * E111 — WhereHasDescendant over a single scheme tree; /// * E142 — GroupByArray with an outer Where filter /// (free PG used to RAISE EXCEPTION, now compiled /// via pvt_build_query_sql → IN-list); /// * E150 — array aggregation via Agg.Sum(arr.Select(s => s)) /// and the indexed variant Agg.Sum(arr[0]); /// * E200 — string concatenation inside ToLower().Contains(...), /// currently broken in . /// public abstract class BugRegressionTestsBase { protected readonly IRedbService Redb; protected BugRegressionTestsBase(IRedbService redb) => Redb = redb; private async Task SeedEmployeesAsync() => await TestDataFactory.SeedEmployees(Redb, 20); /// /// Tree: Company(1_000_000) -> Engineering(500_000) -> Backend(200_000) /// -> Frontend(150_000) /// -> Marketing(300_000) /// private async Task<(long root, long eng, long backend, long frontend, long marketing)> SeedTreeAsync() { var root = TestDataFactory.CreateTreeNode("Company", "BUG-ROOT", 1_000_000m); root.id = await Redb.SaveAsync(root); var eng = TestDataFactory.CreateTreeNode("Engineering", "BUG-ENG", 500_000m); eng.id = await Redb.CreateChildAsync(eng, root); var backend = TestDataFactory.CreateTreeNode("Backend", "BUG-BE", 200_000m); backend.id = await Redb.CreateChildAsync(backend, eng); var frontend = TestDataFactory.CreateTreeNode("Frontend", "BUG-FE", 150_000m); frontend.id = await Redb.CreateChildAsync(frontend, eng); var marketing = TestDataFactory.CreateTreeNode("Marketing", "BUG-MKT", 300_000m); marketing.id = await Redb.CreateChildAsync(marketing, root); return (root.id, eng.id, backend.id, frontend.id, marketing.id); } // ────────────────────────────────────────────────────────────────── // E110 — WhereHasAncestor // ────────────────────────────────────────────────────────────────── [Fact] public async Task E110_WhereHasAncestor_ReturnsDescendantsOfMatchingAncestor() { var (root, eng, be, fe, mkt) = await SeedTreeAsync(); // Only the root (1_000_000) has Budget > 500_000. // Its descendants are: eng, be, fe, mkt — 4 nodes (root itself excluded). var ids = await Redb.TreeQuery() .WhereHasAncestor(anc => anc.Budget > 500_000m) .ToListAsync(); var seeded = new[] { root, eng, be, fe, mkt }; var hits = ids.Where(o => seeded.Contains(o.id)).Select(o => o.id).ToHashSet(); hits.Should().NotContain(root, "root has no ancestor with Budget > 500k"); hits.Should().Contain(new[] { eng, be, fe, mkt }, "all nodes under root must inherit it"); } // ────────────────────────────────────────────────────────────────── // E111 — WhereHasDescendant // ────────────────────────────────────────────────────────────────── [Fact] public async Task E111_WhereHasDescendant_ReturnsAncestorsOfMatchingDescendant() { var (root, eng, be, fe, mkt) = await SeedTreeAsync(); // All non-root nodes (eng, be, fe, mkt) have Budget > 100_000. // Ancestors of any of them are: root and eng (root is ancestor of all four; // eng is ancestor of be & fe). var ids = await Redb.TreeQuery() .WhereHasDescendant(desc => desc.Budget > 100_000m) .ToListAsync(); var seeded = new[] { root, eng, be, fe, mkt }; var hits = ids.Where(o => seeded.Contains(o.id)).Select(o => o.id).ToHashSet(); hits.Should().Contain(new[] { root, eng }, "root and eng have qualifying descendants"); hits.Should().NotContain(new[] { be, fe, mkt }, "leaves have no descendants"); } // ────────────────────────────────────────────────────────────────── // E142 — GroupByArray with outer Where (filter on parent object) // ────────────────────────────────────────────────────────────────── [Fact] public async Task E142_GroupByArray_WithOuterWhere_ReturnsFilteredGroups() { await SeedEmployeesAsync(); // Outer filter: only employees that actually have a Contacts array // populated. Pre-fix this raised // P0001: pvt_build_array_groupby_sql: outer p_filter is not yet supported // After fix the filter compiles via pvt_build_query_sql and is applied // before unnest. var results = await Redb.Query() .Where(e => e.Contacts != null) .GroupByArray(e => e.Contacts!, c => c.Type) .SelectAsync(g => new { Type = g.Key, Count = Agg.Count(g) }); results.Should().NotBeEmpty("outer filter is satisfied by every seeded employee"); results.Select(r => r.Type).Should().Contain("email"); results.Select(r => r.Type).Should().Contain("phone"); results.Should().AllSatisfy(r => { r.Type.Should().NotBeNullOrEmpty(); r.Count.Should().BeGreaterThan(0); }); } [Fact] public async Task E142_GroupByArray_WithOuterWhere_FilterDropsAll() { await SeedEmployeesAsync(); // Outer filter that matches nothing — the SQL must still compile and // return an empty set rather than raising or returning all rows. var results = await Redb.Query() .Where(e => e.FirstName == "__NoSuchEmployee__") .GroupByArray(e => e.Contacts!, c => c.Type) .SelectAsync(g => new { Type = g.Key, Count = Agg.Count(g) }); results.Should().BeEmpty("outer filter matches no parent objects"); } // ────────────────────────────────────────────────────────────────── // E142.b — GroupByArray with HAVING on aggregated element column. // Each seeded employee has exactly 2 contacts (email + phone), so the // total count per Type group is 20 (one per employee). A HAVING // > 0 keeps both, HAVING > 10 keeps both, HAVING > 25 keeps none. // ────────────────────────────────────────────────────────────────── [Fact] public async Task E142b_GroupByArray_HavingCount_KeepsAllGroups() { await SeedEmployeesAsync(); var results = await Redb.Query() .GroupByArray(e => e.Contacts!, c => c.Type) .Having(g => Agg.Count(g) > 10) .SelectAsync(g => new { Type = g.Key, Count = Agg.Count(g) }); results.Select(r => r.Type).Should().Contain(new[] { "email", "phone" }); results.Should().AllSatisfy(r => r.Count.Should().BeGreaterThanOrEqualTo(20)); } [Fact] public async Task E142b_GroupByArray_HavingCount_DropsAllGroups() { await SeedEmployeesAsync(); var results = await Redb.Query() .GroupByArray(e => e.Contacts!, c => c.Type) .Having(g => Agg.Count(g) > 1_000_000) .SelectAsync(g => new { Type = g.Key, Count = Agg.Count(g) }); results.Should().BeEmpty(); } // ────────────────────────────────────────────────────────────────── // E142.c — GroupByArray with outer Where on a Props field // (FirstName == seeded value). Expected: 2 groups (email + phone), // each with Count == 1 because only one employee matched the filter. // ────────────────────────────────────────────────────────────────── [Fact] public async Task E142c_GroupByArray_WithOuterPropsWhere_GroupsMatchEmployees() { await SeedEmployeesAsync(); // Tests share the same DB; account for accumulated rows by computing // the matched-employee count dynamically. var matched = (await Redb.Query() .Where(e => e.FirstName == "Employee005") .ToListAsync()).Count; matched.Should().BeGreaterThan(0); var results = await Redb.Query() .Where(e => e.FirstName == "Employee005") .GroupByArray(e => e.Contacts!, c => c.Type) .SelectAsync(g => new { Type = g.Key, Count = Agg.Count(g) }); results.Should().HaveCount(2); results.Select(r => r.Type).Should().BeEquivalentTo(new[] { "email", "phone" }); results.Should().AllSatisfy(r => r.Count.Should().Be(matched), "each contact-type group must contain exactly one row per matched employee"); } // ────────────────────────────────────────────────────────────────── // E142.d — GroupByArray with multi-aggregation selector // (Count + Sum/Avg over a numeric element field is not possible // on Contacts; we use Skills array which contains strings only, // so we stick to Count + key, and additionally validate that an // outer Where on base "id" combined with HAVING compiles & runs.) // ────────────────────────────────────────────────────────────────── [Fact] public async Task E142d_GroupByArray_OuterWhereAndHaving_Combined() { await SeedEmployeesAsync(); var matched = await Redb.Query() .Where(e => e.IsRemote == true) .ToListAsync(); matched.Should().NotBeEmpty(); var results = await Redb.Query() .Where(e => e.IsRemote == true) .GroupByArray(e => e.Contacts!, c => c.Type) .Having(g => Agg.Count(g) > 0) .SelectAsync(g => new { Type = g.Key, Count = Agg.Count(g) }); // One row per contact of that type over the matched employees: the database is shared, and other tests store // remote employees with no contacts, so "one row per matched employee" held only while they ran later. results.Should().NotBeEmpty(); results.Select(r => r.Type).Should().Contain(new[] { "email", "phone" }); results.Should().AllSatisfy(r => r.Count.Should().Be( matched.Sum(e => e.Props.Contacts?.Count(c => c.Type == r.Type) ?? 0)), "outer Where filter must be applied before array aggregation"); } // ────────────────────────────────────────────────────────────────── // E150 — Array aggregation (regression lock: already works) // ────────────────────────────────────────────────────────────────── [Fact] public async Task E150_ArrayAggregation_SumAverage_AllElements() { await SeedEmployeesAsync(); // Aggregate ALL elements of SkillLevels[] (unnested). var all = await Redb.Query() .AggregateAsync(x => new { Total = Agg.Sum(x.Props.SkillLevels.Select(s => s)), Avg = Agg.Average(x.Props.SkillLevels.Select(s => s)), Count = Agg.Count() }); all.Count.Should().BeGreaterThan(0); all.Total.Should().BeGreaterThan(0); all.Avg.Should().BeGreaterThan(0); } [Fact] public async Task E150_ArrayAggregation_SumAverage_SpecificIndex() { await SeedEmployeesAsync(); // Aggregate only the first element of SkillLevels[] (indexed access). var first = await Redb.Query() .AggregateAsync(x => new { SumFirst = Agg.Sum(x.Props.SkillLevels[0]), AvgFirst = Agg.Average(x.Props.SkillLevels[0]) }); first.SumFirst.Should().BeGreaterThan(0); first.AvgFirst.Should().BeGreaterThan(0); } // ────────────────────────────────────────────────────────────────── // E200 — string concatenation inside ToLower().Contains(...) // Currently broken in BaseFilterExpressionParser.ExtractProperty: // "Expression must be a property access, got MethodBinaryExpression" // The test asserts the expression builds AND returns at least one match // against the seeded data. // ────────────────────────────────────────────────────────────────── [Fact] public async Task E200_StringConcat_ToLowerContains_ParsesAndExecutes() { await SeedEmployeesAsync(); // Seeded names look like "Employee000 Last000"..; lower-case contains "employee" always matches. var hits = await Redb.Query() .Where(e => (e.FirstName + " " + e.LastName).ToLower().Contains("employee")) .ToListAsync(); hits.Should().NotBeEmpty( "string concatenation inside ToLower().Contains(...) must compile to SQL `||` + ILIKE"); } // ────────────────────────────────────────────────────────────────── // GitHub #4 — ChangePasswordAsync is Task ("true if changed"), // so a WRONG current password must return false, not throw // UnauthorizedAccessException. A plain change-password form built // against the documented contract otherwise 500s on the most common // user error. // ────────────────────────────────────────────────────────────────── [Fact] public async Task ChangePassword_WrongCurrent_ReturnsFalse_NotThrows() { var login = "pw_" + Guid.NewGuid().ToString("N").Substring(0, 12); var user = await Redb.UserProvider.CreateUserAsync( new CreateUserRequest { Login = login, Name = login, Password = "correct-horse" }); var wrong = await Redb.UserProvider.ChangePasswordAsync(user, "definitely-wrong", "new-password-123"); wrong.Should().BeFalse("a wrong current password is a user error, not an exception"); var ok = await Redb.UserProvider.ChangePasswordAsync(user, "correct-horse", "new-password-123"); ok.Should().BeTrue("the correct current password changes it"); } }