|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using Dynamo; |
| 4 | +using Dynamo.Graph.Workspaces; |
| 5 | +using Dynamo.PythonMigration.Differ; |
| 6 | +using Dynamo.PythonMigration.MigrationAssistant; |
| 7 | +using NUnit.Framework; |
| 8 | +using PythonNodeModels; |
| 9 | + |
| 10 | +namespace DynamoPythonTests |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class PythonMigrationAssistantViewModelTests : DynamoModelTestBase |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Regression test: when Python.Runtime is an incompatible version (e.g. pythonnet 2.x loaded |
| 17 | + /// instead of 3.x), Py.CreateScope() throws TypeLoadException because PyModule does not exist |
| 18 | + /// in that assembly. The ViewModel must catch this and show an error state rather than crashing. |
| 19 | + /// </summary> |
| 20 | + [Test] |
| 21 | + public void WhenMigrationThrowsTypeLoadExceptionViewModelShowsErrorState() |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + var pyNode = new PythonNode(); |
| 25 | + var workspace = CurrentDynamoModel.CurrentWorkspace as WorkspaceModel; |
| 26 | + var pathManager = CurrentDynamoModel.PathManager; |
| 27 | + |
| 28 | + Func<string, string> brokenMigrator = _ => |
| 29 | + throw new TypeLoadException("Could not load type 'Python.Runtime.PyModule' from assembly 'Python.Runtime, Version=2.5.2.12086'"); |
| 30 | + |
| 31 | + // Act |
| 32 | + PythonMigrationAssistantViewModel viewModel = null; |
| 33 | + Assert.DoesNotThrow(() => |
| 34 | + { |
| 35 | + viewModel = new PythonMigrationAssistantViewModel( |
| 36 | + pyNode, workspace, pathManager, new Version(3, 0), brokenMigrator); |
| 37 | + }); |
| 38 | + |
| 39 | + // Assert |
| 40 | + Assert.IsNotNull(viewModel); |
| 41 | + Assert.AreEqual(State.Error, viewModel.CurrentViewModel.DiffState); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Verifies that a MissingMethodException (e.g. missing member in incompatible Python.Runtime) |
| 46 | + /// during migration is handled gracefully and shows an error state. |
| 47 | + /// </summary> |
| 48 | + [Test] |
| 49 | + public void WhenMigrationThrowsMissingMethodExceptionViewModelShowsErrorState() |
| 50 | + { |
| 51 | + var pyNode = new PythonNode(); |
| 52 | + var workspace = CurrentDynamoModel.CurrentWorkspace as WorkspaceModel; |
| 53 | + var pathManager = CurrentDynamoModel.PathManager; |
| 54 | + |
| 55 | + Func<string, string> brokenMigrator = _ => |
| 56 | + throw new MissingMethodException("Python.Runtime.Py", "CreateScope"); |
| 57 | + |
| 58 | + PythonMigrationAssistantViewModel viewModel = null; |
| 59 | + Assert.DoesNotThrow(() => |
| 60 | + { |
| 61 | + viewModel = new PythonMigrationAssistantViewModel( |
| 62 | + pyNode, workspace, pathManager, new Version(3, 0), brokenMigrator); |
| 63 | + }); |
| 64 | + |
| 65 | + Assert.IsNotNull(viewModel); |
| 66 | + Assert.AreEqual(State.Error, viewModel.CurrentViewModel.DiffState); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Verifies that a FileLoadException (e.g. assembly version conflict) during migration |
| 71 | + /// is handled gracefully and shows an error state. |
| 72 | + /// </summary> |
| 73 | + [Test] |
| 74 | + public void WhenMigrationThrowsFileLoadExceptionViewModelShowsErrorState() |
| 75 | + { |
| 76 | + var pyNode = new PythonNode(); |
| 77 | + var workspace = CurrentDynamoModel.CurrentWorkspace as WorkspaceModel; |
| 78 | + var pathManager = CurrentDynamoModel.PathManager; |
| 79 | + |
| 80 | + Func<string, string> brokenMigrator = _ => |
| 81 | + throw new FileLoadException("Could not load file or assembly 'Python.Runtime'"); |
| 82 | + |
| 83 | + PythonMigrationAssistantViewModel viewModel = null; |
| 84 | + Assert.DoesNotThrow(() => |
| 85 | + { |
| 86 | + viewModel = new PythonMigrationAssistantViewModel( |
| 87 | + pyNode, workspace, pathManager, new Version(3, 0), brokenMigrator); |
| 88 | + }); |
| 89 | + |
| 90 | + Assert.IsNotNull(viewModel); |
| 91 | + Assert.AreEqual(State.Error, viewModel.CurrentViewModel.DiffState); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Verifies that a BadImageFormatException (e.g. 32/64-bit or .NET target mismatch in |
| 96 | + /// Python.Runtime) during migration is handled gracefully and shows an error state. |
| 97 | + /// </summary> |
| 98 | + [Test] |
| 99 | + public void WhenMigrationThrowsBadImageFormatExceptionViewModelShowsErrorState() |
| 100 | + { |
| 101 | + var pyNode = new PythonNode(); |
| 102 | + var workspace = CurrentDynamoModel.CurrentWorkspace as WorkspaceModel; |
| 103 | + var pathManager = CurrentDynamoModel.PathManager; |
| 104 | + |
| 105 | + Func<string, string> brokenMigrator = _ => |
| 106 | + throw new BadImageFormatException("The format of the file 'Python.Runtime' is invalid."); |
| 107 | + |
| 108 | + PythonMigrationAssistantViewModel viewModel = null; |
| 109 | + Assert.DoesNotThrow(() => |
| 110 | + { |
| 111 | + viewModel = new PythonMigrationAssistantViewModel( |
| 112 | + pyNode, workspace, pathManager, new Version(3, 0), brokenMigrator); |
| 113 | + }); |
| 114 | + |
| 115 | + Assert.IsNotNull(viewModel); |
| 116 | + Assert.AreEqual(State.Error, viewModel.CurrentViewModel.DiffState); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments