141 lines
6.4 KiB
C#
141 lines
6.4 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Persistence;
|
|
using ZB.MOM.WW.ScadaBridge.TestSupport;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Persistence;
|
|
|
|
/// <summary>
|
|
/// Site-local SQLite <c>native_alarm_state</c> store — mirrored native alarm
|
|
/// condition snapshots keyed by (instance, source canonical name, source reference).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Backed by a real temp-file LocalDb: <see cref="SiteStorageService"/> takes an
|
|
/// <c>ILocalDb</c> rather than a connection string, and LocalDb has no in-memory mode.
|
|
/// </remarks>
|
|
public class NativeAlarmStateStoreTests : IAsyncLifetime, IDisposable
|
|
{
|
|
private readonly TestLocalDb _localDb;
|
|
private SiteStorageService _storage = null!;
|
|
|
|
/// <summary>Initializes the tests with a temporary local database.</summary>
|
|
public NativeAlarmStateStoreTests()
|
|
{
|
|
_localDb = TestLocalDb.CreateTemp("nas");
|
|
}
|
|
|
|
/// <summary>Prepares the test fixture before the first test runs.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public async Task InitializeAsync()
|
|
{
|
|
_storage = new SiteStorageService(_localDb.Db, NullLogger<SiteStorageService>.Instance);
|
|
await _storage.InitializeAsync();
|
|
}
|
|
|
|
/// <summary>Does nothing; cleanup happens in <c>Dispose</c>.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public Task DisposeAsync() => Task.CompletedTask;
|
|
|
|
/// <summary>Verifies that an upserted alarm row can be read back unchanged.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Upsert_Then_Get_RoundTrips()
|
|
{
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "Tank01.Hi", "{\"Active\":true}", DateTimeOffset.UnixEpoch);
|
|
|
|
var rows = await _storage.GetNativeAlarmsAsync("inst", "Src");
|
|
|
|
Assert.Single(rows);
|
|
Assert.Equal("Tank01.Hi", rows[0].SourceReference);
|
|
Assert.Equal("{\"Active\":true}", rows[0].ConditionJson);
|
|
Assert.Equal(DateTimeOffset.UnixEpoch, rows[0].LastTransitionAt);
|
|
}
|
|
|
|
/// <summary>Verifies that upserting the same key replaces the condition and timestamp.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Upsert_SameKey_ReplacesConditionAndTimestamp()
|
|
{
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "Tank01.Hi", "{\"Active\":true}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "Tank01.Hi", "{\"Active\":false}", DateTimeOffset.UnixEpoch.AddMinutes(5));
|
|
|
|
var rows = await _storage.GetNativeAlarmsAsync("inst", "Src");
|
|
|
|
Assert.Single(rows);
|
|
Assert.Equal("{\"Active\":false}", rows[0].ConditionJson);
|
|
Assert.Equal(DateTimeOffset.UnixEpoch.AddMinutes(5), rows[0].LastTransitionAt);
|
|
}
|
|
|
|
/// <summary>Verifies that a read is scoped to the instance and source canonical name.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Get_ScopesToInstanceAndSourceCanonicalName()
|
|
{
|
|
await _storage.UpsertNativeAlarmAsync("inst", "SrcA", "Tank01.Hi", "{}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("inst", "SrcB", "Tank02.Hi", "{}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("other", "SrcA", "Tank09.Hi", "{}", DateTimeOffset.UnixEpoch);
|
|
|
|
var rows = await _storage.GetNativeAlarmsAsync("inst", "SrcA");
|
|
|
|
Assert.Single(rows);
|
|
Assert.Equal("Tank01.Hi", rows[0].SourceReference);
|
|
}
|
|
|
|
/// <summary>Verifies that deleting removes only the single targeted row.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Delete_RemovesSingleRow()
|
|
{
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "Tank01.Hi", "{}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "Tank01.Lo", "{}", DateTimeOffset.UnixEpoch);
|
|
|
|
await _storage.DeleteNativeAlarmAsync("inst", "Src", "Tank01.Hi");
|
|
|
|
var rows = await _storage.GetNativeAlarmsAsync("inst", "Src");
|
|
Assert.Single(rows);
|
|
Assert.Equal("Tank01.Lo", rows[0].SourceReference);
|
|
}
|
|
|
|
/// <summary>Upserts and deletes apply together in one batch (review 2026-10-21 01/SR-P2).</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ApplyChanges_UpsertsAndDeletesInOneBatch()
|
|
{
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "A", "{}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "B", "{}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("inst", "Src", "C", "{}", DateTimeOffset.UnixEpoch);
|
|
|
|
await _storage.ApplyNativeAlarmChangesAsync("inst", "Src",
|
|
[("D", "{\"n\":1}", null, DateTimeOffset.UnixEpoch)],
|
|
["A", "B"]);
|
|
|
|
var refs = (await _storage.GetNativeAlarmsAsync("inst", "Src")).Select(r => r.SourceReference).OrderBy(x => x).ToArray();
|
|
Assert.Equal(new[] { "C", "D" }, refs);
|
|
}
|
|
|
|
/// <summary>Verifies that clearing an instance removes all of its sources and leaves other instances untouched.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ClearForInstance_RemovesAllSourcesForInstanceOnly()
|
|
{
|
|
await _storage.UpsertNativeAlarmAsync("inst", "SrcA", "Tank01.Hi", "{}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("inst", "SrcB", "Tank02.Hi", "{}", DateTimeOffset.UnixEpoch);
|
|
await _storage.UpsertNativeAlarmAsync("other", "SrcA", "Tank09.Hi", "{}", DateTimeOffset.UnixEpoch);
|
|
|
|
await _storage.ClearNativeAlarmsForInstanceAsync("inst");
|
|
|
|
Assert.Empty(await _storage.GetNativeAlarmsAsync("inst", "SrcA"));
|
|
Assert.Empty(await _storage.GetNativeAlarmsAsync("inst", "SrcB"));
|
|
Assert.Single(await _storage.GetNativeAlarmsAsync("other", "SrcA"));
|
|
}
|
|
|
|
/// <summary>Releases the resources held by this instance.</summary>
|
|
public void Dispose()
|
|
{
|
|
// Dispose first — the master connection anchors the WAL, so the sidecars
|
|
// cannot be removed while it is open.
|
|
var path = _localDb.Path;
|
|
_localDb.Dispose();
|
|
TestLocalDb.DeleteFiles(path);
|
|
}
|
|
}
|