Path: redb/redb.Tests.Integration/Tests/Base/CollectionKindChangeTestsBase.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/CollectionKindChangeTestsBase.cs
----
using redb.Core;
using redb.Core.Exceptions;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// A class of a higher version changes the collection kind of a property (single value, array, dictionary). Values are
/// never converted between kinds: with values stored the sync is refused naming the scheme, the property and both kinds,
/// and the structure, its rows and the scheme's version stay as they were; with no values stored the kind just changes.
/// Before 4.8.0 the kind flipped over the stored rows, which the new class then read wrong until a save dropped them.
///
public abstract class CollectionKindChangeTestsBase : IAsyncLifetime
{
protected readonly IRedbService Redb;
protected CollectionKindChangeTestsBase(IRedbService redb) => Redb = redb;
private static readonly string[] Schemes =
["kind_single_before", "kind_single", "kind_array_before", "kind_array", "kind_array_dict_before", "kind_array_dict",
"kind_dict_before", "kind_dict"];
public async Task InitializeAsync()
{
foreach (var s in Schemes) await ResetAsync(s);
}
public async Task DisposeAsync()
{
foreach (var s in Schemes) await ResetAsync(s);
}
private async Task ResetAsync(string scheme)
{
await Redb.Context.ExecuteAsync($"DELETE FROM _objects WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{scheme}')");
await Redb.Context.ExecuteAsync($"DELETE FROM _structures WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{scheme}')");
await Redb.Context.ExecuteAsync($"DELETE FROM _schemes WHERE _name = '{scheme}'");
Redb.Cache.Clear();
}
private async Task HandOverAsync(string before, string after)
{
await Redb.Context.ExecuteAsync($"UPDATE _schemes SET _name = '{after}' WHERE _name = '{before}'");
Redb.Cache.Clear();
}
private async Task<(long Id, long? Kind, long Rows, long Version)> StructureStateAsync(string scheme, string name)
{
var id = (await Redb.Context.ExecuteScalarAsync(
$"SELECT _id FROM _structures WHERE _name = '{name}' AND _id_parent IS NULL AND _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{scheme}')"))!.Value;
var kind = await Redb.Context.ExecuteScalarAsync($"SELECT _collection_type FROM _structures WHERE _id = {id}");
var rows = await Redb.Context.ExecuteScalarAsync($"SELECT CAST(COUNT(*) AS BIGINT) FROM _values WHERE _id_structure = {id}") ?? 0;
var version = await Redb.Context.ExecuteScalarAsync(
$"SELECT CAST(COALESCE(_version, 0) AS BIGINT) FROM _schemes WHERE _name = '{scheme}'") ?? 0;
return (id, kind, rows, version);
}
private async Task RefusedAsync(string before, string after, string property, Func props,
string storedKind, string declaredKind)
where TBefore : class, new() where TAfter : class, new()
{
await Redb.SyncSchemeAsync();
for (var i = 0; i < 3; i++)
await Redb.SaveAsync(new RedbObject { name = $"kind-{i}", Props = props(i) });
await HandOverAsync(before, after);
var state = await StructureStateAsync(after, property);
state.Rows.Should().BeGreaterThan(0, "precondition: values are stored");
var sync = () => Redb.SyncSchemeAsync();
var e = (await sync.Should().ThrowAsync()).Which;
e.SchemeName.Should().Be(after);
e.PropertyName.Should().Be(property);
e.StructureId.Should().Be(state.Id);
e.StoredKind.Should().Be(storedKind);
e.DeclaredKind.Should().Be(declaredKind);
e.Message.Should().Contain("new property name");
Redb.Cache.Clear();
(await StructureStateAsync(after, property)).Should().Be(state, "the structure, its rows and the scheme's version stay as they were");
}
[Fact]
public Task SingleValueToArray_WithValues_IsRefused() =>
RefusedAsync("kind_single_before", "kind_single", "Level",
i => new() { Tag = $"t{i}", Level = i + 1 }, "single value", "array");
[Fact]
public Task ArrayToSingleValue_WithValues_IsRefused() =>
RefusedAsync("kind_array_before", "kind_array", "Items",
i => new() { Tag = $"t{i}", Items = [i, i + 1] }, "array", "single value");
[Fact]
public Task ArrayToDictionary_WithValues_IsRefused() =>
RefusedAsync("kind_array_dict_before", "kind_array_dict", "Items",
i => new() { Tag = $"t{i}", Items = [i, i + 1] }, "array", "dictionary");
[Fact]
public Task DictionaryToArray_WithValues_IsRefused() =>
RefusedAsync("kind_dict_before", "kind_dict", "Items",
i => new() { Tag = $"t{i}", Items = new() { ["a"] = i, ["b"] = i + 1 } }, "dictionary", "array");
[Fact]
public async Task KindChange_WithoutValues_Applies()
{
// The array is never set: its structure holds no row, so nothing can be read wrong.
await Redb.SyncSchemeAsync();
for (var i = 0; i < 3; i++)
await Redb.SaveAsync(new RedbObject { name = $"kind-{i}", Props = new() { Tag = $"t{i}" } });
await HandOverAsync("kind_array_dict_before", "kind_array_dict");
(await StructureStateAsync("kind_array_dict", "Items")).Rows.Should().Be(0, "precondition");
await Redb.SyncSchemeAsync();
var after = await StructureStateAsync("kind_array_dict", "Items");
after.Kind.Should().Be(redb.Core.Utils.RedbTypeIds.Dictionary);
after.Version.Should().Be(1);
var id = await Redb.SaveAsync(new RedbObject
{ name = "kind-new", Props = new() { Tag = "n", Items = new() { ["x"] = 7 } } });
(await Redb.LoadAsync(id))!.Props.Items.Should().BeEquivalentTo(new Dictionary { ["x"] = 7 });
}
}