feat(template-engine): contained names for composition-derived templates
A composition-derived template now stores its contained name — the
composition slot's InstanceName (e.g. "Pump"), unique only within its
owner — instead of the dotted global path ("Motor Controller.Pump").
The qualified hierarchical name is computed on read.
- TemplateNaming.QualifiedName: walks the OwnerCompositionId chain to
build the dotted path; null-safe, cycle-guarded.
- TemplateConfiguration: the unique index on Template.Name becomes
filtered (WHERE IsDerived = 0) — base templates stay globally unique;
derived templates' uniqueness is the existing (TemplateId,
InstanceName) index on TemplateComposition.
- Migration ContainedDerivedTemplateNames: rewrites derived rows to the
contained name; Down rebuilds the dotted names via a recursive CTE
before restoring the global index.
- TemplateService: composition create/rename store the contained name;
the dotted-name collision pre-checks and cascade-rename are removed
(a slot rename no longer touches nested derived templates).
- TemplateEdit: title shows the contained name; the qualified path is a
breadcrumb subtitle; "composed inside" uses the owner's qualified name.
TDD: 4 TemplateNaming tests + updated composition tests. TemplateEngine
293, ConfigurationDatabase 114, CentralUI 316 green. Migration applied to
the dev cluster and verified in the browser (Motor Controller.Pump now
titled "Pump"; nested Motor Controller.Pump.TempSensor resolves).
Design: docs/plans/2026-05-18-contained-template-names-design.md
This commit is contained in:
87
tests/ScadaLink.TemplateEngine.Tests/TemplateNamingTests.cs
Normal file
87
tests/ScadaLink.TemplateEngine.Tests/TemplateNamingTests.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using ScadaLink.Commons.Entities.Templates;
|
||||
|
||||
namespace ScadaLink.TemplateEngine.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Coverage for <see cref="TemplateNaming.QualifiedName"/> — the computed
|
||||
/// hierarchical name of a composition-derived template. Derived templates store
|
||||
/// only their contained name (the composition slot's <c>InstanceName</c>); the
|
||||
/// dotted path is resolved on read by walking the <c>OwnerCompositionId</c> chain.
|
||||
/// </summary>
|
||||
public class TemplateNamingTests
|
||||
{
|
||||
private static (Dictionary<int, Template> byId, Dictionary<int, TemplateComposition> compById)
|
||||
BuildGraph(params Template[] templates)
|
||||
{
|
||||
var byId = templates.ToDictionary(t => t.Id);
|
||||
var compById = templates
|
||||
.SelectMany(t => t.Compositions)
|
||||
.ToDictionary(c => c.Id);
|
||||
return (byId, compById);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QualifiedName_BaseTemplate_IsJustItsName()
|
||||
{
|
||||
var motorController = new Template("Motor Controller") { Id = 4 };
|
||||
var (byId, compById) = BuildGraph(motorController);
|
||||
|
||||
Assert.Equal("Motor Controller", TemplateNaming.QualifiedName(motorController, byId, compById));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QualifiedName_OneLevelDerived_PrefixesTheOwner()
|
||||
{
|
||||
// Motor Controller composes the Pump template into a slot named "Pump".
|
||||
var motorController = new Template("Motor Controller") { Id = 4 };
|
||||
motorController.Compositions.Add(
|
||||
new TemplateComposition("Pump") { Id = 1014, TemplateId = 4, ComposedTemplateId = 2018 });
|
||||
var derivedPump = new Template("Pump")
|
||||
{
|
||||
Id = 2018, IsDerived = true, OwnerCompositionId = 1014
|
||||
};
|
||||
var (byId, compById) = BuildGraph(motorController, derivedPump);
|
||||
|
||||
Assert.Equal("Motor Controller.Pump", TemplateNaming.QualifiedName(derivedPump, byId, compById));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QualifiedName_NestedDerived_WalksTheWholeChain()
|
||||
{
|
||||
// Motor Controller -> Pump slot -> TempSensor slot.
|
||||
var motorController = new Template("Motor Controller") { Id = 4 };
|
||||
motorController.Compositions.Add(
|
||||
new TemplateComposition("Pump") { Id = 1014, TemplateId = 4, ComposedTemplateId = 2018 });
|
||||
|
||||
var derivedPump = new Template("Pump")
|
||||
{
|
||||
Id = 2018, IsDerived = true, OwnerCompositionId = 1014
|
||||
};
|
||||
derivedPump.Compositions.Add(
|
||||
new TemplateComposition("TempSensor") { Id = 1015, TemplateId = 2018, ComposedTemplateId = 2019 });
|
||||
|
||||
var derivedTempSensor = new Template("TempSensor")
|
||||
{
|
||||
Id = 2019, IsDerived = true, OwnerCompositionId = 1015
|
||||
};
|
||||
var (byId, compById) = BuildGraph(motorController, derivedPump, derivedTempSensor);
|
||||
|
||||
Assert.Equal(
|
||||
"Motor Controller.Pump.TempSensor",
|
||||
TemplateNaming.QualifiedName(derivedTempSensor, byId, compById));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QualifiedName_DerivedWithMissingOwnerLink_FallsBackToStoredName()
|
||||
{
|
||||
// Defensive: a derived template whose owner composition is not in the
|
||||
// lookup must not throw — it falls back to the stored contained name.
|
||||
var orphan = new Template("TempSensor")
|
||||
{
|
||||
Id = 2019, IsDerived = true, OwnerCompositionId = 9999
|
||||
};
|
||||
var (byId, compById) = BuildGraph(orphan);
|
||||
|
||||
Assert.Equal("TempSensor", TemplateNaming.QualifiedName(orphan, byId, compById));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user