Mercurial > hgrepos > Python > libs > data-schema
comparison tests/test_schema.py @ 46:92ae1e882cef
Enhance pickling support
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Wed, 02 Aug 2023 16:51:34 +0200 |
| parents | ea8c2d01a9d9 |
| children | 6b8fcd0d2175 |
comparison
equal
deleted
inserted
replaced
| 45:8a560e0c3180 | 46:92ae1e882cef |
|---|---|
| 22 """Callback for loading test1.schema.yml: Always successful""" | 22 """Callback for loading test1.schema.yml: Always successful""" |
| 23 yield from () | 23 yield from () |
| 24 | 24 |
| 25 | 25 |
| 26 class Pickling(unittest.TestCase): | 26 class Pickling(unittest.TestCase): |
| 27 | |
| 28 def test_sentinel_singleinst(self): | |
| 29 s = data_schema._SENTINEL | |
| 30 s2 = pickle.loads(pickle.dumps(s)) | |
| 31 self.assertIs(s, s2) | |
| 32 self.assertIs(s2, s) | |
| 27 | 33 |
| 28 def test_severity(self): | 34 def test_severity(self): |
| 29 for sev in SEVERITY: | 35 for sev in SEVERITY: |
| 30 b = pickle.dumps(sev) | 36 b = pickle.dumps(sev) |
| 31 sev2 = pickle.loads(b) | 37 sev2 = pickle.loads(b) |
| 110 schema = data_schema._Schema(None, True) | 116 schema = data_schema._Schema(None, True) |
| 111 self.assertIsInstance(schema, dict) | 117 self.assertIsInstance(schema, dict) |
| 112 self.assertEqual(0, len(schema)) | 118 self.assertEqual(0, len(schema)) |
| 113 self.assertFalse(schema) | 119 self.assertFalse(schema) |
| 114 | 120 |
| 121 def test_root_creation_pickle(self): | |
| 122 schema = data_schema._Schema(None, True) | |
| 123 self.assertIsInstance(schema, dict) | |
| 124 schema2 = pickle.loads(pickle.dumps(schema)) | |
| 125 self.assertEqual(schema, schema2) | |
| 126 | |
| 115 def test_root_creation_wrong(self): | 127 def test_root_creation_wrong(self): |
| 116 self.assertRaises( | 128 self.assertRaises( |
| 117 ValueError, | 129 ValueError, |
| 118 data_schema._Schema, | 130 data_schema._Schema, |
| 119 None, | 131 None, |
| 124 self.assertIsNone(schema.parent) | 136 self.assertIsNone(schema.parent) |
| 125 self.assertIs(schema, schema.ROOT) | 137 self.assertIs(schema, schema.ROOT) |
| 126 self.assertTrue(schema.is_sub_root) | 138 self.assertTrue(schema.is_sub_root) |
| 127 self.assertIs(schema, schema.SELF) | 139 self.assertIs(schema, schema.SELF) |
| 128 | 140 |
| 141 def test_root_properties_pickle(self): | |
| 142 schema = data_schema._Schema(None, True) | |
| 143 schema2 = pickle.loads(pickle.dumps(schema)) | |
| 144 self.assertIsNone(schema2.parent) | |
| 145 self.assertIs(schema2, schema2.ROOT) | |
| 146 self.assertTrue(schema2.is_sub_root) | |
| 147 self.assertIs(schema2, schema2.SELF) | |
| 148 | |
| 129 def test_dict_len_bool(self): | 149 def test_dict_len_bool(self): |
| 130 schema = data_schema._Schema(None, True, a=1, b=2) | 150 schema = data_schema._Schema(None, True, a=1, b=2) |
| 131 self.assertTrue(schema) | 151 self.assertTrue(schema) |
| 132 self.assertEqual(2, len(schema)) | 152 self.assertEqual(2, len(schema)) |
| 133 | 153 |
| 134 def test_equality(self): | 154 def test_equality(self): |
| 135 schema1 = data_schema._Schema(None, True, a=1, b=2) | 155 schema1 = data_schema._Schema(None, True, a=1, b=2) |
| 136 schema2 = data_schema._Schema(None, True, b=2, a=1) | 156 schema2 = data_schema._Schema(None, True, b=2, a=1) |
| 137 self.assertEqual(schema1, schema2) | 157 self.assertEqual(schema1, schema2) |
| 138 self.assertIsNot(schema1, schema2) | 158 self.assertIsNot(schema1, schema2) |
| 159 | |
| 160 def test_pickle(self): | |
| 161 schema1 = data_schema._Schema(None, True, a=1, b=2) | |
| 162 schema2 = pickle.loads(pickle.dumps(schema1)) | |
| 163 self.assertEqual(schema1, schema2) | |
| 164 self.assertIsNot(schema1, schema2) | |
| 165 self.assertEqual(2, len(schema2)) | |
| 166 self.assertEqual(1, schema2["a"]) | |
| 167 self.assertEqual(2, schema2["b"]) | |
| 139 | 168 |
| 140 def test_copy(self): | 169 def test_copy(self): |
| 141 schema = data_schema._Schema(None, True, type="str") | 170 schema = data_schema._Schema(None, True, type="str") |
| 142 schema2 = schema.copy() | 171 schema2 = schema.copy() |
| 143 self.assertEqual(schema, schema2) | 172 self.assertEqual(schema, schema2) |
| 197 None, root_object=obj, root_schema=schema, settings=settings) | 226 None, root_object=obj, root_schema=schema, settings=settings) |
| 198 self.assertEqual("<ROOT>", str(ctx)) | 227 self.assertEqual("<ROOT>", str(ctx)) |
| 199 self.assertTrue(ctx.root_object is obj) | 228 self.assertTrue(ctx.root_object is obj) |
| 200 self.assertTrue(ctx.root_schema is schema) | 229 self.assertTrue(ctx.root_schema is schema) |
| 201 self.assertTrue(ctx.settings is settings) | 230 self.assertTrue(ctx.settings is settings) |
| 231 | |
| 232 def test_root_context_pickle(self): | |
| 233 obj = object() | |
| 234 schema = object() | |
| 235 settings = data_schema.ValidationSettings( | |
| 236 skip_keys=[], break_on_keynames_problems=True, | |
| 237 data_stream_loader=None, | |
| 238 schema_loader=data_schema.default_schema_loader) | |
| 239 ctx = data_schema.Context( | |
| 240 None, root_object=obj, root_schema=schema, settings=settings) | |
| 241 self.assertEqual("<ROOT>", str(ctx)) | |
| 242 self.assertTrue(ctx.root_object is obj) | |
| 243 self.assertTrue(ctx.root_schema is schema) | |
| 244 self.assertTrue(ctx.settings is settings) | |
| 245 ctx2 = pickle.loads(pickle.dumps(ctx)) | |
| 246 self.assertEqual(ctx, ctx2) | |
| 202 | 247 |
| 203 def test_parent_of_root_context(self): | 248 def test_parent_of_root_context(self): |
| 204 obj = object() | 249 obj = object() |
| 205 schema = object() | 250 schema = object() |
| 206 settings = data_schema.ValidationSettings( | 251 settings = data_schema.ValidationSettings( |
| 286 root = data_schema.Context(None, settings=settings) | 331 root = data_schema.Context(None, settings=settings) |
| 287 ctx1 = data_schema.Context(root, key="key1") | 332 ctx1 = data_schema.Context(root, key="key1") |
| 288 ctx2 = data_schema.Context(ctx1, index=2) | 333 ctx2 = data_schema.Context(ctx1, index=2) |
| 289 ctx3 = data_schema.Context(ctx2, key="key3") | 334 ctx3 = data_schema.Context(ctx2, key="key3") |
| 290 self.assertEqual("<Context path=`key1 / INDEX:2 / key3'>", repr(ctx3)) | 335 self.assertEqual("<Context path=`key1 / INDEX:2 / key3'>", repr(ctx3)) |
| 336 | |
| 337 def test_repr_pickle(self): | |
| 338 settings = data_schema.ValidationSettings( | |
| 339 skip_keys=[], break_on_keynames_problems=True, | |
| 340 data_stream_loader=None, | |
| 341 schema_loader=data_schema.default_schema_loader) | |
| 342 root = data_schema.Context(None, settings=settings) | |
| 343 ctx1 = data_schema.Context(root, key="key1") | |
| 344 ctx2 = data_schema.Context(ctx1, index=2) | |
| 345 ctx3 = data_schema.Context(ctx2, key="key3") | |
| 346 | |
| 347 ctx3_2 = pickle.loads(pickle.dumps(ctx3)) | |
| 348 | |
| 349 self.assertEqual(ctx3, ctx3_2) | |
| 350 self.assertEqual(repr(ctx3), repr(ctx3_2)) | |
| 351 self.assertEqual("<Context path=`key1 / INDEX:2 / key3'>", repr(ctx3_2)) | |
| 291 | 352 |
| 292 def test_root(self): | 353 def test_root(self): |
| 293 settings = data_schema.ValidationSettings( | 354 settings = data_schema.ValidationSettings( |
| 294 skip_keys=[], break_on_keynames_problems=True, | 355 skip_keys=[], break_on_keynames_problems=True, |
| 295 data_stream_loader=None, | 356 data_stream_loader=None, |
