blob: a4d88dbeaa09bae90ae9a344b4a5a3adb9318e55 [file] [log] [blame]
drh852944e2015-09-10 03:29:11 +00001# 2015-08-12
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements tests for JSON SQL functions extension to the
12# SQLite library.
13#
14# This file contains tests automatically generated from the json1
15# documentation.
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
drhc306e082015-10-08 23:37:00 +000021ifcapable !json1 {
22 finish_test
23 return
24}
25
drh852944e2015-09-10 03:29:11 +000026do_execsql_test json102-100 {
drhe98b6fa2015-09-11 15:32:33 +000027 SELECT json_object('ex','[52,3.14159]');
28} {{{"ex":"[52,3.14159]"}}}
29do_execsql_test json102-110 {
30 SELECT json_object('ex',json('[52,3.14159]'));
31} {{{"ex":[52,3.14159]}}}
32do_execsql_test json102-120 {
33 SELECT json_object('ex',json_array(52,3.14159));
34} {{{"ex":[52,3.14159]}}}
35do_execsql_test json102-130 {
drh12b3b892015-09-11 01:22:41 +000036 SELECT json(' { "this" : "is", "a": [ "test" ] } ');
37} {{{"this":"is","a":["test"]}}}
drhe98b6fa2015-09-11 15:32:33 +000038do_execsql_test json102-140 {
drh852944e2015-09-10 03:29:11 +000039 SELECT json_array(1,2,'3',4);
40} {{[1,2,"3",4]}}
drhe98b6fa2015-09-11 15:32:33 +000041do_execsql_test json102-150 {
drh852944e2015-09-10 03:29:11 +000042 SELECT json_array('[1,2]');
43} {{["[1,2]"]}}
drhe98b6fa2015-09-11 15:32:33 +000044do_execsql_test json102-160 {
drh12b3b892015-09-11 01:22:41 +000045 SELECT json_array(json_array(1,2));
46} {{[[1,2]]}}
drhe98b6fa2015-09-11 15:32:33 +000047do_execsql_test json102-170 {
drh852944e2015-09-10 03:29:11 +000048 SELECT json_array(1,null,'3','[4,5]','{"six":7.7}');
49} {{[1,null,"3","[4,5]","{\"six\":7.7}"]}}
drhe98b6fa2015-09-11 15:32:33 +000050do_execsql_test json102-180 {
drh12b3b892015-09-11 01:22:41 +000051 SELECT json_array(1,null,'3',json('[4,5]'),json('{"six":7.7}'));
52} {{[1,null,"3",[4,5],{"six":7.7}]}}
drhe98b6fa2015-09-11 15:32:33 +000053do_execsql_test json102-190 {
drh852944e2015-09-10 03:29:11 +000054 SELECT json_array_length('[1,2,3,4]');
55} {{4}}
drhe98b6fa2015-09-11 15:32:33 +000056do_execsql_test json102-200 {
57 SELECT json_array_length('[1,2,3,4]', '$');
58} {{4}}
59do_execsql_test json102-210 {
60 SELECT json_array_length('[1,2,3,4]', '$[2]');
61} {{0}}
62do_execsql_test json102-220 {
drh852944e2015-09-10 03:29:11 +000063 SELECT json_array_length('{"one":[1,2,3]}');
64} {{0}}
drhe98b6fa2015-09-11 15:32:33 +000065do_execsql_test json102-230 {
drh852944e2015-09-10 03:29:11 +000066 SELECT json_array_length('{"one":[1,2,3]}', '$.one');
67} {{3}}
drh12b3b892015-09-11 01:22:41 +000068do_execsql_test json102-240 {
drhe98b6fa2015-09-11 15:32:33 +000069 SELECT json_array_length('{"one":[1,2,3]}', '$.two');
drh852944e2015-09-10 03:29:11 +000070} {{}}
drh12b3b892015-09-11 01:22:41 +000071do_execsql_test json102-250 {
drhe98b6fa2015-09-11 15:32:33 +000072 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$');
73} {{{"a":2,"c":[4,5,{"f":7}]}}}
74do_execsql_test json102-260 {
75 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c');
76} {{[4,5,{"f":7}]}}
77do_execsql_test json102-270 {
78 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]');
79} {{{"f":7}}}
80do_execsql_test json102-280 {
81 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f');
82} {{7}}
83do_execsql_test json102-290 {
84 SELECT json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a');
85} {{[[4,5],2]}}
86do_execsql_test json102-300 {
87 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x');
88} {{}}
89do_execsql_test json102-310 {
90 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a');
91} {{[null,2]}}
92do_execsql_test json102-320 {
drh852944e2015-09-10 03:29:11 +000093 SELECT json_insert('{"a":2,"c":4}', '$.a', 99);
94} {{{"a":2,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +000095do_execsql_test json102-330 {
drh852944e2015-09-10 03:29:11 +000096 SELECT json_insert('{"a":2,"c":4}', '$.e', 99);
97} {{{"a":2,"c":4,"e":99}}}
drhe98b6fa2015-09-11 15:32:33 +000098do_execsql_test json102-340 {
drh852944e2015-09-10 03:29:11 +000099 SELECT json_replace('{"a":2,"c":4}', '$.a', 99);
100} {{{"a":99,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +0000101do_execsql_test json102-350 {
drh852944e2015-09-10 03:29:11 +0000102 SELECT json_replace('{"a":2,"c":4}', '$.e', 99);
103} {{{"a":2,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +0000104do_execsql_test json102-360 {
drh852944e2015-09-10 03:29:11 +0000105 SELECT json_set('{"a":2,"c":4}', '$.a', 99);
106} {{{"a":99,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +0000107do_execsql_test json102-370 {
drh852944e2015-09-10 03:29:11 +0000108 SELECT json_set('{"a":2,"c":4}', '$.e', 99);
109} {{{"a":2,"c":4,"e":99}}}
drhe98b6fa2015-09-11 15:32:33 +0000110do_execsql_test json102-380 {
drh12b3b892015-09-11 01:22:41 +0000111 SELECT json_set('{"a":2,"c":4}', '$.c', '[97,96]');
112} {{{"a":2,"c":"[97,96]"}}}
drhe98b6fa2015-09-11 15:32:33 +0000113do_execsql_test json102-390 {
drh12b3b892015-09-11 01:22:41 +0000114 SELECT json_set('{"a":2,"c":4}', '$.c', json('[97,96]'));
115} {{{"a":2,"c":[97,96]}}}
drhe98b6fa2015-09-11 15:32:33 +0000116do_execsql_test json102-400 {
drh12b3b892015-09-11 01:22:41 +0000117 SELECT json_set('{"a":2,"c":4}', '$.c', json_array(97,96));
118} {{{"a":2,"c":[97,96]}}}
drhe98b6fa2015-09-11 15:32:33 +0000119do_execsql_test json102-410 {
drh852944e2015-09-10 03:29:11 +0000120 SELECT json_object('a',2,'c',4);
121} {{{"a":2,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +0000122do_execsql_test json102-420 {
drh852944e2015-09-10 03:29:11 +0000123 SELECT json_object('a',2,'c','{e:5}');
124} {{{"a":2,"c":"{e:5}"}}}
drhe98b6fa2015-09-11 15:32:33 +0000125do_execsql_test json102-430 {
drh12b3b892015-09-11 01:22:41 +0000126 SELECT json_object('a',2,'c',json_object('e',5));
127} {{{"a":2,"c":{"e":5}}}}
drhe98b6fa2015-09-11 15:32:33 +0000128do_execsql_test json102-440 {
drh852944e2015-09-10 03:29:11 +0000129 SELECT json_remove('[0,1,2,3,4]','$[2]');
130} {{[0,1,3,4]}}
drhe98b6fa2015-09-11 15:32:33 +0000131do_execsql_test json102-450 {
drh852944e2015-09-10 03:29:11 +0000132 SELECT json_remove('[0,1,2,3,4]','$[2]','$[0]');
133} {{[1,3,4]}}
drhe98b6fa2015-09-11 15:32:33 +0000134do_execsql_test json102-460 {
drh852944e2015-09-10 03:29:11 +0000135 SELECT json_remove('[0,1,2,3,4]','$[0]','$[2]');
136} {{[1,2,4]}}
drhe98b6fa2015-09-11 15:32:33 +0000137do_execsql_test json102-470 {
drh852944e2015-09-10 03:29:11 +0000138 SELECT json_remove('{"x":25,"y":42}');
139} {{{"x":25,"y":42}}}
drhe98b6fa2015-09-11 15:32:33 +0000140do_execsql_test json102-480 {
drh852944e2015-09-10 03:29:11 +0000141 SELECT json_remove('{"x":25,"y":42}','$.z');
142} {{{"x":25,"y":42}}}
drhe98b6fa2015-09-11 15:32:33 +0000143do_execsql_test json102-490 {
drh852944e2015-09-10 03:29:11 +0000144 SELECT json_remove('{"x":25,"y":42}','$.y');
145} {{{"x":25}}}
drhe98b6fa2015-09-11 15:32:33 +0000146do_execsql_test json102-500 {
drh852944e2015-09-10 03:29:11 +0000147 SELECT json_remove('{"x":25,"y":42}','$');
148} {{}}
drhe98b6fa2015-09-11 15:32:33 +0000149do_execsql_test json102-510 {
drh852944e2015-09-10 03:29:11 +0000150 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}');
151} {{object}}
drhe98b6fa2015-09-11 15:32:33 +0000152do_execsql_test json102-520 {
drh852944e2015-09-10 03:29:11 +0000153 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$');
154} {{object}}
drhe98b6fa2015-09-11 15:32:33 +0000155do_execsql_test json102-530 {
drh852944e2015-09-10 03:29:11 +0000156 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a');
157} {{array}}
drhe98b6fa2015-09-11 15:32:33 +0000158do_execsql_test json102-540 {
drh852944e2015-09-10 03:29:11 +0000159 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[0]');
160} {{integer}}
drhe98b6fa2015-09-11 15:32:33 +0000161do_execsql_test json102-550 {
drh852944e2015-09-10 03:29:11 +0000162 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[1]');
163} {{real}}
drhe98b6fa2015-09-11 15:32:33 +0000164do_execsql_test json102-560 {
drh852944e2015-09-10 03:29:11 +0000165 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[2]');
166} {{true}}
drhe98b6fa2015-09-11 15:32:33 +0000167do_execsql_test json102-570 {
drh852944e2015-09-10 03:29:11 +0000168 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[3]');
169} {{false}}
drhe98b6fa2015-09-11 15:32:33 +0000170do_execsql_test json102-580 {
drh852944e2015-09-10 03:29:11 +0000171 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[4]');
172} {{null}}
drhe98b6fa2015-09-11 15:32:33 +0000173do_execsql_test json102-590 {
drh852944e2015-09-10 03:29:11 +0000174 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[5]');
175} {{text}}
drhe98b6fa2015-09-11 15:32:33 +0000176do_execsql_test json102-600 {
drh852944e2015-09-10 03:29:11 +0000177 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[6]');
178} {{}}
drhe98b6fa2015-09-11 15:32:33 +0000179do_execsql_test json102-610 {
drh12b3b892015-09-11 01:22:41 +0000180 SELECT json_valid(char(123)||'"x":35'||char(125));
drh852944e2015-09-10 03:29:11 +0000181} {{1}}
drhe98b6fa2015-09-11 15:32:33 +0000182do_execsql_test json102-620 {
drh12b3b892015-09-11 01:22:41 +0000183 SELECT json_valid(char(123)||'"x":35');
drh852944e2015-09-10 03:29:11 +0000184} {{0}}
185
drhe6f52782015-09-10 15:22:35 +0000186ifcapable vtab {
drh12b3b892015-09-11 01:22:41 +0000187do_execsql_test json102-1000 {
drh852944e2015-09-10 03:29:11 +0000188 CREATE TABLE user(name,phone);
189 INSERT INTO user(name,phone) VALUES
190 ('Alice','["919-555-2345","804-555-3621"]'),
191 ('Bob','["201-555-8872"]'),
192 ('Cindy','["704-555-9983"]'),
193 ('Dave','["336-555-8421","704-555-4321","803-911-4421"]');
194 SELECT DISTINCT user.name
195 FROM user, json_each(user.phone)
196 WHERE json_each.value LIKE '704-%'
197 ORDER BY 1;
198} {Cindy Dave}
199
drh12b3b892015-09-11 01:22:41 +0000200do_execsql_test json102-1010 {
drh852944e2015-09-10 03:29:11 +0000201 UPDATE user
202 SET phone=json_extract(phone,'$[0]')
203 WHERE json_array_length(phone)<2;
204 SELECT name, substr(phone,1,5) FROM user ORDER BY name;
205} {Alice {["919} Bob 201-5 Cindy 704-5 Dave {["336}}
drh12b3b892015-09-11 01:22:41 +0000206do_execsql_test json102-1011 {
drh852944e2015-09-10 03:29:11 +0000207 SELECT name FROM user WHERE phone LIKE '704-%'
208 UNION
209 SELECT user.name
210 FROM user, json_each(user.phone)
211 WHERE json_valid(user.phone)
212 AND json_each.value LIKE '704-%';
213} {Cindy Dave}
214
drh12b3b892015-09-11 01:22:41 +0000215do_execsql_test json102-1100 {
drh852944e2015-09-10 03:29:11 +0000216 CREATE TABLE big(json JSON);
217 INSERT INTO big(json) VALUES('{
218 "id":123,
219 "stuff":[1,2,3,4],
220 "partlist":[
221 {"uuid":"bb108722-572e-11e5-9320-7f3b63a4ca74"},
222 {"uuid":"c690dc14-572e-11e5-95f9-dfc8861fd535"},
223 {"subassembly":[
224 {"uuid":"6fa5181e-5721-11e5-a04e-57f3d7b32808"}
225 ]}
226 ]
227 }');
228 INSERT INTO big(json) VALUES('{
229 "id":456,
230 "stuff":["hello","world","xyzzy"],
231 "partlist":[
232 {"uuid":false},
233 {"uuid":"c690dc14-572e-11e5-95f9-dfc8861fd535"}
234 ]
235 }');
236} {}
237set correct_answer [list \
238 1 {$.id} 123 \
239 1 {$.stuff[0]} 1 \
240 1 {$.stuff[1]} 2 \
241 1 {$.stuff[2]} 3 \
242 1 {$.stuff[3]} 4 \
243 1 {$.partlist[0].uuid} bb108722-572e-11e5-9320-7f3b63a4ca74 \
244 1 {$.partlist[1].uuid} c690dc14-572e-11e5-95f9-dfc8861fd535 \
245 1 {$.partlist[2].subassembly[0].uuid} 6fa5181e-5721-11e5-a04e-57f3d7b32808 \
246 2 {$.id} 456 \
247 2 {$.stuff[0]} hello \
248 2 {$.stuff[1]} world \
249 2 {$.stuff[2]} xyzzy \
250 2 {$.partlist[0].uuid} 0 \
251 2 {$.partlist[1].uuid} c690dc14-572e-11e5-95f9-dfc8861fd535]
drh12b3b892015-09-11 01:22:41 +0000252do_execsql_test json102-1110 {
drh852944e2015-09-10 03:29:11 +0000253 SELECT big.rowid, fullkey, value
254 FROM big, json_tree(big.json)
255 WHERE json_tree.type NOT IN ('object','array')
256 ORDER BY +big.rowid, +json_tree.id
257} $correct_answer
drh12b3b892015-09-11 01:22:41 +0000258do_execsql_test json102-1120 {
drh852944e2015-09-10 03:29:11 +0000259 SELECT big.rowid, fullkey, atom
260 FROM big, json_tree(big.json)
261 WHERE atom IS NOT NULL
262 ORDER BY +big.rowid, +json_tree.id
263} $correct_answer
264
drh12b3b892015-09-11 01:22:41 +0000265do_execsql_test json102-1130 {
drh852944e2015-09-10 03:29:11 +0000266 SELECT DISTINCT json_extract(big.json,'$.id')
267 FROM big, json_tree(big.json,'$.partlist')
268 WHERE json_tree.key='uuid'
269 AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808';
270} {123}
drh12b3b892015-09-11 01:22:41 +0000271do_execsql_test json102-1131 {
drh852944e2015-09-10 03:29:11 +0000272 SELECT DISTINCT json_extract(big.json,'$.id')
273 FROM big, json_tree(big.json,'$')
274 WHERE json_tree.key='uuid'
275 AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808';
276} {123}
drh12b3b892015-09-11 01:22:41 +0000277do_execsql_test json102-1132 {
drh852944e2015-09-10 03:29:11 +0000278 SELECT DISTINCT json_extract(big.json,'$.id')
279 FROM big, json_tree(big.json)
280 WHERE json_tree.key='uuid'
281 AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808';
282} {123}
drhe6f52782015-09-10 15:22:35 +0000283} ;# end ifcapable vtab
drh852944e2015-09-10 03:29:11 +0000284
dan2e8f5512015-09-17 17:21:09 +0000285#-------------------------------------------------------------------------
286# Test that json_valid() correctly identifies non-ascii range
287# characters as non-whitespace.
288#
289do_execsql_test json102-1201 { SELECT json_valid(char(32) || '"xyz"') } 1
290do_execsql_test json102-1202 { SELECT json_valid(char(200) || '"xyz"') } 0
291
drh4977ccf2015-09-19 11:57:26 +0000292# Off-by-one error in jsonAppendString()
293#
294for {set i 0} {$i<100} {incr i} {
295 set str abcdef[string repeat \" [expr {$i+50}]]uvwxyz
296 do_test json102-[format %d [expr {$i+1300}]] {
297 db eval {SELECT json_extract(json_array($::str),'$[0]')==$::str}
298 } {1}
299}
300
drh852944e2015-09-10 03:29:11 +0000301finish_test