Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Uns/UnsTreeServiceScriptedAlarmTests.cs
T
Joseph Doherty b211911f98
v2-ci / unit-tests (push) Failing after 13m22s
test: migrate AdminUI/ControlPlane/Runtime/Host tests to xunit.v3 (#756)
Akka.TestKit.Xunit2 -> Akka.TestKit.Xunit 1.5.62 (xunit.v3 TestKit; needs xunit.v3 3.2.2, set via
VersionOverride on the four projects). Removes the xunit 2.9.3 pin. IAsyncLifetime returns ValueTask,
SqlServerFactAttribute gains the source-info ctor, and xUnit1051 call sites pass
TestContext.Current.CancellationToken. Test counts unchanged.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-10 08:57:33 -05:00

103 lines
4.5 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
[Trait("Category", "Unit")]
public sealed class UnsTreeServiceScriptedAlarmTests
{
private static UnsTreeService SeededService()
{
var dbName = $"uns-alarm-{Guid.NewGuid():N}";
UnsTreeTestDb.SeedNamed(dbName);
return new UnsTreeService(UnsTreeTestDb.Factory(dbName));
}
private static ScriptedAlarmInput Sample(string id = "SA-1") =>
new(id, "Over-temp", "LimitAlarm", 700, "{TagPath} hot", "SCRIPT-1", HistorizeToAveva: true, Retain: true, Enabled: true);
[Fact]
public async Task Create_then_list_and_load_roundtrips()
{
var svc = SeededService();
var create = await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample(), TestContext.Current.CancellationToken);
create.Ok.ShouldBeTrue();
create.CreatedId.ShouldBe("SA-1");
var rows = await svc.LoadAlarmsForEquipmentAsync(UnsTreeTestDb.SeededEquipmentId, TestContext.Current.CancellationToken);
rows.Count.ShouldBe(1);
rows[0].Name.ShouldBe("Over-temp");
rows[0].Severity.ShouldBe(700);
var dto = await svc.LoadScriptedAlarmAsync("SA-1", TestContext.Current.CancellationToken);
dto.ShouldNotBeNull();
dto!.EquipmentId.ShouldBe(UnsTreeTestDb.SeededEquipmentId);
dto.HistorizeToAveva.ShouldBeTrue();
dto.RowVersion.ShouldNotBeNull();
}
[Fact]
public async Task Create_rejects_duplicate_id()
{
var svc = SeededService();
(await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample(), TestContext.Current.CancellationToken)).Ok.ShouldBeTrue();
var dup = await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample(), TestContext.Current.CancellationToken);
dup.Ok.ShouldBeFalse();
dup.Error.ShouldNotBeNull();
}
[Fact]
public async Task Update_changes_fields()
{
var svc = SeededService();
await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample(), TestContext.Current.CancellationToken);
var dto = await svc.LoadScriptedAlarmAsync("SA-1", TestContext.Current.CancellationToken);
var upd = await svc.UpdateScriptedAlarmAsync("SA-1",
Sample() with { Name = "Renamed", Severity = 250, HistorizeToAveva = false }, dto!.RowVersion, TestContext.Current.CancellationToken);
upd.Ok.ShouldBeTrue();
var after = await svc.LoadScriptedAlarmAsync("SA-1", TestContext.Current.CancellationToken);
after!.Name.ShouldBe("Renamed");
after.Severity.ShouldBe(250);
after.HistorizeToAveva.ShouldBeFalse();
}
[Fact]
public async Task Delete_removes_row()
{
var svc = SeededService();
await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample(), TestContext.Current.CancellationToken);
var dto = await svc.LoadScriptedAlarmAsync("SA-1", TestContext.Current.CancellationToken);
(await svc.DeleteScriptedAlarmAsync("SA-1", dto!.RowVersion, TestContext.Current.CancellationToken)).Ok.ShouldBeTrue();
(await svc.LoadAlarmsForEquipmentAsync(UnsTreeTestDb.SeededEquipmentId, TestContext.Current.CancellationToken)).ShouldBeEmpty();
}
[Fact]
public async Task Create_rejects_duplicate_name_on_same_equipment()
{
var svc = SeededService();
(await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample("SA-1"), TestContext.Current.CancellationToken)).Ok.ShouldBeTrue();
// Same Name ("Over-temp" from Sample), different id, same equipment → rejected.
var dup = await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample("SA-2"), TestContext.Current.CancellationToken);
dup.Ok.ShouldBeFalse();
dup.Error.ShouldNotBeNull();
dup.Error!.ShouldContain("already exists");
}
[Fact]
public async Task LoadAlarmsForEquipment_excludes_other_equipment()
{
var svc = SeededService();
await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, Sample("SA-MINE"), TestContext.Current.CancellationToken);
await svc.CreateScriptedAlarmAsync("EQ-OTHER", Sample("SA-OTHER") with { Name = "Other-alarm" }, TestContext.Current.CancellationToken);
var rows = await svc.LoadAlarmsForEquipmentAsync(UnsTreeTestDb.SeededEquipmentId, TestContext.Current.CancellationToken);
rows.Count.ShouldBe(1);
rows[0].ScriptedAlarmId.ShouldBe("SA-MINE");
}
}