|
| 1 | +using CoreNodeModels; |
| 2 | +using Dynamo.Tests; |
| 3 | +using Dynamo.Utilities; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using NUnit.Framework; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | + |
| 11 | +namespace DynamoCoreWpfTests |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public class PythonMigrationViewExtensionTests : DynamoTestUIBase |
| 15 | + { |
| 16 | + protected override void GetLibrariesToPreload(List<string> libraries) |
| 17 | + { |
| 18 | + libraries.Add("VMDataBridge.dll"); |
| 19 | + libraries.Add("DSCoreNodes.dll"); |
| 20 | + base.GetLibrariesToPreload(libraries); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Confirms that nested custom nodes containing CPython3 Python nodes |
| 25 | + /// are migrated to PythonNet3 and produce expected results. |
| 26 | + /// </summary> |
| 27 | + [Test] |
| 28 | + public void NestedCPythonCustomNodesAreAutoMigratedToPythonNet3() |
| 29 | + { |
| 30 | + Assert.IsTrue( |
| 31 | + View.viewExtensionManager.ViewExtensions.Any(e => e != null && e.Name == "Python Migration"), |
| 32 | + "Python Migration view extension is not loaded."); |
| 33 | + |
| 34 | + // Load custom node definitions into the manager first |
| 35 | + var pythonDir = Path.Combine(GetTestDirectory(ExecutingDirectory), "core", "python"); |
| 36 | + var childPath = Path.Combine(pythonDir, "CNWithCPython_Child.dyf"); |
| 37 | + var parentPath = Path.Combine(pythonDir, "CNWithCPython_Parent.dyf"); |
| 38 | + var graphPath = Path.Combine(pythonDir, "WithNestedCPythonCustomNodes.dyn"); |
| 39 | + var expectedValue = "20"; |
| 40 | + |
| 41 | + Assert.IsTrue(File.Exists(childPath), "Missing test file: " + childPath); |
| 42 | + Assert.IsTrue(File.Exists(parentPath), "Missing test file: " + parentPath); |
| 43 | + Assert.IsTrue(File.Exists(graphPath), "Missing test file: " + graphPath); |
| 44 | + |
| 45 | + // Assert that both custom nodes are containing CPython3 python nodes before they are loaded |
| 46 | + AssertDyfContainsPythonNodesWithEngine(childPath, "CPython3"); |
| 47 | + AssertDyfContainsPythonNodesWithEngine(parentPath, "CPython3"); |
| 48 | + |
| 49 | + Assert.IsTrue(ViewModel.Model.CustomNodeManager.AddUninitializedCustomNode(childPath, true, out _)); |
| 50 | + Assert.IsTrue(ViewModel.Model.CustomNodeManager.AddUninitializedCustomNode(parentPath, true, out _)); |
| 51 | + |
| 52 | + // Open graph and run |
| 53 | + Open(graphPath); |
| 54 | + Run(); |
| 55 | + |
| 56 | + // Assert watch value is expected |
| 57 | + var watch = Model.CurrentWorkspace.NodeFromWorkspace<Watch>("8a7664fa-6764-42a1-b3ed-d9e7535819df"); |
| 58 | + Assert.IsNotNull(watch); |
| 59 | + Assert.AreEqual(expectedValue, watch.CachedValue?.ToString()); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Helper method to assert that a .dyf file contains at least one Python node of a given engine type. |
| 64 | + /// </summary> |
| 65 | + private static void AssertDyfContainsPythonNodesWithEngine(string dyfPath, string expectedEngine) |
| 66 | + { |
| 67 | + Assert.IsTrue(File.Exists(dyfPath), "Missing .dyf file: " + dyfPath); |
| 68 | + |
| 69 | + var root = JObject.Parse(File.ReadAllText(dyfPath)); |
| 70 | + var nodes = root["Nodes"] as JArray; |
| 71 | + Assert.IsNotNull(nodes, "Invalid .dyf JSON: missing 'Nodes' array in " + dyfPath); |
| 72 | + |
| 73 | + bool match = nodes |
| 74 | + .OfType<JObject>() |
| 75 | + .Where(n => (n.Value<string>("ConcreteType") ?? string.Empty) |
| 76 | + .StartsWith("PythonNodeModels", StringComparison.Ordinal)) |
| 77 | + .Select(n => n.Value<string>("Engine") ?? n.Value<string>("EngineName")) |
| 78 | + .Any(engine => string.Equals(engine, expectedEngine, StringComparison.Ordinal)); |
| 79 | + |
| 80 | + Assert.IsTrue(match, $"Expected at least one Python node with engine '{expectedEngine}' in '{Path.GetFileName(dyfPath)}'."); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
0 commit comments