|
| 1 | +"""JSON Patch test cases.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import re |
| 5 | +from collections.abc import Mapping |
| 6 | +from io import StringIO |
| 7 | +from typing import Any |
| 8 | +from typing import Iterator |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from jsonpath import JSONPatch |
| 13 | +from jsonpath import patch |
| 14 | +from jsonpath.exceptions import JSONPatchError |
| 15 | + |
| 16 | + |
| 17 | +class MockMapping(Mapping[str, str]): |
| 18 | + def __getitem__(self, __key: Any) -> Any: |
| 19 | + return "foo" |
| 20 | + |
| 21 | + def __iter__(self) -> Iterator[str]: |
| 22 | + return iter(["foo"]) |
| 23 | + |
| 24 | + def __len__(self) -> int: |
| 25 | + return 1 |
| 26 | + |
| 27 | + |
| 28 | +def test_add_to_immutable_mapping() -> None: |
| 29 | + patch = JSONPatch().add("/foo/bar", "baz") |
| 30 | + with pytest.raises( |
| 31 | + JSONPatchError, match=re.escape("unexpected operation on 'MockMapping' (add:0)") |
| 32 | + ): |
| 33 | + patch.apply({"foo": MockMapping()}) |
| 34 | + |
| 35 | + |
| 36 | +def test_remove_root() -> None: |
| 37 | + patch = JSONPatch().remove("") |
| 38 | + with pytest.raises(JSONPatchError, match=re.escape("can't remove root (remove:0)")): |
| 39 | + patch.apply({"foo": "bar"}) |
| 40 | + |
| 41 | + |
| 42 | +def test_remove_nonexistent_value() -> None: |
| 43 | + patch = JSONPatch().remove("/baz") |
| 44 | + with pytest.raises( |
| 45 | + JSONPatchError, match=re.escape("can't remove nonexistent property (remove:0)") |
| 46 | + ): |
| 47 | + patch.apply({"foo": "bar"}) |
| 48 | + |
| 49 | + |
| 50 | +def test_remove_array_end() -> None: |
| 51 | + patch = JSONPatch().remove("/foo/-") |
| 52 | + with pytest.raises( |
| 53 | + JSONPatchError, match=re.escape("can't remove nonexistent item (remove:0)") |
| 54 | + ): |
| 55 | + patch.apply({"foo": [1, 2, 3]}) |
| 56 | + |
| 57 | + |
| 58 | +def test_remove_from_immutable_mapping() -> None: |
| 59 | + patch = JSONPatch().remove("/bar/foo") |
| 60 | + with pytest.raises( |
| 61 | + JSONPatchError, |
| 62 | + match=re.escape("unexpected operation on 'MockMapping' (remove:0)"), |
| 63 | + ): |
| 64 | + patch.apply({"bar": MockMapping()}) |
| 65 | + |
| 66 | + |
| 67 | +def test_replace_root() -> None: |
| 68 | + assert patch.apply( |
| 69 | + [{"op": "replace", "path": "", "value": [1, 2, 3]}], {"foo": "bar"} |
| 70 | + ) == [1, 2, 3] |
| 71 | + |
| 72 | + |
| 73 | +def test_replace_a_nonexistent_item() -> None: |
| 74 | + with pytest.raises( |
| 75 | + JSONPatchError, match=re.escape("can't replace nonexistent item (replace:0)") |
| 76 | + ): |
| 77 | + patch.apply( |
| 78 | + [{"op": "replace", "path": "/foo/99", "value": 5}], {"foo": [1, 2, 3]} |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_replace_a_nonexistent_value() -> None: |
| 83 | + with pytest.raises( |
| 84 | + JSONPatchError, |
| 85 | + match=re.escape("can't replace nonexistent property (replace:0)"), |
| 86 | + ): |
| 87 | + patch.apply( |
| 88 | + [{"op": "replace", "path": "/foo/bar", "value": 5}], {"foo": {"baz": 10}} |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def test_replace_immutable_mapping() -> None: |
| 93 | + with pytest.raises( |
| 94 | + JSONPatchError, |
| 95 | + match=re.escape("unexpected operation on 'MockMapping' (replace:0)"), |
| 96 | + ): |
| 97 | + patch.apply( |
| 98 | + [{"op": "replace", "path": "/bar/foo", "value": "baz"}], |
| 99 | + {"bar": MockMapping()}, |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def test_move_to_child() -> None: |
| 104 | + with pytest.raises( |
| 105 | + JSONPatchError, |
| 106 | + match=re.escape("can't move object to one of its own children (move:0)"), |
| 107 | + ): |
| 108 | + patch.apply( |
| 109 | + [{"op": "move", "from": "/foo/bar", "path": "/foo/bar/baz"}], |
| 110 | + {"foo": {"bar": {"baz": [1, 2, 3]}}}, |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +def test_move_nonexistent_value() -> None: |
| 115 | + with pytest.raises( |
| 116 | + JSONPatchError, match=re.escape("source object does not exist (move:0)") |
| 117 | + ): |
| 118 | + JSONPatch().move(from_="/foo/bar", path="/bar").apply({"foo": {"baz": 1}}) |
| 119 | + |
| 120 | + |
| 121 | +def test_move_to_root() -> None: |
| 122 | + patch = JSONPatch().move(from_="/foo", path="") |
| 123 | + assert patch.apply({"foo": {"bar": "baz"}}) == {"bar": "baz"} |
| 124 | + |
| 125 | + |
| 126 | +def test_move_to_immutable_mapping() -> None: |
| 127 | + patch = JSONPatch().move(from_="/foo/bar", path="/baz/bar") |
| 128 | + with pytest.raises( |
| 129 | + JSONPatchError, |
| 130 | + match=re.escape("unexpected operation on 'MockMapping' (move:0)"), |
| 131 | + ): |
| 132 | + patch.apply({"foo": {"bar": "hello"}, "baz": MockMapping()}) |
| 133 | + |
| 134 | + |
| 135 | +def test_copy_nonexistent_value() -> None: |
| 136 | + with pytest.raises( |
| 137 | + JSONPatchError, match=re.escape("source object does not exist (copy:0)") |
| 138 | + ): |
| 139 | + JSONPatch().copy(from_="/foo/bar", path="/bar").apply({"foo": {"baz": "hello"}}) |
| 140 | + |
| 141 | + |
| 142 | +def test_copy_to_root() -> None: |
| 143 | + patch = JSONPatch().copy(from_="/foo/bar", path="") |
| 144 | + assert patch.apply({"foo": {"bar": [1, 2, 3]}}) == [1, 2, 3] |
| 145 | + |
| 146 | + |
| 147 | +def test_copy_to_immutable_mapping() -> None: |
| 148 | + with pytest.raises( |
| 149 | + JSONPatchError, |
| 150 | + match=re.escape("unexpected operation on 'MockMapping' (copy:0)"), |
| 151 | + ): |
| 152 | + JSONPatch().copy(from_="/foo/bar", path="/baz/bar").apply( |
| 153 | + {"foo": {"bar": [1, 2, 3]}, "baz": MockMapping()} |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +def test_patch_from_file_like() -> None: |
| 158 | + patch_doc = StringIO( |
| 159 | + json.dumps( |
| 160 | + [ |
| 161 | + {"op": "add", "path": "", "value": {"foo": {}}}, |
| 162 | + {"op": "add", "path": "/foo", "value": {"bar": []}}, |
| 163 | + {"op": "add", "path": "/foo/bar/-", "value": 1}, |
| 164 | + ] |
| 165 | + ) |
| 166 | + ) |
| 167 | + |
| 168 | + patch = JSONPatch(patch_doc) |
| 169 | + assert patch.apply({}) == {"foo": {"bar": [1]}} |
| 170 | + |
| 171 | + |
| 172 | +def test_patch_from_string() -> None: |
| 173 | + patch_doc = json.dumps( |
| 174 | + [ |
| 175 | + {"op": "add", "path": "", "value": {"foo": {}}}, |
| 176 | + {"op": "add", "path": "/foo", "value": {"bar": []}}, |
| 177 | + {"op": "add", "path": "/foo/bar/-", "value": 1}, |
| 178 | + ] |
| 179 | + ) |
| 180 | + |
| 181 | + patch = JSONPatch(patch_doc) |
| 182 | + assert patch.apply({}) == {"foo": {"bar": [1]}} |
| 183 | + |
| 184 | + |
| 185 | +def test_unexpected_patch_ops() -> None: |
| 186 | + with pytest.raises( |
| 187 | + JSONPatchError, |
| 188 | + match=re.escape("expected a sequence of patch operations, found 'MockMapping'"), |
| 189 | + ): |
| 190 | + JSONPatch(MockMapping()) # type: ignore |
| 191 | + |
| 192 | + |
| 193 | +def test_construct_missing_op() -> None: |
| 194 | + with pytest.raises(JSONPatchError, match=re.escape("missing 'op' member at op 0")): |
| 195 | + JSONPatch([{}]) |
| 196 | + |
| 197 | + |
| 198 | +def test_construct_unknown_op() -> None: |
| 199 | + msg = ( |
| 200 | + "expected 'op' to be one of 'add', 'remove', 'replace', " |
| 201 | + "'move', 'copy' or 'test' (foo:0)" |
| 202 | + ) |
| 203 | + with pytest.raises(JSONPatchError, match=re.escape(msg)): |
| 204 | + JSONPatch([{"op": "foo"}]) |
| 205 | + |
| 206 | + |
| 207 | +def test_construct_missing_pointer() -> None: |
| 208 | + msg = "missing property 'path' (add:0)" |
| 209 | + with pytest.raises(JSONPatchError, match=re.escape(msg)): |
| 210 | + JSONPatch([{"op": "add", "value": "foo"}]) |
| 211 | + |
| 212 | + |
| 213 | +def test_construct_missing_value() -> None: |
| 214 | + msg = "missing property 'value' (add:0)" |
| 215 | + with pytest.raises(JSONPatchError, match=re.escape(msg)): |
| 216 | + JSONPatch([{"op": "add", "path": "/foo"}]) |
| 217 | + |
| 218 | + |
| 219 | +def test_construct_pointer_not_a_string() -> None: |
| 220 | + msg = "expected a JSON Pointer string for 'path', found 'int' (add:0)" |
| 221 | + with pytest.raises(JSONPatchError, match=re.escape(msg)): |
| 222 | + JSONPatch([{"op": "add", "path": 5, "value": "foo"}]) |
| 223 | + |
| 224 | + |
| 225 | +def test_apply_to_str() -> None: |
| 226 | + patch_doc = json.dumps( |
| 227 | + [ |
| 228 | + {"op": "add", "path": "", "value": {"foo": {}}}, |
| 229 | + {"op": "add", "path": "/foo", "value": {"bar": []}}, |
| 230 | + {"op": "add", "path": "/foo/bar/-", "value": 1}, |
| 231 | + ] |
| 232 | + ) |
| 233 | + |
| 234 | + data_doc = json.dumps({}) |
| 235 | + assert patch.apply(patch_doc, data_doc) == {"foo": {"bar": [1]}} |
| 236 | + |
| 237 | + |
| 238 | +def test_apply_to_file_like() -> None: |
| 239 | + patch_doc = StringIO( |
| 240 | + json.dumps( |
| 241 | + [ |
| 242 | + {"op": "add", "path": "", "value": {"foo": {}}}, |
| 243 | + {"op": "add", "path": "/foo", "value": {"bar": []}}, |
| 244 | + {"op": "add", "path": "/foo/bar/-", "value": 1}, |
| 245 | + ] |
| 246 | + ) |
| 247 | + ) |
| 248 | + |
| 249 | + data_doc = StringIO(json.dumps({})) |
| 250 | + assert patch.apply(patch_doc, data_doc) == {"foo": {"bar": [1]}} |
0 commit comments