using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common.Tests; public sealed class ValueWriterPrepareTests { private static readonly DateTime T = new(2026, 10, 9, 1, 2, 3, DateTimeKind.Utc); [Fact] public void Non_finite_doubles_become_Empty_and_are_forced_Bad() { const uint uncertain = 0x40000000u; const uint badNoCommunication = 0x80310000u; var rows = LiveWritePreparer.Prepare( [ new(T, HistorianValue.FromDouble(double.NaN), uncertain), new(T.AddTicks(1), HistorianValue.FromDouble(double.PositiveInfinity), 0), new(T.AddTicks(2), HistorianValue.FromDouble(double.NegativeInfinity), badNoCommunication), new(T.AddTicks(3), HistorianValue.FromDouble(1.5), 0), ], () => T); rows.Select(r => r.Value.Kind).ShouldBe([HistorianValueKind.Empty, HistorianValueKind.Empty, HistorianValueKind.Empty, HistorianValueKind.Double]); rows.Select(r => r.StatusCode).ShouldBe([HistorianStatusCodes.BadOutOfRange, HistorianStatusCodes.BadOutOfRange, badNoCommunication, 0u]); } [Fact] public void A_non_bad_Empty_is_stored_BadNoData_and_a_bad_Empty_keeps_its_code() { var rows = LiveWritePreparer.Prepare( [ new(T, HistorianValue.Empty, 0), new(T.AddTicks(1), HistorianValue.Empty, 0x40A40000u), new(T.AddTicks(2), HistorianValue.Empty, 0x80310000u), ], () => T); rows.Select(r => r.StatusCode).ShouldBe([HistorianStatusCodes.BadNoData, HistorianStatusCodes.BadNoData, 0x80310000u]); } [Fact] public void Duplicate_timestamps_keep_the_first_and_null_timestamp_uses_the_clock() { var rows = LiveWritePreparer.Prepare( [ new(T, HistorianValue.FromInt64(1), 0), new(T, HistorianValue.FromInt64(2), 0), new(null, HistorianValue.FromInt64(3), 0), ], () => T.AddDays(1)); rows.Count.ShouldBe(2); rows[0].Value.Integer.ShouldBe(1); rows[1].TimeUtc.ShouldBe(T.AddDays(1)); } [Fact] public void Data_type_mapping_is_one_table() { HistorianDataTypes.FromKind(HistorianValueKind.Double).ShouldBe((byte)DriverDataType.Float64); HistorianDataTypes.FromKind(HistorianValueKind.Boolean).ShouldBe((byte)DriverDataType.Boolean); HistorianDataTypes.FromKind(HistorianValueKind.Empty).ShouldBe((byte)DriverDataType.Float64); } }