summaryrefslogtreecommitdiff
path: root/tests.py
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2017-09-03 10:52:53 +0200
committerStefan Kögl <stefan@skoegl.net>2017-09-03 10:52:53 +0200
commit717b0db91e479ce80251778e00905501f01c717a (patch)
treea3b6e33ad15202a8a357fe262d8a9315b70c28b9 /tests.py
parent4d9adf195be16d8ae99468359e0ac876479482c7 (diff)
parenta2cd6da7e4418c1012fbed76aefc983971fc4808 (diff)
downloadpython-json-patch-717b0db91e479ce80251778e00905501f01c717a.tar.gz
Merge branch 'list_tests@v1.13' into list_tests
https://github.com/selurvedu/python-json-patch
Diffstat (limited to 'tests.py')
-rwxr-xr-xtests.py55
1 files changed, 48 insertions, 7 deletions
diff --git a/tests.py b/tests.py
index 51d9517..9d850fd 100755
--- a/tests.py
+++ b/tests.py
@@ -407,13 +407,18 @@ class OptimizationTests(unittest.TestCase):
self.assertEqual(res, dst)
def test_use_move_instead_of_add_remove(self):
- src = {'foo': [1, 2, 3]}
- dst = {'foo': [3, 1, 2]}
- patch = list(jsonpatch.make_patch(src, dst))
- self.assertEqual(len(patch), 1)
- self.assertEqual(patch[0]['op'], 'move')
- res = jsonpatch.apply_patch(src, patch)
- self.assertEqual(res, dst)
+ def fn(_src, _dst):
+ patch = list(jsonpatch.make_patch(_src, _dst))
+ # Check if there are only 'move' operations
+ for p in patch:
+ self.assertEqual(p['op'], 'move')
+ res = jsonpatch.apply_patch(_src, patch)
+ self.assertEqual(res, _dst)
+
+ fn({'foo': [1, 2, 3]}, {'foo': [3, 1, 2]})
+ fn({'foo': [1, 2, 3]}, {'foo': [3, 2, 1]})
+ fn([1, 2, 3], [3, 1, 2])
+ fn([1, 2, 3], [3, 2, 1])
def test_success_if_replace_inside_dict(self):
src = [{'a': 1, 'foo': {'b': 2, 'd': 5}}]
@@ -464,6 +469,41 @@ class OptimizationTests(unittest.TestCase):
self.assertEqual(patch.patch, exp)
+class ListTests(unittest.TestCase):
+
+ def test_fail_prone_list_1(self):
+ """ Test making and applying a patch of the root is a list """
+ src = ['a', 'r', 'b']
+ dst = ['b', 'o']
+ patch = jsonpatch.make_patch(src, dst)
+ res = patch.apply(src)
+ self.assertEqual(res, dst)
+
+ def test_fail_prone_list_2(self):
+ """ Test making and applying a patch of the root is a list """
+ src = ['a', 'r', 'b', 'x', 'm', 'n']
+ dst = ['b', 'o', 'm', 'n']
+ patch = jsonpatch.make_patch(src, dst)
+ res = patch.apply(src)
+ self.assertEqual(res, dst)
+
+ def test_fail_prone_list_3(self):
+ """ Test making and applying a patch of the root is a list """
+ src = ['boo1', 'bar', 'foo1', 'qux']
+ dst = ['qux', 'bar']
+ patch = jsonpatch.make_patch(src, dst)
+ res = patch.apply(src)
+ self.assertEqual(res, dst)
+
+ def test_fail_prone_list_4(self):
+ """ Test making and applying a patch of the root is a list """
+ src = ['bar1', 59, 'foo1', 'foo']
+ dst = ['foo', 'bar', 'foo1']
+ patch = jsonpatch.make_patch(src, dst)
+ res = patch.apply(src)
+ self.assertEqual(res, dst)
+
+
class InvalidInputTests(unittest.TestCase):
def test_missing_op(self):
@@ -528,6 +568,7 @@ if __name__ == '__main__':
suite.addTest(unittest.makeSuite(ApplyPatchTestCase))
suite.addTest(unittest.makeSuite(EqualityTestCase))
suite.addTest(unittest.makeSuite(MakePatchTestCase))
+ suite.addTest(unittest.makeSuite(ListTests))
suite.addTest(unittest.makeSuite(InvalidInputTests))
suite.addTest(unittest.makeSuite(ConflictTests))
suite.addTest(unittest.makeSuite(OptimizationTests))