|
1 | 1 | import copy |
2 | 2 |
|
3 | 3 | import pytest |
4 | | - |
| 4 | +from typing import Callable |
5 | 5 | from jsonpath_ng.ext.parser import parse as ext_parse |
6 | 6 | from jsonpath_ng.jsonpath import DatumInContext, Fields, Root, This |
7 | 7 | from jsonpath_ng.lexer import JsonPathLexerError |
8 | 8 | from jsonpath_ng.parser import parse as base_parse |
| 9 | +from jsonpath_ng import JSONPath |
9 | 10 |
|
10 | 11 | from .helpers import assert_full_path_equality, assert_value_equality |
11 | 12 |
|
@@ -166,12 +167,26 @@ def test_datumincontext_in_context_nested(): |
166 | 167 | update_test_cases, |
167 | 168 | ) |
168 | 169 | @parsers |
169 | | -def test_update(parse, expression, data, update_value, expected_value): |
| 170 | +def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_value, expected_value): |
170 | 171 | data_copy = copy.deepcopy(data) |
171 | 172 | update_value_copy = copy.deepcopy(update_value) |
172 | 173 | result = parse(expression).update(data_copy, update_value_copy) |
173 | 174 | assert result == expected_value |
174 | 175 |
|
| 176 | + # inplace update testing |
| 177 | + data_copy2 = copy.deepcopy(data) |
| 178 | + update_value_copy2 = copy.deepcopy(update_value) |
| 179 | + datums = parse(expression).find(data_copy2) |
| 180 | + batch_update = isinstance(update_value, list) and len(datums) == len(update_value) |
| 181 | + for i, datum in enumerate(datums): |
| 182 | + if batch_update: |
| 183 | + datum.value = update_value_copy2[i] |
| 184 | + else: |
| 185 | + datum.value = update_value_copy2 |
| 186 | + if isinstance(datum.full_path, (Root, This)): # when the type of `data` is str, int, float etc. |
| 187 | + data_copy2 = datum.value |
| 188 | + assert data_copy2 == expected_value |
| 189 | + |
175 | 190 |
|
176 | 191 | find_test_cases = ( |
177 | 192 | # |
|
0 commit comments