Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Uns/UnsTreeServiceLoadEditTests.cs
T
Joseph Doherty 6a616a1ab2 test(adminui): migrate AdminUI.Tests to v3 greenfield schema + Batch-1 stubs
Migrate tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests (was 358 build errors) to
the v3 Raw-only Tag schema and the Batch-1 AdminUI stubs. Production untouched.

- Driver-page serialization tests: drop the retired pre-declared Tags editor
  (page *TagRow types + DriverOptions.Tags); multi-device pages keep their
  Devices editor (ToOptions(devices)). Enum-serialization round-trip coverage
  (CpuType/ModbusFamily/MelsecFamily/AbCipPlcFamily/AbLegacyPlcFamily/FocasCncSeries)
  preserved intact.
- UnsTreeTestDb: reseed to v3 (Device->Tag raw slice, no EquipmentId/DriverInstanceId
  on Tag, no Namespace).
- UnsTreeService tag tests: assert Batch-1 stubs (CreateTag/UpdateTag refusal,
  empty tag/tag-driver lists); DeleteTag stays live against a raw Tag.
- Equipment #122 guard retained (validates input driver vs line cluster); drop the
  retired persisted-binding assertions + NamespaceId seeds. Area/Line #122 driver-
  orphan guard retired -> assert cross-cluster move now succeeds.
- DeleteCluster: drop the deleted-Namespace child-check test.
- OpcUaClientTagConfigModel/TagConfigValidator: address key FullName -> nodeId.
- ScriptTagCatalog: project surviving Name/DataType/TagConfig; DriverInstanceId null.
- VirtualTag {{equip}} equipment-tag-derived base is dark -> 3 tests skipped (Batch-3).

Result: build green; 507 passed / 3 skipped / 0 failed.
2026-07-15 21:50:48 -04:00

202 lines
6.7 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
/// <summary>
/// Verifies the load-for-edit projections on <see cref="UnsTreeService"/> that prefill the
/// Area/Line/Equipment edit modals and carry the concurrency token back for last-write-wins saves.
/// </summary>
[Trait("Category", "Unit")]
public sealed class UnsTreeServiceLoadEditTests
{
private static (UnsTreeService Service, string DbName) Seeded()
{
var dbName = $"uns-loadedit-{Guid.NewGuid():N}";
UnsTreeTestDb.SeedNamed(dbName);
return (new UnsTreeService(UnsTreeTestDb.Factory(dbName)), dbName);
}
/// <summary>Loading a seeded area maps its fields, owning cluster, and a non-empty RowVersion.</summary>
[Fact]
public async Task LoadArea_returns_dto()
{
var (service, _) = Seeded();
var dto = await service.LoadAreaAsync("AREA-1");
dto.ShouldNotBeNull();
dto.UnsAreaId.ShouldBe("AREA-1");
dto.Name.ShouldBe("assembly");
dto.ClusterId.ShouldBe(UnsTreeTestDb.PopulatedClusterId);
dto.RowVersion.ShouldNotBeNull();
}
/// <summary>Loading a missing area returns null.</summary>
[Fact]
public async Task LoadArea_missing_returns_null()
{
var (service, _) = Seeded();
(await service.LoadAreaAsync("NOPE")).ShouldBeNull();
}
/// <summary>Loading a seeded line maps its fields, parent area, and a non-empty RowVersion.</summary>
[Fact]
public async Task LoadLine_returns_dto()
{
var (service, _) = Seeded();
var dto = await service.LoadLineAsync("LINE-1");
dto.ShouldNotBeNull();
dto.UnsLineId.ShouldBe("LINE-1");
dto.UnsAreaId.ShouldBe("AREA-1");
dto.Name.ShouldBe("line-a");
dto.RowVersion.ShouldNotBeNull();
}
/// <summary>Loading a missing line returns null.</summary>
[Fact]
public async Task LoadLine_missing_returns_null()
{
var (service, _) = Seeded();
(await service.LoadLineAsync("NOPE")).ShouldBeNull();
}
/// <summary>Loading the seeded equipment maps its identity fields, line, and a non-empty RowVersion.</summary>
[Fact]
public async Task LoadEquipment_returns_dto()
{
var (service, _) = Seeded();
var dto = await service.LoadEquipmentAsync(UnsTreeTestDb.SeededEquipmentId);
dto.ShouldNotBeNull();
dto.EquipmentId.ShouldBe(UnsTreeTestDb.SeededEquipmentId);
dto.Name.ShouldBe("machine-1");
dto.MachineCode.ShouldBe("machine_001");
dto.UnsLineId.ShouldBe("LINE-1");
dto.RowVersion.ShouldNotBeNull();
}
/// <summary>Loading a missing equipment returns null.</summary>
[Fact]
public async Task LoadEquipment_missing_returns_null()
{
var (service, _) = Seeded();
(await service.LoadEquipmentAsync("NOPE")).ShouldBeNull();
}
/// <summary>
/// LoadDriversForCluster returns every driver in the cluster (any namespace kind), ordered by id,
/// with the <c>"{Id} — {Name} ({DriverType})"</c> display; drivers in other clusters are excluded.
/// </summary>
[Fact]
public async Task LoadDriversForCluster_returns_cluster_drivers()
{
var (service, dbName) = Seeded();
await using (var db = UnsTreeTestDb.CreateNamed(dbName))
{
db.DriverInstances.Add(new DriverInstance
{
DriverInstanceId = "DRV-B",
ClusterId = UnsTreeTestDb.PopulatedClusterId,
Name = "modbus-b",
DriverType = "Modbus",
DriverConfig = "{}",
});
db.DriverInstances.Add(new DriverInstance
{
DriverInstanceId = "DRV-A",
ClusterId = UnsTreeTestDb.PopulatedClusterId,
Name = "galaxy-a",
DriverType = "Galaxy",
DriverConfig = "{}",
});
// A driver in a different cluster must be excluded.
db.DriverInstances.Add(new DriverInstance
{
DriverInstanceId = "DRV-OTHER",
ClusterId = UnsTreeTestDb.EmptyClusterId,
Name = "other",
DriverType = "S7",
DriverConfig = "{}",
});
await db.SaveChangesAsync();
}
var drivers = await service.LoadDriversForClusterAsync(UnsTreeTestDb.PopulatedClusterId);
drivers.Count.ShouldBe(2);
drivers[0].DriverInstanceId.ShouldBe("DRV-A");
drivers[0].Display.ShouldBe("DRV-A — galaxy-a (Galaxy)");
drivers[1].DriverInstanceId.ShouldBe("DRV-B");
drivers[1].Display.ShouldBe("DRV-B — modbus-b (Modbus)");
}
/// <summary>
/// Loading a seeded Raw tag maps its surviving fields (Name/DataType/AccessLevel/TagConfig) and a
/// non-empty RowVersion. v3: Tag is Raw-only — the equipment + driver bindings were retired, so the
/// DTO's <c>EquipmentId</c> / <c>DriverInstanceId</c> project as empty strings.
/// </summary>
[Fact]
public async Task LoadTag_returns_dto()
{
var (service, _) = Seeded();
var dto = await service.LoadTagAsync("TAG-1");
dto.ShouldNotBeNull();
dto.TagId.ShouldBe("TAG-1");
dto.EquipmentId.ShouldBe(string.Empty);
dto.Name.ShouldBe("speed");
dto.DriverInstanceId.ShouldBe(string.Empty);
dto.DataType.ShouldBe("Float");
dto.AccessLevel.ShouldBe(TagAccessLevel.Read);
dto.TagConfig.ShouldBe("{}");
dto.RowVersion.ShouldNotBeNull();
}
/// <summary>Loading a missing tag returns null.</summary>
[Fact]
public async Task LoadTag_missing_returns_null()
{
var (service, _) = Seeded();
(await service.LoadTagAsync("NOPE")).ShouldBeNull();
}
/// <summary>Loading a seeded virtual tag maps its fields, owning equipment, and a non-empty RowVersion.</summary>
[Fact]
public async Task LoadVirtualTag_returns_dto()
{
var (service, _) = Seeded();
var dto = await service.LoadVirtualTagAsync("VTAG-1");
dto.ShouldNotBeNull();
dto.VirtualTagId.ShouldBe("VTAG-1");
dto.EquipmentId.ShouldBe(UnsTreeTestDb.SeededEquipmentId);
dto.Name.ShouldBe("computed");
dto.DataType.ShouldBe("Double");
dto.ScriptId.ShouldBe("SCRIPT-1");
dto.RowVersion.ShouldNotBeNull();
}
/// <summary>Loading a missing virtual tag returns null.</summary>
[Fact]
public async Task LoadVirtualTag_missing_returns_null()
{
var (service, _) = Seeded();
(await service.LoadVirtualTagAsync("NOPE")).ShouldBeNull();
}
}