Files
CBDDC/tests/ZB.MOM.WW.CBDDC.Core.Tests/OplogEntryTests.cs
Joseph Doherty 8e97061ab8
All checks were successful
NuGet Package Publish / nuget (push) Successful in 1m14s
Implement in-process multi-dataset sync isolation across core, network, persistence, and tests
2026-02-22 11:58:34 -05:00

98 lines
3.4 KiB
C#
Executable File

using System.Globalization;
using System.Text.Json;
namespace ZB.MOM.WW.CBDDC.Core.Tests;
public class OplogEntryTests
{
/// <summary>
/// Verifies that hash computation is deterministic even when payload content differs.
/// </summary>
[Fact]
public void ComputeHash_ShouldBeDeterministic_RegardlessOfPayload()
{
// Arrange
var collection = "test-collection";
var key = "test-key";
var op = OperationType.Put;
var timestamp = new HlcTimestamp(100, 0, "node-1");
var prevHash = "prev-hash";
var payload1 = JsonDocument.Parse("{\"prop\": 1}").RootElement;
var payload2 = JsonDocument.Parse("{\"prop\": 2, \"extra\": \"whitespace\"}").RootElement;
// Act
var entry1 = new OplogEntry(collection, key, op, payload1, timestamp, prevHash);
var entry2 = new OplogEntry(collection, key, op, payload2, timestamp, prevHash);
// Assert
entry2.Hash.ShouldBe(entry1.Hash);
}
/// <summary>
/// Verifies that hash computation uses invariant culture formatting for timestamp values.
/// </summary>
[Fact]
public void ComputeHash_ShouldUseInvariantCulture_ForTimestamp()
{
// Arrange
var originalCulture = CultureInfo.CurrentCulture;
try
{
var culture = CultureInfo.GetCultureInfo("de-DE");
CultureInfo.CurrentCulture = culture;
var timestamp = new HlcTimestamp(123456789, 1, "node");
var entry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
// Act
string hash = entry.ComputeHash();
// Assert
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var expectedEntry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
hash.ShouldBe(expectedEntry.Hash);
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
}
}
/// <summary>
/// Verifies that an entry is valid when its stored hash matches computed content.
/// </summary>
[Fact]
public void IsValid_ShouldReturnTrue_WhenHashMatches()
{
var timestamp = new HlcTimestamp(100, 0, "node-1");
var entry = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev");
entry.IsValid().ShouldBeTrue();
}
/// <summary>
/// Verifies that entries default to the primary dataset when dataset is omitted.
/// </summary>
[Fact]
public void Constructor_ShouldDefaultDatasetId_ToPrimary()
{
var entry = new OplogEntry("col", "key", OperationType.Put, null, new HlcTimestamp(1, 0, "node"), "prev");
entry.DatasetId.ShouldBe(DatasetId.Primary);
}
/// <summary>
/// Verifies that hash computation includes dataset identity to prevent cross-dataset collisions.
/// </summary>
[Fact]
public void ComputeHash_ShouldDiffer_WhenDatasetDiffers()
{
var timestamp = new HlcTimestamp(100, 0, "node-1");
var primary = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev", datasetId: "primary");
var logs = new OplogEntry("col", "key", OperationType.Put, null, timestamp, "prev", datasetId: "logs");
logs.Hash.ShouldNotBe(primary.Hash);
}
}