File: test_query_projection.py

package info (click to toggle)
python-jsonpath 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,028 kB
  • sloc: python: 9,473; makefile: 6
file content (169 lines) | stat: -rw-r--r-- 5,244 bytes parent folder | download
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
"""Test cases for the fluent API projection."""

from typing import Any
from typing import List

import jsonpath
from jsonpath import Projection


def test_select_from_primitive() -> None:
    expr = "$[0].a"
    data = [{"a": 1, "b": 1}, {"a": 2, "b": 2}, {"b": 3, "a": 3}]
    projection = ("nosuchthing",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == []


def test_top_level_array() -> None:
    expr = "$.*"
    data = [{"a": 1, "b": 1}, {"a": 2, "b": 2}, {"b": 3, "a": 3}]
    projection = ("a",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"a": 1}, {"a": 2}, {"a": 3}]


def test_top_level_array_flat_projection() -> None:
    expr = "$.*"
    data = [{"a": 1, "b": 1}, {"a": 2, "b": 2}, {"b": 3, "a": 3}]
    projection = ("a",)
    it = jsonpath.query(expr, data).select(*projection, projection=Projection.FLAT)
    assert list(it) == [[1], [2], [3]]


def test_top_level_array_partial_existence() -> None:
    expr = "$.*"
    data = [{"a": 1, "b": 1}, {"b": 2}, {"b": 3, "a": 3}]
    projection = ("a",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"a": 1}, {"a": 3}]


def test_top_level_array_projection_does_not_existence() -> None:
    expr = "$.*"
    data = [{"a": 1, "b": 1}, {"b": 2}, {"b": 3, "a": 3}]
    projection = ("x",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == []


def test_empty_top_level_array() -> None:
    expr = "$.*"
    data: List[Any] = []
    projection = ("a",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == []


def test_top_level_array_select_many() -> None:
    expr = "$.*"
    data = [{"a": 1, "b": 1, "c": 1}, {"a": 2, "b": 2, "c": 2}, {"b": 3, "a": 3}]
    projection = ("a", "c")
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"a": 1, "c": 1}, {"a": 2, "c": 2}, {"a": 3}]


def test_singular_query() -> None:
    expr = "$.a"
    data = {"a": {"foo": 42, "bar": 7}, "b": 1}
    projection = ("foo",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"foo": 42}]


def test_select_array_element() -> None:
    expr = "$.a"
    data = {"a": {"foo": [42, 7], "bar": 7}, "b": 1}
    projection = ("foo[0]",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"foo": [42]}]


def test_select_array_slice() -> None:
    expr = "$.a"
    data = {"a": {"foo": [1, 2, 42, 7, 3], "bar": 7}, "b": 1}
    projection = ("foo[2:4]",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"foo": [42, 7]}]


def test_select_nested_objects() -> None:
    expr = "$.a"
    data = {"a": {"foo": {"bar": 42}, "bar": 7}, "b": 1}
    projection = ("foo.bar",)
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"foo": {"bar": 42}}]


def test_select_nested_objects_root_projection() -> None:
    expr = "$.a"
    data = {"a": {"foo": {"bar": 42}, "bar": 7}, "b": 1}
    projection = ("foo.bar",)
    it = jsonpath.query(expr, data).select(*projection, projection=Projection.ROOT)
    assert list(it) == [{"a": {"foo": {"bar": 42}}}]


def test_select_nested_objects_flat_projection() -> None:
    expr = "$.a"
    data = {"a": {"foo": {"bar": 42}, "bar": 7}, "b": 1}
    projection = ("foo.bar",)
    it = jsonpath.query(expr, data).select(*projection, projection=Projection.FLAT)
    assert list(it) == [[42]]


def test_sparse_array_selection() -> None:
    expr = "$..products[?@.social]"
    data = {
        "categories": [
            {
                "name": "footwear",
                "products": [
                    {
                        "title": "Trainers",
                        "description": "Fashionable trainers.",
                        "price": 89.99,
                    },
                    {
                        "title": "Barefoot Trainers",
                        "description": "Running trainers.",
                        "price": 130.00,
                    },
                ],
            },
            {
                "name": "headwear",
                "products": [
                    {
                        "title": "Cap",
                        "description": "Baseball cap",
                        "price": 15.00,
                    },
                    {
                        "title": "Beanie",
                        "description": "Winter running hat.",
                        "price": 9.00,
                        "social": {"likes": 12, "shares": 7},
                    },
                ],
            },
        ],
        "price_cap": 10,
    }

    it = jsonpath.query(expr, data).select(
        "title",
        "social.shares",
        projection=Projection.ROOT,
    )

    assert list(it) == [
        {"categories": [{"products": [{"title": "Beanie", "social": {"shares": 7}}]}]}
    ]


def test_pre_compiled_select_many() -> None:
    expr = "$.*"
    data = [{"a": 1, "b": 1, "c": 1}, {"a": 2, "b": 2, "c": 2}, {"b": 3, "a": 3}]
    projection = (jsonpath.compile("a"), "c")
    it = jsonpath.query(expr, data).select(*projection)
    assert list(it) == [{"a": 1, "c": 1}, {"a": 2, "c": 2}, {"a": 3}]