Skip to content

Commit 9d0dff9

Browse files
committed
Update regression test. Support unordered results
1 parent 5599357 commit 9d0dff9

File tree

3 files changed

+101
-24
lines changed

3 files changed

+101
-24
lines changed

project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
:dependencies [[org.clojure/clojure "1.10.1"]]
66
:profiles {:dev {:dependencies [[midje "1.9.6"]
77
[org.flatland/ordered "1.5.7"] ;; https://github.com/owainlewis/yaml/issues/28
8+
[org.clojure/data.json "0.2.7"]
89
[io.forward/yaml "1.0.9"]]
910
:plugins [[lein-midje "3.2.1"]]}})

test/Clojure_json-path.yaml

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ queries:
1818
document: {"a": "first", "2": "second", "b": "third"}
1919
result: "second"
2020
status: pass
21+
- id: array_index_first
22+
selector: $[0]
23+
document: ["first", "second", "third", "forth", "fifth"]
24+
result: "first"
25+
status: pass
2126
- id: array_index_last
2227
selector: $[-1]
2328
document: ["first", "second", "third"]
2429
result: "third"
25-
status: open
30+
status: pass
2631
- id: array_index_slice_end_out_of_bounds
2732
selector: $[1:10]
2833
document: ["first", "second", "third"]
@@ -89,12 +94,14 @@ queries:
8994
selector: $[0:4:2]
9095
document: ["first", "second", "third", "forth", "fifth"]
9196
result: "first"
92-
status: open
97+
status: fail
98+
consensus: ["first", "third"]
9399
- id: array_index_slice_start_equals_end
94100
selector: $[0:0]
95101
document: ["first", "second"]
96102
result: "first"
97-
status: open
103+
status: fail
104+
consensus: []
98105
- id: array_index_slice_start_plus_one
99106
selector: $[0:1]
100107
document: ["first", "second"]
@@ -113,6 +120,17 @@ queries:
113120
result: "first"
114121
status: fail
115122
consensus: ["first", "second"]
123+
- id: filter_current_object_with_value
124+
selector: $[?(@)]
125+
document: ["some value", null, "value"]
126+
result: ["some value", "value"]
127+
status: open
128+
- id: filter_equal
129+
selector: $[?(@.key==42)]
130+
document: [{"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"some": "value"}]
131+
result: [{"some": "value"}]
132+
status: fail
133+
consensus: [{"key": 42}]
116134
- id: filter_greater_than
117135
selector: $[?(@.key>42)]
118136
document: [{"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}]
@@ -135,11 +153,31 @@ queries:
135153
selector: $[?(@.key<=42)]
136154
document: [{"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"some": "value"}]
137155
status: error
156+
- id: filter_null
157+
selector: $[?(@.key==null)]
158+
document: [{"some": "some value"}, {"key": null}, {"key": "value"}]
159+
result: [{"some": "some value"}, {"key": null}]
160+
status: open
161+
- id: filter_with_boolean_operation
162+
selector: $[?(@.key>42 && @.key<44)]
163+
document: [{"key": 42}, {"key": 43}, {"key": 44}]
164+
result: [{"key": 43}, {"key": 44}]
165+
status: open
138166
- id: filter_with_value
139167
selector: $[?(@.key)]
140168
document: [{"some": "some value"}, {"key": "value"}]
141169
result: [{"key": "value"}]
142170
status: pass
171+
- id: filter_with_value_on_null
172+
selector: $[?(@.key)]
173+
document: [{"some": "some value"}, {"key": null}, {"key": "value"}]
174+
result: [{"key": "value"}]
175+
status: open
176+
- id: filter_without_value
177+
selector: $[?(!@.key)]
178+
document: [{"some": "some value"}, {"key": null}, {"key": "value"}]
179+
result: [{"key": "value"}]
180+
status: open
143181
- id: key_bracket_notation
144182
selector: $['key']
145183
document: {"key": "value"}
@@ -149,10 +187,34 @@ queries:
149187
selector: $['key','another']
150188
document: {"key": "value", "another": "entry"}
151189
status: error
190+
- id: key_bracket_notation_union_with_key_on_nested_objects
191+
selector: $['one','three'].key
192+
document: {"one": {"key": "value"}, "two": {"k": "v"}, "three": {"some": "more", "key": "other value"}}
193+
status: error
194+
- id: key_bracket_notation_with_at_literal
195+
selector: $['@']
196+
document: {"@": "value", "another": "entry"}
197+
status: error
198+
consensus: "value"
199+
- id: key_bracket_notation_with_dollar_literal
200+
selector: $['$']
201+
document: {"$": "value", "another": "entry"}
202+
status: error
203+
consensus: "value"
204+
- id: key_bracket_notation_with_dot
205+
selector: $['two.some']
206+
document: {"one": {"key": "value"}, "two": {"some": "more", "key": "other value"}, "two.some": "42"}
207+
status: error
208+
consensus: ["42"]
209+
- id: key_bracket_notation_with_dot_without_quotes
210+
selector: $[two.some]
211+
document: {"one": {"key": "value"}, "two": {"some": "more", "key": "other value"}, "two.some": "42"}
212+
status: error
152213
- id: key_bracket_notation_with_double_quotes
153214
selector: $["key"]
154215
document: {"key": "value"}
155216
status: error
217+
consensus: "value"
156218
- id: key_bracket_notation_with_number
157219
selector: $['0']
158220
document: {"0": "value"}
@@ -177,8 +239,8 @@ queries:
177239
consensus: "value"
178240
- id: key_bracket_notation_with_star_literal
179241
selector: $['*']
180-
document: {"*": "value"}
181-
result: ["value"]
242+
document: {"*": "value", "another": "entry"}
243+
result: ["value", "entry"]
182244
status: fail
183245
consensus: "value"
184246
- id: key_bracket_notation_without_quotes
@@ -189,7 +251,6 @@ queries:
189251
selector: $.['key']
190252
document: {"key": "value"}
191253
status: error
192-
consensus: "value"
193254
- id: key_dot_bracket_notation_with_double_quotes
194255
selector: $.["key"]
195256
document: {"key": "value"}
@@ -230,27 +291,32 @@ queries:
230291
- id: recursive_key
231292
selector: $..key
232293
document: {"object": {"key": "value", "array": [{"key": "something"}, {"key": {"key": "russian dolls"}}]}, "key": "top"}
233-
result: ["top", "value", "something", {"key": "russian dolls"}, "russian dolls"]
294+
ordered: false
295+
result: ["russian dolls", "something", "top", "value", {"key": "russian dolls"}]
234296
status: pass
235297
- id: recursive_key_with_double_quotes
236298
selector: $.."key"
237299
document: {"object": {"key": "value", "array": [{"key": "something"}, {"key": {"key": "russian dolls"}}]}, "key": "top"}
300+
ordered: false
238301
result: []
239302
status: open
240303
- id: recursive_key_with_single_quotes
241304
selector: $..'key'
242305
document: {"object": {"key": "value", "array": [{"key": "something"}, {"key": {"key": "russian dolls"}}]}, "key": "top"}
243-
result: ["top", "value", "something", {"key": "russian dolls"}, "russian dolls"]
306+
ordered: false
307+
result: ["russian dolls", "something", "top", "value", {"key": "russian dolls"}]
244308
status: open
245309
- id: recursive_on_nested_object
246310
selector: $.store..price
247311
document: {"store": {"book": [{"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95}, {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99}, {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99}, {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}], "bicycle": {"color": "red", "price": 19.95}}}
248-
result: [8.95, 12.99, 8.99, 22.99, 19.95]
312+
ordered: false
313+
result: [12.99, 19.95, 22.99, 8.95, 8.99]
249314
status: pass
250315
- id: recursive_wildcard
251316
selector: $..*
252317
document: {"key": "value", "another key": {"complex": "string", "primitives": [0, 1]}}
253-
result: ["value", {"complex": "string", "primitives": [0, 1]}, "string", [0, 1], 0, 1]
318+
ordered: false
319+
result: ["string", "value", 0, 1, [0, 1], {"complex": "string", "primitives": [0, 1]}]
254320
status: pass
255321
- id: recursive_wildcard_on_null_value_array
256322
selector: $..*
@@ -285,7 +351,8 @@ queries:
285351
- id: wildcard_bracket_notation_on_object
286352
selector: $[*]
287353
document: {"some": "string", "int": 42, "object": {"key": "value"}, "array": [0, 1]}
288-
result: ["string", 42, {"key": "value"}, [0, 1]]
354+
ordered: false
355+
result: ["string", 42, [0, 1], {"key": "value"}]
289356
status: pass
290357
- id: wildcard_bracket_notation_with_key_on_nested_objects
291358
selector: $[*].bar[*].baz
@@ -300,5 +367,6 @@ queries:
300367
- id: wildcard_dot_notation_on_object
301368
selector: $.*
302369
document: {"some": "string", "int": 42, "object": {"key": "value"}, "array": [0, 1]}
303-
result: ["string", 42, {"key": "value"}, [0, 1]]
370+
ordered: false
371+
result: ["string", 42, [0, 1], {"key": "value"}]
304372
status: pass

test/json_path/test/regression_test.clj

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns json-path.test.regression-test
22
(:use [json-path])
33
(:require [yaml.core :as yaml]
4+
[clojure.data.json :as json]
45
[clojure
56
[test :refer :all]]))
67

@@ -10,24 +11,28 @@
1011
(deftest regression
1112
(->> (queries_from_suite "test/Clojure_json-path.yaml")
1213
(filter (fn [{status :status}] (= status "pass")))
13-
(map (fn [{:keys [selector document result id]}]
14-
(testing id
15-
(is (= result
16-
(json-path/at-path selector document))))))
14+
(map (fn [{:keys [selector document result id ordered]}]
15+
(let [current (json-path/at-path selector document)
16+
current-reordered (if (= ordered false)
17+
(sort-by json/write-str current)
18+
current)]
19+
(testing id
20+
(is (= result
21+
current-reordered))))))
1722
doall))
1823

19-
(defn- report-change [current {:keys [selector document result status consensus id]}]
24+
(defn- report-change [current-reordered {:keys [selector document result status consensus id]}]
2025
(println
2126
(format "Warning: implementation has changed for %s: %s (previous status %s)"
2227
id selector status))
2328
(when (or (nil? consensus)
24-
(not= consensus current))
29+
(not= consensus current-reordered))
2530
(when-not (= status "error")
26-
(println (format " was %s" (pr-str current))))
27-
(println (format " now is %s" (pr-str current))))
31+
(println (format " was %s" (pr-str result))))
32+
(println (format " now is %s" (pr-str current-reordered))))
2833
(when-not (nil? consensus)
2934
(if (= consensus
30-
current)
35+
current-reordered)
3136
(println " now matching consensus")
3237
(println (format " but should be %s" (pr-str consensus))))))
3338

@@ -37,12 +42,15 @@
3742
(deftest warning-on-changes-for-non-conforming-queries-based-on-consensus
3843
(->> (queries_from_suite "test/Clojure_json-path.yaml")
3944
(filter (fn [{status :status}] (not= status "pass")))
40-
(map (fn [{:keys [selector document result status consensus id] :as query}]
45+
(map (fn [{:keys [selector document result status consensus id ordered] :as query}]
4146
(testing id
4247
(try
43-
(let [current (doall (json-path/at-path selector document))]
48+
(let [current (doall (json-path/at-path selector document))
49+
current-reordered (if (= ordered false)
50+
(sort-by json/write-str current)
51+
current)]
4452
(when (or (= "error" status)
45-
(not= result current))
53+
(not= result current-reordered))
4654
(report-change current query)))
4755
(catch Exception e
4856
(when (not= "error"

0 commit comments

Comments
 (0)