|
1 | 1 | import json |
| 2 | +import os |
2 | 3 | from textwrap import dedent |
| 4 | +from pathlib import Path |
3 | 5 |
|
4 | 6 | from meshroom.core import desc |
5 | 7 | from meshroom.core.graph import Graph |
@@ -43,6 +45,10 @@ class NodeWithListAttributes(desc.Node): |
43 | 45 | ] |
44 | 46 |
|
45 | 47 |
|
| 48 | +def assertPathsAreEqual(pathA, pathB): |
| 49 | + return Path(pathA).resolve().as_posix() == Path(pathB).resolve().as_posix() |
| 50 | + |
| 51 | + |
46 | 52 | def compareGraphsContent(graphA: Graph, graphB: Graph) -> bool: |
47 | 53 | """Returns whether the content (node and deges) of two graphs are considered identical. |
48 | 54 |
|
@@ -214,6 +220,44 @@ def test_importingDifferentNodeVersionCreatesCompatibilityNodes(self, graphSaved |
214 | 220 | assert otherGraph.node(node.name).issue is CompatibilityIssue.VersionConflict |
215 | 221 |
|
216 | 222 |
|
| 223 | +class TestGraphSave: |
| 224 | + def test_generateNextPath(self, graphSavedOnDisk): |
| 225 | + graph: Graph = graphSavedOnDisk |
| 226 | + root = os.path.dirname(graph._filepath) |
| 227 | + # Files with no version number (e.g., "scene.mg" -> "scene1.mg") |
| 228 | + graph._filepath = os.path.join(root, "scene.mg") |
| 229 | + assertPathsAreEqual(graph._generateNextPath(), os.path.join(root, "scene1.mg")) |
| 230 | + # Files with existing version numbers (e.g., "scene1.mg" -> "scene2.mg") |
| 231 | + graph._filepath = os.path.join(root, "scene_1.mg") |
| 232 | + assertPathsAreEqual(graph._generateNextPath(), os.path.join(root, "scene_2.mg")) |
| 233 | + # Edge cases like filenames that are purely numeric (e.g., "123.mg") |
| 234 | + # Also test that the padding is kept ("001" -> "002" and not "2") |
| 235 | + graph._filepath = os.path.join(root, "0123.mg") |
| 236 | + assertPathsAreEqual(graph._generateNextPath(), os.path.join(root, "0124.mg")) |
| 237 | + graph._filepath = os.path.join(root, "scene_001.mg") |
| 238 | + assertPathsAreEqual(graph._generateNextPath(), os.path.join(root, "scene_002.mg")) |
| 239 | + # Files where the next version already exists (e.g., "scene1.mg" when "scene2.mg" exists -> "scene3.mg") |
| 240 | + graph._filepath = os.path.join(root, "scene1.mg") |
| 241 | + open(os.path.join(root, "scene2.mg"), 'a').close() |
| 242 | + assertPathsAreEqual(graph._generateNextPath(), os.path.join(root, "scene3.mg")) |
| 243 | + |
| 244 | + def test_saveAsNewVersion(self, tmp_path): |
| 245 | + graph = Graph("") |
| 246 | + with registeredNodeTypes([SimpleNode]): |
| 247 | + # Create scene |
| 248 | + nodeA = graph.addNewNode(SimpleNode.__name__) |
| 249 | + scenePath = os.path.join(tmp_path, "scene.mg") |
| 250 | + graph._filepath = scenePath |
| 251 | + graph.save() |
| 252 | + assert os.path.exists(scenePath) |
| 253 | + # Modify scene |
| 254 | + nodeB = graph.addNewNode(SimpleNode.__name__) |
| 255 | + nodeA.output.connectTo(nodeB.input) |
| 256 | + graph.saveAsNewVersion() |
| 257 | + newScenePath = os.path.join(tmp_path, "scene1.mg") |
| 258 | + assert os.path.exists(newScenePath) |
| 259 | + |
| 260 | + |
217 | 261 | class TestGraphPartialSerialization: |
218 | 262 | def test_emptyGraph(self): |
219 | 263 | graph = Graph("") |
|
0 commit comments