summaryrefslogtreecommitdiff
path: root/tests.py
diff options
context:
space:
mode:
authorkostya <suffering.death@gmail.com>2017-03-08 13:18:16 +0200
committerkostya <suffering.death@gmail.com>2017-03-08 13:18:16 +0200
commitd1f317a2b796175154ca5e21c7b32eab1c5800fe (patch)
tree03d8772c9a8ca97bf885912e23230e0b30dcfcee /tests.py
parent27f1f987e0c9d101a8dc01cc322d5f31ca89d074 (diff)
downloadpython-json-patch-d1f317a2b796175154ca5e21c7b32eab1c5800fe.tar.gz
Fix: optimization bugs #55, #54, add more tests
Diffstat (limited to 'tests.py')
-rwxr-xr-xtests.py110
1 files changed, 87 insertions, 23 deletions
diff --git a/tests.py b/tests.py
index 5acf24b..72611a3 100755
--- a/tests.py
+++ b/tests.py
@@ -13,6 +13,34 @@ import sys
class ApplyPatchTestCase(unittest.TestCase):
+ def test_js_file(self):
+ with open('./tests.js', 'r') as f:
+ tests = json.load(f)
+ for test in tests:
+ try:
+ if 'expected' not in test:
+ continue
+ result = jsonpatch.apply_patch(test['doc'], test['patch'])
+ self.assertEqual(result, test['expected'])
+ except Exception:
+ if test.get('error'):
+ continue
+ else:
+ raise
+
+ def test_success_if_replaced_dict(self):
+ src = [{'a': 1}, {'b': 2}]
+ dst = [{'a': 1, 'b': 2}]
+ patch = jsonpatch.make_patch(src, dst)
+ self.assertEqual(patch.apply(src), dst)
+
+ def test_success_if_raise_no_error(self):
+ src = [{}]
+ dst = [{'key': ''}]
+ patch = jsonpatch.make_patch(src, dst)
+ patch.apply(src)
+ self.assertTrue(True)
+
def test_apply_patch_from_string(self):
obj = {'foo': 'bar'}
patch = '[{"op": "add", "path": "/baz", "value": "qux"}]'
@@ -308,6 +336,40 @@ class MakePatchTestCase(unittest.TestCase):
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
+
+ def test_escape(self):
+ src = {"x/y": 1}
+ dst = {"x/y": 2}
+ patch = jsonpatch.make_patch(src, dst)
+ self.assertEqual([{"path": "/x~1y", "value": 2, "op": "replace"}], patch.patch)
+ res = patch.apply(src)
+ self.assertEqual(res, dst)
+
+ def test_root_list(self):
+ """ Test making and applying a patch of the root is a list """
+ src = [{'foo': 'bar', 'boo': 'qux'}]
+ dst = [{'baz': 'qux', 'foo': 'boo'}]
+ patch = jsonpatch.make_patch(src, dst)
+ res = patch.apply(src)
+ self.assertEqual(res, dst)
+
+ def test_make_patch_unicode(self):
+ """ Test if unicode keys and values are handled correctly """
+ src = {}
+ dst = {'\xee': '\xee'}
+ patch = jsonpatch.make_patch(src, dst)
+ res = patch.apply(src)
+ self.assertEqual(res, dst)
+
+ def test_issue40(self):
+ """ Tests an issue in _split_by_common_seq reported in #40 """
+
+ src = [8, 7, 2, 1, 0, 9, 4, 3, 5, 6]
+ dest = [7, 2, 1, 0, 9, 4, 3, 6, 5, 8]
+ patch = jsonpatch.make_patch(src, dest)
+
+
+class OptimizationTests(unittest.TestCase):
def test_use_replace_instead_of_remove_add(self):
src = {'foo': [1, 2, 3]}
dst = {'foo': [3, 2, 3]}
@@ -344,41 +406,42 @@ class MakePatchTestCase(unittest.TestCase):
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
- def test_escape(self):
- src = {"x/y": 1}
- dst = {"x/y": 2}
+ def test_success_if_replace_inside_dict(self):
+ src = [{'a': 1, 'foo': {'b': 2, 'd': 5}}]
+ dst = [{'a': 1, 'foo': {'b': 3, 'd': 6}}]
patch = jsonpatch.make_patch(src, dst)
- self.assertEqual([{"path": "/x~1y", "value": 2, "op": "replace"}], patch.patch)
- res = patch.apply(src)
- self.assertEqual(res, dst)
+ self.assertEqual(patch.apply(src), dst)
- def test_root_list(self):
- """ Test making and applying a patch of the root is a list """
- src = [{'foo': 'bar', 'boo': 'qux'}]
- dst = [{'baz': 'qux', 'foo': 'boo'}]
+ def test_success_if_replace_single_value(self):
+ src = [{'a': 1, 'b': 2, 'd': 5}]
+ dst = [{'a': 1, 'c': 3, 'd': 5}]
patch = jsonpatch.make_patch(src, dst)
- res = patch.apply(src)
- self.assertEqual(res, dst)
+ self.assertEqual(patch.apply(src), dst)
- def test_make_patch_unicode(self):
- """ Test if unicode keys and values are handled correctly """
- src = {}
- dst = {'\xee': '\xee'}
+ def test_success_if_replaced_by_object(self):
+ src = [{'a': 1, 'b': 2, 'd': 5}]
+ dst = [{'d': 6}]
patch = jsonpatch.make_patch(src, dst)
- res = patch.apply(src)
- self.assertEqual(res, dst)
+ self.assertEqual(patch.apply(src), dst)
- def test_issue40(self):
- """ Tests an issue in _split_by_common_seq reported in #40 """
+ def test_success_if_correct_patch_appied(self):
+ src = [{'a': 1}, {'b': 2}]
+ dst = [{'a': 1, 'b': 2}]
+ patch = jsonpatch.make_patch(src, dst)
+ self.assertEqual(patch.apply(src), dst)
- src = [8, 7, 2, 1, 0, 9, 4, 3, 5, 6]
- dest = [7, 2, 1, 0, 9, 4, 3, 6, 5, 8]
- patch = jsonpatch.make_patch(src, dest)
+ def test_success_if_correct_expected_patch_appied(self):
+ src = [{"a": 1, "b": 2}]
+ dst = [{"b": 2, "c": 2}]
+ exp = [{u'path': u'/0', u'value': {u'c': 2, u'b': 2}, u'op': u'replace'}]
+ patch = jsonpatch.make_patch(src, dst)
+ self.assertEqual(patch.patch, exp)
def test_minimal_patch(self):
""" Test whether a minimal patch is created, see #36 """
src = [{"foo": 1, "bar": 2}]
dst = [{"foo": 2, "bar": 2}]
+
patch = jsonpatch.make_patch(src, dst)
exp = [
@@ -458,6 +521,7 @@ if __name__ == '__main__':
suite.addTest(unittest.makeSuite(MakePatchTestCase))
suite.addTest(unittest.makeSuite(InvalidInputTests))
suite.addTest(unittest.makeSuite(ConflictTests))
+ suite.addTest(unittest.makeSuite(OptimizationTests))
return suite