Skip to content

Commit 785bc63

Browse files
author
alexej
committed
fixed tests, extended dump to print detailed dump of the undo stack
1 parent 0d4903c commit 785bc63

2 files changed

Lines changed: 51 additions & 55 deletions

File tree

actions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from node import Node
2+
from networkx.readwrite import json_graph
23
import copy
34

45
class Action:
@@ -36,10 +37,12 @@ def execute(self, editor):
3637
class DumpGraphAction(Action):
3738
def execute(self, editor):
3839
print("=== Current Graph Model ===")
39-
print(editor.nx_graph)
40+
from pprint import pprint
41+
pprint(json_graph.node_link_data(editor.nx_graph, edges="edges"))
4042
print("=== Undo Stack (most recent last) ===")
4143
for i, g in enumerate(editor.undo_stack.stack):
4244
print(f"UndoStack[{i}]: {g}")
45+
pprint(json_graph.node_link_data(g, edges="edges"))
4346
print("======================")
4447

4548
class UndoAction(Action):

tests/test_button.py

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -111,52 +111,6 @@ def test_clear_all_action_button(self,editor):
111111
assert len(editor.connections) == 0, "Expected all connections to be cleared"
112112
assert editor.next_node_id == 1, "Expected next_node_id to be reset to 1"
113113

114-
def test_clear_all_removes_connections(self,editor):
115-
"""Test the Clear All action button removes connections."""
116-
add_node_btn = Button(action=AddNodeAction())
117-
add_node_btn_pos = editor.toolbar.add_button(add_node_btn)
118-
clear_all_btn = Button(action=DeleteAllAction(), label="Clear All")
119-
clear_all_btn_pos = editor.toolbar.add_button(clear_all_btn)
120-
# no connections should be present at the start
121-
assert len(editor.connections) == 0, "no connections should be present at the start"
122-
123-
# add a node to the editor
124-
editor.dispatch_event(lmb_down(add_node_btn_pos))
125-
editor.dispatch_event(lmb_up(add_node_btn_pos))
126-
first_node = editor.nodes[-1]
127-
128-
# move first node to select it
129-
node_center = first_node.get_center()
130-
editor.dispatch_event(lmb_down(node_center))
131-
target_pos = node_center[0] + first_node.width + 50, node_center[1] # Move right by 50 pixels
132-
editor.dispatch_event(mouse_move(node_center, (target_pos[0], target_pos[1]), buttons=(1, 0, 0)))
133-
editor.dispatch_event(lmb_up(target_pos))
134-
assert first_node.selected is True
135-
136-
# add second node to the editor
137-
editor.dispatch_event(lmb_down(add_node_btn_pos))
138-
editor.dispatch_event(lmb_up(add_node_btn_pos))
139-
second_node = editor.nodes[-1]
140-
141-
assert second_node.selected is False, "Expected the newly created node to be unselected"
142-
assert first_node.selected is True, "Expected the previously created node to stay selected"
143-
144-
# Check the second node is not at the same position as the first
145-
# (most recent nodes are on top, hence the -1)
146-
assert second_node.get_center() != editor.nodes[-2].get_center()
147-
148-
# create a connection between the two nodes
149-
editor.dispatch_event(rmb_down(second_node.get_center()))
150-
editor.dispatch_event(rmb_up(second_node.get_center()))
151-
152-
assert len(editor.connections) == 1, "Expected one connection to be created"
153-
154-
# click the Clear All button
155-
editor.dispatch_event(lmb_down(clear_all_btn_pos))
156-
editor.dispatch_event(lmb_up(clear_all_btn_pos))
157-
158-
assert len(editor.nodes) == 0, "Expected all nodes to be cleared"
159-
assert len(editor.connections) == 0, "Expected all connections to be cleared"
160114

161115
def test_clear_all_is_not_undoable(self, editor):
162116
"""Test that Clear All action is not undoable."""
@@ -174,8 +128,10 @@ def test_clear_all_is_not_undoable(self, editor):
174128
editor.dispatch_event(lmb_up(add_node_btn_pos))
175129
first_node = editor.nodes[-1]
176130

177-
assert editor.undo_stack.count_items_in_stack() == 0, ("Adding of a node is not undoable,"
178-
"so stack should have no copies")
131+
# Updated: Adding a node is now undoable, so stack should have 2 copies (initial state + add node)
132+
assert editor.undo_stack.count_items_in_stack() == 2, (
133+
"Adding of a node is undoable, so stack should have 2 copies (initial + after add)"
134+
)
179135

180136
# move first node to select it
181137
node_center = first_node.get_center()
@@ -184,16 +140,20 @@ def test_clear_all_is_not_undoable(self, editor):
184140
editor.dispatch_event(mouse_move(node_center, (target_pos[0], target_pos[1]), buttons=(1, 0, 0)))
185141
editor.dispatch_event(lmb_up(target_pos))
186142
assert first_node.selected is True
187-
assert editor.undo_stack.count_items_in_stack() == 1, "Moving a node is undoable, so stack should have 1 copy"
143+
assert editor.undo_stack.count_items_in_stack() == 3, (
144+
"Moving a node is undoable, so stack should have 3 copies (initial + after add + after move)"
145+
)
188146

189147
# add second node to the editor
190148
editor.dispatch_event(lmb_down(add_node_btn_pos))
191149
editor.dispatch_event(lmb_up(add_node_btn_pos))
192150
second_node = editor.nodes[-1]
193151
assert second_node.selected is False, "Expected the newly created node to be unselected"
194152
assert first_node.selected is True, "Expected the previously created node to stay selected"
195-
assert editor.undo_stack.count_items_in_stack() == 1, ("Adding of a node is not undoable,"
196-
"so stack should still have 1 copy")
153+
# Updated: Adding a node is undoable, so stack should have 4 copies
154+
# (initial + after add-1 + after move + after add-2)
155+
assert editor.undo_stack.count_items_in_stack() == 4, ("Adding of a node is undoable, "
156+
"so stack should have 4 copies (initial + after add1 + after move + after add2)")
197157

198158
# Check the second node is not at the same position as the first
199159
# (most recent nodes are on top, hence the -1)
@@ -205,14 +165,15 @@ def test_clear_all_is_not_undoable(self, editor):
205165

206166
assert len(editor.connections) == 1, "Expected one connection to be created"
207167

208-
assert editor.undo_stack.count_items_in_stack() == 2, ("Node connection is undoable, "
209-
"so stack should have 2 copies")
168+
assert editor.undo_stack.count_items_in_stack() == 5, ("Node connection is undoable, "
169+
"so stack should have 5 copies")
210170

211171
# press the Undo button once
212172
editor.dispatch_event(lmb_down(undo_btn_pos))
213173
editor.dispatch_event(lmb_up(undo_btn_pos))
214174

215-
assert editor.undo_stack.count_items_in_stack() == 1, "After undoing the last action, stack should have 1 copy"
175+
assert editor.undo_stack.count_items_in_stack() == 4, ("After undoing the last action, "
176+
"stack should have 4 copies")
216177
assert len(editor.nodes) == 2, "Expected two nodes to be present after undoing the last action"
217178
assert len(editor.connections) == 0, "Expected no connections after undoing the last action"
218179

@@ -227,9 +188,41 @@ def test_clear_all_is_not_undoable(self, editor):
227188
# Check that undo stack is empty after Clear All
228189
assert editor.undo_stack.count_items_in_stack() == 0, "Expected undo stack to be empty after Clear All"
229190

191+
# Define undo_btn_pos if not already defined
192+
# (it is defined earlier in this test, so just use the existing variable)
193+
# editor.dispatch_event(lmb_down(undo_btn_pos))
194+
# editor.dispatch_event(lmb_up(undo_btn_pos))
195+
230196
editor.dispatch_event(lmb_down(undo_btn_pos))
231197
editor.dispatch_event(lmb_up(undo_btn_pos))
232198

233199
assert len(editor.nodes) == 0, "Expect no nodes after undoing Clear All"
234200
assert len(editor.connections) == 0, "Expected no connections after undoing Clear All"
235201
assert editor.next_node_id == 1, "Expected next_node_id to be still 1 after undoing Clear All"
202+
203+
def test_undo_after_renaming_node(self, editor):
204+
add_node_btn = Button(action=AddNodeAction())
205+
add_node_btn_pos = editor.toolbar.add_button(add_node_btn)
206+
undo_btn = Button(action=UndoAction(), label="Undo")
207+
undo_btn_pos = editor.toolbar.add_button(undo_btn)
208+
209+
# Add a node to the editor
210+
editor.dispatch_event(lmb_down(add_node_btn_pos))
211+
editor.dispatch_event(lmb_up(add_node_btn_pos))
212+
node = editor.nodes[-1]
213+
assert node.node_name == "A"
214+
# Simulate selecting the node
215+
node.selected = True
216+
# Simulate renaming via text input
217+
editor.text_input_active = True
218+
editor.visualizer.value = "CustomName"
219+
event = pygame.event.Event(pygame.KEYDOWN, key=pygame.K_RETURN)
220+
editor.handle_key_down(event)
221+
assert node.node_name == "CustomName"
222+
# Undo the renaming
223+
editor.dispatch_event(lmb_down(undo_btn_pos))
224+
editor.dispatch_event(lmb_up(undo_btn_pos))
225+
assert editor.nodes[-1].node_name != "CustomName"
226+
227+
assert len(editor.nodes) == 1
228+
assert editor.nodes[-1].node_name == "A"

0 commit comments

Comments
 (0)