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
|
import copy
from contextlib import nullcontext as does_not_raise
import pytest
from jsonpath_ng.ext import parse
@pytest.mark.parametrize(
"string, initial_data, expected_result",
(
("$.foo", {}, {"foo": 42}),
("$.foo.bar", {}, {"foo": {"bar": 42}}),
("$.foo[0]", {}, {"foo": [42]}),
("$.foo[1]", {}, {"foo": [{}, 42]}),
("$.foo[0].bar", {}, {"foo": [{"bar": 42}]}),
("$.foo[1].bar", {}, {"foo": [{}, {"bar": 42}]}),
("$.foo[0][0]", {}, {"foo": [[42]]}),
("$.foo[1][1]", {}, {"foo": [{}, [{}, 42]]}),
("foo[0]", {}, {"foo": [42]}),
("foo[1]", {}, {"foo": [{}, 42]}),
("foo", {}, {"foo": 42}),
#
# Initial data can be a list if we expect a list back.
("[0]", [], [42]),
("[1]", [], [{}, 42]),
#
# Convert initial data to a list, if necessary.
("[0]", {}, [42]),
("[1]", {}, [{}, 42]),
#
(
'foo[?bar="baz"].qux',
{
"foo": [
{"bar": "baz"},
{"bar": "bizzle"},
]
},
{"foo": [{"bar": "baz", "qux": 42}, {"bar": "bizzle"}]},
),
("[1].foo", [{"foo": 1}, {"bar": 2}], [{"foo": 1}, {"foo": 42, "bar": 2}]),
),
)
def test_update_or_create(string, initial_data, expected_result):
jsonpath = parse(string)
result = jsonpath.update_or_create(initial_data, 42)
assert result == expected_result
@pytest.mark.parametrize(
"string, initial_data, expectation",
(
# Slice not supported
("foo[0:1]", {}, does_not_raise()),
#
# Filter does not create items to meet criteria
('foo[?bar="baz"].qux', {}, does_not_raise()),
#
# Does not convert initial data to a dictionary
("foo", [], pytest.raises(TypeError)),
),
)
def test_unsupported_classes(string, initial_data, expectation):
copied_initial_data = copy.copy(initial_data)
jsonpath = parse(string)
with expectation:
result = jsonpath.update_or_create(initial_data, 42)
assert result != copied_initial_data
|