Path: redb/redb.Tests.Integration/Tests/Unit/RedbHashRenameOrderTests.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/Unit/RedbHashRenameOrderTests.cs
----
using System.Security.Cryptography;
using System.Text;
using redb.Core.Attributes;
using redb.Core.Models.Entities;
using redb.Core.Utils;
namespace redb.Tests.Integration.Tests.Unit;
///
/// The order of the properties in the hash canon: by the first name of the chain,
/// by the property name when there is none, then by the property name; both ordinal. A class without the attribute
/// hashes exactly as before the attribute existed (ordered by name), so no stored hash of an existing database moves,
/// and a rename keeps the property where it sorted, so the hashes stored before the rename stay valid after it.
///
public sealed class RedbHashRenameOrderTests
{
// Declared out of name order on purpose; ordinal order is Alpha, Beta, Mid, alpha, zeta.
private sealed class PlainProps
{
public string zeta { get; set; } = "";
public string Mid { get; set; } = "";
public long Beta { get; set; }
public string alpha { get; set; } = "";
public PlainNested Alpha { get; set; } = new();
}
private sealed class PlainNested
{
public string Street { get; set; } = "";
public string City { get; set; } = "";
}
private sealed class TitleV1
{
public string Alpha { get; set; } = "";
public string Title { get; set; } = "";
public string Zulu { get; set; } = "";
}
private sealed class TitleV2
{
[RedbRename("Title")]
public string Aaron { get; set; } = "";
public string Alpha { get; set; } = "";
public string Zulu { get; set; } = "";
}
private sealed class TitleV3
{
[RedbRename("Title")]
[RedbRename("Aaron")]
public string Zzz { get; set; } = "";
public string Alpha { get; set; } = "";
public string Zulu { get; set; } = "";
}
private sealed class SwapV1
{
public string Left { get; set; } = "";
public string Right { get; set; } = "";
}
private sealed class SwapV2
{
[RedbRename("Left")]
[RedbRename("Tmp")]
public string Right { get; set; } = "";
[RedbRename("Right")]
public string Left { get; set; } = "";
}
private sealed class ReuseProps
{
public string Title { get; set; } = "";
[RedbRename("Title")]
public string Heading { get; set; } = "";
}
private static Guid Md5(string payload) => new(MD5.HashData(Encoding.UTF8.GetBytes(payload)));
private static Guid HashOf(TProps props) where TProps : class, new()
=> RedbHash.ComputeFor(new RedbObject { id = 1, name = "n", Props = props })!.Value;
/// The object hash over a Props canon built by hand: header canon, then the Props hash.
private static Guid Expected(TProps props, string propsCanon) where TProps : class, new()
=> Md5(RedbHash.HeaderCanon(new RedbObject { id = 1, name = "n", Props = props }) + "||" +
Md5(propsCanon).ToString("N"));
[Fact]
public void AClassWithoutTheAttribute_HashesInNameOrder_AsBefore()
{
var props = new PlainProps
{
zeta = "z", Mid = "m", Beta = 2, alpha = "a",
Alpha = new PlainNested { Street = "Gate", City = "Oslo" },
};
var nested = Md5("Oslo|Gate").ToString("N");
HashOf(props).Should().Be(Expected(props, $"{nested}|2|m|a|z"),
"the canon of a class without [RedbRename] is the ordinal name order the stored hashes were computed in");
}
[Fact]
public void ARename_KeepsTheHash_WhereTheNewNameSortsElsewhere()
{
var before = HashOf(new TitleV1 { Alpha = "a", Title = "t", Zulu = "z" });
HashOf(new TitleV2 { Alpha = "a", Aaron = "t", Zulu = "z" }).Should().Be(before);
}
[Fact]
public void AChain_SortsByItsFirstName()
{
var before = HashOf(new TitleV1 { Alpha = "a", Title = "t", Zulu = "z" });
HashOf(new TitleV3 { Alpha = "a", Zzz = "t", Zulu = "z" }).Should().Be(before);
}
[Fact]
public void ASwap_KeepsTheStoredHash_AndSeesTheValuesSwappedBack()
{
var stored = HashOf(new SwapV1 { Left = "x", Right = "y" });
// Swapped through Tmp: the old Left is Right now, the old Right is Left.
HashOf(new SwapV2 { Right = "x", Left = "y" }).Should().Be(stored);
HashOf(new SwapV2 { Right = "y", Left = "x" }).Should().NotBe(stored,
"ordered by the current names these values would hash as the stored row, and the save would skip them");
}
[Fact]
public void AFreedNameTakenAgain_SortsAfterTheRenamedProperty()
{
var props = new ReuseProps { Heading = "h", Title = "t" };
// Both sort by Title; the tie is broken by the property name: Heading, then Title.
HashOf(props).Should().Be(Expected(props, "h|t"));
}
}