Skip to content

Commit c4e03a0

Browse files
committed
tests/canvamain: Add tests for unsafe save/load confirmation
1 parent b489b01 commit c4e03a0

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

orangecanvas/application/tests/test_mainwindow.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
import tempfile
3+
from contextlib import contextmanager
34
from unittest.mock import patch
45

56
from AnyQt.QtWidgets import QToolButton, QDialog, QMessageBox
67

78
from .. import addons
89
from ..outputview import TextStream
10+
from ...scheme import SchemeNode, Scheme
911
from ...utils.shtools import temp_named_file
1012
from ...utils.pickle import swp_name
1113
from ...gui.test import QAppTestCase
@@ -161,6 +163,68 @@ def test_save(self):
161163
w.save_scheme()
162164
self.assertEqual(w.current_document().path(), self.filename)
163165

166+
@contextmanager
167+
def patch_messagebox_exec(self, return_value):
168+
with patch("AnyQt.QtWidgets.QMessageBox.exec",
169+
return_value=return_value) as f:
170+
with patch("AnyQt.QtWidgets.QMessageBox.exec_",
171+
f):
172+
yield f
173+
174+
def test_save_unsafe_warn(self):
175+
w = self.w
176+
doc = w.current_document()
177+
doc.setPath(self.filename)
178+
node = SchemeNode(self.registry.widget("one"))
179+
node.properties = {"a": object()}
180+
doc.addNode(node)
181+
182+
def contents():
183+
with open(self.filename, "r", encoding="utf-8") as f:
184+
return f.read()
185+
with self.patch_messagebox_exec(QMessageBox.Abort) as f:
186+
w.save_scheme()
187+
f.assert_called_with()
188+
self.assertEqual(contents(), "")
189+
with self.patch_messagebox_exec(QMessageBox.Discard) as f:
190+
w.save_scheme()
191+
f.assert_called_with()
192+
self.assertNotIn("pickle", contents())
193+
194+
with self.patch_messagebox_exec(QMessageBox.Ignore) as f:
195+
w.save_scheme()
196+
f.assert_called_with()
197+
self.assertIn("pickle", contents())
198+
199+
def test_load_unsafe_ask(self):
200+
w = self.w
201+
workflow = Scheme()
202+
node = SchemeNode(self.registry.widget("one"))
203+
node.properties = {"a": object()}
204+
workflow.add_node(node)
205+
with open(self.filename, "wb") as f:
206+
workflow.save_to(f, pickle_fallback=True)
207+
208+
with self.patch_messagebox_exec(QMessageBox.Abort) as f:
209+
w.load_scheme(self.filename)
210+
f.assert_called_with()
211+
self.assertEqual(len(w.current_document().scheme().nodes), 0)
212+
self.assertTrue(w.is_transient())
213+
214+
with self.patch_messagebox_exec(return_value=QMessageBox.No) as f:
215+
w.load_scheme(self.filename)
216+
f.assert_called_with()
217+
workflow = w.current_document().scheme()
218+
self.assertEqual(len(workflow.nodes), 1)
219+
self.assertEqual(workflow.nodes[0].properties, {})
220+
221+
with self.patch_messagebox_exec(QMessageBox.Yes) as f:
222+
w.load_scheme(self.filename)
223+
f.assert_called_with()
224+
workflow = w.current_document().scheme()
225+
self.assertEqual(len(workflow.nodes), 1)
226+
self.assertEqual(workflow.nodes[0].properties["a"].__class__, object)
227+
164228
def test_save_swp(self):
165229
w = self.w
166230
swpname = swp_name(w)

0 commit comments

Comments
 (0)