Path: redb/redb.Tests.Integration/Tests/Base/FreeListItemJsonTestsBase.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/FreeListItemJsonTestsBase.cs
----
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Extensions;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// The Free in-database JSON builders wrote a list
/// item as {id, idList, value, alias[, object]}, while the model reads id_list and id_object: every
/// Free load lost both the list link and the object link of the list items in Props, and PostgreSQL / SQLite built the
/// whole linked object for a key the deserializer ignores. Owner decision 2026-09-15: a list item is
/// {id, id_list, value, alias, id_object}, with no nested object.
///
public abstract class FreeListItemJsonTestsBase
{
protected abstract void UseProvider(RedbOptionsBuilder options);
private ServiceProvider Build()
{
var services = new ServiceCollection();
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
services.AddRedb(options =>
{
UseProvider(options);
options.Configure(c =>
{
c.EnablePropsCache = false;
c.CacheDomain = "free-listitem-json";
});
});
return services.BuildServiceProvider();
}
private sealed record Seed(long RootId, long ListId, long LinkedItemId, long PlainItemId, long LinkedObjectId);
private static async Task SeedAsync(ServiceProvider sp)
{
var tag = Guid.NewGuid().ToString("N")[..8];
var redb = sp.GetRequiredService();
await redb.InitializeAsync(ensureCreated: true);
await redb.SyncSchemeAsync();
await redb.SyncSchemeAsync();
await redb.SyncSchemeAsync();
await redb.InitializeTypeRegistryAsync();
var linkedObjectId = await redb.SaveAsync(new RedbObject
{ name = $"lijson-linked-{tag}", Props = new SimpleProps { Title = "linked" } });
var list = await redb.ListProvider.SaveListAsync(RedbList.Create($"lijson-{tag}", "lijson"));
var linked = await redb.ListProvider.SaveListItemAsync(new RedbListItem { IdList = list.Id, Value = "linked", IdObject = linkedObjectId });
var plain = await redb.ListProvider.SaveListItemAsync(new RedbListItem { IdList = list.Id, Value = "plain" });
var rootId = await redb.SaveAsync(new RedbObject
{
name = $"lijson-root-{tag}",
Props = new CtProbeProps { Label = "root", Status = linked, Roles = [linked, plain] }
});
return new Seed(rootId, list.Id, linked.Id, plain.Id, linkedObjectId);
}
private static void AssertLinks(string path, CtProbeProps props, Seed seed)
{
AssertItem($"{path}, Status", props.Status, seed.LinkedItemId, seed.ListId, "linked", seed.LinkedObjectId);
props.Roles.Should().HaveCount(2, $"{path}: both list items are materialized");
AssertItem($"{path}, Roles[0]", props.Roles![0], seed.LinkedItemId, seed.ListId, "linked", seed.LinkedObjectId);
AssertItem($"{path}, Roles[1]", props.Roles[1], seed.PlainItemId, seed.ListId, "plain", null);
}
private static void AssertItem(string path, RedbListItem? item, long id, long listId, string value, long? objectId)
{
item.Should().NotBeNull($"{path}: the list item is materialized");
item!.Id.Should().Be(id, $"{path}: id");
item.Value.Should().Be(value, $"{path}: value");
item.IdList.Should().Be(listId, $"{path}: the item keeps the list it belongs to");
item.IdObject.Should().Be(objectId, $"{path}: the item keeps its object link");
}
private static readonly string[] ItemJsonKeys = ["id", "id_list", "value", "alias", "id_object"];
private static void AssertItemJson(string path, JsonElement item, long? objectId)
{
item.EnumerateObject().Select(p => p.Name).Should().BeEquivalentTo(ItemJsonKeys,
$"{path}: a list item in the object JSON is exactly id, id_list, value, alias, id_object - no idList, no nested object");
var link = item.GetProperty("id_object");
if (objectId == null)
link.ValueKind.Should().Be(JsonValueKind.Null, $"{path}: the item has no object link");
else
link.GetInt64().Should().Be(objectId.Value, $"{path}: the object link");
}
[Fact]
public async Task LoadAsync_ListItems_KeepListAndObjectLinks()
{
await using var sp = Build();
var seed = await SeedAsync(sp);
await using var scope = sp.CreateAsyncScope();
var root = await scope.ServiceProvider.GetRequiredService().LoadAsync(seed.RootId, depth: 10);
AssertLinks("async load", root!.Props, seed);
}
[Fact]
public async Task SyncLoad_ListItems_KeepListAndObjectLinks()
{
await using var sp = Build();
var seed = await SeedAsync(sp);
await using var scope = sp.CreateAsyncScope();
var root = scope.ServiceProvider.GetRequiredService().Load(seed.RootId, depth: 10);
AssertLinks("sync load", root!.Props, seed);
}
[Fact]
public async Task ObjectJson_ListItem_IsIdListValueAliasObjectLink_WithoutNestedObject()
{
await using var sp = Build();
var seed = await SeedAsync(sp);
await using var scope = sp.CreateAsyncScope();
var json = await scope.ServiceProvider.GetRequiredService().LoadJsonAsync(seed.RootId, depth: 10);
using var doc = JsonDocument.Parse(json!);
var properties = doc.RootElement.GetProperty("properties");
AssertItemJson("Status", properties.GetProperty("Status"), seed.LinkedObjectId);
var roles = properties.GetProperty("Roles");
AssertItemJson("Roles[0]", roles[0], seed.LinkedObjectId);
AssertItemJson("Roles[1]", roles[1], null);
}
}