Path: redb/redb.Tests.Integration/Tests/Base/EnumDictionaryKeyTestsBase.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/EnumDictionaryKeyTestsBase.cs ---- using System.Linq.Expressions; using redb.Core; using redb.Core.Data; using redb.Core.Exceptions; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// A dictionary keyed by an enum, by a flags enum and by a nullable enum: saved, loaded, filtered by ContainsKey, by /// D[key] == value and by a field of the entry, and read back in a stable key order. Filters are compared with the same /// LINQ over the seeded objects in memory. A stored key that no longer names a member of the enum stops the load. /// public abstract class EnumDictionaryKeyTestsBase { protected readonly IRedbService Redb; protected EnumDictionaryKeyTestsBase(IRedbService redb) => Redb = redb; private static EnumKeyProps Sample(string tag, int i) => new() { Marker = tag, ByShade = i % 2 == 0 ? new() { [KeyShade.Red] = 10 + i, [KeyShade.Green] = 20 + i, [KeyShade.Blue] = 30 + i } : new() { [KeyShade.Green] = 20 + i }, EntryByShade = new() { [KeyShade.Blue] = new() { Name = i % 2 == 0 ? $"{tag}-hit" : $"{tag}-miss", Rank = i }, [KeyShade.Red] = new() { Name = $"{tag}-other", Rank = 100 + i }, }, ByAccess = new() { [KeyAccess.Read] = "r", [KeyAccess.Read | KeyAccess.Write] = i % 2 == 0 ? "rw" : "rw-odd", [KeyAccess.None] = "none", }, ByMaybeShade = i % 2 == 0 ? new() { [KeyShade.Green] = 5, [KeyShade.Red] = 6 } : new() { [KeyShade.Blue] = 7 }, }; private async Task<(string Tag, List<(long Id, EnumKeyProps Props)> Seeded)> SeedAsync() { await Redb.SyncSchemeAsync(); var tag = "ek-" + Guid.NewGuid().ToString("N")[..8]; var seeded = new List<(long, EnumKeyProps)>(); for (var i = 0; i < 4; i++) { var props = Sample(tag, i); var id = await Redb.SaveAsync(new RedbObject { name = $"{tag}-{i}", Props = props }); seeded.Add((id, props)); } return (tag, seeded); } private async Task MatchesLinqAsync(Func>> filterOf, int expectedCount) { var (tag, seeded) = await SeedAsync(); var filter = filterOf(tag); var expected = seeded.Where(s => filter.Compile()(s.Props)).Select(s => s.Id).ToList(); expected.Should().HaveCount(expectedCount, "precondition"); var query = Redb.Query().Where(p => p.Marker == tag).Where(filter); (await query.ToListAsync()).Select(o => o.Id).Should().BeEquivalentTo(expected); (await query.CountAsync()).Should().Be(expectedCount); } [Fact] public async Task Load_ReturnsEveryEntry_OfEveryKeyKind() { var (_, seeded) = await SeedAsync(); foreach (var (id, props) in seeded) { var loaded = (await Redb.LoadAsync(id))!.Props; loaded.ByShade.Should().BeEquivalentTo(props.ByShade); loaded.EntryByShade.Should().BeEquivalentTo(props.EntryByShade); loaded.ByAccess.Should().BeEquivalentTo(props.ByAccess, "a flags combination is one key"); loaded.ByMaybeShade.Should().BeEquivalentTo(props.ByMaybeShade); } } [Fact] public async Task Query_ReturnsEveryEntry_OfEveryKeyKind() { var (tag, seeded) = await SeedAsync(); var loaded = await Redb.Query().Where(p => p.Marker == tag).ToListAsync(); foreach (var o in loaded) { var props = seeded.Single(s => s.Id == o.Id).Props; o.Props.ByShade.Should().BeEquivalentTo(props.ByShade); o.Props.ByAccess.Should().BeEquivalentTo(props.ByAccess); o.Props.ByMaybeShade.Should().BeEquivalentTo(props.ByMaybeShade); } } [Fact] public Task ContainsKey() => MatchesLinqAsync(_ => p => p.ByShade!.ContainsKey(KeyShade.Red), 2); [Fact] public Task IndexerEquals() => MatchesLinqAsync(_ => p => p.ByShade![KeyShade.Green] == 21, 1); [Fact] public Task EntryField() => MatchesLinqAsync(tag => p => p.EntryByShade![KeyShade.Blue].Name == tag + "-hit", 2); [Fact] public Task FlagsCombination() { var both = KeyAccess.Read | KeyAccess.Write; return MatchesLinqAsync(_ => p => p.ByAccess![both] == "rw", 2); } [Fact] public Task FlagsSingleMember_IsNotTheCombination() => MatchesLinqAsync(_ => p => p.ByAccess!.ContainsKey(KeyAccess.Write), 0); [Fact] public Task NullableKey_ContainsKey() { KeyShade? green = KeyShade.Green; return MatchesLinqAsync(_ => p => p.ByMaybeShade!.ContainsKey(green), 2); } [Fact] public Task NullableKey_IndexerEquals() { // ContainsKey first only for the LINQ in memory, where a missing key throws; redb finds no row for it. KeyShade? blue = KeyShade.Blue; return MatchesLinqAsync(_ => p => p.ByMaybeShade!.ContainsKey(blue) && p.ByMaybeShade[blue] == 7, 2); } [Fact] public async Task Keys_ComeBackInOneStableOrder_ByLoadAndByQuery() { var (tag, seeded) = await SeedAsync(); var (id, _) = seeded[0]; var byLoad = (await Redb.LoadAsync(id))!.Props; var byQuery = (await Redb.Query().Where(p => p.Marker == tag).ToListAsync()).Single(o => o.Id == id).Props; // The order of the keys as the enum compares them: by value (Green = 1, Blue = 2, Red = 3), not by name. var shadeOrder = new[] { KeyShade.Green, KeyShade.Blue, KeyShade.Red }; byLoad.ByShade!.Keys.Should().Equal(shadeOrder); byQuery.ByShade!.Keys.Should().Equal(shadeOrder); var accessOrder = new[] { KeyAccess.None, KeyAccess.Read, KeyAccess.Read | KeyAccess.Write }; byLoad.ByAccess!.Keys.Should().Equal(accessOrder); byQuery.ByAccess!.Keys.Should().Equal(accessOrder); } /// Changes a stored key of one object to text no member of the enum has (a member renamed since). protected virtual string RenameStoredKeySql(long objectId) => $"UPDATE _values SET _array_index = 'Crimson' WHERE _id_object = {objectId} AND _array_index = 'Red'"; [Fact] public async Task StoredKey_ThatNoLongerParses_StopsTheLoad_NamingFieldAndKey() { var (_, seeded) = await SeedAsync(); var (id, _) = seeded[1]; // ByShade holds Green only; EntryByShade holds Red await Redb.Context.ExecuteAsync(RenameStoredKeySql(id)); var load = () => Redb.LoadAsync(id); AssertUnreadableKey((await load.Should().ThrowAsync()).Which); } /// /// What a load reports for a stored key that does not read as the key type. Pro: /// naming the field, the stored key and the key type; no entry is skipped. /// protected virtual void AssertUnreadableKey(Exception thrown) { var e = thrown.Should().BeOfType().Subject; e.Field.Should().Be("EntryByShade"); e.StoredKey.Should().Be("Crimson"); e.KeyType.Should().Be(typeof(KeyShade)); e.Message.Should().Contain("EntryByShade").And.Contain("Crimson").And.Contain(nameof(KeyShade)); } }