1 #[macro_use]
2 extern crate serde_json;
3
4 use common::{read_json, select_and_then_compare, setup};
5
6 mod common;
7
8 #[test]
op_object_eq()9 fn op_object_eq() {
10 setup();
11
12 select_and_then_compare(
13 "$.school[?(@.friends == @.friends)]",
14 read_json("./benchmark/data_obj.json"),
15 json!([{
16 "friends": [
17 {"id": 0, "name": "Millicent Norman"},
18 {"id": 1, "name": "Vincent Cannon" },
19 {"id": 2, "name": "Gray Berry"}
20 ]
21 }]),
22 );
23 }
24
25 #[test]
op_object_ge()26 fn op_object_ge() {
27 setup();
28
29 select_and_then_compare(
30 "$.friends[?(@.id >= 2)]",
31 read_json("./benchmark/data_obj.json"),
32 json!([
33 { "id" : 2, "name" : "Gray Berry" }
34 ]),
35 );
36 }
37
38 #[test]
op_object_or_default()39 fn op_object_or_default() {
40 setup();
41
42 select_and_then_compare(
43 "$.friends[?(@.id >= 2 || @.id == 1)]",
44 read_json("./benchmark/data_obj.json"),
45 json!([
46 { "id" : 2, "name" : "Gray Berry" },
47 { "id" : 1, "name" : "Vincent Cannon" }
48 ]),
49 );
50 }
51
52 #[test]
op_object_and_or()53 fn op_object_and_or() {
54 setup();
55
56 select_and_then_compare(
57 "$.friends[?( (@.id >= 2 || @.id == 1) && @.id == 0)]",
58 read_json("./benchmark/data_obj.json"),
59 json!([]),
60 );
61 }
62
63 #[test]
op_result_type()64 fn op_result_type() {
65 setup();
66
67 select_and_then_compare(
68 "$..friends[?(@.id == $.index)].id",
69 read_json("./benchmark/data_obj.json"),
70 json!([0, 0]),
71 );
72 }
73
74 #[test]
op_absolute_path_result_type()75 fn op_absolute_path_result_type() {
76 setup();
77
78 select_and_then_compare(
79 "$..book[?($.store.bicycle.price < @.price)].price",
80 read_json("./benchmark/example.json"),
81 json!([22.99]),
82 );
83 }
84
85 #[test]
op_complicated()86 fn op_complicated() {
87 setup();
88
89 select_and_then_compare(
90 "$..book[?( (@.price == 12.99 || @.category == 'reference') && @.price > 10)].price",
91 read_json("./benchmark/example.json"),
92 json!([12.99]),
93 );
94 }
95
96 #[test]
op_gt()97 fn op_gt() {
98 setup();
99
100 select_and_then_compare(
101 "$..[?(@.age > 40)]",
102 json!([
103 { "name": "이름1", "age": 40, "phone": "+33 12341234" },
104 { "name": "이름2", "age": 42, "phone": "++44 12341234" }
105 ]),
106 json!([
107 { "name" : "이름2", "age" : 42, "phone" : "++44 12341234" }
108 ]),
109 );
110 }
111
112 #[test]
op_ge()113 fn op_ge() {
114 setup();
115
116 select_and_then_compare(
117 "$..[?(@.age >= 30)]",
118 json!({
119 "school": {
120 "friends": [
121 {"name": "친구1", "age": 20},
122 {"name": "친구2", "age": 20}
123 ]
124 },
125 "friends": [
126 {"name": "친구3", "age": 30},
127 {"name": "친구4"}
128 ]}),
129 json!([
130 { "name" : "친구3", "age" : 30 }
131 ]),
132 );
133 }
134
135 #[test]
op_eq_for_number()136 fn op_eq_for_number() {
137 setup();
138
139 select_and_then_compare("$.[?(@.a == 1)]", json!({ "a": 1 }), json!([{ "a": 1 }]));
140 }
141
142 #[test]
op_ne_for_number()143 fn op_ne_for_number() {
144 setup();
145
146 select_and_then_compare("$.[?(@.a != 2)]", json!({ "a": 1 }), json!([{ "a": 1 }]));
147 }
148
149 #[test]
op_lt_for_number()150 fn op_lt_for_number() {
151 setup();
152
153 select_and_then_compare("$.[?(@.a < 2)]", json!({ "a": 1 }), json!([{ "a": 1 }]));
154 }
155
156 #[test]
op_le_for_number()157 fn op_le_for_number() {
158 setup();
159
160 select_and_then_compare("$.[?(@.a <= 1)]", json!({ "a": 1 }), json!([{ "a": 1 }]));
161 }
162
163 #[test]
op_gt_for_number()164 fn op_gt_for_number() {
165 setup();
166
167 select_and_then_compare("$.[?(@.a > 0)]", json!({ "a": 1 }), json!([{ "a": 1 }]));
168 }
169
170 #[test]
op_ge_for_number()171 fn op_ge_for_number() {
172 setup();
173
174 select_and_then_compare("$.[?(@.a >= 0)]", json!({ "a": 1 }), json!([{ "a": 1 }]));
175 }
176
177
178
179 #[test]
op_eq_for_string_value()180 fn op_eq_for_string_value() {
181 setup();
182
183 select_and_then_compare(
184 r#"$.[?(@.a == "b")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]),
185 );
186 }
187
188 #[test]
op_ne_for_string_value()189 fn op_ne_for_string_value() {
190 setup();
191
192 select_and_then_compare(
193 r#"$.[?(@.a != "c")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]),
194 );
195
196 }
197
198 #[test]
op_lt_for_string_value()199 fn op_lt_for_string_value() {
200 setup();
201
202 select_and_then_compare(
203 r#"$.[?(@.a < "b")]"#, json!({ "a": "b" }), json!([]),
204 );
205 }
206
207 #[test]
op_le_for_string_value()208 fn op_le_for_string_value() {
209 setup();
210
211 select_and_then_compare(
212 r#"$.[?(@.a <= "b")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]),
213 );
214 }
215
216 #[test]
op_gt_for_string_value()217 fn op_gt_for_string_value() {
218 setup();
219
220 select_and_then_compare(
221 r#"$.[?(@.a > "b")]"#, json!({ "a": "b" }), json!([]),
222 );
223 }
224
225 #[test]
op_ge_for_string_value()226 fn op_ge_for_string_value() {
227 setup();
228
229 select_and_then_compare(
230 r#"$.[?(@.a >= "b")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]),
231 );
232 }
233
234 #[test]
op_eq_for_object_value()235 fn op_eq_for_object_value() {
236 setup();
237
238 select_and_then_compare(
239 r#"$.[?(@.a == @.c)]"#,
240 json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}),
241 json!([{"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}]),
242 );
243 }
244
245 #[test]
op_ne_for_object_value()246 fn op_ne_for_object_value() {
247 setup();
248
249 select_and_then_compare(
250 r#"$.[?(@.a != @.c)]"#,
251 json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}),
252 json!([]),
253 );
254 }
255
256 #[test]
op_lt_for_object_value()257 fn op_lt_for_object_value() {
258 setup();
259
260 select_and_then_compare(
261 r#"$.[?(@.a < @.c)]"#,
262 json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}),
263 json!([]),
264 );
265 }
266
267 #[test]
op_le_for_object_value()268 fn op_le_for_object_value() {
269 setup();
270
271 select_and_then_compare(
272 r#"$.[?(@.a <= @.c)]"#,
273 json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}),
274 json!([]),
275 );
276 }
277
278 #[test]
op_gt_for_object_value()279 fn op_gt_for_object_value() {
280 setup();
281
282 select_and_then_compare(
283 r#"$.[?(@.a > @.c)]"#,
284 json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}),
285 json!([]),
286 );
287 }
288
289 #[test]
op_ge_for_object_value()290 fn op_ge_for_object_value() {
291 setup();
292
293 select_and_then_compare(
294 r#"$.[?(@.a >= @.c)]"#,
295 json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}),
296 json!([]),
297 );
298 }
299
300 #[test]
op_eq_for_complex_value()301 fn op_eq_for_complex_value() {
302 setup();
303
304 select_and_then_compare(
305 r#"$.[?(1 == @.a)]"#,
306 json!({ "a": { "b": 1 } }),
307 json!([]),
308 );
309 }
310
311 #[test]
op_ne_for_complex_value()312 fn op_ne_for_complex_value() {
313 setup();
314
315 select_and_then_compare(
316 r#"$.[?("1" != @.a)]"#,
317 json!({ "a": { "b": 1 } }),
318 json!([]),
319 );
320 }
321
322 #[test]
op_le_for_complex_value()323 fn op_le_for_complex_value() {
324 setup();
325
326 select_and_then_compare(
327 r#"$.[?(@.a <= 1)]"#,
328 json!({ "a": { "b": 1 } }),
329 json!([]),
330 );
331 }
332
333 #[test]
op_gt_for_complex_value()334 fn op_gt_for_complex_value() {
335 setup();
336
337 select_and_then_compare(
338 r#"$.[?(@.a > "1")]"#,
339 json!({ "a": { "b": 1 } }),
340 json!([]),
341 );
342 }
343
344 #[test]
op_compare_different_types()345 fn op_compare_different_types() {
346 setup();
347
348 for path in [
349 r#"$[?("1" == 1)]"#,
350 r#"$[?(1 == "1")]"#,
351 r#"$[?(true == 1)]"#,
352 r#"$[?(@ == 1)]"#,
353 ]
354 .iter()
355 {
356 select_and_then_compare(path, json!({}), json!([]));
357 }
358 }
359
360 #[test]
op_for_same_type()361 fn op_for_same_type() {
362 setup();
363
364 select_and_then_compare(
365 r#"$..[?(@.a == 1)]"#,
366 json!({
367 "a": 1,
368 "b" : {"a": 1},
369 "c" : {"a": 1}
370 }),
371 json!([
372 {"a": 1},
373 {"a": 1}
374 ]),
375 );
376 }