|
2 | 2 | Test read write |
3 | 3 | """ |
4 | 4 | import io |
| 5 | +from functools import partial |
| 6 | + |
5 | 7 | from xml.etree import ElementTree as ET |
6 | 8 |
|
7 | 9 | from ...gui import test |
@@ -155,6 +157,87 @@ def test_scheme_to_interm(self): |
155 | 157 | interm = scheme_to_interm(workflow) |
156 | 158 | self.assertEqual(parsed, interm) |
157 | 159 |
|
| 160 | + def test_properties_serialize(self): |
| 161 | + workflow = Scheme() |
| 162 | + workflow.load_from( |
| 163 | + io.BytesIO(FOOBAR_v20.encode()), |
| 164 | + registry=foo_registry(with_replaces=True), |
| 165 | + ) |
| 166 | + n1, n2 = workflow.nodes |
| 167 | + self.assertEqual(n1.properties, {"a": 1, "b": 2}) |
| 168 | + self.assertEqual(n2.properties, {"a": 1, "b": 2}) |
| 169 | + f = io.BytesIO() |
| 170 | + workflow.save_to( |
| 171 | + f, data_serializer=partial(readwrite.default_serializer, |
| 172 | + data_format="json")) |
| 173 | + f.seek(0) |
| 174 | + scheme = readwrite.parse_ows_stream(f) |
| 175 | + self.assertEqual(scheme.nodes[0].data.format, "json") |
| 176 | + f.seek(0) |
| 177 | + rl = [] |
| 178 | + rl.append(rl) |
| 179 | + n2.properties = {"a": {"b": rl}} |
| 180 | + with self.assertRaises(readwrite.UnserializableValueError): |
| 181 | + workflow.save_to(f) |
| 182 | + |
| 183 | + n2.properties = {"a": {"b": Obj()}} |
| 184 | + |
| 185 | + with self.assertRaises(readwrite.UnserializableTypeError): |
| 186 | + workflow.save_to(f) |
| 187 | + |
| 188 | + def test_properties_serialize_pickle_fallback(self): |
| 189 | + reg = small_testing_registry() |
| 190 | + workflow = Scheme() |
| 191 | + node = SchemeNode(reg.widget("one")) |
| 192 | + workflow.add_node(node) |
| 193 | + rl = [] |
| 194 | + rl.append(rl) |
| 195 | + node.properties = {"a": {"b": Obj()}} |
| 196 | + f = io.BytesIO() |
| 197 | + workflow.save_to(f, pickle_fallback=True) |
| 198 | + contents = f.getvalue() |
| 199 | + w1 = Scheme() |
| 200 | + |
| 201 | + with self.assertRaises(readwrite.UnsupportedPickleFormatError): |
| 202 | + w1.load_from(io.BytesIO(contents), registry=reg) |
| 203 | + |
| 204 | + w1.clear() |
| 205 | + w1.load_from( |
| 206 | + io.BytesIO(contents), registry=reg, |
| 207 | + data_deserializer=readwrite.default_deserializer_with_pickle_fallback |
| 208 | + ) |
| 209 | + self.assertEqual(node.properties, w1.nodes[0].properties) |
| 210 | + |
| 211 | + def test_properties_deserialize_error_handler(self): |
| 212 | + reg = small_testing_registry() |
| 213 | + workflow = Scheme() |
| 214 | + node = SchemeNode(reg.widget("one")) |
| 215 | + workflow.add_node(node) |
| 216 | + node.properties = {"a": {"b": Obj()}} |
| 217 | + f = io.BytesIO() |
| 218 | + workflow.save_to(f, data_serializer=readwrite.default_serializer_with_pickle_fallback) |
| 219 | + contents = f.getvalue() |
| 220 | + workflow = Scheme() |
| 221 | + |
| 222 | + errors = [] |
| 223 | + warnings = [] |
| 224 | + workflow.load_from( |
| 225 | + io.BytesIO(contents), registry=reg, error_handler=errors.append, |
| 226 | + warning_handler=warnings.append, |
| 227 | + ) |
| 228 | + self.assertEqual(len(errors), 1) |
| 229 | + self.assertEqual(len(warnings), 1) |
| 230 | + |
| 231 | + self.assertIsInstance(errors[0], readwrite.UnsupportedPickleFormatError) |
| 232 | + self.assertIs(errors[0].node, workflow.nodes[0]) |
| 233 | + self.assertIsInstance(warnings[0], readwrite.DeserializationWarning) |
| 234 | + self.assertIs(warnings[0].node, workflow.nodes[0]) |
| 235 | + |
| 236 | + |
| 237 | +class Obj: |
| 238 | + def __eq__(self, other): |
| 239 | + return isinstance(other, Obj) |
| 240 | + |
158 | 241 |
|
159 | 242 | def foo_registry(with_replaces=True): |
160 | 243 | reg = WidgetRegistry() |
@@ -207,5 +290,9 @@ def foo_registry(with_replaces=True): |
207 | 290 | <text id="0" rect="10, 10, 30, 30" type="text/plain">Hello World</text> |
208 | 291 | <arrow id="1" start="30, 30" end="60, 60" fill="red" /> |
209 | 292 | </annotations> |
| 293 | + <node_properties> |
| 294 | + <properties format="literal" node_id="0">{'a': 1, 'b': 2}</properties> |
| 295 | + <properties format="literal" node_id="1">{'a': 1, 'b': 2}</properties> |
| 296 | + </node_properties> |
210 | 297 | </scheme> |
211 | 298 | """ |
0 commit comments