From 3cced6085dd03013e4562639f7772430ab632fbf Mon Sep 17 00:00:00 2001 From: Cyril Roelandt Date: Mon, 17 Feb 2014 22:07:18 +0100 Subject: [PATCH] Python 3: Fix JsonPatch-related issues In Python 3, the generated JSON patches have a different representation than those generated with Python 2. For instance, the patch generated in test_patch_should_replace_missing_core_properties() is: '[{"op": "replace", "value": "red", "path": "/color"}]' in Python 2, but: '[{"path": "/color", "value": "red", "op": "replace"}]' in Python 3. Therefore, the comparison fails, and the test fail. We fix the tests by comparing JsonPatch objects rather than strings. Closes-Bug: #1281374 Change-Id: Id435e87664b2d2aa0ba69f2df1c1fad431651963 --- tests/v2/test_schemas.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/v2/test_schemas.py b/tests/v2/test_schemas.py index 7622d49e..9fc3b193 100644 --- a/tests/v2/test_schemas.py +++ b/tests/v2/test_schemas.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +from jsonpatch import JsonPatch import testtools import warlock @@ -53,6 +54,11 @@ _SCHEMA = schemas.Schema({ }) +def compare_json_patches(a, b): + """Return 0 if a and b describe the same JSON patch.""" + return JsonPatch.from_string(a) == JsonPatch.from_string(b) + + class TestSchemaProperty(testtools.TestCase): def test_property_minimum(self): prop = schemas.SchemaProperty('size') @@ -111,7 +117,7 @@ class TestSchemaBasedModel(testtools.TestCase): patch = original.patch expected = '[{"path": "/color", "value": "red", "op": "replace"}]' - self.assertEqual(patch, expected) + self.assertTrue(compare_json_patches(patch, expected)) def test_patch_should_add_extra_properties(self): obj = { @@ -123,7 +129,7 @@ class TestSchemaBasedModel(testtools.TestCase): patch = original.patch expected = '[{"path": "/weight", "value": "10", "op": "add"}]' - self.assertEqual(patch, expected) + self.assertTrue(compare_json_patches(patch, expected)) def test_patch_should_replace_extra_properties(self): obj = { @@ -136,7 +142,7 @@ class TestSchemaBasedModel(testtools.TestCase): patch = original.patch expected = '[{"path": "/weight", "value": "22", "op": "replace"}]' - self.assertEqual(patch, expected) + self.assertTrue(compare_json_patches(patch, expected)) def test_patch_should_remove_extra_properties(self): obj = { @@ -149,7 +155,7 @@ class TestSchemaBasedModel(testtools.TestCase): patch = original.patch expected = '[{"path": "/weight", "op": "remove"}]' - self.assertEqual(patch, expected) + self.assertTrue(compare_json_patches(patch, expected)) def test_patch_should_remove_core_properties(self): obj = { @@ -162,4 +168,4 @@ class TestSchemaBasedModel(testtools.TestCase): patch = original.patch expected = '[{"path": "/color", "op": "remove"}]' - self.assertEqual(patch, expected) + self.assertTrue(compare_json_patches(patch, expected))