|
| 1 | +"""Default filter expression comparison test cases from examples in |
| 2 | +draft-ietf-jsonpath-base-11. |
| 3 | +
|
| 4 | +The test cases defined here are taken from version 11 of the JSONPath |
| 5 | +internet draft, draft-ietf-jsonpath-base-11. In accordance with |
| 6 | +https://trustee.ietf.org/license-info, Revised BSD License text |
| 7 | +is included bellow. |
| 8 | +
|
| 9 | +See https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-11 |
| 10 | +
|
| 11 | +Copyright (c) 2023 IETF Trust and the persons identified as authors |
| 12 | +of the code. All rights reserved.Redistribution and use in source and |
| 13 | +binary forms, with or without modification, are permitted provided |
| 14 | +that the following conditions are met: |
| 15 | +
|
| 16 | +- Redistributions of source code must retain the above copyright |
| 17 | + notice, this list of conditions and the following disclaimer. |
| 18 | +- Redistributions in binary form must reproduce the above copyright |
| 19 | + notice, this list of conditions and the following disclaimer in the |
| 20 | + documentation and/or other materials provided with the |
| 21 | + distribution. |
| 22 | +- Neither the name of Internet Society, IETF or IETF Trust, nor the |
| 23 | + names of specific contributors, may be used to endorse or promote |
| 24 | + products derived from this software without specific prior written |
| 25 | + permission. |
| 26 | +
|
| 27 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | +“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | +""" |
| 39 | +# pylint: disable=missing-class-docstring, missing-function-docstring |
| 40 | + |
| 41 | +import dataclasses |
| 42 | +import operator |
| 43 | + |
| 44 | +import pytest |
| 45 | + |
| 46 | +from jsonpath import JSONPathEnvironment |
| 47 | +from jsonpath.filter import UNDEFINED |
| 48 | + |
| 49 | + |
| 50 | +@dataclasses.dataclass |
| 51 | +class Case: |
| 52 | + description: str |
| 53 | + left: object |
| 54 | + op: str |
| 55 | + right: object |
| 56 | + want: bool |
| 57 | + |
| 58 | + |
| 59 | +DATA = {"obj": {"x": "y"}, "arr": [2, 3]} |
| 60 | + |
| 61 | +TEST_CASES = [ |
| 62 | + Case( |
| 63 | + description="$.absent1 == $.absent2", |
| 64 | + left=UNDEFINED, |
| 65 | + op="==", |
| 66 | + right=UNDEFINED, |
| 67 | + want=True, |
| 68 | + ), |
| 69 | + Case( |
| 70 | + description="$.absent1 <= $.absent2", |
| 71 | + left=UNDEFINED, |
| 72 | + op="<=", |
| 73 | + right=UNDEFINED, |
| 74 | + want=True, |
| 75 | + ), |
| 76 | + Case( |
| 77 | + description="$.absent == 'g'", |
| 78 | + left=UNDEFINED, |
| 79 | + op="==", |
| 80 | + right="g", |
| 81 | + want=False, |
| 82 | + ), |
| 83 | + Case( |
| 84 | + description="$.absent1 != $.absent2", |
| 85 | + left=UNDEFINED, |
| 86 | + op="!=", |
| 87 | + right=UNDEFINED, |
| 88 | + want=False, |
| 89 | + ), |
| 90 | + Case( |
| 91 | + description="$.absent != 'g'", |
| 92 | + left=UNDEFINED, |
| 93 | + op="!=", |
| 94 | + right="g", |
| 95 | + want=True, |
| 96 | + ), |
| 97 | + Case( |
| 98 | + description="1 <= 2", |
| 99 | + left=1, |
| 100 | + op="<=", |
| 101 | + right=2, |
| 102 | + want=True, |
| 103 | + ), |
| 104 | + Case( |
| 105 | + description="1 > 2", |
| 106 | + left=1, |
| 107 | + op=">", |
| 108 | + right=2, |
| 109 | + want=False, |
| 110 | + ), |
| 111 | + Case( |
| 112 | + description="13 == '13'", |
| 113 | + left=13, |
| 114 | + op="==", |
| 115 | + right="13", |
| 116 | + want=False, |
| 117 | + ), |
| 118 | + Case( |
| 119 | + description="'a' <= 'b'", |
| 120 | + left="a", |
| 121 | + op="<=", |
| 122 | + right="b", |
| 123 | + want=True, |
| 124 | + ), |
| 125 | + Case( |
| 126 | + description="'a' > 'b'", |
| 127 | + left="a", |
| 128 | + op=">", |
| 129 | + right="b", |
| 130 | + want=False, |
| 131 | + ), |
| 132 | + Case( |
| 133 | + description="$.obj == $.arr", |
| 134 | + left=DATA["obj"], |
| 135 | + op="==", |
| 136 | + right=DATA["arr"], |
| 137 | + want=False, |
| 138 | + ), |
| 139 | + Case( |
| 140 | + description="$.obj != $.arr", |
| 141 | + left=DATA["obj"], |
| 142 | + op="!=", |
| 143 | + right=DATA["arr"], |
| 144 | + want=True, |
| 145 | + ), |
| 146 | + Case( |
| 147 | + description="$.obj == $.obj", |
| 148 | + left=DATA["obj"], |
| 149 | + op="==", |
| 150 | + right=DATA["obj"], |
| 151 | + want=True, |
| 152 | + ), |
| 153 | + Case( |
| 154 | + description="$.obj != $.obj", |
| 155 | + left=DATA["obj"], |
| 156 | + op="!=", |
| 157 | + right=DATA["obj"], |
| 158 | + want=False, |
| 159 | + ), |
| 160 | + Case( |
| 161 | + description="$.arr == $.arr", |
| 162 | + left=DATA["arr"], |
| 163 | + op="==", |
| 164 | + right=DATA["arr"], |
| 165 | + want=True, |
| 166 | + ), |
| 167 | + Case( |
| 168 | + description="$.arr != $.arr", |
| 169 | + left=DATA["arr"], |
| 170 | + op="!=", |
| 171 | + right=DATA["arr"], |
| 172 | + want=False, |
| 173 | + ), |
| 174 | + Case( |
| 175 | + description="$.arr == 17", |
| 176 | + left=DATA["arr"], |
| 177 | + op="==", |
| 178 | + right=17, |
| 179 | + want=False, |
| 180 | + ), |
| 181 | + Case( |
| 182 | + description="$.arr != 17", |
| 183 | + left=DATA["arr"], |
| 184 | + op="!=", |
| 185 | + right=17, |
| 186 | + want=True, |
| 187 | + ), |
| 188 | + Case( |
| 189 | + description="$.obj <= $.arr", |
| 190 | + left=DATA["obj"], |
| 191 | + op="<=", |
| 192 | + right=DATA["arr"], |
| 193 | + want=False, |
| 194 | + ), |
| 195 | + Case( |
| 196 | + description="$.obj < $.arr", |
| 197 | + left=DATA["obj"], |
| 198 | + op="<", |
| 199 | + right=DATA["arr"], |
| 200 | + want=False, |
| 201 | + ), |
| 202 | + Case( |
| 203 | + description="$.obj <= $.obj", |
| 204 | + left=DATA["obj"], |
| 205 | + op="<=", |
| 206 | + right=DATA["obj"], |
| 207 | + want=True, |
| 208 | + ), |
| 209 | + Case( |
| 210 | + description="$.arr <= $.arr", |
| 211 | + left=DATA["arr"], |
| 212 | + op="<=", |
| 213 | + right=DATA["arr"], |
| 214 | + want=True, |
| 215 | + ), |
| 216 | + Case( |
| 217 | + description="1 <= $.arr", |
| 218 | + left=1, |
| 219 | + op="<=", |
| 220 | + right=DATA["arr"], |
| 221 | + want=False, |
| 222 | + ), |
| 223 | + Case( |
| 224 | + description="1 >= $.arr", |
| 225 | + left=1, |
| 226 | + op=">=", |
| 227 | + right=DATA["arr"], |
| 228 | + want=False, |
| 229 | + ), |
| 230 | + Case( |
| 231 | + description="1 > $.arr", |
| 232 | + left=1, |
| 233 | + op=">", |
| 234 | + right=DATA["arr"], |
| 235 | + want=False, |
| 236 | + ), |
| 237 | + Case( |
| 238 | + description="1 < $.arr", |
| 239 | + left=1, |
| 240 | + op="<", |
| 241 | + right=DATA["arr"], |
| 242 | + want=False, |
| 243 | + ), |
| 244 | + Case( |
| 245 | + description="true <= true", |
| 246 | + left=True, |
| 247 | + op="<=", |
| 248 | + right=True, |
| 249 | + want=True, |
| 250 | + ), |
| 251 | + Case( |
| 252 | + description="true > true", |
| 253 | + left=True, |
| 254 | + op=">", |
| 255 | + right=True, |
| 256 | + want=False, |
| 257 | + ), |
| 258 | +] |
| 259 | + |
| 260 | + |
| 261 | +@pytest.fixture |
| 262 | +def env() -> JSONPathEnvironment: |
| 263 | + return JSONPathEnvironment() |
| 264 | + |
| 265 | + |
| 266 | +# pylint: disable=redefined-outer-name |
| 267 | +@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description")) |
| 268 | +def test_compare_ieft(env: JSONPathEnvironment, case: Case) -> None: |
| 269 | + result = env.compare(case.left, case.op, case.right) |
| 270 | + assert result == case.want |
0 commit comments