Path: redb/redb.Tests.Integration/Tests/Base/DictionaryEntryKeyFilterTestsBase.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/DictionaryEntryKeyFilterTestsBase.cs
----
using System.Linq.Expressions;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// A filter on a field of a dictionary entry whose value is a class (ByTuple[(2024, "Q1")].Name == v) finds the
/// entry by its key as it is stored, for every key type: text, number, tuple, bool, DateTime and double. Each object
/// has two entries per dictionary, so a key written in another form than the stored one finds nothing, and a key that
/// matched every entry would find the other one too. Compared with the same LINQ over the seeded objects in memory.
///
public abstract class DictionaryEntryKeyFilterTestsBase
{
protected readonly IRedbService Redb;
protected DictionaryEntryKeyFilterTestsBase(IRedbService redb) => Redb = redb;
private static readonly DateTime DayOne = new(2024, 3, 1, 8, 30, 0, DateTimeKind.Utc);
private static readonly DateTime DayTwo = new(2024, 3, 2, 8, 30, 0, DateTimeKind.Utc);
private async Task<(string Tag, List Props)> SeedAsync()
{
await Redb.SyncSchemeAsync();
var tag = "dk-" + Guid.NewGuid().ToString("N")[..8];
var props = new List();
for (var i = 0; i < 4; i++)
{
var hit = i % 2 == 0 ? $"{tag}-hit" : $"{tag}-miss";
KeyedEntry First() => new() { Name = hit, Rank = i };
KeyedEntry Second() => new() { Name = $"{tag}-second", Rank = 10 + i };
var p = new DictionaryKeyProps
{
Marker = tag,
ByText = new() { ["a"] = First(), ["b"] = Second() },
ByNumber = new() { [1] = First(), [2] = Second() },
ByTuple = new() { [(2024, "Q1")] = First(), [(2024, "Q2")] = Second() },
ByFlag = new() { [true] = First(), [false] = Second() },
ByDate = new() { [DayOne] = First(), [DayTwo] = Second() },
ByRatio = new() { [0.1] = First(), [2.25] = Second() }
};
await Redb.SaveAsync(new RedbObject { name = $"{tag}-{i}", Props = p });
props.Add(p);
}
return (tag, props);
}
private async Task MatchesLinqAsync(Func>> filterOf)
{
var (tag, seeded) = await SeedAsync();
var filter = filterOf(tag + "-hit");
var expected = seeded.Where(filter.Compile()).Select(p => p.ByNumber![1].Rank).ToList();
expected.Should().HaveCount(2, "precondition");
var query = Redb.Query().Where(p => p.Marker == tag).Where(filter);
(await query.ToListAsync()).Select(o => o.Props.ByNumber![1].Rank).Should().BeEquivalentTo(expected);
(await query.CountAsync()).Should().Be(2);
}
[Fact]
public Task TextKey() => MatchesLinqAsync(v => p => p.ByText!["a"].Name == v);
[Fact]
public Task NumberKey() => MatchesLinqAsync(v => p => p.ByNumber![1].Name == v);
[Fact]
public Task TupleKey()
{
// An expression tree takes no tuple literal: the key is a captured value, as in a query of an application.
var quarter = (2024, "Q1");
return MatchesLinqAsync(v => p => p.ByTuple![quarter].Name == v);
}
[Fact]
public Task BoolKey() => MatchesLinqAsync(v => p => p.ByFlag![true].Name == v);
[Fact]
public Task DateTimeKey() => MatchesLinqAsync(v => p => p.ByDate![DayOne].Name == v);
[Fact]
// 0.1 is stored in its round-trip form (0.10000000000000001), which ToString() does not give.
public Task DoubleKey() => MatchesLinqAsync(v => p => p.ByRatio![0.1].Name == v);
}