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
|
import dataclasses
import operator
import pytest
from jsonpath import JSONPathEnvironment
@dataclasses.dataclass
class Case:
description: str
path: str
want: str
TEST_CASES = [
Case(description="empty", path="", want="$"),
Case(description="just root", path="$", want="$"),
Case(description="implicit root dot property", path=".thing", want="$['thing']"),
Case(description="root dot property", path="$.thing", want="$['thing']"),
Case(
description="root double quoted property", path='$["thing"]', want="$['thing']"
),
Case(
description="root single quoted property", path="$['thing']", want="$['thing']"
),
Case(
description="root quoted property with non-ident chars",
path="$['anything{!%']",
want="$['anything{!%']",
),
Case(description="root bracket index", path="$[1]", want="$[1]"),
Case(description="root slice", path="$[1:-1]", want="$[1:-1:1]"),
Case(description="root slice with step", path="$[1:-1:2]", want="$[1:-1:2]"),
Case(description="root slice with empty start", path="$[:-1]", want="$[:-1:1]"),
Case(description="root slice with empty stop", path="$[1:]", want="$[1::1]"),
Case(description="root dot wild", path="$.*", want="$[*]"),
Case(description="root bracket wild", path="$[*]", want="$[*]"),
Case(description="root selector list", path="$[1,2]", want="$[1, 2]"),
Case(
description="root selector list with slice",
path="$[1,5:-1:1]",
want="$[1, 5:-1:1]",
),
Case(
description="root selector list with quoted properties",
path="$[\"some\",'thing']",
want="$['some', 'thing']",
),
Case(
description="filter self dot property",
path="[?(@.thing)]",
want="$[?@['thing']]",
),
Case(
description="filter root dot property",
path="$.some[?($.thing)]",
want="$['some'][?$['thing']]",
),
Case(
description="filter with equality test",
path="$.some[?(@.thing == 7)]",
want="$['some'][?@['thing'] == 7]",
),
Case(
description="filter with >=",
path="$.some[?(@.thing >= 7)]",
want="$['some'][?@['thing'] >= 7]",
),
Case(
description="filter with >=",
path="$.some[?(@.thing >= 7)]",
want="$['some'][?@['thing'] >= 7]",
),
Case(
description="filter with !=",
path="$.some[?(@.thing != 7)]",
want="$['some'][?@['thing'] != 7]",
),
Case(
description="filter with <>",
path="$.some[?(@.thing != 7)]",
want="$['some'][?@['thing'] != 7]",
),
Case(
description="filter with regex",
path="$.some[?(@.thing =~ /(foo|bar)/i)]",
want="$['some'][?@['thing'] =~ /(foo|bar)/i]",
),
Case(
description="filter with list membership test",
path="$.some[?(@.thing in ['foo', 'bar', 42])]",
want="$['some'][?@['thing'] in ['foo', 'bar', 42]]",
),
Case(
description="filter with boolean literals",
path="$.some[?(true == false)]",
want="$['some'][?true == false]",
),
Case(
description="filter with nil literal",
path="$.some[?(@.thing == nil)]",
want="$['some'][?@['thing'] == nil]",
),
Case(
description="null is the same as nil",
path="$.some[?(@.thing == null)]",
want="$['some'][?@['thing'] == nil]",
),
Case(
description="none is the same as nil",
path="$.some[?(@.thing == none)]",
want="$['some'][?@['thing'] == nil]",
),
Case(
description="filter with test for undefined",
path="$.some[?(@.thing == undefined)]",
want="$['some'][?@['thing'] == undefined]",
),
Case(
description="missing is the same as undefined",
path="$.some[?(@.thing == missing)]",
want="$['some'][?@['thing'] == undefined]",
),
Case(
description="filter with string literal",
path="$.some[?(@.thing == 'foo')]",
want="$['some'][?@['thing'] == 'foo']",
),
Case(
description="filter with integer literal",
path="$.some[?(@.thing == 1)]",
want="$['some'][?@['thing'] == 1]",
),
Case(
description="filter with float literal",
path="$.some[?(@.thing == 1.1)]",
want="$['some'][?@['thing'] == 1.1]",
),
Case(
description="filter with logical not",
path="$.some[?(@.thing > 1 and not $.other)]",
want="$['some'][?@['thing'] > 1 && !$['other']]",
),
Case(
description="filter with grouped expression",
path="$.some[?(@.thing > 1 and ($.foo or $.bar))]",
want="$['some'][?@['thing'] > 1 && ($['foo'] || $['bar'])]",
),
Case(
description="keys selector",
path="$.some.~",
want="$['some'][~]",
),
Case(
description="current key identifier",
path="$[?# > 2]",
want="$[?# > 2]",
),
Case(
description="comparison to single quoted string literal with escape",
path="$[?@.foo == 'ba\\'r']",
want="$[?@['foo'] == 'ba\\'r']",
),
Case(
description="comparison to double quoted string literal with escape",
path='$[?@.foo == "ba\\"r"]',
want="$[?@['foo'] == 'ba\"r']",
),
Case(
description="not binds more tightly than or",
path="$[?!@.a || !@.b]",
want="$[?!@['a'] || !@['b']]",
),
Case(
description="not binds more tightly than and",
path="$[?!@.a && !@.b]",
want="$[?!@['a'] && !@['b']]",
),
Case(
description="control precedence with parens",
path="$[?!(@.a && !@.b)]",
want="$[?!(@['a'] && !@['b'])]",
),
Case(
description="issue 70",
path=r"$[?@ =~ /\d/]",
want="$[?@ =~ /\\d/]",
),
Case(
description="escaped slash in regex literal",
path=r"$[?@ =~ /\\d/]",
want="$[?@ =~ /\\\\d/]",
),
Case(
description="match function",
path=r"$[?match(@, '\\d')]",
want="$[?match(@, '\\\\d')]",
),
]
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_default_parser(env: JSONPathEnvironment, case: Case) -> None:
path = env.compile(case.path)
assert str(path) == case.want
|