From 1268e09ffaead08f22184b63b3ad34bb41ab8bab Mon Sep 17 00:00:00 2001 From: Artyom Nikitin Date: Mon, 23 Nov 2020 23:53:48 +0300 Subject: test: custom operations --- tests.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests.py b/tests.py index b5b7b9a..0788d48 100755 --- a/tests.py +++ b/tests.py @@ -10,6 +10,11 @@ import unittest import jsonpatch import jsonpointer import sys +try: + from types import MappingProxyType +except ImportError: + # Python < 3.3 + MappingProxyType = dict class ApplyPatchTestCase(unittest.TestCase): @@ -938,6 +943,26 @@ class CustomJsonPointerTests(unittest.TestCase): self.assertEqual(res, {'foo': {'bar': {'baz': 'qux'}}}) +class CustomOperationTests(unittest.TestCase): + + def test_custom_operation(self): + + class IdentityOperation(jsonpatch.PatchOperation): + def apply(self, obj): + return obj + + class JsonPatch(jsonpatch.JsonPatch): + operations = MappingProxyType( + identity=IdentityOperation, + **jsonpatch.JsonPatch.operations + ) + + patch = JsonPatch([{'op': 'identity', 'path': '/'}]) + self.assertIn('identity', patch.operations) + res = patch.apply({}) + self.assertEqual(res, {}) + + if __name__ == '__main__': modules = ['jsonpatch'] @@ -956,6 +981,7 @@ if __name__ == '__main__': suite.addTest(unittest.makeSuite(JsonPatchCreationTest)) suite.addTest(unittest.makeSuite(UtilityMethodTests)) suite.addTest(unittest.makeSuite(CustomJsonPointerTests)) + suite.addTest(unittest.makeSuite(CustomOperationTests)) return suite -- cgit v1.2.1