|
8 | 8 | parse_json_path, |
9 | 9 | ) |
10 | 10 | from qdrant_client.local.payload_value_extractor import value_by_key |
11 | | -from qdrant_client.local.payload_value_setter import set_value_by_key |
| 11 | +from qdrant_client.local.payload_value_setter import delete_value_by_key, set_value_by_key |
12 | 12 |
|
13 | 13 |
|
14 | 14 | def test_parse_json_path() -> None: |
@@ -557,3 +557,42 @@ def test_set_value_by_key() -> None: |
557 | 557 | set_value_by_key(payload, parse_json_path(key), new_value) |
558 | 558 | assert payload == {"a": {"c": [[]]}}, payload |
559 | 559 | # endregion |
| 560 | + |
| 561 | + |
| 562 | +def test_delete_value_by_key() -> None: |
| 563 | + def delete(payload: dict, key: str) -> dict: |
| 564 | + delete_value_by_key(payload, parse_json_path(key)) |
| 565 | + return payload |
| 566 | + |
| 567 | + # region top-level |
| 568 | + assert delete({"a": 1, "b": 2}, "a") == {"b": 2} |
| 569 | + assert delete({"a": {"c": 2}}, "nope") == {"a": {"c": 2}} |
| 570 | + # endregion |
| 571 | + |
| 572 | + # region nested keys, siblings preserved |
| 573 | + assert delete({"a": {"b": 1, "c": 2}, "top": 9}, "a.b") == {"a": {"c": 2}, "top": 9} |
| 574 | + assert delete({"a": {"b": {"c": 1, "d": 2}}}, "a.b.c") == {"a": {"b": {"d": 2}}} |
| 575 | + assert delete({"the": {"nested.key": 1, "b": 2}}, 'the."nested.key"') == {"the": {"b": 2}} |
| 576 | + # endregion |
| 577 | + |
| 578 | + # region arrays |
| 579 | + assert delete({"loc": [{"x": 1}, {"x": 2}]}, "loc[0].x") == {"loc": [{}, {"x": 2}]} |
| 580 | + assert delete({"loc": [{"x": 1, "y": 2}, {"x": 3}]}, "loc[].x") == {"loc": [{"y": 2}, {}]} |
| 581 | + assert delete({"loc": [[1, 2], [3, 4]]}, "loc[][]") == {"loc": [[], []]} |
| 582 | + # a terminal wildcard clears the array, a terminal index is a no-op |
| 583 | + assert delete({"loc": [1, 2, 3], "top": 9}, "loc[]") == {"loc": [], "top": 9} |
| 584 | + assert delete({"loc": [{"x": 1}, {"x": 2}]}, "loc[0]") == {"loc": [{"x": 1}, {"x": 2}]} |
| 585 | + assert delete({"loc": [1, 2, 3]}, "loc[1]") == {"loc": [1, 2, 3]} |
| 586 | + # arrays are not implicitly flattened, unlike in filters |
| 587 | + assert delete({"loc": [{"x": 1}]}, "loc.x") == {"loc": [{"x": 1}]} |
| 588 | + # endregion |
| 589 | + |
| 590 | + # region paths that do not resolve |
| 591 | + assert delete({"loc": [{"x": 1}]}, "loc[5].x") == {"loc": [{"x": 1}]} |
| 592 | + assert delete({"a": {"c": 2}}, "a.b") == {"a": {"c": 2}} |
| 593 | + assert delete({"a": {"c": 2}}, "nope.nested") == {"a": {"c": 2}} |
| 594 | + assert delete({"a": 5}, "a.b") == {"a": 5} |
| 595 | + assert delete({"loc": {"x": 1}}, "loc[]") == {"loc": {"x": 1}} |
| 596 | + # a dotted path never matches a literal key containing a dot |
| 597 | + assert delete({"a.b": 1, "a": {"b": 2}}, "a.b") == {"a.b": 1, "a": {}} |
| 598 | + # endregion |
0 commit comments