Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Configuration.Tests/ScriptingEntitiesTests.cs
T
Joseph Doherty 90de55535d fix(config): #1040 #1049 script FKs, unique ZTag/SAPID indexes, reservation ledger retired
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.
2026-12-03 15:14:26 -05:00

158 lines
7.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
/// <summary>
/// Verifies the Phase 7 Stream E entities (<see cref="Script"/>, <see cref="VirtualTag"/>,
/// <see cref="ScriptedAlarm"/>) register correctly in the EF model, map to the expected
/// tables/columns/indexes, and carry the check constraints the plan decisions call for.
/// Introspection only — no SQL Server required.
/// </summary>
[Trait("Category", "Unit")]
public sealed class ScriptingEntitiesTests
{
private static OtOpcUaConfigDbContext BuildCtx()
{
var options = new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
.UseSqlServer("Server=(local);Database=dummy;Integrated Security=true") // not connected
.Options;
return new OtOpcUaConfigDbContext(options);
}
private static Microsoft.EntityFrameworkCore.Metadata.IModel DesignModel(OtOpcUaConfigDbContext ctx)
=> ctx.GetService<IDesignTimeModel>().Model;
/// <summary>Verifies that the Script entity is registered with the expected table name and columns.</summary>
[Fact]
public void Script_entity_registered_with_expected_table_and_columns()
{
using var ctx = BuildCtx();
var entity = ctx.Model.FindEntityType(typeof(Script)).ShouldNotBeNull();
entity.GetTableName().ShouldBe("Script");
entity.FindProperty(nameof(Script.ScriptRowId)).ShouldNotBeNull();
entity.FindProperty(nameof(Script.ScriptId)).ShouldNotBeNull()
.GetMaxLength().ShouldBe(64);
entity.FindProperty(nameof(Script.SourceCode)).ShouldNotBeNull()
.GetColumnType().ShouldBe("nvarchar(max)");
entity.FindProperty(nameof(Script.SourceHash)).ShouldNotBeNull()
.GetMaxLength().ShouldBe(64);
entity.FindProperty(nameof(Script.Language)).ShouldNotBeNull()
.GetMaxLength().ShouldBe(16);
}
/// <summary>Verifies that the Script entity has a unique logical ID constraint.</summary>
[Fact]
public void Script_has_unique_logical_id()
{
// v2 live-edit dropped the per-generation qualifier; Script.ScriptId is globally unique.
using var ctx = BuildCtx();
var entity = ctx.Model.FindEntityType(typeof(Script)).ShouldNotBeNull();
// lmxopcua#1040: the logical id is the principal key of the VirtualTag / ScriptedAlarm FKs, so it is an
// alternate key (a unique constraint) named as the unique index it replaced.
var key = entity.GetKeys().SingleOrDefault(k => k.Properties.Select(p => p.Name).SequenceEqual([nameof(Script.ScriptId)]));
key.ShouldNotBeNull("Script.ScriptId must be a unique (alternate) key");
key.GetName().ShouldBe("UX_Script_LogicalId");
}
/// <summary>Verifies that the VirtualTag entity is registered with the expected trigger check constraint.</summary>
[Fact]
public void VirtualTag_entity_registered_with_trigger_check_constraint()
{
using var ctx = BuildCtx();
var entity = DesignModel(ctx).FindEntityType(typeof(VirtualTag)).ShouldNotBeNull();
entity.GetTableName().ShouldBe("VirtualTag");
var checks = entity.GetCheckConstraints().Select(c => c.Name).ToArray();
checks.ShouldContain("CK_VirtualTag_Trigger_AtLeastOne");
checks.ShouldContain("CK_VirtualTag_TimerInterval_Min");
}
/// <summary>Verifies that the VirtualTag entity has a unique index on its logical key.</summary>
[Fact]
public void VirtualTag_has_unique_index_on_logical_key()
{
// v2 live-edit dropped the per-generation qualifier on VirtualTag's uniqueness.
using var ctx = BuildCtx();
var entity = ctx.Model.FindEntityType(typeof(VirtualTag)).ShouldNotBeNull();
entity.GetIndexes().Any(i => i.IsUnique).ShouldBeTrue("VirtualTag needs at least one unique index");
}
/// <summary>Verifies that the VirtualTag entity has ChangeTriggered, Historize, and TimerIntervalMs properties.</summary>
[Fact]
public void VirtualTag_has_ChangeTriggered_and_Historize_flags()
{
using var ctx = BuildCtx();
var entity = ctx.Model.FindEntityType(typeof(VirtualTag)).ShouldNotBeNull();
entity.FindProperty(nameof(VirtualTag.ChangeTriggered)).ShouldNotBeNull()
.ClrType.ShouldBe(typeof(bool));
entity.FindProperty(nameof(VirtualTag.Historize)).ShouldNotBeNull()
.ClrType.ShouldBe(typeof(bool));
entity.FindProperty(nameof(VirtualTag.TimerIntervalMs)).ShouldNotBeNull()
.ClrType.ShouldBe(typeof(int?));
}
/// <summary>Verifies that the ScriptedAlarm entity is registered with severity and type check constraints.</summary>
[Fact]
public void ScriptedAlarm_entity_registered_with_severity_and_type_checks()
{
using var ctx = BuildCtx();
var entity = DesignModel(ctx).FindEntityType(typeof(ScriptedAlarm)).ShouldNotBeNull();
entity.GetTableName().ShouldBe("ScriptedAlarm");
var checks = entity.GetCheckConstraints().Select(c => c.Name).ToArray();
checks.ShouldContain("CK_ScriptedAlarm_Severity_Range");
checks.ShouldContain("CK_ScriptedAlarm_AlarmType");
}
/// <summary>Verifies that ScriptedAlarm has HistorizeToAveva defaulted to true per plan decision 15.</summary>
[Fact]
public void ScriptedAlarm_has_HistorizeToAveva_default_true_per_plan_decision_15()
{
// Defaults live on the CLR default assignment — verify the initializer.
var alarm = new ScriptedAlarm
{
ScriptedAlarmId = "a1",
EquipmentId = "eq1",
Name = "n",
AlarmType = "LimitAlarm",
MessageTemplate = "m",
PredicateScriptId = "s1",
};
alarm.HistorizeToAveva.ShouldBeTrue();
alarm.Retain.ShouldBeTrue();
alarm.Severity.ShouldBe(500);
alarm.Enabled.ShouldBeTrue();
}
// The ConfigDb-backed ScriptedAlarmState entity + table were retired in per-cluster mesh Phase 4
// (Task 9): scripted-alarm condition state lives in the replicated LocalDb store. The entity-model
// tests that covered it are gone with it.
/// <summary>Verifies that all new Phase 7 entities are exposed via DbSet properties.</summary>
[Fact]
public void All_new_entities_exposed_via_DbSet()
{
using var ctx = BuildCtx();
ctx.Scripts.ShouldNotBeNull();
ctx.VirtualTags.ShouldNotBeNull();
ctx.ScriptedAlarms.ShouldNotBeNull();
}
/// <summary>Verifies that the squashed V3Initial migration exists in the assembly.</summary>
[Fact]
public void V3Initial_migration_exists_in_assembly()
{
// 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();
}
}