Skip to content

Commit bb4ea7b

Browse files
author
Artyom Nikitin
committed
test: custo json pointer
1 parent ab775d1 commit bb4ea7b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,59 @@ def test_create_with_pointer(self):
671671
self.assertEqual(result, expected)
672672

673673

674+
class CustomJsonPointerTests(unittest.TestCase):
675+
676+
class CustomJsonPointer(jsonpointer.JsonPointer):
677+
pass
678+
679+
def test_apply_patch_from_string(self):
680+
obj = {'foo': 'bar'}
681+
patch = '[{"op": "add", "path": "/baz", "value": "qux"}]'
682+
res = jsonpatch.apply_patch(
683+
obj, patch,
684+
pointer_cls=self.CustomJsonPointer,
685+
)
686+
self.assertTrue(obj is not res)
687+
self.assertTrue('baz' in res)
688+
self.assertEqual(res['baz'], 'qux')
689+
690+
def test_apply_patch_from_object(self):
691+
obj = {'foo': 'bar'}
692+
res = jsonpatch.apply_patch(
693+
obj, [{'op': 'add', 'path': '/baz', 'value': 'qux'}],
694+
pointer_cls=self.CustomJsonPointer,
695+
)
696+
self.assertTrue(obj is not res)
697+
698+
def test_make_patch(self):
699+
src = {'foo': 'bar', 'boo': 'qux'}
700+
dst = {'baz': 'qux', 'foo': 'boo'}
701+
patch = jsonpatch.make_patch(
702+
src, dst, pointer_cls=self.CustomJsonPointer,
703+
)
704+
res = patch.apply(src)
705+
self.assertTrue(src is not res)
706+
self.assertEqual(patch.pointer_cls, self.CustomJsonPointer)
707+
self.assertTrue(patch._ops)
708+
for op in patch._ops:
709+
self.assertEqual(op.pointer_cls, self.CustomJsonPointer)
710+
711+
def test_operations(self):
712+
patch = jsonpatch.JsonPatch([
713+
{'op': 'add', 'path': '/foo', 'value': [1, 2, 3]},
714+
{'op': 'move', 'path': '/baz', 'from': '/foo'},
715+
{'op': 'add', 'path': '/baz', 'value': [1, 2, 3]},
716+
{'op': 'remove', 'path': '/baz/1'},
717+
{'op': 'test', 'path': '/baz', 'value': [1, 3]},
718+
{'op': 'replace', 'path': '/baz/0', 'value': 42},
719+
{'op': 'remove', 'path': '/baz/1'},
720+
], pointer_cls=self.CustomJsonPointer)
721+
self.assertEqual(patch.apply({}), {'baz': [42]})
722+
self.assertEqual(patch.pointer_cls, self.CustomJsonPointer)
723+
self.assertTrue(patch._ops)
724+
for op in patch._ops:
725+
self.assertEqual(op.pointer_cls, self.CustomJsonPointer)
726+
674727

675728
if __name__ == '__main__':
676729
modules = ['jsonpatch']
@@ -687,6 +740,7 @@ def get_suite():
687740
suite.addTest(unittest.makeSuite(ConflictTests))
688741
suite.addTest(unittest.makeSuite(OptimizationTests))
689742
suite.addTest(unittest.makeSuite(JsonPointerTests))
743+
suite.addTest(unittest.makeSuite(CustomJsonPointerTests))
690744
return suite
691745

692746

0 commit comments

Comments
 (0)