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
This commit is contained in:
Cyril Roelandt
2014-02-17 22:07:18 +01:00
parent b8a850c5b3
commit 3cced6085d

View File

@@ -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))