|
1 | | -# TODO: |
| 1 | +import asyncio |
| 2 | +from typing import List |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import jsonpath |
| 7 | + |
| 8 | + |
| 9 | +def test_convenience_compile() -> None: |
| 10 | + # Implicit root identifier works by default, but not when strict=True. |
| 11 | + path = jsonpath.compile("a.*") |
| 12 | + assert isinstance(path, jsonpath.JSONPath) |
| 13 | + assert path.findall({"a": [1, 2, 3]}) == [1, 2, 3] |
| 14 | + |
| 15 | + |
| 16 | +def test_convenience_compile_strict() -> None: |
| 17 | + with pytest.raises(jsonpath.JSONPathSyntaxError): |
| 18 | + jsonpath.compile("a.*", strict=True) |
| 19 | + |
| 20 | + path = jsonpath.compile("$.a.*", strict=True) |
| 21 | + assert isinstance(path, jsonpath.JSONPath) |
| 22 | + assert path.findall({"a": [1, 2, 3]}) == [1, 2, 3] |
| 23 | + |
| 24 | + |
| 25 | +def test_convenience_findall() -> None: |
| 26 | + assert jsonpath.findall("a.*", {"a": [1, 2, 3]}) == [1, 2, 3] |
| 27 | + |
| 28 | + |
| 29 | +def test_convenience_findall_strict() -> None: |
| 30 | + with pytest.raises(jsonpath.JSONPathSyntaxError): |
| 31 | + jsonpath.findall("a.*", {"a": [1, 2, 3]}, strict=True) |
| 32 | + |
| 33 | + assert jsonpath.findall("$.a.*", {"a": [1, 2, 3]}, strict=True) == [1, 2, 3] |
| 34 | + |
| 35 | + |
| 36 | +def test_convenience_findall_async() -> None: |
| 37 | + async def coro() -> List[object]: |
| 38 | + return await jsonpath.findall_async("a.*", {"a": [1, 2, 3]}) |
| 39 | + |
| 40 | + assert asyncio.run(coro()) == [1, 2, 3] |
| 41 | + |
| 42 | + |
| 43 | +def test_convenience_findall_async_strict() -> None: |
| 44 | + async def coro() -> List[object]: |
| 45 | + with pytest.raises(jsonpath.JSONPathSyntaxError): |
| 46 | + await jsonpath.findall_async("a.*", {"a": [1, 2, 3]}, strict=True) |
| 47 | + |
| 48 | + return await jsonpath.findall_async("$.a.*", {"a": [1, 2, 3]}, strict=True) |
| 49 | + |
| 50 | + assert asyncio.run(coro()) == [1, 2, 3] |
| 51 | + |
| 52 | + |
| 53 | +def test_convenience_finditer() -> None: |
| 54 | + matches = list(jsonpath.finditer("a.*", {"a": [1, 2, 3]})) |
| 55 | + assert [m.obj for m in matches] == [1, 2, 3] |
| 56 | + |
| 57 | + |
| 58 | +def test_convenience_finditer_strict() -> None: |
| 59 | + with pytest.raises(jsonpath.JSONPathSyntaxError): |
| 60 | + list(jsonpath.finditer("a.*", {"a": [1, 2, 3]}, strict=True)) |
| 61 | + |
| 62 | + matches = list(jsonpath.finditer("$.a.*", {"a": [1, 2, 3]}, strict=True)) |
| 63 | + assert [m.obj for m in matches] == [1, 2, 3] |
| 64 | + |
| 65 | + |
| 66 | +def test_convenience_finditer_async_strict() -> None: |
| 67 | + async def coro() -> List[object]: |
| 68 | + with pytest.raises(jsonpath.JSONPathSyntaxError): |
| 69 | + await jsonpath.finditer_async("a.*", {"a": [1, 2, 3]}, strict=True) |
| 70 | + |
| 71 | + it = await jsonpath.finditer_async("$.a.*", {"a": [1, 2, 3]}, strict=True) |
| 72 | + return [m.obj async for m in it] |
| 73 | + |
| 74 | + assert asyncio.run(coro()) == [1, 2, 3] |
| 75 | + |
| 76 | + |
| 77 | +def test_convenience_match() -> None: |
| 78 | + match = jsonpath.match("a.*", {"a": [1, 2, 3]}) |
| 79 | + assert isinstance(match, jsonpath.JSONPathMatch) |
| 80 | + assert match.obj == 1 |
| 81 | + |
| 82 | + |
| 83 | +def test_convenience_match_strict() -> None: |
| 84 | + with pytest.raises(jsonpath.JSONPathSyntaxError): |
| 85 | + jsonpath.match("a.*", {"a": [1, 2, 3]}, strict=True) |
| 86 | + |
| 87 | + match = jsonpath.match("$.a.*", {"a": [1, 2, 3]}) |
| 88 | + assert isinstance(match, jsonpath.JSONPathMatch) |
| 89 | + assert match.obj == 1 |
| 90 | + |
| 91 | + |
| 92 | +def test_convenience_query() -> None: |
| 93 | + query = jsonpath.query("a.*", {"a": [1, 2, 3]}) |
| 94 | + assert isinstance(query, jsonpath.Query) |
| 95 | + assert list(query.values()) == [1, 2, 3] |
| 96 | + |
| 97 | + |
| 98 | +def test_convenience_query_strict() -> None: |
| 99 | + with pytest.raises(jsonpath.JSONPathSyntaxError): |
| 100 | + jsonpath.query("a.*", {"a": [1, 2, 3]}, strict=True) |
| 101 | + |
| 102 | + query = jsonpath.query("$.a.*", {"a": [1, 2, 3]}) |
| 103 | + assert isinstance(query, jsonpath.Query) |
| 104 | + assert list(query.values()) == [1, 2, 3] |
0 commit comments