using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors; using Xunit; namespace Admin.NET.Plugin.AiDOP.Tests.DataPlatform; public sealed class MdpRelationalValueTests { [Fact] public void RejectsInvalidTextAndAmbiguousOrMissingLocalTime() { Assert.Equal("物料", RelationalFieldValues.Decode([0xce, 0xef, 0xc1, 0xcf], "gbk")); Assert.Throws(() => RelationalFieldValues.Decode([0xc3, 0x28], "utf-8")); var zone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York"); Assert.Throws(() => RelationalFieldValues.ToUtc(new DateTime(2026, 11, 1, 1, 30, 0), zone)); Assert.Throws(() => RelationalFieldValues.ToUtc(new DateTime(2026, 3, 8, 2, 30, 0), zone)); } [Fact] public void DecimalFitsOnlyWhenNoSignificantDigitIsLost() { Assert.Equal(1.23m, RelationalFieldValues.DecimalValue("1.2300", 4, 2)); Assert.Throws(() => RelationalFieldValues.DecimalValue("1.2301", 4, 2)); Assert.Throws(() => RelationalFieldValues.DecimalValue("100.00", 4, 2)); Assert.Throws(() => RelationalFieldValues.DecimalValue("0.0000000000000000000000000000001", 28, 28)); } [Fact] public void VersionedRawMetadataCannotChangeTheLegacyPushDecision() { var identity = new IngestDatasetIdentity(1, 2, 3, 4, "TEST", PurchaseOrderSchemaRegistry.DatasetKey); var prepared = new IngestBatchPreparer(new PurchaseOrderSchemaRegistry()).Prepare(identity, PurchaseOrderSchemaRegistry.SourceVersion, """ {"data":{"list":[{"PoNo":"P1","PoLine":"01","ItemCode":"X","OrderQty":1, "sourceUpdatedAt":"2026-11-21T00:00:00Z","_mdpConversionError":["optionalField"]}]}} """u8, new IngestLimits(10240, 10, 16)); Assert.Single(PurchaseBatchProcessor.Process(prepared, "test").Package.Items); var trusted = prepared with { Batch = prepared.Batch with { SourceWindow = new(null, null, IngestSourceWindow.VerifiedRelationalSnapshot) }}; var result = PurchaseBatchProcessor.Process(trusted, "test"); Assert.Empty(result.Package.Items); Assert.Equal("RELATIONAL_CONVERSION_FAILED", Assert.Single(result.Quarantines).Reason); Assert.True(RelationalReconciliation.IsTerminal("PARTIAL")); Assert.False(RelationalReconciliation.IsTerminal("PROCESSING")); } [Theory] [InlineData(4, false, "old", "Stale")] [InlineData(5, true, "same", "Duplicate")] [InlineData(5, true, "other", "Conflict")] [InlineData(6, false, "new", "Apply")] public void EarlierOrConflictingRowsCannotReplaceTheLatestTombstone(long version, bool deleted, string hash, string expected) { var previous = new RelationalVersionedRow(5, true, "same"); Assert.Equal(expected, previous.Decide(new(version, deleted, hash)).ToString()); } }