Path: redb/redb.Tests.Integration/Tests/Base/NestedCollectionsRefusalTestsBase.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/NestedCollectionsRefusalTestsBase.cs
----
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;
///
/// A collection directly inside a collection (a list of lists, jagged and multi-dimensional arrays, a list or array of
/// dictionaries, a list of arrays, a dictionary of lists or of dictionaries). Before, each inner collection was stored as
/// the name of its type (a multi-dimensional array flat) and the object could not be loaded. Now the scheme sync refuses
/// the property before it writes anything, naming the class, the property and its type; a save against a scheme synced
/// before the refusal existed refuses the same way before any row is written. byte[] as an element or value and a
/// collection inside a class element keep working.
///
public abstract class NestedCollectionsRefusalTestsBase
{
protected readonly IRedbService Redb;
protected NestedCollectionsRefusalTestsBase(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("Wrap the inner collection in a class");
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 ListOfLists_SyncIsRefused()
=> SyncIsRefusedAsync("NestedListOfListsProps", "Value", "List>");
[Fact]
public Task JaggedArray_SyncIsRefused()
=> SyncIsRefusedAsync("NestedJaggedArrayProps", "Value", "Int32[][]");
[Fact]
public Task ListOfArrays_SyncIsRefused()
=> SyncIsRefusedAsync("NestedListOfArraysProps", "Value", "List");
[Fact]
public Task ListOfDictionaries_SyncIsRefused()
=> SyncIsRefusedAsync("NestedListOfDictionariesProps", "Value",
"List>");
[Fact]
public Task ArrayOfDictionaries_SyncIsRefused()
=> SyncIsRefusedAsync("NestedArrayOfDictionariesProps", "Value",
"Dictionary[]");
[Fact]
public Task DictionaryOfLists_SyncIsRefused()
=> SyncIsRefusedAsync("NestedDictionaryOfListsProps", "Value",
"Dictionary>");
[Fact]
public Task DictionaryOfDictionaries_SyncIsRefused()
=> SyncIsRefusedAsync("NestedDictionaryOfDictionariesProps", "Value",
"Dictionary>");
[Fact]
public Task MultidimensionalArray_SyncIsRefused()
=> SyncIsRefusedAsync("NestedMultidimensionalArrayProps", "Value", "Int32[,]");
[Fact]
public Task NestedCollectionInsideAClassElement_SyncIsRefused_NamingTheElementClass()
=> SyncIsRefusedAsync("NestedGridHolder", "Grid", "List>");
// --- allowed shapes, pinned ---
[Fact]
public async Task BytesAsElementOrValue_ReadBackAsSaved()
{
await Redb.SyncSchemeAsync();
var saved = new NestedBytesCollectionsProps
{
Chunks = [[1, 2, 3], [], [255]],
Blobs = new() { ["a"] = [9, 8], ["b"] = [] }
};
var id = await Redb.SaveAsync(new RedbObject { name = "nested-bytes", Props = saved });
var loaded = (await Redb.LoadAsync(id))!.Props;
loaded.Chunks.Should().BeEquivalentTo(saved.Chunks, o => o.WithStrictOrdering());
loaded.Blobs.Should().BeEquivalentTo(saved.Blobs);
}
[Fact]
public async Task CollectionInsideAClassElement_ReadsBackAsSaved()
{
await Redb.SyncSchemeAsync();
var saved = new NestedCollectionInElementProps
{
Items =
[
new() { Name = "x", Tags = ["a", "b"], Scores = new() { ["s"] = 1 } },
new() { Name = "y", Tags = ["c"] }
],
ByKey = new() { ["k"] = new() { Name = "z", Tags = ["d", "e"], Scores = new() { ["t"] = 2 } } }
};
var id = await Redb.SaveAsync(new RedbObject { name = "nested-in-element", Props = saved });
var loaded = (await Redb.LoadAsync(id))!.Props;
loaded.Should().BeEquivalentTo(saved, o => o.WithStrictOrdering());
}
// --- save against a scheme synced before the refusal existed ---
private async Task SaveIsRefusedAsync(TProps props, string found)
where TShape : class, new() where TProps : class, new()
{
var scheme = await Redb.SyncSchemeAsync();
var name = "nested-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("Value");
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 ListOfLists_OnASchemeSyncedBefore_SaveIsRefused()
=> SaveIsRefusedAsync(
new() { Value = [[1, 2], [3]] }, "a collection element of type List");
[Fact]
public Task DictionaryOfLists_OnASchemeSyncedBefore_SaveIsRefused()
=> SaveIsRefusedAsync(
new() { Value = new() { ["a"] = [1] } }, "a collection element of type List");
[Fact]
public Task MultidimensionalArray_OnASchemeSyncedBefore_SaveIsRefused()
=> SaveIsRefusedAsync(
new() { Value = new[,] { { 1, 2 }, { 3, 4 } } }, "a multi-dimensional array Int32[,]");
}