76 lines
3.6 KiB
C#
76 lines
3.6 KiB
C#
using PLCSharp.Models;
|
|
using PLCSharp.VVMs.Vision;
|
|
using System.IO;
|
|
|
|
namespace PLCSharp.Tests;
|
|
|
|
public class VisionAssetRenameTests
|
|
{
|
|
[Fact]
|
|
public void CameraAliasKeepsDiscoveryIdentityAndRestoresBothDocuments()
|
|
{
|
|
var root = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
using var project = ProjectDirectory.New();
|
|
var camera = new PLCSharp.VVMs.Vision.Camera.CameraBase { Name = "CAM" };
|
|
var function = new VisionFunction { Name = "Inspect", RecipeID = project.DefaultRecipeId };
|
|
var shot = new VisionFlow { Type = VisionFlowType.拍照 };
|
|
shot.StringParams["Camera"] = camera.Name;
|
|
function.VisionFlows.Add(shot);
|
|
project.Data.AddRange(camera, function);
|
|
project.Save(root.FullName);
|
|
var files = Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories)
|
|
.ToDictionary(path => path, File.ReadAllBytes);
|
|
var plan = project.PlanVisionAssetRename("Cameras", camera.ID, "Alias");
|
|
project.ApplyRename(plan);
|
|
Assert.Equal("CAM", camera.Params.DiscoveryName);
|
|
Assert.Equal("Alias", shot.StringParams["Camera"]);
|
|
using (var reopened = ProjectDirectory.Open(root.FullName))
|
|
{
|
|
var persisted = reopened.Data.Cameras.Find(camera.ID);
|
|
Assert.Equal("Alias", persisted.Name);
|
|
Assert.Equal("CAM", persisted.Params.DiscoveryName);
|
|
}
|
|
project.ApplyRename(project.PlanUndo(Guid.NewGuid(), plan));
|
|
Assert.Null(camera.Params.DiscoveryName);
|
|
Assert.Equal("CAM", shot.StringParams["Camera"]);
|
|
foreach (var (path, bytes) in files) Assert.Equal(bytes, File.ReadAllBytes(path));
|
|
}
|
|
finally { root.Delete(true); }
|
|
}
|
|
|
|
[Fact]
|
|
public void ImplicitTemplateBelongsToOneFunctionAndUndoRestoresAbsentKey()
|
|
{
|
|
var root = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
using var project = ProjectDirectory.New();
|
|
var a = new VisionFunction { Name = "A", RecipeID = project.DefaultRecipeId };
|
|
var b = new VisionFunction { Name = "B", RecipeID = project.DefaultRecipeId };
|
|
foreach (var owner in new[] { a, b })
|
|
{
|
|
owner.VisionFlows.Add(new VisionFlow { Type = VisionFlowType.ORB匹配 });
|
|
project.Data.Add(owner);
|
|
}
|
|
var image = new LocalImageData { Name = "ORB_Template", FuncID = a.ID };
|
|
project.Data.AddRange(image, new LocalImageData { Name = image.Name, FuncID = b.ID },
|
|
new LocalImageData { Name = "Existing", FuncID = a.ID });
|
|
project.Save(root.FullName);
|
|
var path = Path.Combine(root.FullName, "configuration", "vision.json");
|
|
var bytes = File.ReadAllBytes(path);
|
|
Assert.ThrowsAny<IOException>(() => project.PlanVisionAssetRename("LocalImageDatas", image.ID, "Existing"));
|
|
var plan = project.PlanVisionAssetRename("LocalImageDatas", image.ID, "Renamed");
|
|
Assert.Equal(a.ID, Assert.Single(plan.VisionReferences).FunctionId);
|
|
project.ApplyRename(plan);
|
|
Assert.Equal("Renamed", a.VisionFlows[0].StringParams["TemplateName"]);
|
|
Assert.False(b.VisionFlows[0].StringParams.ContainsKey("TemplateName"));
|
|
project.ApplyRename(project.PlanUndo(Guid.NewGuid(), plan));
|
|
Assert.False(a.VisionFlows[0].StringParams.ContainsKey("TemplateName"));
|
|
Assert.Equal(bytes, File.ReadAllBytes(path));
|
|
}
|
|
finally { root.Delete(true); }
|
|
}
|
|
}
|