Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jsonpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
import copy
import functools
import json
from collections.abc import Mapping, MutableMapping, MutableSequence, Sequence
from collections.abc import (Iterator, Mapping, MutableMapping, MutableSequence,
Sequence)
from difflib import SequenceMatcher
from types import MappingProxyType

Expand Down Expand Up @@ -515,7 +516,7 @@ class JsonPatch(object):
})

def __init__(self, patch, pointer_cls=JsonPointer):
self.patch = patch
self.patch = list(patch) if isinstance(patch, Iterator) else patch
self.pointer_cls = pointer_cls

# Verify that the structure of the patch document
Expand Down
30 changes: 30 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@

class ApplyPatchTestCase(unittest.TestCase):

def test_operation_iterators_remain_reusable_after_validation(self):
operations = [
{'op': 'add', 'path': '/a', 'value': 1},
{'op': 'replace', 'path': '/a', 'value': 2},
]
for factory in (iter, lambda ops: (op for op in ops)):
with self.subTest(factory=factory):
patch = jsonpatch.JsonPatch(factory(operations))
self.assertEqual(list(patch), operations)
self.assertEqual(json.loads(patch.to_string()), operations)
self.assertEqual(patch.apply({}), {'a': 2})
self.assertEqual(patch.apply({}), {'a': 2})

def test_empty_operation_iterator_is_false(self):
patch = jsonpatch.JsonPatch(iter([]))
self.assertFalse(patch)
self.assertEqual(patch.to_string(), '[]')

def test_invalid_operation_iterator_is_rejected(self):
operations = iter([
{'op': 'add', 'path': '/a', 'value': 1},
{'op': 'not-an-operation', 'path': '/a'},
])
with self.assertRaises(jsonpatch.InvalidJsonPatch):
jsonpatch.JsonPatch(operations)

def test_list_operations_keep_the_original_container(self):
operations = [{'op': 'add', 'path': '/a', 'value': 1}]
self.assertIs(jsonpatch.JsonPatch(operations).patch, operations)

def test_diff_without_optimization_uses_basic_operations(self):
documents = [
({'old': {'value': 1}}, {'new': {'value': 1}}),
Expand Down
Loading