1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
import pytest
from jsonpath import JSONPatch
from jsonpath import JSONPatchError
from jsonpath import JSONPointerIndexError
from jsonpath import findall
from jsonpath import pointer
def test_issue_72_andy() -> None:
query = "andy"
data = {"andy": [1, 2, 3]}
assert findall(query, data) == [[1, 2, 3]]
def test_issue_72_orders() -> None:
query = "orders"
data = {"orders": [1, 2, 3]}
assert findall(query, data) == [[1, 2, 3]]
def test_issue_103() -> None:
query = "$..book[?(@.borrowers[?(@.name == _.name)])]"
data = {
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"borrowers": [
{"name": "John", "id": 101},
{"name": "Jane", "id": 102},
],
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99,
"borrowers": [{"name": "Peter", "id": 103}],
},
],
"bicycle": {"color": "red", "price": 19.95},
}
}
filter_context = {"name": "John"}
want = [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"borrowers": [{"name": "John", "id": 101}, {"name": "Jane", "id": 102}],
}
]
assert findall(query, data, filter_context=filter_context) == want
def test_quoted_reserved_word_and() -> None:
query = "$['and']"
data = {"and": [1, 2, 3]}
assert findall(query, data) == [[1, 2, 3]]
def test_quoted_reserved_word_or() -> None:
query = "$['or']"
data = {"or": [1, 2, 3]}
assert findall(query, data) == [[1, 2, 3]]
def test_issue_115() -> None:
data = {
"users": [
{"name": "Sue", "score": 100},
{"name": "John", "score": 86},
{"name": "Sally", "score": 84},
{"name": "Jane", "score": 55},
]
}
assert pointer.resolve("/users/0/score", data) == 100 # noqa: PLR2004
# Negative index
with pytest.raises(JSONPointerIndexError):
pointer.resolve("/users/-1/score", data)
def test_issue_117() -> None:
# When the target value is an array of length 2, /foo/2 is the same as /foo/-
patch = JSONPatch().add(path="/foo/2", value=99)
data = {"foo": ["bar", "baz"]}
assert patch.apply(data) == {"foo": ["bar", "baz", 99]}
# Array length + 1 raises
patch = JSONPatch().add(path="/foo/3", value=99)
data = {"foo": ["bar", "baz"]}
with pytest.raises(JSONPatchError):
patch.apply(data)
def test_issue_124() -> None:
query_raw = r"$[?@type =~ /studio\/material\/.*/]"
query = "$[?@type =~ /studio\\/material\\/.*/]"
data = [
{"type": "studio/material/a"},
{"type": "studio/material/b"},
{"type": "studio foo"},
]
want = [{"type": "studio/material/a"}, {"type": "studio/material/b"}]
assert findall(query, data) == want
assert findall(query_raw, data) == want
|