Path: redb/redb.Tests.Integration/Tests/Base/TopLevelCollectionsTestsBase.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/TopLevelCollectionsTestsBase.cs ---- using System.Collections.ObjectModel; using redb.Core; using redb.Core.Exceptions; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// Collection types at the top of a property. HashSet, ISet, ObservableCollection, Queue, Stack, IReadOnlyList, /// SortedDictionary and IReadOnlyDictionary were stored as one text value holding the name of the type, and the object /// could not be loaded. Now the scheme sync refuses such a property before it writes anything, naming the class, the /// property, its type and the supported collection types; a save against a scheme synced before the refusal existed /// refuses the same way before any row is written. The supported shapes read back as saved. /// public abstract class TopLevelCollectionsTestsBase { protected readonly IRedbService Redb; protected TopLevelCollectionsTestsBase(IRedbService redb) => Redb = redb; private async Task SyncIsRefusedAsync(string owner, string property, string typeName) where TProps : class, new() { var act = () => Redb.SyncSchemeAsync(); var refusal = (await act.Should().ThrowAsync()).Which; refusal.OwnerName.Should().Be(owner); refusal.PropertyName.Should().Be(property); refusal.PropertyTypeName.Should().Be(typeName); refusal.Message.Should().Contain(RedbUnsupportedCollectionException.SupportedTypes); var schemes = await Redb.Context.ExecuteScalarAsync( $"SELECT COUNT(*) FROM _schemes WHERE _name = '{typeof(TProps).FullName}'"); schemes.Should().Be(0, "the sync refuses before it writes the scheme row"); } [Fact] public Task HashSet_SyncIsRefused() => SyncIsRefusedAsync("TopHashSetProps", "Value", "HashSet"); [Fact] public Task ISet_SyncIsRefused() => SyncIsRefusedAsync("TopISetProps", "Value", "ISet"); [Fact] public Task ObservableCollection_SyncIsRefused() => SyncIsRefusedAsync("TopObservableCollectionProps", "Value", "ObservableCollection"); [Fact] public Task Queue_SyncIsRefused() => SyncIsRefusedAsync("TopQueueProps", "Value", "Queue"); [Fact] public Task Stack_SyncIsRefused() => SyncIsRefusedAsync("TopStackProps", "Value", "Stack"); [Fact] public Task IReadOnlyList_SyncIsRefused() => SyncIsRefusedAsync("TopIReadOnlyListProps", "Value", "IReadOnlyList"); [Fact] public Task SortedDictionary_SyncIsRefused() => SyncIsRefusedAsync("TopSortedDictionaryProps", "Value", "SortedDictionary"); [Fact] public Task IReadOnlyDictionary_SyncIsRefused() => SyncIsRefusedAsync("TopIReadOnlyDictionaryProps", "Value", "IReadOnlyDictionary"); [Fact] public Task HashSetInsideAClassElement_SyncIsRefused_NamingTheElementClass() => SyncIsRefusedAsync("TopHashSetHolder", "Tags", "HashSet"); // --- supported shapes, pinned --- private async Task AssertRoundTripAsync(TProps props, Func value) where TProps : class, new() { await Redb.SyncSchemeAsync(); var id = await Redb.SaveAsync(new RedbObject { name = "top-" + Guid.NewGuid().ToString("N")[..8], Props = props }); var loaded = (await Redb.LoadAsync(id))!.Props; value(loaded).Should().BeEquivalentTo(value(props)); } [Fact] public Task ListOfInt_RoundTrips() => AssertRoundTripAsync(new TopListOfIntProps { Value = [3, 1, 2] }, p => p.Value); [Fact] public Task IEnumerableOfLong_RoundTrips() => AssertRoundTripAsync(new TopIEnumerableOfLongProps { Value = new List { 3, 1, 2 } }, p => p.Value); [Fact] public Task IEnumerableOfString_RoundTrips() => AssertRoundTripAsync(new TopIEnumerableOfStringProps { Value = new List { "c", "a" } }, p => p.Value); [Fact] public Task ICollectionOfString_RoundTrips() => AssertRoundTripAsync(new TopICollectionOfStringProps { Value = new List { "c", "a" } }, p => p.Value); [Fact] public Task IDictionary_RoundTrips() => AssertRoundTripAsync(new TopIDictionaryProps { Value = new Dictionary { ["b"] = 2, ["a"] = 1 } }, p => p.Value); // --- save against a scheme synced before the refusal existed --- private async Task SaveIsRefusedAsync(TProps props, string property, string found) where TShape : class, new() where TProps : class, new() { var scheme = await Redb.SyncSchemeAsync(); var name = "top-guard-" + Guid.NewGuid().ToString("N")[..8]; var obj = new RedbObject { name = name, Props = props }; ((IRedbObject)obj).SchemeId = scheme.Id; var act = () => Redb.SaveAsync(obj); var refusal = (await act.Should().ThrowAsync()).Which; refusal.PropertyName.Should().Be(property); refusal.PropertyTypeName.Should().Be(found); var objects = await Redb.Context.ExecuteScalarAsync($"SELECT COUNT(*) FROM _objects WHERE _name = '{name}'"); objects.Should().Be(0, "the save refuses while the value rows are built, before anything is written"); } [Fact] public Task HashSet_OnASchemeSyncedBefore_SaveIsRefused() => SaveIsRefusedAsync(new() { Value = [1, 2] }, "Value", "HashSet"); [Fact] public Task SortedDictionary_OnASchemeSyncedBefore_SaveIsRefused() => SaveIsRefusedAsync( new() { Value = new() { ["a"] = 1 } }, "Value", "SortedDictionary"); [Fact] public Task HashSetInsideAClassElement_OnASchemeSyncedBefore_SaveIsRefused() => SaveIsRefusedAsync( new() { Items = [new TopHashSetHolder { Label = "a", Tags = [1] }] }, "Tags", "HashSet"); }