Path: redb/redb.Tests.Integration/Tests/Base/ElementFoldOrderTestsBase.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/ElementFoldOrderTestsBase.cs ---- using Microsoft.Extensions.DependencyInjection; using redb.Core; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// DistinctBy and OrderBy over a whole collection field read the collection folded into one key, its elements in index /// order (a dictionary: its values in key order). Every path that reads the key - the pivot, the streams form, the plain /// sort with no pivot at all - must read the same key, so equal lists are one key and the sort follows the folded list. /// public abstract class ElementFoldOrderTestsBase : IAsyncLifetime { private readonly IServiceProvider _services; private string _clearSql = ""; protected ElementFoldOrderTestsBase(IServiceProvider services) => _services = services; // The two schemes are this class's own: every object in them is removed before and after each test. public async Task InitializeAsync() { await using var scope = _services.CreateAsyncScope(); var redb = scope.ServiceProvider.GetRequiredService(); var lists = await redb.SyncSchemeAsync(); var dictionaries = await redb.SyncSchemeAsync(); _clearSql = $"DELETE FROM _objects WHERE _id_scheme IN ({lists.Id}, {dictionaries.Id})"; await redb.Context.ExecuteAsync(_clearSql); } public async Task DisposeAsync() { await using var scope = _services.CreateAsyncScope(); await scope.ServiceProvider.GetRequiredService().Context.ExecuteAsync(_clearSql); } private static RedbObject Item(string name, params string[] tags) => new() { Name = name, Props = new ElementFoldOrderProps { Label = "x", Tags = tags.ToList() } }; // a and b hold one list (b written by the bulk path); c has the same elements in another order; d shares the first // element. The keys sort a,b < a,z < b,a < t1,t2,t3 < t3,t1,t2 (a, b) < t3,t9 as joined text, as an array and as a // JSON array alike. private static async Task SeedListsAsync(IRedbService redb) { await redb.SaveAsync(Item("a", "t3", "t1", "t2")); await redb.AddNewObjectsAsync(new[] { Item("b", "t3", "t1", "t2") }); await redb.SaveAsync(Item("c", "t1", "t2", "t3")); await redb.SaveAsync(Item("d", "t3", "t9")); await redb.SaveAsync(Item("e", "b", "a")); await redb.SaveAsync(Item("f", "a", "z")); await redb.SaveAsync(Item("g", "a", "b")); } private static void ShouldBeSixKeys(IEnumerable names) { var list = names.ToList(); list.Should().HaveCount(6, "a and b hold one list; every other list is a key of its own"); list.Count(n => n is "a" or "b").Should().Be(1); list.Should().Contain(new[] { "c", "d", "e", "f", "g" }); } [Fact] public async Task DistinctBy_OverAWholeList_EqualListsAreOneKey_AndASortByItFollowsTheList() { await using var scope = _services.CreateAsyncScope(); var redb = scope.ServiceProvider.GetRequiredService(); await SeedListsAsync(redb); ShouldBeSixKeys((await redb.Query().DistinctBy(x => x.Tags).ToListAsync()).Select(o => o.Name)); var ordered = await redb.Query().DistinctBy(x => x.Tags).OrderBy(x => x.Tags).ToListAsync(); ordered.Select(o => o.Name is "b" ? "a" : o.Name).Should().Equal("g", "f", "e", "c", "a", "d"); } [Fact] public async Task DistinctBy_OverAWholeList_UnderAPropsFilterAndAPage_ReadsTheSameKey() { await using var scope = _services.CreateAsyncScope(); var redb = scope.ServiceProvider.GetRequiredService(); await SeedListsAsync(redb); var page = await redb.Query() .Where(x => x.Label == "x").DistinctBy(x => x.Tags).Take(100).ToListAsync(); ShouldBeSixKeys(page.Select(o => o.Name)); } [Fact] public async Task OrderBy_OverAWholeList_WithNoFilter_SortsByTheListInIndexOrder() { await using var scope = _services.CreateAsyncScope(); var redb = scope.ServiceProvider.GetRequiredService(); await SeedListsAsync(redb); for (var run = 0; run < 2; run++) { var ordered = await redb.Query().OrderBy(x => x.Tags).ThenByRedb(o => o.Id).ToListAsync(); ordered.Select(o => o.Name).Should().Equal(new[] { "g", "f", "e", "c", "a", "b", "d" }, "the keys are a,b < a,z < b,a < t1,t2,t3 < t3,t1,t2 (twice, by id) < t3,t9"); } var page = await redb.Query().OrderBy(x => x.Tags).ThenByRedb(o => o.Id).Take(3).ToListAsync(); page.Select(o => o.Name).Should().Equal("g", "f", "e"); } [Fact] public async Task OrderBy_OverAWholeDictionary_SortsByItsValuesInKeyOrder() { await using var scope = _services.CreateAsyncScope(); var redb = scope.ServiceProvider.GetRequiredService(); // Values in key order: h -> 2,1; i -> 1,9; j -> 1,2. Keys inserted out of order on purpose. await redb.SaveAsync(new RedbObject { Name = "h", Props = new() { Codes = new() { ["b"] = "1", ["a"] = "2" } } }); await redb.SaveAsync(new RedbObject { Name = "i", Props = new() { Codes = new() { ["b"] = "9", ["a"] = "1" } } }); await redb.SaveAsync(new RedbObject { Name = "j", Props = new() { Codes = new() { ["a"] = "1", ["b"] = "2" } } }); var ordered = await redb.Query().OrderBy(x => x.Codes).ThenByRedb(o => o.Id).ToListAsync(); ordered.Select(o => o.Name).Should().Equal(new[] { "j", "i", "h" }, "the keys are 1,2 < 1,9 < 2,1"); } }