Path: redb/redb.Tests.Integration/Tests/SqlitePro/SqliteProIndexerPropsTests.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/SqlitePro/SqliteProIndexerPropsTests.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Attributes;
using redb.Core.Models.Entities;
using redb.Core.Pro.Extensions;
using redb.SQLite.Pro.Extensions;
namespace redb.Tests.Integration.Tests.SqlitePro;
/// A class with an indexer, used as a nested Props class.
public class IndexedPart
{
private readonly Dictionary _extra = new();
public string? Code { get; set; }
public string this[string key]
{
get => _extra.TryGetValue(key, out var v) ? v : "";
set => _extra[key] = value;
}
}
[RedbScheme("Save: props with an indexer", Name = "props_indexer_probe")]
public class PropsWithIndexer
{
private readonly Dictionary _extra = new();
public string? Title { get; set; }
public IndexedPart? Part { get; set; }
public string this[string key]
{
get => _extra.TryGetValue(key, out var v) ? v : "";
set => _extra[key] = value;
}
}
///
/// An indexer is not a field: the scheme sync and the value writer skip it. The object hash called its getter without
/// an index and the save failed with , so a Props class
/// with an indexer (at the top or in a nested class) could not be saved at all. It saves its properties; the indexer
/// is not stored.
///
public class SqliteProIndexerPropsTests
{
private const string Cs = "Data Source=redb_tests_props_indexer_pro.db";
[Fact]
public async Task PropsClassWithAnIndexer_SavesItsProperties_AndTheIndexerIsNotStored()
{
Fixtures.SqliteTestSupport.DeleteDbFiles(Cs);
var services = new ServiceCollection();
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
services.AddRedbPro(options => options.UseSqlite(Cs));
await using var provider = services.BuildServiceProvider();
var redb = provider.GetRequiredService();
await redb.InitializeAsync(ensureCreated: true);
await redb.SyncSchemeAsync();
var props = new PropsWithIndexer { Title = "t", Part = new IndexedPart { Code = "c" } };
props["a"] = "x";
props.Part["b"] = "y";
var id = await redb.SaveAsync(new RedbObject { name = "i", Props = props });
var loaded = await redb.LoadAsync(id);
loaded.Should().NotBeNull();
loaded!.Props.Title.Should().Be("t");
loaded.Props.Part!.Code.Should().Be("c");
loaded.Props["a"].Should().BeEmpty("an indexer is not a field and is not stored");
loaded.Props.Part["b"].Should().BeEmpty();
loaded.Props.Title = "t2";
(await redb.SaveAsync(loaded)).Should().Be(id);
(await redb.LoadAsync(id))!.Props.Title.Should().Be("t2");
}
}