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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
"""Test cases from examples in draft-ietf-jsonpath-base-11.
The test cases defined here are taken from version 11 of the JSONPath
internet draft, draft-ietf-jsonpath-base-11. In accordance with
https://trustee.ietf.org/license-info, Revised BSD License text
is included bellow.
See https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-11
Copyright (c) 2023 IETF Trust and the persons identified as authors
of the code. All rights reserved.Redistribution and use in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
- Neither the name of Internet Society, IETF or IETF Trust, nor the
names of specific contributors, may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import asyncio
import dataclasses
import operator
from typing import Any
from typing import List
from typing import Mapping
from typing import Sequence
from typing import Union
import pytest
from jsonpath import JSONPathEnvironment
@dataclasses.dataclass
class Case:
description: str
path: str
data: Union[Sequence[Any], Mapping[str, Any]]
want: Union[Sequence[Any], Mapping[str, Any]]
FILTER_SELECTOR_DATA = {
"a": [3, 5, 1, 2, 4, 6, {"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}],
"o": {"p": 1, "q": 2, "r": 3, "s": 5, "t": {"u": 6}},
"e": "f",
}
TEST_CASES = [
Case(description="root", path="$", data={"k": "v"}, want=[{"k": "v"}]),
Case(
description="name selector - named value in nested object (single quote)",
path="$.o['j j']['k.k']",
data={"o": {"j j": {"k.k": 3}}, "'": {"@": 2}},
want=[3],
),
Case(
description="name selector - named value in nested object (double quote)",
path='$.o["j j"]["k.k"]',
data={"o": {"j j": {"k.k": 3}}, "'": {"@": 2}},
want=[3],
),
Case(
description="name selector - unusual member names",
path='$["\'"]["@"]',
data={"o": {"j j": {"k.k": 3}}, "'": {"@": 2}},
want=[2],
),
Case(
description="wildcard selector - object values",
path="$[*]",
data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
want=[{"j": 1, "k": 2}, [5, 3]],
),
Case(
description="wildcard selector - object values (dot property)",
path="$.o[*]",
data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
want=[1, 2],
),
Case(
description="wildcard selector - double wild",
path="$.o[*, *]",
data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
want=[1, 2, 1, 2],
),
Case(
description="wildcard selector - dot property wild",
path="$.a[*]",
data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
want=[5, 3],
),
Case(
description="index selector - element of array",
path="$[1]",
data=["a", "b"],
want=["b"],
),
Case(
description="index selector - element of array, from the end",
path="$[-2]",
data=["a", "b"],
want=["a"],
),
Case(
description="array slice selector - slice with default step",
path="$[1:3]",
data=["a", "b", "c", "d", "e", "f", "g"],
want=["b", "c"],
),
Case(
description="array slice selector - slice with no end index",
path="$[5:]",
data=["a", "b", "c", "d", "e", "f", "g"],
want=["f", "g"],
),
Case(
description="array slice selector - slice with negative step",
path="$[5:1:-2]",
data=["a", "b", "c", "d", "e", "f", "g"],
want=["f", "d"],
),
Case(
description="array slice selector - slice in reverse order",
path="$[::-1]",
data=["a", "b", "c", "d", "e", "f", "g"],
want=["g", "f", "e", "d", "c", "b", "a"],
),
Case(
description="filter selector - Member value comparison",
path="$.a[?(@.b == 'kilo')]",
data=FILTER_SELECTOR_DATA,
want=[{"b": "kilo"}],
),
Case(
description="filter selector - Array value comparison",
path="$.a[?(@>3.5)]",
data=FILTER_SELECTOR_DATA,
want=[5, 4, 6],
),
Case(
description="filter selector - Array value existence",
path="$.a[?(@.b)]",
data=FILTER_SELECTOR_DATA,
want=[{"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}],
),
Case(
description="filter selector - Existence of non-singular queries",
path="$[?(@.*)]",
data=FILTER_SELECTOR_DATA,
want=[
[3, 5, 1, 2, 4, 6, {"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}],
{"p": 1, "q": 2, "r": 3, "s": 5, "t": {"u": 6}},
],
),
Case(
description="filter selector - Nested filters",
path="$[?(@[?(@.b)])]",
data=FILTER_SELECTOR_DATA,
want=[[3, 5, 1, 2, 4, 6, {"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}]],
),
Case(
description="filter selector - Array value logical OR",
path='$.a[?(@<2 || @.b == "k")]',
data=FILTER_SELECTOR_DATA,
want=[1, {"b": "k"}],
),
Case(
description="filter selector - Array value regular expression match",
path='$.a[?match(@.b, "[jk]")]',
data=FILTER_SELECTOR_DATA,
want=[{"b": "j"}, {"b": "k"}],
),
Case(
description="filter selector - Array value regular expression search",
path='$.a[?search(@.b, "[jk]")]',
data=FILTER_SELECTOR_DATA,
want=[{"b": "j"}, {"b": "k"}, {"b": "kilo"}],
),
Case(
description="filter selector - Object value logical AND",
path="$.o[?(@>1 && @<4)]",
data=FILTER_SELECTOR_DATA,
want=[2, 3],
),
Case(
description="filter selector - Object value logical OR",
path="$.o[?(@.u || @.x)]",
data=FILTER_SELECTOR_DATA,
want=[{"u": 6}],
),
Case(
description="filter selector - Comparison of queries with no values",
path="$.a[?(@.b == $.x)]",
data=FILTER_SELECTOR_DATA,
want=[3, 5, 1, 2, 4, 6],
),
Case(
description=(
"filter selector - Comparisons of primitive and of structured values"
),
path="$.a[?(@ == @)]",
data=FILTER_SELECTOR_DATA,
want=[3, 5, 1, 2, 4, 6, {"b": "j"}, {"b": "k"}, {"b": {}}, {"b": "kilo"}],
),
Case(
description=("child segment - Indices"),
path="$[0, 3]",
data=["a", "b", "c", "d", "e", "f", "g"],
want=["a", "d"],
),
Case(
description=("child segment - Slice and index"),
path="$[0:2, 5]",
data=["a", "b", "c", "d", "e", "f", "g"],
want=["a", "b", "f"],
),
Case(
description=("child segment - Duplicated entries"),
path="$[0, 0]",
data=["a", "b", "c", "d", "e", "f", "g"],
want=["a", "a"],
),
Case(
description=("descendant segment - Object values"),
path="$..j",
data={"o": {"j": 1, "k": 2}, "a": [5, 3, [{"j": 4}, {"k": 6}]]},
want=[1, 4],
),
Case(
description=("descendant segment - Array values"),
path="$..[0]",
data={"o": {"j": 1, "k": 2}, "a": [5, 3, [{"j": 4}, {"k": 6}]]},
want=[5, {"j": 4}],
),
Case(
description=("descendant segment - All values"),
path="$..[*]",
data={"o": {"j": 1, "k": 2}, "a": [5, 3, [{"j": 4}, {"k": 6}]]},
want=[
{"j": 1, "k": 2},
[5, 3, [{"j": 4}, {"k": 6}]],
1,
2,
5,
3,
[{"j": 4}, {"k": 6}],
{"j": 4},
{"k": 6},
4,
6,
],
),
Case(
description=("descendant segment - Input value is visited"),
path="$..o",
data={"o": {"j": 1, "k": 2}, "a": [5, 3, [{"j": 4}, {"k": 6}]]},
want=[{"j": 1, "k": 2}],
),
Case(
description=("descendant segment - Multiple segments"),
path="$.a..[0, 1]",
data={"o": {"j": 1, "k": 2}, "a": [5, 3, [{"j": 4}, {"k": 6}]]},
want=[5, 3, {"j": 4}, {"k": 6}],
),
Case(
description=("null semantics - Object value"),
path="$.a",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[None],
),
Case(
description=("null semantics - null used as array"),
path="$.a[0]",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[],
),
Case(
description=("null semantics - null used as object"),
path="$.a.d",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[],
),
Case(
description=("null semantics - Array value"),
path="$.b[0]",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[None],
),
Case(
description=("null semantics - Array value wild"),
path="$.b[*]",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[None],
),
Case(
description=("null semantics - Existence"),
path="$.b[?(@)]",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[None],
),
Case(
description=("null semantics - Comparison"),
path="$.b[?(@==null)]",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[None],
),
Case(
description=("null semantics - Comparison with 'missing' value"),
path="$.c[?(@.d==null)]",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[],
),
Case(
description=(
"null semantics - Not JSON null at all, just a member name string"
),
path="$.null",
data={"a": None, "b": [None], "c": [{}], "null": 1},
want=[1],
),
Case(
description=("filter, length function, string data"),
path="$[?(length(@.a)>=2)]",
data=[{"a": "ab"}, {"a": "d"}],
want=[{"a": "ab"}],
),
Case(
description=("filter, length function, array data"),
path="$[?(length(@.a)>=2)]",
data=[{"a": [1, 2, 3]}, {"a": [1]}],
want=[{"a": [1, 2, 3]}],
),
Case(
description=("filter, length function, missing data"),
path="$[?(length(@.a)>=2)]",
data=[{"d": "f"}],
want=[],
),
Case(
description=("filter, count function"),
path="$[?(count(@..*)>2)]",
data=[{"a": [1, 2, 3]}, {"a": [1], "d": "f"}, {"a": 1, "d": "f"}],
want=[{"a": [1, 2, 3]}, {"a": [1], "d": "f"}],
),
]
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_find_ieft(env: JSONPathEnvironment, case: Case) -> None:
path = env.compile(case.path)
assert path.findall(case.data) == case.want
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_find_ieft_async(env: JSONPathEnvironment, case: Case) -> None:
path = env.compile(case.path)
async def coro() -> List[object]:
return await path.findall_async(case.data)
assert asyncio.run(coro()) == case.want
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_hash_path(env: JSONPathEnvironment, case: Case) -> None:
"""Test that paths are hashable."""
hash(env.compile(case.path))
|