Skip to content

Commit 2b6aa45

Browse files
committed
[tests] Add tests and more protection for the rename in core/graph
1 parent 591c04e commit 2b6aa45

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

meshroom/core/graph.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,23 @@ def addNode(self, node, uniqueName=None):
514514
return node
515515

516516
def renameNode(self, node: Node, newName: str):
517-
""" Rename a node in the Node Graph
517+
""" Rename a node in the Node Graph.
518+
If the proposed name is already assigned to a node then it will create a unique name
518519
519520
Args:
520521
node (Node): Node to rename.
521522
newName (str): New name of the node. Must be unique in the graph.
522523
"""
524+
# Handle empty string
525+
if not newName:
526+
return
527+
usedNames = {n._name for n in self._nodes if n != node}
528+
# Make sure we rename to an available name
529+
if newName in usedNames:
530+
newName = self._createUniqueNodeName(newName, usedNames)
531+
# Rename in the dict model
523532
self._nodes.rename(node._name, newName)
524-
# Finally rename
533+
# Finally rename the node name property and notify Qt
525534
node._name = newName
526535
node.nodeNameChanged.emit()
527536

meshroom/ui/commands.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,9 @@ def __init__(self, graph, node, name, parent=None):
161161
self.name = name
162162

163163
def redoImpl(self):
164-
newName = self.name
165-
usedNames = {n._name for n in self.graph._nodes}
166-
if newName in usedNames:
167-
newName = self.graph._createUniqueNodeName(self.name, usedNames)
168-
self.setText(f"Rename Node {self.oldName} to {newName}")
164+
self.setText(f"Rename Node {self.oldName} to {self.name}")
169165
self.graph.renameNode(self.node, self.name)
170-
return newName
166+
return self.node._name
171167

172168
def undoImpl(self):
173169
self.graph.renameNode(self.node, self.oldName)

tests/test_graph.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,27 @@ def test_duplicate_nodes():
278278
assert nMap[n2][0].input.inputLink == nMap[n1][0].output
279279
assert nMap[n3][0].input.inputLink == nMap[n1][0].output
280280
assert nMap[n3][0].input2.inputLink == nMap[n2][0].output
281+
282+
283+
def test_rename_nodes():
284+
"""
285+
Test renaming nodes.
286+
"""
287+
288+
graph = Graph("")
289+
ls0 = graph.addNewNode("Ls")
290+
ls1 = graph.addNewNode("Ls")
291+
ls2 = graph.addNewNode("Ls")
292+
293+
# Test with empty string
294+
assert ls0.name == "Ls_1"
295+
graph.renameNode(ls0, "")
296+
assert ls0.name == "Ls_1"
297+
298+
# Rename
299+
graph.renameNode(ls0, "nodels")
300+
assert ls0.name == "nodels"
301+
graph.renameNode(ls1, "nodels")
302+
assert ls1.name == "nodels_1"
303+
graph.renameNode(ls2, "nodels")
304+
assert ls2.name == "nodels_2"

0 commit comments

Comments
 (0)