Skip to content

Commit 1268e09

Browse files
author
Artyom Nikitin
committed
test: custom operations
1 parent b8083d7 commit 1268e09

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
import jsonpatch
1111
import jsonpointer
1212
import sys
13+
try:
14+
from types import MappingProxyType
15+
except ImportError:
16+
# Python < 3.3
17+
MappingProxyType = dict
1318

1419

1520
class ApplyPatchTestCase(unittest.TestCase):
@@ -938,6 +943,26 @@ def test_json_patch_with_prefix_pointer(self):
938943
self.assertEqual(res, {'foo': {'bar': {'baz': 'qux'}}})
939944

940945

946+
class CustomOperationTests(unittest.TestCase):
947+
948+
def test_custom_operation(self):
949+
950+
class IdentityOperation(jsonpatch.PatchOperation):
951+
def apply(self, obj):
952+
return obj
953+
954+
class JsonPatch(jsonpatch.JsonPatch):
955+
operations = MappingProxyType(
956+
identity=IdentityOperation,
957+
**jsonpatch.JsonPatch.operations
958+
)
959+
960+
patch = JsonPatch([{'op': 'identity', 'path': '/'}])
961+
self.assertIn('identity', patch.operations)
962+
res = patch.apply({})
963+
self.assertEqual(res, {})
964+
965+
941966
if __name__ == '__main__':
942967
modules = ['jsonpatch']
943968

@@ -956,6 +981,7 @@ def get_suite():
956981
suite.addTest(unittest.makeSuite(JsonPatchCreationTest))
957982
suite.addTest(unittest.makeSuite(UtilityMethodTests))
958983
suite.addTest(unittest.makeSuite(CustomJsonPointerTests))
984+
suite.addTest(unittest.makeSuite(CustomOperationTests))
959985
return suite
960986

961987

0 commit comments

Comments
 (0)