Path: redb/redb.Tests.Integration/Tests/Base/KeyDrawTestsBase.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/KeyDrawTestsBase.cs ---- using redb.Core; using redb.Core.Models.Configuration; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// How many keys a save draws from the key generator. A ChangeTracking save of an existing object draws keys only for /// the value rows it really inserts; it used to draw one for every value row it built, and the diff then threw most of /// them away (a changed object burnt one key per value row on every save). DeleteInsert inserts every row again and /// draws a key for each, as before. Keys are counted by drawing one before and one after the save: the generator hands /// them out in order, and the tests of a collection run one at a time. /// public abstract class KeyDrawTestsBase { protected readonly IRedbService Redb; protected KeyDrawTestsBase(IRedbService redb) => Redb = redb; private static KeyDrawProps Props() => new() { Tag = "t", Score = 1, Levels = [1, 2, 3], Lines = [new KeyDrawLine { Name = "a", Rank = 1 }, new KeyDrawLine { Name = "b", Rank = 2 }], Main = new KeyDrawLine { Name = "m", Rank = 9 } }; private async Task KeysDrawnBy(Func save) { var before = await Redb.Context.NextObjectIdAsync(); await save(); var after = await Redb.Context.NextObjectIdAsync(); return after - before - 1; } private Task ValueRowsAsync(long id) => Redb.Context.ExecuteScalarAsync($"SELECT CAST(COUNT(*) AS BIGINT) FROM _values WHERE _id_object = {id}"); private async Task WithStrategy(PropsSaveStrategy strategy, Func> act) { var saving = Redb.Configuration.PropsSaveStrategy; try { Redb.Configuration.PropsSaveStrategy = strategy; return await act(); } finally { Redb.Configuration.PropsSaveStrategy = saving; } } private async Task> SeedAsync() { await Redb.SyncSchemeAsync(); var id = await Redb.SaveAsync(new RedbObject { name = "kd", Props = Props() }); return (await Redb.LoadAsync(id))!; } [Fact] public async Task ChangeTracking_OneScalarChanged_DrawsNoKey() { var obj = await SeedAsync(); obj.Props.Score = 2; var drawn = await WithStrategy(PropsSaveStrategy.ChangeTracking, () => KeysDrawnBy(() => Redb.SaveAsync(obj))); drawn.Should().Be(0, "the changed row is updated in place"); (await Redb.LoadAsync(obj.id))!.Props.Score.Should().Be(2); } [Fact] public async Task ChangeTracking_OneArrayElementAdded_DrawsOneKey() { var obj = await SeedAsync(); obj.Props.Levels = [1, 2, 3, 4]; var drawn = await WithStrategy(PropsSaveStrategy.ChangeTracking, () => KeysDrawnBy(() => Redb.SaveAsync(obj))); drawn.Should().Be(1, "one element row is inserted"); (await Redb.LoadAsync(obj.id))!.Props.Levels.Should().Equal(1, 2, 3, 4); } [Fact] public async Task ChangeTracking_OneClassElementAdded_DrawsAKeyPerInsertedRow() { var obj = await SeedAsync(); obj.Props.Lines!.Add(new KeyDrawLine { Name = "c", Rank = 3 }); var drawn = await WithStrategy(PropsSaveStrategy.ChangeTracking, () => KeysDrawnBy(() => Redb.SaveAsync(obj))); drawn.Should().Be(3, "the element row and its two fields are inserted"); var lines = (await Redb.LoadAsync(obj.id))!.Props.Lines!; lines.Select(l => (l.Name, l.Rank)).Should().Equal(("a", 1), ("b", 2), ("c", 3)); } [Fact] public async Task ChangeTracking_NothingChanged_DrawsNoKey() { var obj = await SeedAsync(); var drawn = await WithStrategy(PropsSaveStrategy.ChangeTracking, () => KeysDrawnBy(() => Redb.SaveAsync(obj))); drawn.Should().Be(0); } [Fact] public async Task DeleteInsert_DrawsAKeyPerRow_AsItInsertsThemAll() { var obj = await SeedAsync(); obj.Props.Score = 2; var drawn = await WithStrategy(PropsSaveStrategy.DeleteInsert, () => KeysDrawnBy(() => Redb.SaveAsync(obj))); drawn.Should().Be((await ValueRowsAsync(obj.id))!.Value, "DeleteInsert inserts every value row again"); (await Redb.LoadAsync(obj.id))!.Props.Score.Should().Be(2); } [Fact] public async Task NewObject_DrawsAKeyPerRowAndOneForTheObject() { await Redb.SyncSchemeAsync(); long id = 0; var drawn = await KeysDrawnBy(async () => id = await Redb.SaveAsync(new RedbObject { name = "kd-new", Props = Props() })); drawn.Should().Be((await ValueRowsAsync(id))!.Value + 1); } }