Path: redb/redb.Tests.Integration/Tests/Base/InterfaceCollectionsTestsBase.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/InterfaceCollectionsTestsBase.cs
----
using redb.Core;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// A list declared as IEnumerable<T>, ICollection<T> or IList<T> and a dictionary declared as
/// IDictionary<K, V> read back as saved, with a numeric element narrower than the stored one, a nullable element,
/// a class element and an empty list. The sync makes the same structure as for List<T> and the rows are right.
///
public abstract class InterfaceCollectionsTestsBase
{
protected readonly IRedbService Redb;
protected InterfaceCollectionsTestsBase(IRedbService redb) => Redb = redb;
private async Task RoundTripAsync(TProps props, Func value) where TProps : class, new()
{
await Redb.SyncSchemeAsync();
var id = await Redb.SaveAsync(new RedbObject { name = "iface-" + Guid.NewGuid().ToString("N")[..8], Props = props });
var loaded = (await Redb.LoadAsync(id))!.Props;
value(loaded).Should().BeEquivalentTo(value(props), o => o.WithStrictOrdering());
}
[Fact] public Task IEnumerableOfInt() => RoundTripAsync(new IfaceIEnumerableOfIntProps { Value = new List { 3, -1, 2 } }, p => p.Value);
[Fact] public Task IEnumerableOfShort() => RoundTripAsync(new IfaceIEnumerableOfShortProps { Value = new List { 3, -1, 2 } }, p => p.Value);
[Fact] public Task IEnumerableOfByte() => RoundTripAsync(new IfaceIEnumerableOfByteProps { Value = new List { 3, 1, 255 } }, p => p.Value);
[Fact] public Task IEnumerableOfFloat() => RoundTripAsync(new IfaceIEnumerableOfFloatProps { Value = new List { 1.5f, -2.25f } }, p => p.Value);
[Fact] public Task IEnumerableOfDecimal() => RoundTripAsync(new IfaceIEnumerableOfDecimalProps { Value = new List { 1.5m, -2.25m, 12345678.901m } }, p => p.Value);
[Fact] public Task ICollectionOfInt() => RoundTripAsync(new IfaceICollectionOfIntProps { Value = new List { 3, -1, 2 } }, p => p.Value);
[Fact] public Task ICollectionOfShort() => RoundTripAsync(new IfaceICollectionOfShortProps { Value = new List { 3, -1, 2 } }, p => p.Value);
[Fact] public Task ICollectionOfByte() => RoundTripAsync(new IfaceICollectionOfByteProps { Value = new List { 3, 1, 255 } }, p => p.Value);
[Fact] public Task ICollectionOfFloat() => RoundTripAsync(new IfaceICollectionOfFloatProps { Value = new List { 1.5f, -2.25f } }, p => p.Value);
[Fact] public Task ICollectionOfDecimal() => RoundTripAsync(new IfaceICollectionOfDecimalProps { Value = new List { 1.5m, -2.25m, 12345678.901m } }, p => p.Value);
[Fact] public Task IListOfInt() => RoundTripAsync(new IfaceIListOfIntProps { Value = new List { 3, -1, 2 } }, p => p.Value);
[Fact] public Task IListOfShort() => RoundTripAsync(new IfaceIListOfShortProps { Value = new List { 3, -1, 2 } }, p => p.Value);
[Fact] public Task IListOfByte() => RoundTripAsync(new IfaceIListOfByteProps { Value = new List { 3, 1, 255 } }, p => p.Value);
[Fact] public Task IListOfFloat() => RoundTripAsync(new IfaceIListOfFloatProps { Value = new List { 1.5f, -2.25f } }, p => p.Value);
[Fact] public Task IListOfDecimal() => RoundTripAsync(new IfaceIListOfDecimalProps { Value = new List { 1.5m, -2.25m, 12345678.901m } }, p => p.Value);
[Fact] public Task IDictionaryOfInt() => RoundTripAsync(new IfaceIDictionaryOfIntProps { Value = new Dictionary { ["a"] = -1, ["b"] = 2 } }, p => p.Value);
[Fact] public Task IDictionaryOfShort() => RoundTripAsync(new IfaceIDictionaryOfShortProps { Value = new Dictionary { ["a"] = -1, ["b"] = 2 } }, p => p.Value);
[Fact] public Task IDictionaryOfByte() => RoundTripAsync(new IfaceIDictionaryOfByteProps { Value = new Dictionary { ["a"] = 255, ["b"] = 1 } }, p => p.Value);
[Fact] public Task IDictionaryOfFloat() => RoundTripAsync(new IfaceIDictionaryOfFloatProps { Value = new Dictionary { ["a"] = 1.5f, ["b"] = -2.25f } }, p => p.Value);
[Fact] public Task IDictionaryOfDecimal() => RoundTripAsync(new IfaceIDictionaryOfDecimalProps { Value = new Dictionary { ["a"] = 1.5m, ["b"] = -2.25m } }, p => p.Value);
[Fact] public Task IListOfNullableInt_KeepsTheNull() => RoundTripAsync(new IfaceIListOfNullableIntProps { Value = new List { 1, null, 3 } }, p => p.Value);
[Fact] public Task IListOfInt_Empty() => RoundTripAsync(new IfaceIListOfIntProps { Value = new List() }, p => p.Value);
[Fact] public Task IEnumerableOfShort_Empty() => RoundTripAsync(new IfaceIEnumerableOfShortProps { Value = new List() }, p => p.Value);
[Fact] public Task IListOfClass() => RoundTripAsync(new IfaceIListOfClassProps { Value = new List { new() { Name = "a", Count = 1 }, new() { Name = "b", Count = 2 } } }, p => p.Value);
[Fact] public Task ICollectionOfClass() => RoundTripAsync(new IfaceICollectionOfClassProps { Value = new List { new() { Name = "a", Count = 1 } } }, p => p.Value);
[Fact] public Task IEnumerableOfClass() => RoundTripAsync(new IfaceIEnumerableOfClassProps { Value = new List { new() { Name = "a", Count = 1 } } }, p => p.Value);
private async Task ReferencesReadBackAsync(Func>, TProps> make,
Func>?> value) where TProps : class, new()
{
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
var a = new RedbObject { name = "iface-ref-a", Props = new() { Name = "a", Count = 1 } };
var b = new RedbObject { name = "iface-ref-b", Props = new() { Name = "b", Count = 2 } };
await Redb.SaveAsync(a);
await Redb.SaveAsync(b);
var id = await Redb.SaveAsync(new RedbObject { name = "iface-" + Guid.NewGuid().ToString("N")[..8], Props = make([a, b]) });
var loaded = value((await Redb.LoadAsync(id))!.Props)!.ToList();
loaded.Select(r => r.id).Should().Equal(a.id, b.id);
loaded.Select(r => r.Props?.Name).Should().Equal("a", "b");
}
[Fact]
public Task IListOfReference() => ReferencesReadBackAsync(l => new IfaceIListOfReferenceProps { Value = l }, p => p.Value);
[Fact]
public Task IEnumerableOfReference() => ReferencesReadBackAsync(l => new IfaceIEnumerableOfReferenceProps { Value = l }, p => p.Value);
}