Path: redb/redb.Tests.Integration/Tests/Base/HashCanonCollisionTestsBase.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/HashCanonCollisionTestsBase.cs
----
using System.Globalization;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Utils;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// Changes the hash canon must see. Under ChangeTracking with AutoRecomputeHash an object whose recomputed hash equals
/// the stored one skips the whole value pipeline, and a collection or nested class whose hash equals the stored one is
/// skipped by the diff. Two different values with the same canon string therefore lose the change: these tests edit an
/// object from the first value into the second, save and load, and expect the second. The pairs collide on the
/// separators of the canon ('|' between class properties, ',' between elements and entries, '=' between a key and its
/// value) and on dictionary keys canonicalised through ToString(). Run on the ChangeTracking fixtures.
///
public abstract class HashCanonCollisionTestsBase : IAsyncLifetime
{
protected readonly IRedbService Redb;
protected HashCanonCollisionTestsBase(IRedbService redb) => Redb = redb;
public async Task InitializeAsync() => await ResetAsync();
public async Task DisposeAsync() => await ResetAsync();
private async Task ResetAsync()
{
foreach (var scheme in new[] { "HashCanonProbe", "HashCanonClassKeyProbe", "HashCanonTupleKeyProbe" })
await Redb.Context.ExecuteAsync(
$"DELETE FROM _objects WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{scheme}')");
}
/// Saves , loads it, applies , saves, and loads again.
private async Task> SaveEditLoadAsync(TProps first, Action edit)
where TProps : class, new()
{
await Redb.SyncSchemeAsync();
var id = await Redb.SaveAsync(new RedbObject { name = "hash-canon", Props = first });
var loaded = await Redb.LoadAsync(id, depth: 2);
edit(loaded!.Props);
await Redb.SaveAsync(loaded);
return (await Redb.LoadAsync(id, depth: 2))!;
}
[Fact]
public async Task PipeInsideAString_ChangeIsSaved()
{
// Canon of both: "x|y|" followed by the same tail.
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { A = "x|y", B = "" },
p => { p.A = "x"; p.B = "y|"; });
after.Props.A.Should().Be("x");
after.Props.B.Should().Be("y|");
}
[Fact]
public async Task CommaInsideAnElement_ChangeIsSaved()
{
// Canon of both: "[a,b]".
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Tags = ["a,b"] },
p => p.Tags = ["a", "b"]);
after.Props.Tags.Should().Equal("a", "b");
}
[Fact]
public async Task EqualsAndCommaInsideADictionaryValue_ChangeIsSaved()
{
// Canon of both: "{a=1,b=2}".
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Dict = new() { ["a"] = "1,b=2" } },
p => p.Dict = new() { ["a"] = "1", ["b"] = "2" });
after.Props.Dict.Should().Equal(new Dictionary { ["a"] = "1", ["b"] = "2" });
}
[Fact]
public async Task CollectionHeadCollision_WithAnotherChange_CollectionIsSaved()
{
// The root hash differs (Label), so the object takes the value pipeline; the head hash of Tags collides and
// the diff skips the collection.
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Label = "one", Tags = ["a,b"] },
p => { p.Label = "two"; p.Tags = ["a", "b"]; });
after.Props.Label.Should().Be("two");
after.Props.Tags.Should().Equal("a", "b");
}
[Fact]
public async Task ClassElementCollision_WithAnotherChange_ElementIsSaved()
{
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Label = "one", Items = [new HashCanonPair { A = "x|y", B = "" }] },
p => { p.Label = "two"; p.Items = [new HashCanonPair { A = "x", B = "y|" }]; });
after.Props.Label.Should().Be("two");
after.Props.Items.Should().ContainSingle();
after.Props.Items![0].A.Should().Be("x");
after.Props.Items[0].B.Should().Be("y|");
}
[Fact]
public async Task NestedClassCollision_WithAnotherChange_NestedClassIsSaved()
{
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Label = "one", Inner = new HashCanonPair { A = "x|y", B = "" } },
p => { p.Label = "two"; p.Inner = new HashCanonPair { A = "x", B = "y|" }; });
after.Props.Label.Should().Be("two");
after.Props.Inner!.A.Should().Be("x");
after.Props.Inner.B.Should().Be("y|");
}
[Fact]
public async Task NullElementAgainstTheStringNull_ChangeIsSaved()
{
// Canon of both: "[null]".
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Tags = [null] },
p => p.Tags = ["null"]);
after.Props.Tags.Should().Equal("null");
}
[Fact]
public async Task NullPropertyAgainstAnEmptyString_ChangeIsSaved()
{
// Canon of both: "" in the place of A.
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { A = null, B = "b" },
p => p.A = "");
after.Props.A.Should().Be("");
}
[Fact]
public async Task EmptyCollectionAgainstOneEmptyString_ChangeIsSaved()
{
// Canon of both: "[]".
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Tags = [] },
p => p.Tags = [""]);
after.Props.Tags.Should().Equal("");
}
[Fact]
public async Task NullDictionaryValueAgainstTheStringNull_ChangeIsSaved()
{
// Canon of both: "{a=null}".
var after = await SaveEditLoadAsync(
new HashCanonProbeProps { Dict = new() { ["a"] = null! } },
p => p.Dict = new() { ["a"] = "null" });
after.Props.Dict.Should().Equal(new Dictionary { ["a"] = "null" });
}
[Fact]
public async Task ClassKeyWithoutToString_KeyOnlyChangeIsSaved()
{
// Canon of both keys through ToString(): the type name.
var after = await SaveEditLoadAsync(
new HashCanonClassKeyProps { ByKey = new() { [new HashCanonKey { Code = "a", Rank = 1 }] = "v" } },
p => p.ByKey = new() { [new HashCanonKey { Code = "b", Rank = 2 }] = "v" });
after.Props.ByKey.Should().ContainSingle();
after.Props.ByKey!.Keys.Single().Code.Should().Be("b");
after.Props.ByKey.Keys.Single().Rank.Should().Be(2);
}
[Fact]
public async Task TupleKeyOfDoubleAndDate_HashDoesNotDependOnTheCulture()
{
await Redb.SyncSchemeAsync();
var at = new DateTime(2026, 9, 16, 12, 30, 15, DateTimeKind.Utc);
var props = new HashCanonTupleKeyProps { ByTuple = new() { [(1.5, at)] = "v" } };
var id = await Under("ru-RU", () => Redb.SaveAsync(new RedbObject { name = "hash-canon", Props = props }));
var loaded = await Under("en-US", () => Redb.LoadAsync(id, depth: 2));
var recomputed = Under("en-US", () => Task.FromResult(RedbHash.ComputeFor(loaded!))).Result;
recomputed.Should().Be(loaded!.hash,
"a node with another culture must recompute the hash the first node stored, or every save rewrites the object");
}
private static async Task Under(string culture, Func> action)
{
var previous = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
try { return await action(); }
finally { CultureInfo.CurrentCulture = previous; }
}
}