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.
131 lines
5.3 KiB
C#
131 lines
5.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
|
|
|
|
/// <summary>
|
|
/// Verifies <see cref="DraftSnapshotFactory.FromConfigDbAsync"/> materialises a
|
|
/// <see cref="DraftSnapshot"/> from the live config DB whose Tag/VirtualTag rows feed the
|
|
/// equipment-signal collision rule — the one rule wired into the deploy gate (Task 3).
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class DraftSnapshotFactoryTests : IDisposable
|
|
{
|
|
private readonly OtOpcUaConfigDbContext _db;
|
|
|
|
/// <summary>Initializes a new instance with an isolated in-memory config DB.</summary>
|
|
public DraftSnapshotFactoryTests()
|
|
{
|
|
var options = new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
|
.UseInMemoryDatabase($"draft-snapshot-{Guid.NewGuid():N}")
|
|
.Options;
|
|
_db = new OtOpcUaConfigDbContext(options);
|
|
}
|
|
|
|
/// <summary>Disposes the database context.</summary>
|
|
public void Dispose() => _db.Dispose();
|
|
|
|
/// <summary>Seeds an Equipment plus a raw Tag and a VirtualTag; the snapshot must carry both
|
|
/// signal collections. v3: Tags are raw-only, so the equipment-signal collision rule now operates
|
|
/// on VirtualTags — two VirtualTags sharing (EquipmentId, Name) must surface the collision.</summary>
|
|
[Fact]
|
|
public async Task FromConfigDb_populates_Tags_and_VirtualTags_and_surfaces_collision()
|
|
{
|
|
SeedEquipment("eq-1");
|
|
_db.Tags.Add(BuildTag(name: "speed"));
|
|
_db.VirtualTags.Add(BuildVirtualTag(equipmentId: "eq-1", name: "speed", suffix: "a"));
|
|
_db.VirtualTags.Add(BuildVirtualTag(equipmentId: "eq-1", name: "speed", suffix: "b"));
|
|
await _db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
|
|
var snapshot = await DraftSnapshotFactory.FromConfigDbAsync(_db, TestContext.Current.CancellationToken);
|
|
|
|
snapshot.Tags.Count.ShouldBe(1);
|
|
snapshot.VirtualTags.Count.ShouldBe(2);
|
|
DraftValidator.Validate(snapshot).ShouldContain(e => e.Code == "UnsEffectiveNameCollision");
|
|
}
|
|
|
|
/// <summary>Distinct VirtualTag names under the same equipment do not collide, so the snapshot
|
|
/// validates clean of the collision code.</summary>
|
|
[Fact]
|
|
public async Task FromConfigDb_no_collision_when_names_differ()
|
|
{
|
|
SeedEquipment("eq-1");
|
|
_db.Tags.Add(BuildTag(name: "speed"));
|
|
_db.VirtualTags.Add(BuildVirtualTag(equipmentId: "eq-1", name: "temperature"));
|
|
await _db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
|
|
var snapshot = await DraftSnapshotFactory.FromConfigDbAsync(_db, TestContext.Current.CancellationToken);
|
|
|
|
snapshot.Tags.Count.ShouldBe(1);
|
|
snapshot.VirtualTags.Count.ShouldBe(1);
|
|
DraftValidator.Validate(snapshot).ShouldNotContain(e => e.Code == "UnsEffectiveNameCollision");
|
|
}
|
|
|
|
/// <summary>
|
|
/// R03:3.2 — the deploy path validates AND composes from ONE snapshot, so the tables the composer
|
|
/// needs beyond the validator's set (clusters, nodes, API keys) must ride the same read.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task FromConfigDb_carries_clusters_nodes_and_api_keys_for_the_composer()
|
|
{
|
|
_db.ServerClusters.Add(new ServerCluster
|
|
{
|
|
ClusterId = "SITE-A", Name = "Site A", Enterprise = "zb", Site = "site-a",
|
|
NodeCount = 1, RedundancyMode = RedundancyMode.None, CreatedBy = "test",
|
|
});
|
|
_db.ClusterNodes.Add(new ClusterNode
|
|
{
|
|
NodeId = "site-a-1:4053", ClusterId = "SITE-A", Host = "site-a-1",
|
|
ApplicationUri = "urn:OtOpcUa:site-a-1", Enabled = true, CreatedBy = "test",
|
|
});
|
|
_db.ApiKeys.Add(new ApiKey
|
|
{
|
|
ApiKeyId = "k1", Name = "scada", SecretHash = "hash", Principal = "scadabridge", Enabled = true,
|
|
});
|
|
await _db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
|
|
var snapshot = await DraftSnapshotFactory.FromConfigDbAsync(_db, TestContext.Current.CancellationToken);
|
|
|
|
snapshot.ServerClusters.ShouldHaveSingleItem().ClusterId.ShouldBe("SITE-A");
|
|
snapshot.ClusterNodes.ShouldHaveSingleItem().NodeId.ShouldBe("site-a-1:4053");
|
|
snapshot.ApiKeys.ShouldHaveSingleItem().ApiKeyId.ShouldBe("k1");
|
|
}
|
|
|
|
private void SeedEquipment(string equipmentId)
|
|
{
|
|
var uuid = Guid.NewGuid();
|
|
_db.Equipment.Add(new Equipment
|
|
{
|
|
EquipmentUuid = uuid,
|
|
EquipmentId = equipmentId,
|
|
Name = "eq",
|
|
UnsLineId = "line-a",
|
|
MachineCode = "m",
|
|
});
|
|
}
|
|
|
|
// v3: Tags are raw-only (DeviceId FK, no EquipmentId/DriverInstanceId).
|
|
private static Tag BuildTag(string name) => new()
|
|
{
|
|
TagId = $"tag-{name}",
|
|
DeviceId = "dev-1",
|
|
Name = name,
|
|
DataType = "Float",
|
|
AccessLevel = TagAccessLevel.Read,
|
|
TagConfig = "{}",
|
|
};
|
|
|
|
private static VirtualTag BuildVirtualTag(string equipmentId, string name, string suffix = "") => new()
|
|
{
|
|
VirtualTagId = $"vtag-{name}{suffix}",
|
|
EquipmentId = equipmentId,
|
|
Name = name,
|
|
DataType = "Float",
|
|
ScriptId = "s-1",
|
|
};
|
|
}
|