|
| 1 | +"""Shared fixtures for jsonpath-python tests.""" |
| 2 | + |
1 | 3 | import json |
2 | | -from collections import namedtuple |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 |
|
6 | | -TestCase = namedtuple("TestCase", ("expr", "data", "result")) |
7 | | - |
8 | | -with open("tests/data/2.json", "rb") as f: |
9 | | - data = json.load(f) |
10 | | - |
11 | 7 |
|
12 | | -@pytest.fixture( |
13 | | - params=[ |
14 | | - TestCase("$.*", data, list(data.values())), |
15 | | - TestCase("$.book", data, [data["book"]]), |
16 | | - TestCase("$[book]", data, [data["book"]]), |
17 | | - TestCase("$.'a.b c'", data, [data["a.b c"]]), |
18 | | - TestCase("$['a.b c']", data, [data["a.b c"]]), |
19 | | - # recursive descent |
20 | | - TestCase("$..price", data, [8.95, 12.99, 8.99, 22.99, 19.95]), |
21 | | - # slice |
22 | | - TestCase("$.book[1:3]", data, data["book"][1:3]), |
23 | | - TestCase("$.book[1:-1]", data, data["book"][1:-1]), |
24 | | - TestCase("$.book[0:-1:2]", data, data["book"][0:-1:2]), |
25 | | - TestCase("$.book[-1:1]", data, data["book"][-1:1]), |
26 | | - TestCase("$.book[-1:-11:3]", data, data["book"][-1:-11:3]), |
27 | | - TestCase("$.book[:]", data, data["book"][:]), |
28 | | - # filter |
29 | | - TestCase("$.book[?(@.price>8 and @.price<9)].price", data, [8.95, 8.99]), |
30 | | - TestCase('$.book[?(@.category=="reference")].category', data, ["reference"]), |
31 | | - TestCase( |
32 | | - '$.book[?(@.category!="reference" and @.price<9)].title', |
33 | | - data, |
34 | | - ["Moby Dick"], |
35 | | - ), |
36 | | - TestCase( |
37 | | - '$.book[?(@.author=="Herman Melville" or @.author=="Evelyn Waugh")].author', |
38 | | - data, |
39 | | - ["Evelyn Waugh", "Herman Melville"], |
40 | | - ), |
41 | | - # sort |
42 | | - TestCase("$.book[/(price)].price", data, [8.95, 8.99, 12.99, 22.99]), |
43 | | - TestCase("$.book[/(~price)].price", data, [22.99, 12.99, 8.99, 8.95]), |
44 | | - TestCase("$.book[/(category,price)].price", data, [8.99, 12.99, 22.99, 8.95]), |
45 | | - TestCase( |
46 | | - "$.book[/(brand.version)].brand.version", |
47 | | - data, |
48 | | - ["v0.0.1", "v1.0.0", "v1.0.2", "v1.0.3"], |
49 | | - ), |
50 | | - TestCase("$.scores[/(score)].score", data, [60, 85, 90, 95, 100]), |
51 | | - TestCase( |
52 | | - "$.scores[/(score)].(score)", |
53 | | - data, |
54 | | - [ |
55 | | - {"score": 60}, |
56 | | - {"score": 85}, |
57 | | - {"score": 90}, |
58 | | - {"score": 95}, |
59 | | - {"score": 100}, |
60 | | - ], |
61 | | - ), |
62 | | - TestCase( |
63 | | - "$.book[*].(title)", |
64 | | - data, |
65 | | - [ |
66 | | - {"title": "Sayings of the Century"}, |
67 | | - {"title": "Sword of Honour"}, |
68 | | - {"title": "Moby Dick"}, |
69 | | - {"title": "The Lord of the Rings"}, |
70 | | - ], |
71 | | - ), |
72 | | - TestCase( |
73 | | - "$.book[/(category,price)].(title,price)", |
74 | | - data, |
75 | | - [ |
76 | | - {"title": "Moby Dick", "price": 8.99}, |
77 | | - {"title": "Sword of Honour", "price": 12.99}, |
78 | | - {"title": "The Lord of the Rings", "price": 22.99}, |
79 | | - {"title": "Sayings of the Century", "price": 8.95}, |
80 | | - ], |
81 | | - ), |
82 | | - TestCase( |
83 | | - "$.book[*].(title,brand.version)", |
84 | | - data, |
85 | | - [ |
86 | | - {"title": "Sayings of the Century", "brand.version": "v1.0.0"}, |
87 | | - {"title": "Sword of Honour", "brand.version": "v0.0.1"}, |
88 | | - {"title": "Moby Dick", "brand.version": "v1.0.2"}, |
89 | | - {"title": "The Lord of the Rings", "brand.version": "v1.0.3"}, |
90 | | - ], |
91 | | - ), |
92 | | - ] |
93 | | -) |
94 | | -def value_cases(request): |
95 | | - return request.param |
| 8 | +@pytest.fixture(scope="session") |
| 9 | +def sample_data(): |
| 10 | + """Standard test data for JSONPath tests.""" |
| 11 | + with open("tests/data/2.json", "rb") as f: |
| 12 | + return json.load(f) |
96 | 13 |
|
97 | 14 |
|
98 | | -@pytest.fixture( |
99 | | - params=[ |
100 | | - TestCase("$.*", data, ["$['a.b c']", "$.book", "$.bicycle", "$.scores"]), |
101 | | - TestCase("$.book", data, ["$.book"]), |
102 | | - TestCase("$[book]", data, ["$.book"]), |
103 | | - TestCase("$.'a.b c'", data, ["$['a.b c']"]), |
104 | | - TestCase("$['a.b c']", data, ["$['a.b c']"]), |
105 | | - # recursive descent |
106 | | - TestCase( |
107 | | - "$..price", |
108 | | - data, |
109 | | - [ |
110 | | - "$.book[0].price", |
111 | | - "$.book[1].price", |
112 | | - "$.book[2].price", |
113 | | - "$.book[3].price", |
114 | | - "$.bicycle.price", |
115 | | - ], |
116 | | - ), |
117 | | - # slice |
118 | | - TestCase("$.book[1:3]", data, ["$.book[1]", "$.book[2]"]), |
119 | | - TestCase("$.book[1:-1]", data, ["$.book[1]", "$.book[2]"]), |
120 | | - TestCase("$.book[0:-1:2]", data, ["$.book[0]", "$.book[2]"]), |
121 | | - TestCase("$.book[-1:1]", data, []), |
122 | | - TestCase("$.book[-1:-11:3]", data, []), |
123 | | - TestCase("$.book[:]", data, ["$.book[0]", "$.book[1]", "$.book[2]", "$.book[3]"]), |
124 | | - # filter |
125 | | - TestCase( |
126 | | - "$.book[?(@.price>8 and @.price<9)].price", |
127 | | - data, |
128 | | - ["$.book[0].price", "$.book[2].price"], |
129 | | - ), |
130 | | - TestCase('$.book[?(@.category=="reference")].category', data, ["$.book[0].category"]), |
131 | | - TestCase( |
132 | | - '$.book[?(@.category!="reference" and @.price<9)].title', |
133 | | - data, |
134 | | - ["$.book[2].title"], |
135 | | - ), |
136 | | - TestCase( |
137 | | - '$.book[?(@.author=="Herman Melville" or @.author=="Evelyn Waugh")].author', |
138 | | - data, |
139 | | - ["$.book[1].author", "$.book[2].author"], |
140 | | - ), |
141 | | - # sort |
142 | | - TestCase( |
143 | | - "$.book[/(price)].price", |
144 | | - data, |
145 | | - ["$.book[0].price", "$.book[2].price", "$.book[1].price", "$.book[3].price"], |
146 | | - ), |
147 | | - TestCase( |
148 | | - "$.book[/(~price)].price", |
149 | | - data, |
150 | | - ["$.book[3].price", "$.book[1].price", "$.book[2].price", "$.book[0].price"], |
151 | | - ), |
152 | | - TestCase( |
153 | | - "$.book[/(category,price)].price", |
154 | | - data, |
155 | | - ["$.book[2].price", "$.book[1].price", "$.book[3].price", "$.book[0].price"], |
156 | | - ), |
157 | | - TestCase( |
158 | | - "$.book[/(brand.version)].brand.version", |
159 | | - data, |
160 | | - [ |
161 | | - "$.book[1].brand.version", |
162 | | - "$.book[0].brand.version", |
163 | | - "$.book[2].brand.version", |
164 | | - "$.book[3].brand.version", |
165 | | - ], |
166 | | - ), |
167 | | - TestCase( |
168 | | - "$.scores[/(score)].score", |
169 | | - data, |
170 | | - [ |
171 | | - "$.scores.chinese.score", |
172 | | - "$.scores.chemistry.score", |
173 | | - "$.scores.physic.score", |
174 | | - "$.scores.english.score", |
175 | | - "$.scores.math.score", |
176 | | - ], |
177 | | - ), |
178 | | - ] |
179 | | -) |
180 | | -def path_cases(request): |
181 | | - return request.param |
| 15 | +@pytest.fixture(scope="session") |
| 16 | +def large_data(): |
| 17 | + """Large test data for performance tests.""" |
| 18 | + with open("tests/data/1.json") as f: |
| 19 | + return json.load(f) |
0 commit comments