Path: redb/redb.Tests.Integration/Tests/Base/DictionaryTestsBase.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/DictionaryTestsBase.cs
----
using redb.Core;
using redb.Core.Exceptions;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Helpers;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// Dictionary querying tests — ContainsKey, indexer, nested.
///
public abstract class DictionaryTestsBase
{
protected readonly IRedbService Redb;
protected DictionaryTestsBase(IRedbService redb) => Redb = redb;
private async Task> SeedAsync()
{
return await TestDataFactory.SeedEmployees(Redb, 20);
}
[Fact]
public async Task DictContainsKey_Filters()
{
var ids = await SeedAsync();
var results = await Redb.Query()
.Where(e => e.PhoneDirectory!.ContainsKey("desk"))
.Take(20)
.ToListAsync();
results.Should().NotBeEmpty();
results.Should().AllSatisfy(r =>
r.Props.PhoneDirectory.Should().ContainKey("desk"));
}
[Fact]
public async Task DictIndexer_ValueComparison()
{
var ids = await SeedAsync();
var results = await Redb.Query()
.Where(e => e.BonusByYear![2023] > 5000m)
.Take(20)
.ToListAsync();
results.Should().NotBeEmpty();
results.Should().AllSatisfy(r =>
r.Props.BonusByYear![2023].Should().BeGreaterThan(5000m));
}
[Fact]
public async Task DictNestedClassProperty_Filters()
{
var ids = await SeedAsync();
var results = await Redb.Query()
.Where(e => e.OfficeLocations!["HQ"].City == "New York")
.Take(20)
.ToListAsync();
results.Should().NotBeEmpty();
results.Should().AllSatisfy(r =>
r.Props.OfficeLocations!["HQ"].City.Should().Be("New York"));
}
[Fact]
public async Task DictTupleKey_Filters()
{
var ids = await SeedAsync();
var reviewKey = (Year: 2024, Quarter: "Q1");
var results = await Redb.Query()
.Where(e => e.PerformanceReviews![reviewKey] == "Excellent")
.Take(20)
.ToListAsync();
results.Should().NotBeEmpty();
results.Should().AllSatisfy(r =>
r.Props.PerformanceReviews![(2024, "Q1")].Should().Be("Excellent"));
}
///
/// The provider's bound on a dictionary key (_array_index), or null when it has none. SQL Server: 430
/// (NVARCHAR(430)); PostgreSQL and SQLite: unbounded text.
///
protected virtual int? DictionaryKeyBound => null;
private static RedbObject EmployeeWithKey(int index, string key)
{
var employee = TestDataFactory.CreateEmployee(index);
employee.Props.PhoneDirectory = new Dictionary { ["desk"] = "100", [key] = "200" };
return employee;
}
[Fact]
public async Task DictKeyOverTheProviderBound_IsRefusedBeforeTheWrite_OnEverySavePath()
{
var bound = DictionaryKeyBound ?? 430;
var tooLong = new string('k', bound + 1);
var fits = new string('f', bound);
// An object already stored: a refused re-save must leave its dictionary as it was.
var stored = EmployeeWithKey(9100, "home");
var storedId = await Redb.SaveAsync(stored);
stored.Props.PhoneDirectory![tooLong] = "300";
Func resave = () => Redb.SaveAsync(stored);
Func single = () => Redb.SaveAsync(EmployeeWithKey(9101, tooLong));
Func batch = () => Redb.SaveAsync(new List>
{ EmployeeWithKey(9102, "a"), EmployeeWithKey(9103, tooLong) });
Func addNew = () => Redb.AddNewObjectsAsync(new List>
{ EmployeeWithKey(9104, tooLong) });
if (DictionaryKeyBound is { } limit)
{
foreach (var act in new[] { resave, single, batch, addNew })
{
var refused = (await act.Should().ThrowAsync()).Which;
refused.Length.Should().Be(limit + 1);
refused.Limit.Should().Be(limit);
refused.Property.Should().Be(nameof(EmployeeProps.PhoneDirectory));
}
var reloaded = await Redb.LoadAsync(storedId);
reloaded!.Props.PhoneDirectory.Should().BeEquivalentTo(
new Dictionary { ["desk"] = "100", ["home"] = "200" },
"the refusal comes before any row of the save is written");
}
else
{
foreach (var act in new[] { resave, single, batch, addNew })
await act.Should().NotThrowAsync("this provider stores keys of any length");
var reloaded = await Redb.LoadAsync(storedId);
reloaded!.Props.PhoneDirectory.Should().ContainKey(tooLong);
}
var fitId = await Redb.SaveAsync(EmployeeWithKey(9105, fits));
(await Redb.LoadAsync(fitId))!.Props.PhoneDirectory.Should().ContainKey(fits,
"a key exactly at the bound fits");
}
}