90de55535d
One migration, ScriptForeignKeysAndExternalIdUniqueness: - FK_VirtualTag_Script / FK_ScriptedAlarm_Script (Restrict) onto Script.ScriptId, which becomes NOT NULL and an alternate key (unique constraint UX_Script_LogicalId replaces the index). - UX_Equipment_ZTag / UX_Equipment_SAPID filtered UNIQUE indexes (blank = no identifier, the deploy gate's reading) replace the non-unique IX_ ones. - ExternalIdReservation + sp_ReleaseExternalIdReservation dropped (decision: retire — no writer since the generation model; uniqueness is gate + indexes). The /reservations page, query, ReservationKind, DraftSnapshot.ActiveReservations and the reservation pre-flight rule go too. - A read-only pre-check THROWs 51049 naming every offending row class (NULL ScriptId, dangling VirtualTag/ScriptedAlarm script, duplicated ZTag/SAPID, any reservation row) and changes nothing; Down restores the previous shape. Authoring mirrors so the constraints are never the first to say no: UnsTreeService refuses a missing script for VirtualTag/ScriptedAlarm create/update, and a ZTag/SAPID another equipment holds on create/update/import (owner named; import keeps the clean rows); values stored trimmed. ScriptEdit maps a concurrent-bind FK refusal to a message. Migration pre-check, Down round-trip, FK and index behaviour pinned against real SQL with positive controls.
149 lines
7.3 KiB
C#
149 lines
7.3 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
|
|
|
/// <summary>
|
|
/// lmxopcua#1049 / #1040: the authoring mirrors of the new database constraints. A ZTag / SAPID another equipment
|
|
/// holds (case-insensitive, trimmed) is refused on create, update and CSV import with the owner named — not left
|
|
/// to <c>UX_Equipment_ZTag</c> / <c>UX_Equipment_SAPID</c> to fail the save (or a whole import batch) with no row
|
|
/// named; a VirtualTag / scripted alarm naming a script that does not exist is refused before
|
|
/// <c>FK_VirtualTag_Script</c> / <c>FK_ScriptedAlarm_Script</c> would.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class UnsTreeServiceExternalIdAndScriptReferenceTests
|
|
{
|
|
private static (UnsTreeService Service, string DbName) Fresh()
|
|
{
|
|
var dbName = $"uns-extid-{Guid.NewGuid():N}";
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
db.UnsAreas.Add(new UnsArea { UnsAreaId = "AREA-1", ClusterId = "MAIN", Name = "a" });
|
|
db.UnsLines.Add(new UnsLine { UnsLineId = "LINE-1", UnsAreaId = "AREA-1", Name = "l" });
|
|
db.SaveChanges();
|
|
return (new UnsTreeService(UnsTreeTestDb.Factory(dbName)), dbName);
|
|
}
|
|
|
|
private static EquipmentInput Input(string name, string machineCode, string? zTag = null, string? sapId = null) =>
|
|
new(name, machineCode, "LINE-1",
|
|
ZTag: zTag, SAPID: sapId, Manufacturer: null, Model: null, SerialNumber: null,
|
|
HardwareRevision: null, SoftwareRevision: null, YearOfConstruction: null,
|
|
AssetLocation: null, ManufacturerUri: null, DeviceManualUri: null, Enabled: true);
|
|
|
|
/// <summary>Create refuses a ZTag another equipment holds and names it; a trimmed copy counts as the same value.</summary>
|
|
[Fact]
|
|
public async Task Create_refuses_a_ZTag_another_equipment_holds()
|
|
{
|
|
var (svc, dbName) = Fresh();
|
|
var ct = TestContext.Current.CancellationToken;
|
|
(await svc.CreateEquipmentAsync(Input("m1", "MC1", zTag: "ZT-1"), ct)).Ok.ShouldBeTrue();
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
var owner = db.Equipment.Single().EquipmentId;
|
|
|
|
var dup = await svc.CreateEquipmentAsync(Input("m2", "MC2", zTag: " ZT-1 "), ct);
|
|
|
|
dup.Ok.ShouldBeFalse();
|
|
dup.Error.ShouldNotBeNull().ShouldContain("ZTag 'ZT-1'");
|
|
dup.Error.ShouldContain(owner);
|
|
}
|
|
|
|
/// <summary>Update refuses a SAPID another equipment holds, but keeping its OWN value is fine.</summary>
|
|
[Fact]
|
|
public async Task Update_refuses_another_equipments_SAPID_but_keeps_its_own()
|
|
{
|
|
var (svc, dbName) = Fresh();
|
|
var ct = TestContext.Current.CancellationToken;
|
|
(await svc.CreateEquipmentAsync(Input("m1", "MC1", sapId: "SAP-1"), ct)).Ok.ShouldBeTrue();
|
|
(await svc.CreateEquipmentAsync(Input("m2", "MC2", sapId: "SAP-2"), ct)).Ok.ShouldBeTrue();
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
var second = db.Equipment.Single(e => e.MachineCode == "MC2");
|
|
|
|
var clash = await svc.UpdateEquipmentAsync(second.EquipmentId, Input("m2", "MC2", sapId: "SAP-1"), second.RowVersion, ct);
|
|
clash.Ok.ShouldBeFalse();
|
|
clash.Error.ShouldNotBeNull().ShouldContain("SAPID 'SAP-1'");
|
|
|
|
var own = await svc.UpdateEquipmentAsync(second.EquipmentId, Input("m2-renamed", "MC2", sapId: "SAP-2"), second.RowVersion, ct);
|
|
own.Ok.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>Blank identifiers are stored as NULL and never clash; values are stored trimmed.</summary>
|
|
[Fact]
|
|
public async Task Blank_identifiers_are_none_and_values_are_stored_trimmed()
|
|
{
|
|
var (svc, dbName) = Fresh();
|
|
var ct = TestContext.Current.CancellationToken;
|
|
(await svc.CreateEquipmentAsync(Input("m1", "MC1", zTag: " ", sapId: ""), ct)).Ok.ShouldBeTrue();
|
|
(await svc.CreateEquipmentAsync(Input("m2", "MC2", zTag: " ", sapId: ""), ct)).Ok.ShouldBeTrue();
|
|
(await svc.CreateEquipmentAsync(Input("m3", "MC3", zTag: " ZT-3 "), ct)).Ok.ShouldBeTrue();
|
|
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
db.Equipment.Count(e => e.ZTag == null && e.SAPID == null).ShouldBe(2);
|
|
db.Equipment.Single(e => e.MachineCode == "MC3").ZTag.ShouldBe("ZT-3");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Import names each clashing row — against the DB and against an earlier row of the same file — and still
|
|
/// imports the clean rows (the clash no longer fails the whole batch at SaveChanges).
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Import_names_clashing_rows_and_imports_the_rest()
|
|
{
|
|
var (svc, dbName) = Fresh();
|
|
var ct = TestContext.Current.CancellationToken;
|
|
(await svc.CreateEquipmentAsync(Input("m1", "MC1", zTag: "ZT-1"), ct)).Ok.ShouldBeTrue();
|
|
|
|
var result = await svc.ImportEquipmentAsync(
|
|
[
|
|
Input("m2", "MC2", zTag: "zt-1"),
|
|
Input("m3", "MC3", sapId: "SAP-3"),
|
|
Input("m4", "MC4", sapId: "SAP-3"),
|
|
Input("m5", "MC5", zTag: "ZT-5", sapId: "SAP-5"),
|
|
], ct);
|
|
|
|
result.Inserted.ShouldBe(2);
|
|
result.Errors.Count.ShouldBe(2);
|
|
result.Errors.ShouldContain(e => e.Contains("'MC2'") && e.Contains("ZTag"));
|
|
result.Errors.ShouldContain(e => e.Contains("'MC4'") && e.Contains("SAPID 'SAP-3'"));
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
db.Equipment.Select(e => e.MachineCode).ToList().ShouldBe(["MC1", "MC3", "MC5"], ignoreOrder: true);
|
|
}
|
|
|
|
/// <summary>A VirtualTag naming a missing script is refused with the script named; positive control binds SCRIPT-1.</summary>
|
|
[Fact]
|
|
public async Task A_virtual_tag_naming_a_missing_script_is_refused()
|
|
{
|
|
var dbName = $"uns-vtscript-{Guid.NewGuid():N}";
|
|
UnsTreeTestDb.SeedNamed(dbName);
|
|
var svc = new UnsTreeService(UnsTreeTestDb.Factory(dbName));
|
|
var ct = TestContext.Current.CancellationToken;
|
|
|
|
var missing = await svc.CreateVirtualTagAsync(UnsTreeTestDb.SeededEquipmentId,
|
|
new VirtualTagInput("VT-X", "x", DataType: "Double", ScriptId: "SCRIPT-GONE", ChangeTriggered: true,
|
|
TimerIntervalMs: null, Historize: false, Enabled: true), ct);
|
|
missing.Ok.ShouldBeFalse();
|
|
missing.Error.ShouldNotBeNull().ShouldContain("SCRIPT-GONE");
|
|
|
|
var ok = await svc.CreateVirtualTagAsync(UnsTreeTestDb.SeededEquipmentId,
|
|
new VirtualTagInput("VT-Y", "y", DataType: "Double", ScriptId: "SCRIPT-1", ChangeTriggered: true,
|
|
TimerIntervalMs: null, Historize: false, Enabled: true), ct);
|
|
ok.Ok.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>A scripted alarm naming a missing predicate script is refused.</summary>
|
|
[Fact]
|
|
public async Task A_scripted_alarm_naming_a_missing_predicate_script_is_refused()
|
|
{
|
|
var dbName = $"uns-sascript-{Guid.NewGuid():N}";
|
|
UnsTreeTestDb.SeedNamed(dbName);
|
|
var svc = new UnsTreeService(UnsTreeTestDb.Factory(dbName));
|
|
|
|
var result = await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId,
|
|
new ScriptedAlarmInput("SA-X", "x", "LimitAlarm", 700, "m", "SCRIPT-GONE", HistorizeToAveva: true, Retain: true, Enabled: true),
|
|
TestContext.Current.CancellationToken);
|
|
|
|
result.Ok.ShouldBeFalse();
|
|
result.Error.ShouldNotBeNull().ShouldContain("SCRIPT-GONE");
|
|
}
|
|
}
|