-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjson_path_test.clj
More file actions
133 lines (129 loc) · 4.53 KB
/
json_path_test.clj
File metadata and controls
133 lines (129 loc) · 4.53 KB
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
(ns json-path.test.json-path-test
[:use
[json-path]
[midje.sweet]])
(unfinished)
(def keys-of-many-types
[{:key 1}
{:key 3}
{:key "nice"}
{:key true}
{:key nil}
{:key false}
{:key {}}
{:key []}
{:key -1}
{:key 0}
{:key ""}])
(facts
(at-path "$" ...json...) => ...json...
(at-path "$.hello" {:hello "world"}) => "world"
(at-path "$.hello.world" {:hello {:world "foo"}}) => "foo"
(at-path "$..world" {:hello {:world "foo"},
:baz {:world "bar",
:quuz {:world "zux"}}}) => ["foo", "bar", "zux"]
(at-path "$.*.world" {:a {:world "foo"},
:b {:world "bar",
:c {:world "baz"}}}) => ["foo", "bar"]
(at-path "$.foo[*]" {:foo ["a", "b", "c"]}) => ["a", "b", "c"]
(at-path "$[*]" {:foo 1 :bar [2 3]}) => [1 [2 3]]
(at-path "$..*" {:foo 1 :bar [2 3]}) => [1 [2 3] 2 3]
(at-path "$[-2]" [1 2 3]) => 2
(at-path "$[?(@.bar<2)]" [{:bar 1} {:bar 2}]) => [{:bar 1}]
(at-path "$.foo[?(@.bar=\"baz\")].hello"
{:foo [{:bar "wrong" :hello "goodbye"}
{:bar "baz" :hello "world"}]}) => ["world"]
(at-path "$.foo[?(@.id=$.id)].text"
{:id 45, :foo [{:id 12, :text "bar"},
{:id 45, :text "hello"}]}) => ["hello"]
(at-path "$.foo[*].bar[*].baz"
{:foo [{:bar [{:baz "hello"}]}]}) => ["hello"]
;; Filter expression with boolean and operator
(at-path "$[?(@.key>42 && @.key<44)]"
[{:key 42}, {:key 43}, {:key 44}]) => [{:key 43}]
;; Filter expression with boolean and operator and value false
(at-path "$[?(@.key>0 && false)]"
keys-of-many-types) => []
;; Filter expression with boolean and operator and value true
(at-path "$[?(@.key>0 && true)]"
keys-of-many-types) => [{:key 1}, {:key 3}]
;; Filter expression with boolean or operator
(at-path "$[?(@.key>43 || @.key<43)]"
[{:key 42}, {:key 43}, {:key 44}]) => [{:key 42}, {:key 44}]
;; Filter expression with boolean or operator and value false
(at-path "$[?(@.key>0 || false)]"
keys-of-many-types) => [{:key 1}, {:key 3}]
;; Filter expression with boolean or operator and value true
(at-path "$[?(@.key>0 || true)]"
keys-of-many-types) => keys-of-many-types
;; Filter expression with value true
(at-path "$[?(@.key)]"
[{:some "some value"}
{:key true}
{:key false}
{:key nil}
{:key "value"}
{:key ""}
{:key 0}
{:key 1}
{:key -1}
{:key 42}
{:key {}}
{:key []}])
=> [{:key true}
{:key false}
{:key "value"}
{:key ""}
{:key 0}
{:key 1}
{:key -1}
{:key 42}
{:key {}}
{:key []}])
;; TODO: Filter expression with different grouped operators
;; NOTE: the parser will need to be updated in order to get this test to work
; (at-path "$[?(@.a && (@.b || @.c))]"
; [{:a true}
; {:a true, :b true}
; {:a true, :b true, :c true}
; {:b true, :c true}
; {:a true, :c true}
; {:c true}
; {:b true}])
; => [{:a true, :b true}
; {:a true, :b true, :c true}
; {:a true, :c true}])
(facts
(-> (query "$.hello"
{:hello "world"})
:path) => [:hello]
(-> (query "$"
{:hello "world"})
:path) => []
(->> (query "$..world" {:baz {:world "bar",
:quuz {:world "zux"}}})
(map :value)) => '("bar" "zux")
(->> (query "$..world" {:baz {:world "bar",
:quuz {:world "zux"}}})
(map :path)) => '([:baz :world] [:baz :quuz :world])
(->> (query "$.foo[*]" {:foo ["a", "b", "c"]})
(map :path)) => '([:foo 0] [:foo 1] [:foo 2])
(-> (query "$.hello"
{:hello "world"})
:value) => "world"
(-> (query "$.hello/world"
{:hello/world "foo"})
:value) => "foo"
(-> (query "$.hello/world.world/name"
{:hello/world {:world/name "earth"}})
:value) => "earth"
(->> (query "$.foo[?(@.bar=\"baz\")].hello"
{:foo [{:bar "wrong" :hello "goodbye"}
{:bar "baz" :hello "world"}]})
(map :path)) => '([:foo 1 :hello])
(->> (query "$[?(@.key>42 && @.key<44)]"
[{:key 42}, {:key 43}, {:key 44}])
(map :value)) => [{:key 43}]
(->> (query "$[?(@.key>43 || @.key<43)]"
[{:key 42}, {:key 43}, {:key 44}])
(map :value)) => [{:key 42}, {:key 44}])