Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ad16a76

Browse files
author
Nikita Glukhov
committed
Add json tests for jsonpath extensions
1 parent d1ae7d5 commit ad16a76

File tree

2 files changed

+323
-8
lines changed

2 files changed

+323
-8
lines changed

src/test/regress/expected/json_jsonpath.out

Lines changed: 265 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,32 @@ select json '[1]' @? 'strict $[1.2]';
172172

173173
(1 row)
174174

175+
select json_path_query('[1]', 'strict $[1.2]');
176+
ERROR: jsonpath array subscript is out of bounds
177+
select json_path_query('{}', 'strict $[0.3]');
178+
ERROR: object subscript must be a string or number
179+
select json '{}' @? 'lax $[0.3]';
180+
?column?
181+
----------
182+
t
183+
(1 row)
184+
185+
select json_path_query('{}', 'strict $[1.2]');
186+
ERROR: object subscript must be a string or number
187+
select json '{}' @? 'lax $[1.2]';
188+
?column?
189+
----------
190+
f
191+
(1 row)
192+
193+
select json_path_query('{}', 'strict $[-2 to 3]');
194+
ERROR: jsonpath array accessor can only be applied to an array
195+
select json '{}' @? 'lax $[-2 to 3]';
196+
?column?
197+
----------
198+
t
199+
(1 row)
200+
175201
select json '{"a": [1,2,3], "b": [3,4,5]}' @? '$ ? (@.a[*] > @.b[*])';
176202
?column?
177203
----------
@@ -244,7 +270,7 @@ select json_path_query('{}', 'lax $.a');
244270
select json_path_query('{}', 'strict $.a');
245271
ERROR: JSON object does not contain key "a"
246272
select json_path_query('1', 'strict $[1]');
247-
ERROR: jsonpath array accessor can only be applied to an array
273+
ERROR: jsonpath array accessor can only be applied to an array or object
248274
select json_path_query('1', 'strict $[*]');
249275
ERROR: jsonpath wildcard array accessor can only be applied to an array
250276
select json_path_query('[]', 'strict $[1]');
@@ -339,6 +365,12 @@ select json_path_query('1', 'lax $[*]');
339365
1
340366
(1 row)
341367

368+
select json_path_query('{}', 'lax $[0]');
369+
json_path_query
370+
-----------------
371+
{}
372+
(1 row)
373+
342374
select json_path_query('[1]', 'lax $[0]');
343375
json_path_query
344376
-----------------
@@ -379,6 +411,12 @@ select json_path_query('[1]', '$[last]');
379411
1
380412
(1 row)
381413

414+
select json_path_query('{}', '$[last]');
415+
json_path_query
416+
-----------------
417+
{}
418+
(1 row)
419+
382420
select json_path_query('[1,2,3]', '$[last]');
383421
json_path_query
384422
-----------------
@@ -1438,20 +1476,18 @@ select json_path_query('[null, 1, "abc", "abd", "aBdC", "abdacb", "adc\nabc", "b
14381476
(2 rows)
14391477

14401478
select json_path_query('null', '$.datetime()');
1441-
ERROR: jsonpath item method .datetime() can only be applied to a string value
1479+
ERROR: jsonpath item method .datetime() can only be applied to a string or number
14421480
select json_path_query('true', '$.datetime()');
1443-
ERROR: jsonpath item method .datetime() can only be applied to a string value
1444-
select json_path_query('1', '$.datetime()');
1445-
ERROR: jsonpath item method .datetime() can only be applied to a string value
1481+
ERROR: jsonpath item method .datetime() can only be applied to a string or number
14461482
select json_path_query('[]', '$.datetime()');
14471483
json_path_query
14481484
-----------------
14491485
(0 rows)
14501486

14511487
select json_path_query('[]', 'strict $.datetime()');
1452-
ERROR: jsonpath item method .datetime() can only be applied to a string value
1488+
ERROR: jsonpath item method .datetime() can only be applied to a string or number
14531489
select json_path_query('{}', '$.datetime()');
1454-
ERROR: jsonpath item method .datetime() can only be applied to a string value
1490+
ERROR: jsonpath item method .datetime() can only be applied to a string or number
14551491
select json_path_query('""', '$.datetime()');
14561492
ERROR: unrecognized datetime format
14571493
HINT: use datetime template argument for explicit format specification
@@ -1488,6 +1524,25 @@ ERROR: timezone argument of jsonpath item method .datetime() is out of integer
14881524
select json_path_query('"aaaa"', '$.datetime("HH24")');
14891525
ERROR: invalid value "aa" for "HH24"
14901526
DETAIL: Value must be an integer.
1527+
-- Standard extension: UNIX epoch to timestamptz
1528+
select json_path_query('0', '$.datetime()');
1529+
json_path_query
1530+
-----------------------------
1531+
"1970-01-01T00:00:00+00:00"
1532+
(1 row)
1533+
1534+
select json_path_query('0', '$.datetime().type()');
1535+
json_path_query
1536+
----------------------------
1537+
"timestamp with time zone"
1538+
(1 row)
1539+
1540+
select json_path_query('1490216035.5', '$.datetime()');
1541+
json_path_query
1542+
-------------------------------
1543+
"2017-03-22T20:53:55.5+00:00"
1544+
(1 row)
1545+
14911546
select json '"10-03-2017"' @? '$.datetime("dd-mm-yyyy")';
14921547
?column?
14931548
----------
@@ -1975,6 +2030,12 @@ SELECT json_path_query('[{"a": 1}, {"a": 2}]', '$[*] ? (@.a > 10)');
19752030
-----------------
19762031
(0 rows)
19772032

2033+
SELECT json_path_query('[{"a": 1}, {"a": 2}]', '[$[*].a]');
2034+
json_path_query
2035+
-----------------
2036+
[1, 2]
2037+
(1 row)
2038+
19782039
SELECT json_path_query_array('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a');
19792040
ERROR: JSON object does not contain key "a"
19802041
SELECT json_path_query_array('[{"a": 1}, {"a": 2}]', '$[*].a');
@@ -2007,6 +2068,12 @@ SELECT json_path_query_array('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*].a
20072068
[]
20082069
(1 row)
20092070

2071+
SELECT json_path_query_array('[{"a": 1}, {"a": 2}]', '[$[*].a]');
2072+
json_path_query_array
2073+
-----------------------
2074+
[[1, 2]]
2075+
(1 row)
2076+
20102077
SELECT json_path_query_first('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a');
20112078
ERROR: JSON object does not contain key "a"
20122079
SELECT json_path_query_first('[{"a": 1}, {"a": 2}]', '$[*].a');
@@ -2075,3 +2142,194 @@ SELECT json '[{"a": 1}, {"a": 2}]' @@ '$[*].a > 2';
20752142
f
20762143
(1 row)
20772144

2145+
-- extension: path sequences
2146+
select json_path_query('[1,2,3,4,5]', '10, 20, $[*], 30');
2147+
json_path_query
2148+
-----------------
2149+
10
2150+
20
2151+
1
2152+
2
2153+
3
2154+
4
2155+
5
2156+
30
2157+
(8 rows)
2158+
2159+
select json_path_query('[1,2,3,4,5]', 'lax 10, 20, $[*].a, 30');
2160+
json_path_query
2161+
-----------------
2162+
10
2163+
20
2164+
30
2165+
(3 rows)
2166+
2167+
select json_path_query('[1,2,3,4,5]', 'strict 10, 20, $[*].a, 30');
2168+
ERROR: jsonpath member accessor can only be applied to an object
2169+
select json_path_query('[1,2,3,4,5]', '-(10, 20, $[1 to 3], 30)');
2170+
json_path_query
2171+
-----------------
2172+
-10
2173+
-20
2174+
-2
2175+
-3
2176+
-4
2177+
-30
2178+
(6 rows)
2179+
2180+
select json_path_query('[1,2,3,4,5]', 'lax (10, 20.5, $[1 to 3], "30").double()');
2181+
json_path_query
2182+
-----------------
2183+
10
2184+
20.5
2185+
2
2186+
3
2187+
4
2188+
30
2189+
(6 rows)
2190+
2191+
select json_path_query('[1,2,3,4,5]', '$[(0, $[*], 5) ? (@ == 3)]');
2192+
json_path_query
2193+
-----------------
2194+
4
2195+
(1 row)
2196+
2197+
select json_path_query('[1,2,3,4,5]', '$[(0, $[*], 3) ? (@ == 3)]');
2198+
ERROR: jsonpath array subscript is not a single numeric value
2199+
-- extension: array constructors
2200+
select json_path_query('[1, 2, 3]', '[]');
2201+
json_path_query
2202+
-----------------
2203+
[]
2204+
(1 row)
2205+
2206+
select json_path_query('[1, 2, 3]', '[1, 2, $[*], 4, 5]');
2207+
json_path_query
2208+
-----------------------
2209+
[1, 2, 1, 2, 3, 4, 5]
2210+
(1 row)
2211+
2212+
select json_path_query('[1, 2, 3]', '[1, 2, $[*], 4, 5][*]');
2213+
json_path_query
2214+
-----------------
2215+
1
2216+
2
2217+
1
2218+
2
2219+
3
2220+
4
2221+
5
2222+
(7 rows)
2223+
2224+
select json_path_query('[1, 2, 3]', '[(1, (2, $[*])), (4, 5)]');
2225+
json_path_query
2226+
-----------------------
2227+
[1, 2, 1, 2, 3, 4, 5]
2228+
(1 row)
2229+
2230+
select json_path_query('[1, 2, 3]', '[[1, 2], [$[*], 4], 5, [(1,2)?(@ > 5)]]');
2231+
json_path_query
2232+
-------------------------------
2233+
[[1, 2], [1, 2, 3, 4], 5, []]
2234+
(1 row)
2235+
2236+
select json_path_query('[1, 2, 3]', 'strict [1, 2, $[*].a, 4, 5]');
2237+
ERROR: jsonpath member accessor can only be applied to an object
2238+
select json_path_query('[[1, 2], [3, 4, 5], [], [6, 7]]', '[$[*][*] ? (@ > 3)]');
2239+
json_path_query
2240+
-----------------
2241+
[4, 5, 6, 7]
2242+
(1 row)
2243+
2244+
-- extension: object constructors
2245+
select json_path_query('[1, 2, 3]', '{}');
2246+
json_path_query
2247+
-----------------
2248+
{}
2249+
(1 row)
2250+
2251+
select json_path_query('[1, 2, 3]', '{a: 2 + 3, "b": [$[*], 4, 5]}');
2252+
json_path_query
2253+
--------------------------------
2254+
{"a": 5, "b": [1, 2, 3, 4, 5]}
2255+
(1 row)
2256+
2257+
select json_path_query('[1, 2, 3]', '{a: 2 + 3, "b": [$[*], 4, 5]}.*');
2258+
json_path_query
2259+
-----------------
2260+
5
2261+
[1, 2, 3, 4, 5]
2262+
(2 rows)
2263+
2264+
select json_path_query('[1, 2, 3]', '{a: 2 + 3, "b": [$[*], 4, 5]}[*]');
2265+
json_path_query
2266+
--------------------------------
2267+
{"a": 5, "b": [1, 2, 3, 4, 5]}
2268+
(1 row)
2269+
2270+
select json_path_query('[1, 2, 3]', '{a: 2 + 3, "b": ($[*], 4, 5)}');
2271+
ERROR: value in jsonpath object constructor must be a singleton
2272+
select json_path_query('[1, 2, 3]', '{a: 2 + 3, "b": {x: $, y: $[1] > 2, z: "foo"}}');
2273+
json_path_query
2274+
---------------------------------------------------------
2275+
{"a": 5, "b": {"x": [1, 2, 3], "y": false, "z": "foo"}}
2276+
(1 row)
2277+
2278+
-- extension: object subscripting
2279+
select json '{"a": 1}' @? '$["a"]';
2280+
?column?
2281+
----------
2282+
t
2283+
(1 row)
2284+
2285+
select json '{"a": 1}' @? '$["b"]';
2286+
?column?
2287+
----------
2288+
f
2289+
(1 row)
2290+
2291+
select json '{"a": 1}' @? 'strict $["b"]';
2292+
?column?
2293+
----------
2294+
2295+
(1 row)
2296+
2297+
select json '{"a": 1}' @? '$["b", "a"]';
2298+
?column?
2299+
----------
2300+
t
2301+
(1 row)
2302+
2303+
select json_path_query('{"a": 1}', '$["a"]');
2304+
json_path_query
2305+
-----------------
2306+
1
2307+
(1 row)
2308+
2309+
select json_path_query('{"a": 1}', 'strict $["b"]');
2310+
ERROR: JSON object does not contain the specified key
2311+
select json_path_query('{"a": 1}', 'lax $["b"]');
2312+
json_path_query
2313+
-----------------
2314+
(0 rows)
2315+
2316+
select json_path_query('{"a": 1, "b": 2}', 'lax $["b", "c", "b", "a", 0 to 3]');
2317+
json_path_query
2318+
------------------
2319+
2
2320+
2
2321+
1
2322+
{"a": 1, "b": 2}
2323+
(4 rows)
2324+
2325+
select json_path_query('null', '{"a": 1}["a"]');
2326+
json_path_query
2327+
-----------------
2328+
1
2329+
(1 row)
2330+
2331+
select json_path_query('null', '{"a": 1}["b"]');
2332+
json_path_query
2333+
-----------------
2334+
(0 rows)
2335+

0 commit comments

Comments
 (0)