comparison tests/test_schema.py @ 49:6b8fcd0d2175

Use "==" instaead of "is" when comparing parent schemata. This fixes equality tests of Context instances.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 03 Aug 2023 02:21:13 +0200
parents 92ae1e882cef
children
comparison
equal deleted inserted replaced
48:a0b464f6ab1f 49:6b8fcd0d2175
191 self.assertIs(copied_child.SELF, copied_child) 191 self.assertIs(copied_child.SELF, copied_child)
192 self.assertIsNot(copied_child.SELF, root_schema) 192 self.assertIsNot(copied_child.SELF, root_schema)
193 self.assertEqual(child_schema, copied_child) 193 self.assertEqual(child_schema, copied_child)
194 self.assertIs(copied_child["b"], d2) 194 self.assertIs(copied_child["b"], d2)
195 195
196 def test_nested_copy_pickle(self):
197 d1 = {}
198 d2 = {}
199 root_schema = data_schema._Schema(None, True, type="str", b=d1)
200 child_schema = data_schema._Schema(root_schema, True, type="bool", b=d2)
201 copied_child = child_schema.copy()
202 self.assertIs(copied_child.ROOT, root_schema)
203 self.assertIs(copied_child.SELF, copied_child)
204 self.assertIsNot(copied_child.SELF, root_schema)
205 self.assertEqual(child_schema, copied_child)
206 self.assertIs(copied_child["b"], d2)
207
208 copied_child2 = pickle.loads(pickle.dumps(copied_child))
209 self.assertEqual(copied_child, copied_child2)
210 self.assertIs(copied_child2.SELF, copied_child2)
211 self.assertEqual(root_schema, copied_child2.parent)
212
196 def test_nested_deepcopy(self): 213 def test_nested_deepcopy(self):
197 d1 = {} 214 d1 = {}
198 d2 = {} 215 d2 = {}
199 root_schema = data_schema._Schema(None, True, type="str", b=d1) 216 root_schema = data_schema._Schema(None, True, type="str", b=d1)
200 child_schema = data_schema._Schema(root_schema, True, type="bool", b=d2) 217 child_schema = data_schema._Schema(root_schema, True, type="bool", b=d2)
203 self.assertIs(copied_child.SELF, copied_child) 220 self.assertIs(copied_child.SELF, copied_child)
204 self.assertEqual(child_schema, copied_child) 221 self.assertEqual(child_schema, copied_child)
205 self.assertIsNot(copied_child["b"], d2) 222 self.assertIsNot(copied_child["b"], d2)
206 self.assertNotEqual(root_schema, child_schema) 223 self.assertNotEqual(root_schema, child_schema)
207 224
225 def test_nested_deepcopy_pickle(self):
226 d1 = {}
227 d2 = {}
228 root_schema = data_schema._Schema(None, True, type="str", b=d1)
229 child_schema = data_schema._Schema(root_schema, True, type="bool", b=d2)
230 copied_child = copy.deepcopy(child_schema)
231 self.assertIs(copied_child.ROOT, root_schema)
232 self.assertIs(copied_child.SELF, copied_child)
233 self.assertEqual(child_schema, copied_child)
234 self.assertIsNot(copied_child["b"], d2)
235 self.assertNotEqual(root_schema, child_schema)
236
237 copied_child2 = pickle.loads(pickle.dumps(copied_child))
238 self.assertEqual(copied_child, copied_child2)
239 self.assertIs(copied_child2.SELF, copied_child2)
240 self.assertEqual(root_schema, copied_child2.parent)
241
208 242
209 class ContextCheck(unittest.TestCase): 243 class ContextCheck(unittest.TestCase):
210 244
211 def test_root_without_settings(self): 245 def test_root_without_settings(self):
212 self.assertRaises(TypeError, 246 self.assertRaises(TypeError,
215 root_object=object(), 249 root_object=object(),
216 schema=dict()) 250 schema=dict())
217 251
218 def test_root_context(self): 252 def test_root_context(self):
219 obj = object() 253 obj = object()
220 schema = object() 254 schema = data_schema._Schema(None, True)
221 settings = data_schema.ValidationSettings( 255 settings = data_schema.ValidationSettings(
222 skip_keys=[], break_on_keynames_problems=True, 256 skip_keys=[], break_on_keynames_problems=True,
223 data_stream_loader=None, 257 data_stream_loader=None,
224 schema_loader=data_schema.default_schema_loader) 258 schema_loader=data_schema.default_schema_loader)
225 ctx = data_schema.Context( 259 ctx = data_schema.Context(
229 self.assertTrue(ctx.root_schema is schema) 263 self.assertTrue(ctx.root_schema is schema)
230 self.assertTrue(ctx.settings is settings) 264 self.assertTrue(ctx.settings is settings)
231 265
232 def test_root_context_pickle(self): 266 def test_root_context_pickle(self):
233 obj = object() 267 obj = object()
234 schema = object() 268 schema = data_schema._Schema(None, True)
235 settings = data_schema.ValidationSettings( 269 settings = data_schema.ValidationSettings(
236 skip_keys=[], break_on_keynames_problems=True, 270 skip_keys=[], break_on_keynames_problems=True,
237 data_stream_loader=None, 271 data_stream_loader=None,
238 schema_loader=data_schema.default_schema_loader) 272 schema_loader=data_schema.default_schema_loader)
239 ctx = data_schema.Context( 273 ctx = data_schema.Context(
245 ctx2 = pickle.loads(pickle.dumps(ctx)) 279 ctx2 = pickle.loads(pickle.dumps(ctx))
246 self.assertEqual(ctx, ctx2) 280 self.assertEqual(ctx, ctx2)
247 281
248 def test_parent_of_root_context(self): 282 def test_parent_of_root_context(self):
249 obj = object() 283 obj = object()
250 schema = object() 284 schema = data_schema._Schema(None, True)
251 settings = data_schema.ValidationSettings( 285 settings = data_schema.ValidationSettings(
252 skip_keys=[], break_on_keynames_problems=True, 286 skip_keys=[], break_on_keynames_problems=True,
253 data_stream_loader=None, 287 data_stream_loader=None,
254 schema_loader=data_schema.default_schema_loader) 288 schema_loader=data_schema.default_schema_loader)
255 ctx = data_schema.Context( 289 ctx = data_schema.Context(
298 settings = data_schema.ValidationSettings( 332 settings = data_schema.ValidationSettings(
299 skip_keys=[], break_on_keynames_problems=True, 333 skip_keys=[], break_on_keynames_problems=True,
300 data_stream_loader=None, 334 data_stream_loader=None,
301 schema_loader=data_schema.default_schema_loader) 335 schema_loader=data_schema.default_schema_loader)
302 obj = object() 336 obj = object()
303 schema = object() 337 schema = data_schema._Schema(None, True)
304 ctx = data_schema.Context( 338 ctx = data_schema.Context(
305 None, root_object=obj, root_schema=schema, settings=settings) 339 None, root_object=obj, root_schema=schema, settings=settings)
306 self.assertEqual("<ROOT>", str(ctx)) 340 self.assertEqual("<ROOT>", str(ctx))
307 self.assertTrue(ctx.root_object is obj) 341 self.assertTrue(ctx.root_object is obj)
308 self.assertTrue(ctx.root_schema is schema) 342 self.assertTrue(ctx.root_schema is schema)
613 r = data_schema.try_get_reference( 647 r = data_schema.try_get_reference(
614 "#fo%25o", ctx, self.empty_schema) 648 "#fo%25o", ctx, self.empty_schema)
615 self.assertEqual("bar", r) 649 self.assertEqual("bar", r)
616 650
617 def test_schema_ref_must_have_fragment(self): 651 def test_schema_ref_must_have_fragment(self):
618 ctx = data_schema.Context(None, root_schema={"foo": "bar"}, settings=None) 652 ctx = data_schema.Context(None, root_schema=data_schema._Schema(None, True, {"foo": "bar"}), settings=None)
619 self.assertRaises( 653 self.assertRaises(
620 data_schema.SchemaError, 654 data_schema.SchemaError,
621 data_schema.try_get_reference, 655 data_schema.try_get_reference,
622 "schema:", 656 "schema:",
623 ctx, 657 ctx,
624 self.empty_schema) 658 self.empty_schema)
625 659
626 def test_schema_ref_must_have_absolute_fragment(self): 660 def test_schema_ref_must_have_absolute_fragment(self):
627 ctx = data_schema.Context(None, root_schema={"foo": "bar"}, settings=None) 661 ctx = data_schema.Context(None, root_schema=data_schema._Schema(None, True, {"foo": "bar"}), settings=None)
628 self.assertRaises( 662 self.assertRaises(
629 data_schema.SchemaError, 663 data_schema.SchemaError,
630 data_schema.try_get_reference, 664 data_schema.try_get_reference,
631 "schema:#", 665 "schema:#",
632 ctx, 666 ctx,