v3 batch1 WP1: greenfield DbContext + V3Initial squash + seeds + tests
DbContext (OtOpcUaConfigDbContext): - Drop Namespace/EquipmentImportBatch/EquipmentImportRow DbSets + Configure methods. - Add RawFolder/TagGroup/UnsTagReference DbSets + Configure methods with sibling-name filtered unique indexes over nullable parents (two-filtered pattern per level). - Reshape DriverInstance (RawFolderId + UX_DriverInstance_Folder_Name/_ClusterRoot_Name), Tag (required DeviceId, nullable TagGroupId, UX_Tag_Group_Name/_Device_Name, IX_Tag_Device), Equipment (drop DriverInstanceId/DeviceId + IX_Equipment_Driver), Device (UX_Device_Driver_Name). Migration squash -> V3Initial: - Delete the whole Migrations/ folder; scaffold one V3Initial (applies clean to a fresh DB). - Port the hand-written procs + AuthorizationGrants into V3Initial.StoredProcedures.cs. The v1 generation-model procs (ConfigGeneration/GenerationId, dead since V2HostingAlignment) are rewritten as v3-schema-valid retirement stubs that preserve the two-role EXECUTE-grant surface + AuthorizationTests behaviors; sp_ComputeGenerationDiff now references the v3 tables; sp_ReleaseExternalIdReservation ported verbatim. Compile-fixes for the build gate (Wave-C-owned, minimal + TODO(v3 WP4) markers): - DraftSnapshot/DraftSnapshotFactory: drop the retired Namespaces list. - DraftValidator: delete namespace-binding + Galaxy-FullName rules; collision rule now VirtualTag-only. Seeds -> v3 schema: - seed-clusters.sql summary SELECTs; all 5 scripts/smoke/seed-*.sql rewritten (DriverInstance RawFolderId, Device+DeviceConfig endpoint, Tag DeviceId, UnsTagReference; no ConfigGeneration/ Namespace/sp_PublishGeneration). Verified: all seeds apply clean against a fresh V3Initial DB. Tests (Configuration.Tests, 107 pass): - SchemaComplianceTests rewritten to v3 tables + filtered indexes. - New RawSchemaIntegrityTests: DB-enforced sibling-name uniqueness per raw level + UnsTagReference (Equipment,Tag) uniqueness. - Delete DraftValidatorGalaxyFullNameCorpusTests (WP2/WP6 replaces); fix DraftValidatorTests + DraftSnapshotFactoryTests to v3 shapes; repoint scripting-migration existence test to V3Initial.
This commit is contained in:
@@ -29,30 +29,32 @@ public sealed class DraftSnapshotFactoryTests : IDisposable
|
||||
/// <summary>Disposes the database context.</summary>
|
||||
public void Dispose() => _db.Dispose();
|
||||
|
||||
/// <summary>Seeds one Equipment plus a Tag and a VirtualTag sharing (EquipmentId, Name); the
|
||||
/// snapshot must carry both signal collections AND the validator must flag the collision.</summary>
|
||||
/// <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(equipmentId: "eq-1", name: "speed"));
|
||||
_db.VirtualTags.Add(BuildVirtualTag(equipmentId: "eq-1", name: "speed"));
|
||||
_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();
|
||||
|
||||
var snapshot = await DraftSnapshotFactory.FromConfigDbAsync(_db);
|
||||
|
||||
snapshot.Tags.Count.ShouldBe(1);
|
||||
snapshot.VirtualTags.Count.ShouldBe(1);
|
||||
snapshot.VirtualTags.Count.ShouldBe(2);
|
||||
DraftValidator.Validate(snapshot).ShouldContain(e => e.Code == "EquipmentSignalNameCollision");
|
||||
}
|
||||
|
||||
/// <summary>A Tag and a VirtualTag with distinct names under the same equipment do not collide,
|
||||
/// so the snapshot validates clean of the collision code.</summary>
|
||||
/// <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(equipmentId: "eq-1", name: "speed"));
|
||||
_db.Tags.Add(BuildTag(name: "speed"));
|
||||
_db.VirtualTags.Add(BuildVirtualTag(equipmentId: "eq-1", name: "temperature"));
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
@@ -109,26 +111,25 @@ public sealed class DraftSnapshotFactoryTests : IDisposable
|
||||
EquipmentUuid = uuid,
|
||||
EquipmentId = equipmentId,
|
||||
Name = "eq",
|
||||
DriverInstanceId = "d",
|
||||
UnsLineId = "line-a",
|
||||
MachineCode = "m",
|
||||
});
|
||||
}
|
||||
|
||||
private static Tag BuildTag(string equipmentId, string name) => new()
|
||||
// v3: Tags are raw-only (DeviceId FK, no EquipmentId/DriverInstanceId).
|
||||
private static Tag BuildTag(string name) => new()
|
||||
{
|
||||
TagId = $"tag-{name}",
|
||||
DriverInstanceId = "d",
|
||||
EquipmentId = equipmentId,
|
||||
DeviceId = "dev-1",
|
||||
Name = name,
|
||||
DataType = "Float",
|
||||
AccessLevel = TagAccessLevel.Read,
|
||||
TagConfig = "{}",
|
||||
};
|
||||
|
||||
private static VirtualTag BuildVirtualTag(string equipmentId, string name) => new()
|
||||
private static VirtualTag BuildVirtualTag(string equipmentId, string name, string suffix = "") => new()
|
||||
{
|
||||
VirtualTagId = $"vtag-{name}",
|
||||
VirtualTagId = $"vtag-{name}{suffix}",
|
||||
EquipmentId = equipmentId,
|
||||
Name = name,
|
||||
DataType = "Float",
|
||||
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
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>
|
||||
/// Characterization net (R2-11) pinning the DraftValidator Galaxy rule's <em>explicit</em>-FullName
|
||||
/// semantics across the golden TagConfig corpus BEFORE the private FullName reader is delegated to
|
||||
/// <c>TagConfigIntent.Parse(...).ExplicitFullName</c>. Distinct from the walker/composer raw-blob
|
||||
/// fallback: <c>GalaxyTagMissingReference</c> fires whenever the TagConfig has no usable
|
||||
/// <em>explicit</em> string <c>FullName</c> — i.e. absent / non-string / whitespace / non-object /
|
||||
/// malformed — and does NOT fire only when a non-blank string FullName is present. Must be green on
|
||||
/// the unmodified tree and stay green through the swap.
|
||||
/// </summary>
|
||||
public sealed class DraftValidatorGalaxyFullNameCorpusTests
|
||||
{
|
||||
[Theory]
|
||||
// Present, non-blank string FullName → rule does NOT fire
|
||||
[InlineData("{\"FullName\":\"X.Y\"}", false)]
|
||||
[InlineData("{\"FullName\":\"A.B\",\"region\":\"Coils\"}", false)]
|
||||
// Absent / non-string / whitespace / non-object / malformed → rule FIRES
|
||||
[InlineData("{}", true)]
|
||||
[InlineData("{\"region\":\"HoldingRegisters\"}", true)]
|
||||
[InlineData("{\"FullName\":123}", true)]
|
||||
[InlineData("{\"FullName\":\" \"}", true)]
|
||||
[InlineData("[1,2]", true)]
|
||||
[InlineData("not json {", true)]
|
||||
public void Galaxy_missing_reference_fires_iff_no_explicit_string_FullName(string tagConfig, bool shouldFire)
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1,
|
||||
ClusterId = "c",
|
||||
DriverInstances =
|
||||
[
|
||||
new DriverInstance
|
||||
{
|
||||
DriverInstanceId = "d-galaxy", ClusterId = "c", NamespaceId = "ns-1",
|
||||
Name = "Galaxy", DriverType = "GalaxyMxGateway", DriverConfig = "{}",
|
||||
},
|
||||
],
|
||||
Tags =
|
||||
[
|
||||
new Tag
|
||||
{
|
||||
TagId = "tag-galaxytag", DriverInstanceId = "d-galaxy", EquipmentId = "eq-1",
|
||||
Name = "galaxytag", FolderPath = null, DataType = "Float",
|
||||
AccessLevel = TagAccessLevel.Read, TagConfig = tagConfig,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
var fired = DraftValidator.Validate(draft)
|
||||
.Any(e => e.Code == "GalaxyTagMissingReference" && e.Context == "tag-galaxytag");
|
||||
fired.ShouldBe(shouldFire);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,14 @@ using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// v3 WP1: the namespace-binding + Galaxy-FullName rules are retired (Namespace entity gone;
|
||||
/// Tags are raw-only, not equipment-bound). The retained rules (UNS segments, path length,
|
||||
/// EquipmentUuid immutability, external-id reservation preflight, EquipmentId derivation,
|
||||
/// cluster topology, VirtualTag equipment-signal collision) are exercised below against the v3
|
||||
/// entity shapes. WP4 (Wave C) adds the new v3 rules (raw-name charset, historized-tagname length,
|
||||
/// UNS effective-leaf uniqueness).
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class DraftValidatorTests
|
||||
{
|
||||
@@ -32,7 +40,6 @@ public sealed class DraftValidatorTests
|
||||
EquipmentUuid = uuid,
|
||||
EquipmentId = DraftValidator.DeriveEquipmentId(uuid),
|
||||
Name = name,
|
||||
DriverInstanceId = "d",
|
||||
UnsLineId = "line-a",
|
||||
MachineCode = "m",
|
||||
},
|
||||
@@ -44,35 +51,6 @@ public sealed class DraftValidatorTests
|
||||
hasUnsError.ShouldBe(!shouldPass);
|
||||
}
|
||||
|
||||
/// <summary>Verifies that namespace cannot be bound to a different cluster.</summary>
|
||||
[Fact]
|
||||
public void Cross_cluster_namespace_binding_is_rejected()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c-A",
|
||||
Namespaces = [new Namespace { NamespaceId = "ns-1", ClusterId = "c-B", NamespaceUri = "urn:x", Kind = NamespaceKind.Equipment }],
|
||||
DriverInstances = [new DriverInstance { DriverInstanceId = "d-1", ClusterId = "c-A", NamespaceId = "ns-1", Name = "drv", DriverType = "Modbus", DriverConfig = "{}" }],
|
||||
};
|
||||
|
||||
var errors = DraftValidator.Validate(draft);
|
||||
errors.ShouldContain(e => e.Code == "BadCrossClusterNamespaceBinding");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that namespace in the same cluster is accepted.</summary>
|
||||
[Fact]
|
||||
public void Same_cluster_namespace_binding_is_accepted()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c-A",
|
||||
Namespaces = [new Namespace { NamespaceId = "ns-1", ClusterId = "c-A", NamespaceUri = "urn:x", Kind = NamespaceKind.Equipment }],
|
||||
DriverInstances = [new DriverInstance { DriverInstanceId = "d-1", ClusterId = "c-A", NamespaceId = "ns-1", Name = "drv", DriverType = "Modbus", DriverConfig = "{}" }],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldNotContain(e => e.Code == "BadCrossClusterNamespaceBinding");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that equipment UUID must remain immutable across generations.</summary>
|
||||
[Fact]
|
||||
public void EquipmentUuid_change_across_generations_is_rejected()
|
||||
@@ -84,8 +62,8 @@ public sealed class DraftValidatorTests
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 2, ClusterId = "c",
|
||||
Equipment = [new Equipment { EquipmentUuid = newUuid, EquipmentId = eid, Name = "eq", DriverInstanceId = "d", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
PriorEquipment = [new Equipment { EquipmentUuid = oldUuid, EquipmentId = eid, Name = "eq", DriverInstanceId = "d", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
Equipment = [new Equipment { EquipmentUuid = newUuid, EquipmentId = eid, Name = "eq", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
PriorEquipment = [new Equipment { EquipmentUuid = oldUuid, EquipmentId = eid, Name = "eq", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldContain(e => e.Code == "EquipmentUuidImmutable");
|
||||
@@ -101,7 +79,7 @@ public sealed class DraftValidatorTests
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
Equipment = [new Equipment { EquipmentUuid = uuid, EquipmentId = DraftValidator.DeriveEquipmentId(uuid), Name = "eq", DriverInstanceId = "d", UnsLineId = "line-a", MachineCode = "m", ZTag = "ZT-001" }],
|
||||
Equipment = [new Equipment { EquipmentUuid = uuid, EquipmentId = DraftValidator.DeriveEquipmentId(uuid), Name = "eq", UnsLineId = "line-a", MachineCode = "m", ZTag = "ZT-001" }],
|
||||
ActiveReservations = [new ExternalIdReservation { Kind = ReservationKind.ZTag, Value = "ZT-001", EquipmentUuid = otherUuid, ClusterId = "c", FirstPublishedBy = "t" }],
|
||||
};
|
||||
|
||||
@@ -116,96 +94,38 @@ public sealed class DraftValidatorTests
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
Equipment = [new Equipment { EquipmentUuid = uuid, EquipmentId = "EQ-operator-typed", Name = "eq", DriverInstanceId = "d", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
Equipment = [new Equipment { EquipmentUuid = uuid, EquipmentId = "EQ-operator-typed", Name = "eq", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldContain(e => e.Code == "EquipmentIdNotDerived");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the canonical Galaxy driver type (GalaxyMxGateway, per PR 7.2 —
|
||||
/// it was "Galaxy" pre-PR-7.2) is a standard Equipment-kind driver: binding it to an
|
||||
/// Equipment namespace produces no kind-mismatch error.</summary>
|
||||
/// <summary>Verifies that all violations are reported simultaneously (single-pass validation).</summary>
|
||||
[Fact]
|
||||
public void GalaxyMxGateway_driver_in_Equipment_namespace_is_allowed()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
Namespaces = [new Namespace { NamespaceId = "ns-1", ClusterId = "c", NamespaceUri = "urn:x", Kind = NamespaceKind.Equipment }],
|
||||
DriverInstances = [new DriverInstance { DriverInstanceId = "d-1", ClusterId = "c", NamespaceId = "ns-1", Name = "drv", DriverType = "GalaxyMxGateway", DriverConfig = "{}" }],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldBeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>Verifies that all validation errors are reported simultaneously.</summary>
|
||||
[Fact]
|
||||
public void Draft_with_three_violations_surfaces_all_three()
|
||||
public void Draft_with_multiple_violations_surfaces_all_of_them()
|
||||
{
|
||||
var uuid = Guid.NewGuid();
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c-A",
|
||||
Namespaces = [new Namespace { NamespaceId = "ns-1", ClusterId = "c-B", NamespaceUri = "urn:x", Kind = NamespaceKind.Equipment }],
|
||||
DriverInstances = [new DriverInstance { DriverInstanceId = "d-1", ClusterId = "c-A", NamespaceId = "ns-1", Name = "drv", DriverType = "GalaxyMxGateway", DriverConfig = "{}" }],
|
||||
Equipment = [new Equipment { EquipmentUuid = uuid, EquipmentId = "EQ-wrong", Name = "BAD NAME", DriverInstanceId = "d-1", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
// EQ-wrong is not the canonical derivation, and "BAD NAME" fails the UNS segment regex.
|
||||
Equipment = [new Equipment { EquipmentUuid = uuid, EquipmentId = "EQ-wrong", Name = "BAD NAME", UnsLineId = "line-a", MachineCode = "m" }],
|
||||
};
|
||||
|
||||
var errors = DraftValidator.Validate(draft);
|
||||
errors.ShouldContain(e => e.Code == "BadCrossClusterNamespaceBinding");
|
||||
errors.ShouldContain(e => e.Code == "EquipmentIdNotDerived");
|
||||
errors.ShouldContain(e => e.Code == "UnsSegmentInvalid");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
// Full-config probe — proves a realistic canonical deployed config passes ALL rules.
|
||||
// This guards the deploy-path gate flip (reject-on-ANY-error): if a clean canonical
|
||||
// config fired any rule, flipping the gate would block every deploy.
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
||||
/// <summary>Probe for the deploy-path gate activation: a full, realistic config modelling
|
||||
/// the REAL deployed shape. Galaxy is now a standard Equipment-kind driver (the SystemPlatform
|
||||
/// namespace split is retired), so the GalaxyMxGateway driver + its Galaxy-hierarchy Tags
|
||||
/// (EquipmentId=null from the seed) live in the Equipment namespace alongside a Modbus driver
|
||||
/// + UNS area/line + canonical Equipment rows + VirtualTags from the company overlay. This must
|
||||
/// produce ZERO validation errors so the reject-on-any-error gate is safe to activate.</summary>
|
||||
/// <summary>Probe for the deploy-path gate: a full, realistic v3 config (UNS area/line +
|
||||
/// canonical Equipment + distinct-named VirtualTags) must produce ZERO validation errors so the
|
||||
/// reject-on-any-error deploy gate is safe to activate.</summary>
|
||||
[Fact]
|
||||
public void Full_realistic_config_passes_all_rules()
|
||||
{
|
||||
// Equipment side: Galaxy + Modbus drivers in a single Equipment namespace + UNS + canonical equipment.
|
||||
var eqNamespace = new Namespace
|
||||
{
|
||||
NamespaceId = "MAIN-OPCUA-equipment",
|
||||
ClusterId = "MAIN",
|
||||
Kind = NamespaceKind.Equipment,
|
||||
NamespaceUri = "urn:zb:main:equipment",
|
||||
};
|
||||
|
||||
// Galaxy hierarchy (OtOpcUa seed shape): GalaxyMxGateway driver, Tags carry EquipmentId=null.
|
||||
var spDriver = new DriverInstance
|
||||
{
|
||||
DriverInstanceId = "main-galaxy",
|
||||
ClusterId = "MAIN",
|
||||
NamespaceId = eqNamespace.NamespaceId,
|
||||
Name = "Galaxy",
|
||||
DriverType = "GalaxyMxGateway",
|
||||
DriverConfig = "{}",
|
||||
};
|
||||
|
||||
var eqDriver = new DriverInstance
|
||||
{
|
||||
DriverInstanceId = "main-modbus",
|
||||
ClusterId = "MAIN",
|
||||
NamespaceId = eqNamespace.NamespaceId,
|
||||
Name = "Modbus",
|
||||
DriverType = "Modbus",
|
||||
DriverConfig = "{}",
|
||||
};
|
||||
|
||||
var area = new UnsArea { UnsAreaId = "area-filling", ClusterId = "MAIN", Name = "filling" };
|
||||
var line = new UnsLine { UnsLineId = "line-1", UnsAreaId = area.UnsAreaId, Name = "line-1" };
|
||||
|
||||
// Canonical EquipmentIds — derived from the EquipmentUuid via the same rule the overlay loader uses.
|
||||
var rinserUuid = Guid.NewGuid();
|
||||
var fillerUuid = Guid.NewGuid();
|
||||
var rinser = new Equipment
|
||||
@@ -213,43 +133,27 @@ public sealed class DraftValidatorTests
|
||||
EquipmentUuid = rinserUuid,
|
||||
EquipmentId = DraftValidator.DeriveEquipmentId(rinserUuid),
|
||||
Name = "rinser-01",
|
||||
DriverInstanceId = eqDriver.DriverInstanceId,
|
||||
UnsLineId = line.UnsLineId,
|
||||
MachineCode = "machine_001",
|
||||
ZTag = null,
|
||||
SAPID = null,
|
||||
};
|
||||
var filler = new Equipment
|
||||
{
|
||||
EquipmentUuid = fillerUuid,
|
||||
EquipmentId = DraftValidator.DeriveEquipmentId(fillerUuid),
|
||||
Name = "filler-02",
|
||||
DriverInstanceId = eqDriver.DriverInstanceId,
|
||||
UnsLineId = line.UnsLineId,
|
||||
MachineCode = "machine_002",
|
||||
ZTag = null,
|
||||
SAPID = null,
|
||||
};
|
||||
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 0,
|
||||
ClusterId = string.Empty, // global snapshot — matches DraftSnapshotFactory.FromConfigDbAsync
|
||||
// Enterprise/Site left null — matches the deploy path's conservative fallback
|
||||
Namespaces = [eqNamespace],
|
||||
DriverInstances = [spDriver, eqDriver],
|
||||
UnsAreas = [area],
|
||||
UnsLines = [line],
|
||||
Equipment = [rinser, filler],
|
||||
Tags =
|
||||
[
|
||||
// Galaxy-hierarchy tags from the seed: EquipmentId null, Galaxy folder hierarchy.
|
||||
BuildTag(equipmentId: null, name: "PV", folderPath: "Area.Tank01"),
|
||||
BuildTag(equipmentId: null, name: "SP", folderPath: "Area.Tank01"),
|
||||
],
|
||||
VirtualTags =
|
||||
[
|
||||
// One per equipment, names distinct within each owning equipment.
|
||||
BuildVirtualTag(equipmentId: rinser.EquipmentId, name: "oee"),
|
||||
BuildVirtualTag(equipmentId: filler.EquipmentId, name: "throughput"),
|
||||
],
|
||||
@@ -264,133 +168,46 @@ public sealed class DraftValidatorTests
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
// Driver-less Equipment namespace — core safety claim (feat/driverless-equipment-namespace)
|
||||
// ValidateNoEquipmentSignalNameCollision — v3: VirtualTag name uniqueness within equipment
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
||||
/// <summary>Characterisation test: a draft that contains an Equipment-kind namespace with
|
||||
/// ZERO <see cref="DriverInstance"/> rows, and a single <see cref="Equipment"/> whose
|
||||
/// <see cref="Equipment.DriverInstanceId"/> is <see langword="null"/>, must pass
|
||||
/// <see cref="DraftValidator.Validate"/> with no errors. This locks in the design's core
|
||||
/// safety claim — the validator must never implicitly require a driver for Equipment-namespace
|
||||
/// equipment.</summary>
|
||||
/// <summary>Two VirtualTags sharing (EquipmentId, Name) collide on a single OPC UA NodeId.</summary>
|
||||
[Fact]
|
||||
public void Validate_accepts_driverless_equipment_in_driverless_equipment_namespace()
|
||||
{
|
||||
// An Equipment-kind namespace — no DriverInstance rows at all for this namespace.
|
||||
var eqNamespace = new Namespace
|
||||
{
|
||||
NamespaceId = "MAIN-OPCUA-equipment",
|
||||
ClusterId = "MAIN",
|
||||
Kind = NamespaceKind.Equipment,
|
||||
NamespaceUri = "urn:zb:main:equipment",
|
||||
};
|
||||
|
||||
// UNS topology required by ValidateUnsSegments / ValidatePathLength.
|
||||
var area = new UnsArea { UnsAreaId = "area-filling", ClusterId = "MAIN", Name = "filling" };
|
||||
var line = new UnsLine { UnsLineId = "line-1", UnsAreaId = area.UnsAreaId, Name = "line-1" };
|
||||
|
||||
// Canonical EquipmentId derived from UUID — satisfies ValidateEquipmentIdDerivation.
|
||||
var uuid = Guid.NewGuid();
|
||||
var equipment = new Equipment
|
||||
{
|
||||
EquipmentUuid = uuid,
|
||||
EquipmentId = DraftValidator.DeriveEquipmentId(uuid),
|
||||
Name = "rinser-01",
|
||||
DriverInstanceId = null, // ← driver-less: the property under test
|
||||
UnsLineId = line.UnsLineId,
|
||||
MachineCode = "machine_001",
|
||||
ZTag = null,
|
||||
SAPID = null,
|
||||
};
|
||||
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 0,
|
||||
ClusterId = string.Empty, // global snapshot — matches DraftSnapshotFactory.FromConfigDbAsync
|
||||
Namespaces = [eqNamespace], // Equipment namespace present
|
||||
DriverInstances = [], // ← zero drivers: the other half of the safety claim
|
||||
UnsAreas = [area],
|
||||
UnsLines = [line],
|
||||
Equipment = [equipment],
|
||||
};
|
||||
|
||||
var errors = DraftValidator.Validate(draft);
|
||||
|
||||
errors.ShouldBeEmpty(
|
||||
"a driver-less Equipment in an Equipment-kind namespace with no DriverInstances must pass " +
|
||||
"all validator rules; firing rules: " +
|
||||
string.Join("; ", errors.Select(e => $"[{e.Code}] {e.Message}")));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
// ValidateNoEquipmentSignalNameCollision — Tag/VirtualTag NodeId collision
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
||||
/// <summary>Verifies that an equipment-bound Tag and a VirtualTag sharing the same
|
||||
/// equipment+name collide on a single OPC UA NodeId.</summary>
|
||||
[Fact]
|
||||
public void Tag_and_VirtualTag_same_equipment_and_name_collide()
|
||||
public void Two_VirtualTags_same_equipment_and_name_collide()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
Tags = [BuildTag(equipmentId: "eq-1", name: "speed", folderPath: null)],
|
||||
VirtualTags = [BuildVirtualTag(equipmentId: "eq-1", name: "speed")],
|
||||
VirtualTags =
|
||||
[
|
||||
BuildVirtualTag(equipmentId: "eq-1", name: "speed", suffix: "a"),
|
||||
BuildVirtualTag(equipmentId: "eq-1", name: "speed", suffix: "b"),
|
||||
],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldContain(e => e.Code == "EquipmentSignalNameCollision");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that a Tag in a sub-folder does not collide with a VirtualTag at the
|
||||
/// equipment root even when the names match — the NodeId keys differ.</summary>
|
||||
/// <summary>VirtualTags with the same name under DIFFERENT equipment do not collide.</summary>
|
||||
[Fact]
|
||||
public void Same_name_different_folder_does_not_collide()
|
||||
public void Same_name_different_equipment_does_not_collide()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
Tags = [BuildTag(equipmentId: "eq-1", name: "speed", folderPath: "metrics")],
|
||||
VirtualTags = [BuildVirtualTag(equipmentId: "eq-1", name: "speed")],
|
||||
VirtualTags =
|
||||
[
|
||||
BuildVirtualTag(equipmentId: "eq-1", name: "speed"),
|
||||
BuildVirtualTag(equipmentId: "eq-2", name: "speed"),
|
||||
],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldNotContain(e => e.Code == "EquipmentSignalNameCollision");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that a FolderPath-scoped tag (EquipmentId == null) is excluded from the
|
||||
/// equipment-signal node space and so never collides with an equipment VirtualTag.</summary>
|
||||
[Fact]
|
||||
public void Unbound_tag_EquipmentId_null_does_not_collide_with_equipment_vtag()
|
||||
private static VirtualTag BuildVirtualTag(string equipmentId, string name, string suffix = "") => new()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
Tags = [BuildTag(equipmentId: null, name: "speed", folderPath: null)],
|
||||
VirtualTags = [BuildVirtualTag(equipmentId: "eq-1", name: "speed")],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldNotContain(e => e.Code == "EquipmentSignalNameCollision");
|
||||
}
|
||||
|
||||
private static Tag BuildTag(
|
||||
string? equipmentId,
|
||||
string name,
|
||||
string? folderPath,
|
||||
string driverInstanceId = "d",
|
||||
string tagConfig = "{}") => new()
|
||||
{
|
||||
TagId = $"tag-{name}",
|
||||
DriverInstanceId = driverInstanceId,
|
||||
EquipmentId = equipmentId,
|
||||
Name = name,
|
||||
FolderPath = folderPath,
|
||||
DataType = "Float",
|
||||
AccessLevel = TagAccessLevel.Read,
|
||||
TagConfig = tagConfig,
|
||||
};
|
||||
|
||||
private static VirtualTag BuildVirtualTag(string equipmentId, string name) => new()
|
||||
{
|
||||
VirtualTagId = $"vtag-{name}",
|
||||
VirtualTagId = $"vtag-{equipmentId}-{name}{suffix}",
|
||||
EquipmentId = equipmentId,
|
||||
Name = name,
|
||||
DataType = "Float",
|
||||
@@ -398,58 +215,7 @@ public sealed class DraftValidatorTests
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
// ValidateGalaxyTagFullName — Galaxy equipment tags must carry TagConfig.FullName
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
||||
/// <summary>Verifies that an equipment-scoped Tag bound to a GalaxyMxGateway driver whose
|
||||
/// TagConfig has no FullName (a Galaxy tag with no Galaxy reference) is rejected — it would
|
||||
/// subscribe to nothing.</summary>
|
||||
[Fact]
|
||||
public void GalaxyTag_missing_FullName_is_rejected()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
DriverInstances = [new DriverInstance { DriverInstanceId = "d-galaxy", ClusterId = "c", NamespaceId = "ns-1", Name = "Galaxy", DriverType = "GalaxyMxGateway", DriverConfig = "{}" }],
|
||||
Tags = [BuildTag(equipmentId: "eq-1", name: "galaxytag", folderPath: null, driverInstanceId: "d-galaxy", tagConfig: "{}")],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldContain(e => e.Code == "GalaxyTagMissingReference" && e.Context == "tag-galaxytag");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that an equipment-scoped Galaxy tag carrying a TagConfig.FullName
|
||||
/// reference is accepted.</summary>
|
||||
[Fact]
|
||||
public void GalaxyTag_with_FullName_is_accepted()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
DriverInstances = [new DriverInstance { DriverInstanceId = "d-galaxy", ClusterId = "c", NamespaceId = "ns-1", Name = "Galaxy", DriverType = "GalaxyMxGateway", DriverConfig = "{}" }],
|
||||
Tags = [BuildTag(equipmentId: "eq-1", name: "galaxytag", folderPath: null, driverInstanceId: "d-galaxy", tagConfig: "{\"FullName\":\"X.Y\"}")],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldNotContain(e => e.Code == "GalaxyTagMissingReference");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that an equipment-scoped Tag bound to a NON-Galaxy driver with an empty
|
||||
/// TagConfig is NOT flagged as a Galaxy tag missing its reference — only GalaxyMxGateway-bound
|
||||
/// equipment tags carry the Galaxy FullName requirement.</summary>
|
||||
[Fact]
|
||||
public void GalaxyTag_check_skips_nonGalaxy_equipment_tag()
|
||||
{
|
||||
var draft = new DraftSnapshot
|
||||
{
|
||||
GenerationId = 1, ClusterId = "c",
|
||||
DriverInstances = [new DriverInstance { DriverInstanceId = "d-modbus", ClusterId = "c", NamespaceId = "ns-1", Name = "Modbus", DriverType = "Modbus", DriverConfig = "{}" }],
|
||||
Tags = [BuildTag(equipmentId: "eq-1", name: "galaxytag", folderPath: null, driverInstanceId: "d-modbus", tagConfig: "{}")],
|
||||
};
|
||||
|
||||
DraftValidator.Validate(draft).ShouldNotContain(e => e.Code == "GalaxyTagMissingReference");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
// Phase 6.3 task #148 part 2 — ValidateClusterTopology
|
||||
// Phase 6.3 task #148 part 2 — ValidateClusterTopology (unchanged in v3)
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
||||
/// <summary>Verifies that cluster topology validation checks node count against declared redundancy mode.</summary>
|
||||
@@ -479,7 +245,6 @@ public sealed class DraftValidatorTests
|
||||
[Fact]
|
||||
public void ValidateClusterTopology_flags_disabled_node_mismatch()
|
||||
{
|
||||
// Declared 2 + Hot, but one node disabled — runtime would boot InvalidTopology.
|
||||
var cluster = BuildCluster(nodeCount: 2, mode: RedundancyMode.Hot);
|
||||
var nodes = new[]
|
||||
{
|
||||
@@ -491,11 +256,6 @@ public sealed class DraftValidatorTests
|
||||
errors.ShouldContain(e => e.Code == "ClusterEnabledNodeCountMismatch");
|
||||
}
|
||||
|
||||
// v2: the "exactly one Primary per cluster" check is gone — Akka cluster's
|
||||
// role-leader-of-"driver" elects the primary at runtime. The corresponding
|
||||
// ValidateClusterTopology_flags_multiple_Primary test (and the
|
||||
// ClusterMultiplePrimary error code it asserted) were removed alongside Task 14d.
|
||||
|
||||
/// <summary>Verifies that a valid standalone cluster passes validation.</summary>
|
||||
[Fact]
|
||||
public void ValidateClusterTopology_returns_no_errors_on_valid_standalone()
|
||||
@@ -540,13 +300,6 @@ public sealed class DraftValidatorTests
|
||||
[Fact]
|
||||
public void PathLength_uses_actual_Enterprise_Site_when_provided()
|
||||
{
|
||||
// Craft a snapshot where the 32+32 approximation would flag PathTooLong but the
|
||||
// actual Enterprise/Site lengths would not. Equipment name intentionally exceeds the
|
||||
// UNS-segment 32-char limit (it would also trigger UnsSegmentInvalid — that is fine;
|
||||
// both checks are independent and the test is filtering only on PathTooLong).
|
||||
//
|
||||
// approx: 32+32 + 32+32+90 +4 = 222 > 200 → would flag PathTooLong
|
||||
// actual: 2 +1 + 32+32+90 +4 = 161 ≤ 200 → no PathTooLong
|
||||
var areaId = "area-a";
|
||||
var lineId = "line-b";
|
||||
var uuid = Guid.NewGuid();
|
||||
@@ -567,7 +320,6 @@ public sealed class DraftValidatorTests
|
||||
EquipmentUuid = uuid,
|
||||
EquipmentId = DraftValidator.DeriveEquipmentId(uuid),
|
||||
Name = eqName,
|
||||
DriverInstanceId = "d",
|
||||
UnsLineId = lineId,
|
||||
MachineCode = "m",
|
||||
},
|
||||
@@ -584,11 +336,6 @@ public sealed class DraftValidatorTests
|
||||
[Fact]
|
||||
public void PathLength_conservative_fallback_when_Enterprise_Site_absent()
|
||||
{
|
||||
// Without Enterprise/Site on the snapshot the validator assumes 32+32.
|
||||
// A path whose segments sum to 93 chars (area=32 + line=32 + eq=29) fits
|
||||
// under 200 even with the 32+32 approximation (32+32+93+4 = 161 ≤ 200) and
|
||||
// must NOT be flagged — the fallback must not over-penalise valid paths that
|
||||
// would also be valid under real Enterprise/Site values.
|
||||
var areaId = "area-x";
|
||||
var lineId = "line-y";
|
||||
var uuid = Guid.NewGuid();
|
||||
@@ -597,7 +344,6 @@ public sealed class DraftValidatorTests
|
||||
{
|
||||
GenerationId = 1,
|
||||
ClusterId = "c",
|
||||
// Enterprise and Site deliberately omitted — conservative fallback path
|
||||
UnsAreas = [new UnsArea { UnsAreaId = areaId, ClusterId = "c", Name = new string('a', 32) }],
|
||||
UnsLines = [new UnsLine { UnsLineId = lineId, UnsAreaId = areaId, Name = new string('b', 32) }],
|
||||
Equipment =
|
||||
@@ -607,7 +353,6 @@ public sealed class DraftValidatorTests
|
||||
EquipmentUuid = uuid,
|
||||
EquipmentId = DraftValidator.DeriveEquipmentId(uuid),
|
||||
Name = new string('c', 29),
|
||||
DriverInstanceId = "d",
|
||||
UnsLineId = lineId,
|
||||
MachineCode = "m",
|
||||
},
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// v3 WP1 — exercises the DB-enforceable integrity + sibling-name uniqueness rules of the Raw tree
|
||||
/// against the real SQL schema (the filtered unique indexes over nullable parent columns and the
|
||||
/// UnsTagReference (Equipment, Tag) uniqueness). Runs against the shared SchemaComplianceFixture DB;
|
||||
/// every row uses a GUID-suffixed logical id so tests do not clobber one another.
|
||||
/// </summary>
|
||||
[Trait("Category", "SchemaCompliance")]
|
||||
[Collection(nameof(SchemaComplianceCollection))]
|
||||
public sealed class RawSchemaIntegrityTests
|
||||
{
|
||||
private readonly SchemaComplianceFixture _fixture;
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="RawSchemaIntegrityTests"/> class.</summary>
|
||||
/// <param name="fixture">The schema compliance fixture (real SQL DB).</param>
|
||||
public RawSchemaIntegrityTests(SchemaComplianceFixture fixture) => _fixture = fixture;
|
||||
|
||||
private OtOpcUaConfigDbContext NewContext()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
||||
.UseSqlServer(_fixture.ConnectionString)
|
||||
.Options;
|
||||
return new OtOpcUaConfigDbContext(options);
|
||||
}
|
||||
|
||||
// Logical ids are nvarchar(64); prefix + '-' + 32 hex stays well under that.
|
||||
private static string Id(string prefix) => $"{prefix}-{Guid.NewGuid():N}";
|
||||
|
||||
private string SeedCluster()
|
||||
{
|
||||
var clusterId = Id("cl");
|
||||
using var db = NewContext();
|
||||
db.ServerClusters.Add(new ServerCluster
|
||||
{
|
||||
ClusterId = clusterId,
|
||||
Name = Id("Cluster"),
|
||||
Enterprise = "zb",
|
||||
Site = "dev",
|
||||
NodeCount = 1,
|
||||
RedundancyMode = RedundancyMode.None,
|
||||
CreatedBy = "wp1-test",
|
||||
});
|
||||
db.SaveChanges();
|
||||
return clusterId;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RawFolder_root_sibling_name_must_be_unique_within_cluster()
|
||||
{
|
||||
var clusterId = SeedCluster();
|
||||
using var db = NewContext();
|
||||
db.RawFolders.Add(new RawFolder { RawFolderId = Id("rf"), ClusterId = clusterId, Name = "plc-area" });
|
||||
db.RawFolders.Add(new RawFolder { RawFolderId = Id("rf"), ClusterId = clusterId, Name = "plc-area" });
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RawFolder_same_root_name_in_different_cluster_is_allowed()
|
||||
{
|
||||
var clusterA = SeedCluster();
|
||||
var clusterB = SeedCluster();
|
||||
using var db = NewContext();
|
||||
db.RawFolders.Add(new RawFolder { RawFolderId = Id("rf"), ClusterId = clusterA, Name = "shared-name" });
|
||||
db.RawFolders.Add(new RawFolder { RawFolderId = Id("rf"), ClusterId = clusterB, Name = "shared-name" });
|
||||
|
||||
Should.NotThrow(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RawFolder_nested_sibling_name_must_be_unique_within_parent()
|
||||
{
|
||||
var clusterId = SeedCluster();
|
||||
var parentId = Id("rf-parent");
|
||||
using var db = NewContext();
|
||||
db.RawFolders.Add(new RawFolder { RawFolderId = parentId, ClusterId = clusterId, Name = "parent" });
|
||||
db.RawFolders.Add(new RawFolder { RawFolderId = Id("rf"), ClusterId = clusterId, ParentRawFolderId = parentId, Name = "child" });
|
||||
db.RawFolders.Add(new RawFolder { RawFolderId = Id("rf"), ClusterId = clusterId, ParentRawFolderId = parentId, Name = "child" });
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DriverInstance_cluster_root_sibling_name_must_be_unique()
|
||||
{
|
||||
var clusterId = SeedCluster();
|
||||
using var db = NewContext();
|
||||
db.DriverInstances.Add(new DriverInstance { DriverInstanceId = Id("di"), ClusterId = clusterId, Name = "modbus", DriverType = "Modbus", DriverConfig = "{}" });
|
||||
db.DriverInstances.Add(new DriverInstance { DriverInstanceId = Id("di"), ClusterId = clusterId, Name = "modbus", DriverType = "Modbus", DriverConfig = "{}" });
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Device_sibling_name_must_be_unique_within_driver()
|
||||
{
|
||||
var driverId = Id("di");
|
||||
using var db = NewContext();
|
||||
db.Devices.Add(new Device { DeviceId = Id("dev"), DriverInstanceId = driverId, Name = "plc-1", DeviceConfig = "{}" });
|
||||
db.Devices.Add(new Device { DeviceId = Id("dev"), DriverInstanceId = driverId, Name = "plc-1", DeviceConfig = "{}" });
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TagGroup_device_root_sibling_name_must_be_unique()
|
||||
{
|
||||
var deviceId = Id("dev");
|
||||
using var db = NewContext();
|
||||
db.TagGroups.Add(new TagGroup { TagGroupId = Id("tg"), DeviceId = deviceId, Name = "motors" });
|
||||
db.TagGroups.Add(new TagGroup { TagGroupId = Id("tg"), DeviceId = deviceId, Name = "motors" });
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_device_root_sibling_name_must_be_unique()
|
||||
{
|
||||
var deviceId = Id("dev");
|
||||
using var db = NewContext();
|
||||
db.Tags.Add(NewTag(deviceId, tagGroupId: null, name: "speed"));
|
||||
db.Tags.Add(NewTag(deviceId, tagGroupId: null, name: "speed"));
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_grouped_sibling_name_must_be_unique_within_group()
|
||||
{
|
||||
var deviceId = Id("dev");
|
||||
var groupId = Id("tg");
|
||||
using var db = NewContext();
|
||||
db.Tags.Add(NewTag(deviceId, tagGroupId: groupId, name: "speed"));
|
||||
db.Tags.Add(NewTag(deviceId, tagGroupId: groupId, name: "speed"));
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_same_name_in_different_groups_is_allowed()
|
||||
{
|
||||
var deviceId = Id("dev");
|
||||
using var db = NewContext();
|
||||
db.Tags.Add(NewTag(deviceId, tagGroupId: Id("tg"), name: "speed"));
|
||||
db.Tags.Add(NewTag(deviceId, tagGroupId: Id("tg"), name: "speed"));
|
||||
|
||||
Should.NotThrow(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnsTagReference_equipment_tag_pair_must_be_unique()
|
||||
{
|
||||
var equipmentId = Id("eq");
|
||||
var tagId = Id("tag");
|
||||
using var db = NewContext();
|
||||
db.UnsTagReferences.Add(new UnsTagReference { UnsTagReferenceId = Id("utr"), EquipmentId = equipmentId, TagId = tagId });
|
||||
db.UnsTagReferences.Add(new UnsTagReference { UnsTagReferenceId = Id("utr"), EquipmentId = equipmentId, TagId = tagId });
|
||||
|
||||
Should.Throw<DbUpdateException>(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnsTagReference_same_tag_different_equipment_is_allowed()
|
||||
{
|
||||
var tagId = Id("tag");
|
||||
using var db = NewContext();
|
||||
db.UnsTagReferences.Add(new UnsTagReference { UnsTagReferenceId = Id("utr"), EquipmentId = Id("eq"), TagId = tagId });
|
||||
db.UnsTagReferences.Add(new UnsTagReference { UnsTagReferenceId = Id("utr"), EquipmentId = Id("eq"), TagId = tagId });
|
||||
|
||||
Should.NotThrow(() => db.SaveChanges());
|
||||
}
|
||||
|
||||
private static Tag NewTag(string deviceId, string? tagGroupId, string name) => new()
|
||||
{
|
||||
TagId = Id("tag"),
|
||||
DeviceId = deviceId,
|
||||
TagGroupId = tagGroupId,
|
||||
Name = name,
|
||||
DataType = "Float",
|
||||
AccessLevel = TagAccessLevel.Read,
|
||||
TagConfig = "{}",
|
||||
};
|
||||
}
|
||||
@@ -28,14 +28,15 @@ public sealed class SchemaComplianceTests
|
||||
{
|
||||
"ServerCluster", "ClusterNode", "ClusterNodeCredential",
|
||||
"ConfigAuditLog",
|
||||
"Namespace", "UnsArea", "UnsLine",
|
||||
"UnsArea", "UnsLine",
|
||||
// v3 Raw-tree tables (Namespace retired; RawFolder/TagGroup/UnsTagReference added)
|
||||
"RawFolder", "TagGroup", "UnsTagReference",
|
||||
"DriverInstance", "Device", "Equipment", "Tag", "PollGroup", "VirtualTag",
|
||||
"NodeAcl", "ExternalIdReservation",
|
||||
"DriverHostStatus",
|
||||
"DriverInstanceResilienceStatus",
|
||||
"LdapGroupRoleMapping",
|
||||
"EquipmentImportBatch",
|
||||
"EquipmentImportRow",
|
||||
// v3 dropped the orphaned EquipmentImportBatch / EquipmentImportRow tables
|
||||
"Script", "ScriptedAlarm", "ScriptedAlarmState",
|
||||
// v2 deploy-model tables (Phase 1 of Akka + fused-hosting alignment)
|
||||
"Deployment", "NodeDeploymentState", "ConfigEdit", "DataProtectionKeys",
|
||||
@@ -60,6 +61,15 @@ SELECT name FROM sys.tables WHERE name <> '__EFMigrationsHistory' ORDER BY name;
|
||||
{
|
||||
("UX_ClusterNodeCredential_Value", "([Enabled]=(1))"),
|
||||
("UX_ExternalIdReservation_KindValue_Active", "([ReleasedAt] IS NULL)"),
|
||||
// v3 sibling-name filtered unique indexes over nullable parent columns.
|
||||
("UX_RawFolder_Parent_Name", "([ParentRawFolderId] IS NOT NULL)"),
|
||||
("UX_RawFolder_ClusterRoot_Name", "([ParentRawFolderId] IS NULL)"),
|
||||
("UX_DriverInstance_Folder_Name", "([RawFolderId] IS NOT NULL)"),
|
||||
("UX_DriverInstance_ClusterRoot_Name", "([RawFolderId] IS NULL)"),
|
||||
("UX_TagGroup_Parent_Name", "([ParentTagGroupId] IS NOT NULL)"),
|
||||
("UX_TagGroup_DeviceRoot_Name", "([ParentTagGroupId] IS NULL)"),
|
||||
("UX_Tag_Group_Name", "([TagGroupId] IS NOT NULL)"),
|
||||
("UX_Tag_Device_Name", "([TagGroupId] IS NULL)"),
|
||||
};
|
||||
|
||||
var rows = QueryRows(@"
|
||||
@@ -148,20 +158,36 @@ SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Equipment
|
||||
columns.ShouldContain(col, $"Equipment missing expected column: {col}");
|
||||
}
|
||||
|
||||
/// <summary>Verifies that Namespace has at least one unique index.</summary>
|
||||
/// <summary>Verifies the retired v2 tables (Namespace, EquipmentImportBatch/Row) are absent.</summary>
|
||||
[Fact]
|
||||
public void Namespace_has_some_unique_index()
|
||||
public void Retired_v2_tables_are_absent()
|
||||
{
|
||||
// v2 dropped the "per-generation" qualifier from namespace uniqueness when live-edit
|
||||
// replaced draft/publish. The v2 index name is implementation-defined; assert that
|
||||
// *some* unique index exists on Namespace to catch unintentional index drops.
|
||||
var indexes = QueryStrings(@"
|
||||
var tables = QueryStrings("SELECT name FROM sys.tables;").ToHashSet();
|
||||
tables.ShouldNotContain("Namespace");
|
||||
tables.ShouldNotContain("EquipmentImportBatch");
|
||||
tables.ShouldNotContain("EquipmentImportRow");
|
||||
}
|
||||
|
||||
/// <summary>Verifies the new v3 Raw-tree tables each carry their logical-id unique index.</summary>
|
||||
[Fact]
|
||||
public void V3_raw_tree_tables_have_logical_id_unique_indexes()
|
||||
{
|
||||
var expected = new[]
|
||||
{
|
||||
("RawFolder", "UX_RawFolder_LogicalId"),
|
||||
("TagGroup", "UX_TagGroup_LogicalId"),
|
||||
("UnsTagReference", "UX_UnsTagReference_LogicalId"),
|
||||
};
|
||||
|
||||
foreach (var (table, index) in expected)
|
||||
{
|
||||
var indexes = QueryStrings($@"
|
||||
SELECT i.name
|
||||
FROM sys.indexes i
|
||||
JOIN sys.tables t ON i.object_id = t.object_id
|
||||
WHERE t.name = 'Namespace' AND i.is_unique = 1;").ToList();
|
||||
|
||||
indexes.ShouldNotBeEmpty();
|
||||
WHERE t.name = '{table}' AND i.is_unique = 1;").ToHashSet();
|
||||
indexes.ShouldContain(index, $"{table} missing unique index {index}");
|
||||
}
|
||||
}
|
||||
|
||||
private List<string> QueryStrings(string sql)
|
||||
|
||||
@@ -182,13 +182,13 @@ public sealed class ScriptingEntitiesTests
|
||||
ctx.ScriptedAlarmStates.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the AddPhase7ScriptingTables migration exists in the assembly.</summary>
|
||||
/// <summary>Verifies that the squashed V3Initial migration exists in the assembly.</summary>
|
||||
[Fact]
|
||||
public void AddPhase7ScriptingTables_migration_exists_in_assembly()
|
||||
public void V3Initial_migration_exists_in_assembly()
|
||||
{
|
||||
// The migration type carries the design-time snapshot + Up/Down methods EF uses to
|
||||
// apply the schema. Missing = schema won't roll forward in deployments.
|
||||
var t = typeof(Migrations.AddPhase7ScriptingTables);
|
||||
// v3 WP1 squashed every prior migration into V3Initial. The migration type carries the
|
||||
// design-time snapshot + Up/Down (incl. the scripting tables) EF uses to apply the schema.
|
||||
var t = typeof(Migrations.V3Initial);
|
||||
t.ShouldNotBeNull();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user