|
1 | 1 | import os |
2 | 2 | import tempfile |
| 3 | +from contextlib import contextmanager |
3 | 4 | from unittest.mock import patch |
4 | 5 |
|
5 | 6 | from AnyQt.QtWidgets import QToolButton, QDialog, QMessageBox |
6 | 7 |
|
7 | 8 | from .. import addons |
8 | 9 | from ..outputview import TextStream |
| 10 | +from ...scheme import SchemeNode, Scheme |
9 | 11 | from ...utils.shtools import temp_named_file |
10 | 12 | from ...utils.pickle import swp_name |
11 | 13 | from ...gui.test import QAppTestCase |
@@ -161,6 +163,68 @@ def test_save(self): |
161 | 163 | w.save_scheme() |
162 | 164 | self.assertEqual(w.current_document().path(), self.filename) |
163 | 165 |
|
| 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 | + |
164 | 228 | def test_save_swp(self): |
165 | 229 | w = self.w |
166 | 230 | swpname = swp_name(w) |
|
0 commit comments