diff options
author | Christian Lyder Jacobsen <christian@yocuda.com> | 2020-03-06 11:41:38 +0100 |
---|---|---|
committer | Christian Lyder Jacobsen <christian@yocuda.com> | 2020-03-06 12:07:24 +0100 |
commit | 0167d345ee9d7ef0f74b947ec3a7ea94def178be (patch) | |
tree | 34b4358b9b96ac73ca06c5d741bd7c344cb164a8 /tests.py | |
parent | e99d178396f69f8891a62e21434c2783b76146b2 (diff) | |
download | python-json-patch-0167d345ee9d7ef0f74b947ec3a7ea94def178be.tar.gz |
Subclassing can override json dumper and loader
Additionally:
* from_string gets a loads parameter
* to_string gets a dumps_parameter
* documentation added
* added more tests
Diffstat (limited to 'tests.py')
-rwxr-xr-x | tests.py | 59 |
1 files changed, 51 insertions, 8 deletions
@@ -268,6 +268,34 @@ class EqualityTestCase(unittest.TestCase): self.assertEqual(json.dumps(patch_obj), patch.to_string()) +def custom_types_dumps(obj): + def default(obj): + if isinstance(obj, decimal.Decimal): + return {'__decimal__': str(obj)} + raise TypeError('Unknown type') + + return json.dumps(obj, default=default) + + +def custom_types_loads(obj): + def as_decimal(dct): + if '__decimal__' in dct: + return decimal.Decimal(dct['__decimal__']) + return dct + + return json.loads(obj, object_hook=as_decimal) + + +class CustomTypesJsonPatch(jsonpatch.JsonPatch): + @staticmethod + def json_dumper(obj): + return custom_types_dumps(obj) + + @staticmethod + def json_loader(obj): + return custom_types_loads(obj) + + class MakePatchTestCase(unittest.TestCase): def test_apply_patch_to_copy(self): @@ -446,18 +474,33 @@ 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 test_custom_types_diff(self): + old = {'value': decimal.Decimal('1.0')} + new = {'value': decimal.Decimal('1.00')} + generated_patch = jsonpatch.JsonPatch.from_diff( + old, new, dumps=custom_types_dumps) + str_patch = generated_patch.to_string(dumps=custom_types_dumps) + loaded_patch = jsonpatch.JsonPatch.from_string( + str_patch, loads=custom_types_loads) + self.assertEqual(generated_patch, loaded_patch) + new_from_patch = jsonpatch.apply_patch(old, generated_patch) + self.assertEqual(new, new_from_patch) - def dumps(obj): - return json.dumps(obj, default=default) + def test_custom_types_subclass(self): + old = {'value': decimal.Decimal('1.0')} + new = {'value': decimal.Decimal('1.00')} + generated_patch = CustomTypesJsonPatch.from_diff(old, new) + str_patch = generated_patch.to_string() + loaded_patch = CustomTypesJsonPatch.from_string(str_patch) + self.assertEqual(generated_patch, loaded_patch) + new_from_patch = jsonpatch.apply_patch(old, loaded_patch) + self.assertEqual(new, new_from_patch) + def test_custom_types_subclass_load(self): old = {'value': decimal.Decimal('1.0')} new = {'value': decimal.Decimal('1.00')} - patch = jsonpatch.JsonPatch.from_diff(old, new, dumps=dumps) + patch = CustomTypesJsonPatch.from_string( + '[{"op": "replace", "path": "/value", "value": {"__decimal__": "1.00"}}]') new_from_patch = jsonpatch.apply_patch(old, patch) self.assertEqual(new, new_from_patch) |