diff options
author | Christian Lyder Jacobsen <christian@yocuda.com> | 2020-01-31 11:58:59 +0100 |
---|---|---|
committer | Christian Lyder Jacobsen <christian@yocuda.com> | 2020-01-31 12:05:32 +0100 |
commit | e99d178396f69f8891a62e21434c2783b76146b2 (patch) | |
tree | 86bbacebbc42cd0a4c93fa1541443dcce688d670 /tests.py | |
parent | 91f61241adc9d104e1811eeaf4d9dc6518d6786e (diff) | |
download | python-json-patch-e99d178396f69f8891a62e21434c2783b76146b2.tar.gz |
Make it possible for from_diff to support custom types (issue #107)
Diffstat (limited to 'tests.py')
-rwxr-xr-x | tests.py | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -4,6 +4,7 @@ from __future__ import unicode_literals import json +import decimal import doctest import unittest import jsonpatch @@ -445,6 +446,20 @@ class MakePatchTestCase(unittest.TestCase): self.assertEqual(res, dst) self.assertIsInstance(res['A'], float) + def test_custom_types(self): + def default(obj): + if isinstance(obj, decimal.Decimal): + return str(obj) + raise TypeError('Unknown type') + + def dumps(obj): + return json.dumps(obj, default=default) + + old = {'value': decimal.Decimal('1.0')} + new = {'value': decimal.Decimal('1.00')} + patch = jsonpatch.JsonPatch.from_diff(old, new, dumps=dumps) + new_from_patch = jsonpatch.apply_patch(old, patch) + self.assertEqual(new, new_from_patch) class OptimizationTests(unittest.TestCase): |