|
| 1 | +from jsonpath.jsonpath import JSONPath |
| 2 | + |
| 3 | + |
| 4 | +def test_update_value(): |
| 5 | + data = { |
| 6 | + "store": { |
| 7 | + "book": [ |
| 8 | + {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95} |
| 9 | + ] |
| 10 | + } |
| 11 | + } |
| 12 | + jp = JSONPath("$.store.book[0].price") |
| 13 | + result = jp.update(data, 10.0) |
| 14 | + assert result["store"]["book"][0]["price"] == 10.0 |
| 15 | + |
| 16 | + |
| 17 | +def test_update_function(): |
| 18 | + data = {"count": 1} |
| 19 | + jp = JSONPath("$.count") |
| 20 | + result = jp.update(data, lambda x: x + 1) |
| 21 | + assert result["count"] == 2 |
| 22 | + |
| 23 | + |
| 24 | +def test_update_root(): |
| 25 | + data = {"a": 1} |
| 26 | + jp = JSONPath("$") |
| 27 | + result = jp.update(data, {"b": 2}) |
| 28 | + assert result == {"b": 2} |
| 29 | + |
| 30 | + |
| 31 | +def test_update_list_index(): |
| 32 | + data = [1, 2, 3] |
| 33 | + jp = JSONPath("$[1]") |
| 34 | + result = jp.update(data, 5) |
| 35 | + assert result == [1, 5, 3] |
| 36 | + |
| 37 | + |
| 38 | +def test_update_multiple(): |
| 39 | + data = {"items": [{"value": 1}, {"value": 2}, {"value": 3}]} |
| 40 | + jp = JSONPath("$.items[*].value") |
| 41 | + result = jp.update(data, 0) |
| 42 | + for item in result["items"]: |
| 43 | + assert item["value"] == 0 |
| 44 | + |
| 45 | + |
| 46 | +def test_update_multiple_func(): |
| 47 | + data = {"items": [{"value": 1}, {"value": 2}, {"value": 3}]} |
| 48 | + jp = JSONPath("$.items[*].value") |
| 49 | + result = jp.update(data, lambda x: x * 2) |
| 50 | + assert result["items"][0]["value"] == 2 |
| 51 | + assert result["items"][1]["value"] == 4 |
| 52 | + assert result["items"][2]["value"] == 6 |
| 53 | + |
| 54 | + |
| 55 | +def test_update_with_filter(): |
| 56 | + data = {"books": [{"price": 10, "title": "A"}, {"price": 20, "title": "B"}, {"price": 30, "title": "C"}]} |
| 57 | + # Update price where price > 15 |
| 58 | + jp = JSONPath("$.books[?(@.price > 15)].price") |
| 59 | + result = jp.update(data, 0) |
| 60 | + assert result["books"][0]["price"] == 10 # Unchanged |
| 61 | + assert result["books"][1]["price"] == 0 # Updated |
| 62 | + assert result["books"][2]["price"] == 0 # Updated |
| 63 | + |
| 64 | + |
| 65 | +def test_update_slice(): |
| 66 | + data = [0, 1, 2, 3, 4] |
| 67 | + jp = JSONPath("$[1:4]") # Indices 1, 2, 3 |
| 68 | + result = jp.update(data, 9) |
| 69 | + assert result == [0, 9, 9, 9, 4] |
| 70 | + |
| 71 | + |
| 72 | +def test_update_special_keys(): |
| 73 | + data = {"complex.key": 1, "key with space": 2, "normal": 3} |
| 74 | + # Note: jsonpath-python might handle keys with dots using ['...'] syntax in path output |
| 75 | + jp = JSONPath("$['complex.key']") |
| 76 | + result = jp.update(data, 10) |
| 77 | + assert result["complex.key"] == 10 |
| 78 | + |
| 79 | + jp = JSONPath("$['key with space']") |
| 80 | + result = jp.update(data, 20) |
| 81 | + assert result["key with space"] == 20 |
| 82 | + |
| 83 | + |
| 84 | +def test_update_no_match(): |
| 85 | + data = {"a": 1} |
| 86 | + jp = JSONPath("$.b") |
| 87 | + result = jp.update(data, 2) |
| 88 | + assert result == {"a": 1} |
| 89 | + |
| 90 | + |
| 91 | +def test_update_nested_structure(): |
| 92 | + data = {"a": [{"b": [1, 2]}, {"b": [3, 4]}]} |
| 93 | + jp = JSONPath("$.a[*].b[1]") |
| 94 | + result = jp.update(data, 99) |
| 95 | + assert result["a"][0]["b"][1] == 99 |
| 96 | + assert result["a"][1]["b"][1] == 99 |
| 97 | + |
| 98 | + |
| 99 | +def test_update_recursive(): |
| 100 | + data = { |
| 101 | + "store": { |
| 102 | + "book": [{"category": "reference", "price": 8.95}, {"category": "fiction", "price": 12.99}], |
| 103 | + "bicycle": {"color": "red", "price": 19.95}, |
| 104 | + } |
| 105 | + } |
| 106 | + jp = JSONPath("$..price") |
| 107 | + result = jp.update(data, 10.0) |
| 108 | + assert result["store"]["book"][0]["price"] == 10.0 |
| 109 | + assert result["store"]["book"][1]["price"] == 10.0 |
| 110 | + assert result["store"]["bicycle"]["price"] == 10.0 |
| 111 | + |
| 112 | + |
| 113 | +def test_update_union(): |
| 114 | + data = {"a": 1, "b": 2, "c": 3} |
| 115 | + jp = JSONPath("$[a,b]") |
| 116 | + result = jp.update(data, 0) |
| 117 | + assert result["a"] == 0 |
| 118 | + assert result["b"] == 0 |
| 119 | + assert result["c"] == 3 |
| 120 | + |
| 121 | + |
| 122 | +def test_update_quote_keys(): |
| 123 | + data = {'c"d': 1, "normal": 2} |
| 124 | + jp = JSONPath("$['c\"d']") |
| 125 | + result = jp.update(data, 99) |
| 126 | + assert result['c"d'] == 99 |
0 commit comments