changeset 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 a0b464f6ab1f
children 0dd9a251f884
files data_schema/__init__.py tests/test_schema.py
diffstat 2 files changed, 42 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/data_schema/__init__.py	Thu Aug 03 02:19:46 2023 +0200
+++ b/data_schema/__init__.py	Thu Aug 03 02:21:13 2023 +0200
@@ -377,7 +377,7 @@
     def __eq__(self, other):
         if not isinstance(other, _Schema):
             return NotImplemented
-        return (self.parent is other.parent
+        return ((self.parent == other.parent)
                 and bool(self.is_sub_root) == bool(other.is_sub_root)
                 and dict(self) == dict(other))
 
@@ -495,7 +495,7 @@
                 and (self._key_index == other._key_index)
 # XXX FIXME ???
 #                and (self.root_object == other.root_object)
-#                and (self.root_schema == other.root_schema)
+                and (self.root_schema == other.root_schema)
 #                and (self._current_object == other._current_object)
                 and (self._settings == other._settings)
         )
--- a/tests/test_schema.py	Thu Aug 03 02:19:46 2023 +0200
+++ b/tests/test_schema.py	Thu Aug 03 02:21:13 2023 +0200
@@ -193,6 +193,23 @@
         self.assertEqual(child_schema, copied_child)
         self.assertIs(copied_child["b"], d2)
 
+    def test_nested_copy_pickle(self):
+        d1 = {}
+        d2 = {}
+        root_schema = data_schema._Schema(None, True, type="str", b=d1)
+        child_schema = data_schema._Schema(root_schema, True, type="bool", b=d2)
+        copied_child = child_schema.copy()
+        self.assertIs(copied_child.ROOT, root_schema)
+        self.assertIs(copied_child.SELF, copied_child)
+        self.assertIsNot(copied_child.SELF, root_schema)
+        self.assertEqual(child_schema, copied_child)
+        self.assertIs(copied_child["b"], d2)
+
+        copied_child2 = pickle.loads(pickle.dumps(copied_child))
+        self.assertEqual(copied_child, copied_child2)
+        self.assertIs(copied_child2.SELF, copied_child2)
+        self.assertEqual(root_schema, copied_child2.parent)
+
     def test_nested_deepcopy(self):
         d1 = {}
         d2 = {}
@@ -205,6 +222,23 @@
         self.assertIsNot(copied_child["b"], d2)
         self.assertNotEqual(root_schema, child_schema)
 
+    def test_nested_deepcopy_pickle(self):
+        d1 = {}
+        d2 = {}
+        root_schema = data_schema._Schema(None, True, type="str", b=d1)
+        child_schema = data_schema._Schema(root_schema, True, type="bool", b=d2)
+        copied_child = copy.deepcopy(child_schema)
+        self.assertIs(copied_child.ROOT, root_schema)
+        self.assertIs(copied_child.SELF, copied_child)
+        self.assertEqual(child_schema, copied_child)
+        self.assertIsNot(copied_child["b"], d2)
+        self.assertNotEqual(root_schema, child_schema)
+
+        copied_child2 = pickle.loads(pickle.dumps(copied_child))
+        self.assertEqual(copied_child, copied_child2)
+        self.assertIs(copied_child2.SELF, copied_child2)
+        self.assertEqual(root_schema, copied_child2.parent)
+
 
 class ContextCheck(unittest.TestCase):
 
@@ -217,7 +251,7 @@
 
     def test_root_context(self):
         obj = object()
-        schema = object()
+        schema = data_schema._Schema(None, True)
         settings = data_schema.ValidationSettings(
             skip_keys=[], break_on_keynames_problems=True,
             data_stream_loader=None,
@@ -231,7 +265,7 @@
 
     def test_root_context_pickle(self):
         obj = object()
-        schema = object()
+        schema = data_schema._Schema(None, True)
         settings = data_schema.ValidationSettings(
             skip_keys=[], break_on_keynames_problems=True,
             data_stream_loader=None,
@@ -247,7 +281,7 @@
 
     def test_parent_of_root_context(self):
         obj = object()
-        schema = object()
+        schema = data_schema._Schema(None, True)
         settings = data_schema.ValidationSettings(
             skip_keys=[], break_on_keynames_problems=True,
             data_stream_loader=None,
@@ -300,7 +334,7 @@
             data_stream_loader=None,
             schema_loader=data_schema.default_schema_loader)
         obj = object()
-        schema = object()
+        schema = data_schema._Schema(None, True)
         ctx = data_schema.Context(
             None, root_object=obj, root_schema=schema, settings=settings)
         self.assertEqual("<ROOT>", str(ctx))
@@ -615,7 +649,7 @@
         self.assertEqual("bar", r)
 
     def test_schema_ref_must_have_fragment(self):
-        ctx = data_schema.Context(None, root_schema={"foo": "bar"}, settings=None)
+        ctx = data_schema.Context(None, root_schema=data_schema._Schema(None, True, {"foo": "bar"}), settings=None)
         self.assertRaises(
             data_schema.SchemaError,
             data_schema.try_get_reference,
@@ -624,7 +658,7 @@
             self.empty_schema)
 
     def test_schema_ref_must_have_absolute_fragment(self):
-        ctx = data_schema.Context(None, root_schema={"foo": "bar"}, settings=None)
+        ctx = data_schema.Context(None, root_schema=data_schema._Schema(None, True, {"foo": "bar"}), settings=None)
         self.assertRaises(
             data_schema.SchemaError,
             data_schema.try_get_reference,