blob: da9fbd1b76ea46ab4c8ba1e8ff76c2fd6d5e2592 [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
21load_static_extension db json
22do_execsql_test json102-100 {
drhe98b6fa2015-09-11 15:32:33 +000023 SELECT json_object('ex','[52,3.14159]');
24} {{{"ex":"[52,3.14159]"}}}
25do_execsql_test json102-110 {
26 SELECT json_object('ex',json('[52,3.14159]'));
27} {{{"ex":[52,3.14159]}}}
28do_execsql_test json102-120 {
29 SELECT json_object('ex',json_array(52,3.14159));
30} {{{"ex":[52,3.14159]}}}
31do_execsql_test json102-130 {
drh12b3b892015-09-11 01:22:41 +000032 SELECT json(' { "this" : "is", "a": [ "test" ] } ');
33} {{{"this":"is","a":["test"]}}}
drhe98b6fa2015-09-11 15:32:33 +000034do_execsql_test json102-140 {
drh852944e2015-09-10 03:29:11 +000035 SELECT json_array(1,2,'3',4);
36} {{[1,2,"3",4]}}
drhe98b6fa2015-09-11 15:32:33 +000037do_execsql_test json102-150 {
drh852944e2015-09-10 03:29:11 +000038 SELECT json_array('[1,2]');
39} {{["[1,2]"]}}
drhe98b6fa2015-09-11 15:32:33 +000040do_execsql_test json102-160 {
drh12b3b892015-09-11 01:22:41 +000041 SELECT json_array(json_array(1,2));
42} {{[[1,2]]}}
drhe98b6fa2015-09-11 15:32:33 +000043do_execsql_test json102-170 {
drh852944e2015-09-10 03:29:11 +000044 SELECT json_array(1,null,'3','[4,5]','{"six":7.7}');
45} {{[1,null,"3","[4,5]","{\"six\":7.7}"]}}
drhe98b6fa2015-09-11 15:32:33 +000046do_execsql_test json102-180 {
drh12b3b892015-09-11 01:22:41 +000047 SELECT json_array(1,null,'3',json('[4,5]'),json('{"six":7.7}'));
48} {{[1,null,"3",[4,5],{"six":7.7}]}}
drhe98b6fa2015-09-11 15:32:33 +000049do_execsql_test json102-190 {
drh852944e2015-09-10 03:29:11 +000050 SELECT json_array_length('[1,2,3,4]');
51} {{4}}
drhe98b6fa2015-09-11 15:32:33 +000052do_execsql_test json102-200 {
53 SELECT json_array_length('[1,2,3,4]', '$');
54} {{4}}
55do_execsql_test json102-210 {
56 SELECT json_array_length('[1,2,3,4]', '$[2]');
57} {{0}}
58do_execsql_test json102-220 {
drh852944e2015-09-10 03:29:11 +000059 SELECT json_array_length('{"one":[1,2,3]}');
60} {{0}}
drhe98b6fa2015-09-11 15:32:33 +000061do_execsql_test json102-230 {
drh852944e2015-09-10 03:29:11 +000062 SELECT json_array_length('{"one":[1,2,3]}', '$.one');
63} {{3}}
drh12b3b892015-09-11 01:22:41 +000064do_execsql_test json102-240 {
drhe98b6fa2015-09-11 15:32:33 +000065 SELECT json_array_length('{"one":[1,2,3]}', '$.two');
drh852944e2015-09-10 03:29:11 +000066} {{}}
drh12b3b892015-09-11 01:22:41 +000067do_execsql_test json102-250 {
drhe98b6fa2015-09-11 15:32:33 +000068 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$');
69} {{{"a":2,"c":[4,5,{"f":7}]}}}
70do_execsql_test json102-260 {
71 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c');
72} {{[4,5,{"f":7}]}}
73do_execsql_test json102-270 {
74 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]');
75} {{{"f":7}}}
76do_execsql_test json102-280 {
77 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f');
78} {{7}}
79do_execsql_test json102-290 {
80 SELECT json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a');
81} {{[[4,5],2]}}
82do_execsql_test json102-300 {
83 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x');
84} {{}}
85do_execsql_test json102-310 {
86 SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a');
87} {{[null,2]}}
88do_execsql_test json102-320 {
drh852944e2015-09-10 03:29:11 +000089 SELECT json_insert('{"a":2,"c":4}', '$.a', 99);
90} {{{"a":2,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +000091do_execsql_test json102-330 {
drh852944e2015-09-10 03:29:11 +000092 SELECT json_insert('{"a":2,"c":4}', '$.e', 99);
93} {{{"a":2,"c":4,"e":99}}}
drhe98b6fa2015-09-11 15:32:33 +000094do_execsql_test json102-340 {
drh852944e2015-09-10 03:29:11 +000095 SELECT json_replace('{"a":2,"c":4}', '$.a', 99);
96} {{{"a":99,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +000097do_execsql_test json102-350 {
drh852944e2015-09-10 03:29:11 +000098 SELECT json_replace('{"a":2,"c":4}', '$.e', 99);
99} {{{"a":2,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +0000100do_execsql_test json102-360 {
drh852944e2015-09-10 03:29:11 +0000101 SELECT json_set('{"a":2,"c":4}', '$.a', 99);
102} {{{"a":99,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +0000103do_execsql_test json102-370 {
drh852944e2015-09-10 03:29:11 +0000104 SELECT json_set('{"a":2,"c":4}', '$.e', 99);
105} {{{"a":2,"c":4,"e":99}}}
drhe98b6fa2015-09-11 15:32:33 +0000106do_execsql_test json102-380 {
drh12b3b892015-09-11 01:22:41 +0000107 SELECT json_set('{"a":2,"c":4}', '$.c', '[97,96]');
108} {{{"a":2,"c":"[97,96]"}}}
drhe98b6fa2015-09-11 15:32:33 +0000109do_execsql_test json102-390 {
drh12b3b892015-09-11 01:22:41 +0000110 SELECT json_set('{"a":2,"c":4}', '$.c', json('[97,96]'));
111} {{{"a":2,"c":[97,96]}}}
drhe98b6fa2015-09-11 15:32:33 +0000112do_execsql_test json102-400 {
drh12b3b892015-09-11 01:22:41 +0000113 SELECT json_set('{"a":2,"c":4}', '$.c', json_array(97,96));
114} {{{"a":2,"c":[97,96]}}}
drhe98b6fa2015-09-11 15:32:33 +0000115do_execsql_test json102-410 {
drh852944e2015-09-10 03:29:11 +0000116 SELECT json_object('a',2,'c',4);
117} {{{"a":2,"c":4}}}
drhe98b6fa2015-09-11 15:32:33 +0000118do_execsql_test json102-420 {
drh852944e2015-09-10 03:29:11 +0000119 SELECT json_object('a',2,'c','{e:5}');
120} {{{"a":2,"c":"{e:5}"}}}
drhe98b6fa2015-09-11 15:32:33 +0000121do_execsql_test json102-430 {
drh12b3b892015-09-11 01:22:41 +0000122 SELECT json_object('a',2,'c',json_object('e',5));
123} {{{"a":2,"c":{"e":5}}}}
drhe98b6fa2015-09-11 15:32:33 +0000124do_execsql_test json102-440 {
drh852944e2015-09-10 03:29:11 +0000125 SELECT json_remove('[0,1,2,3,4]','$[2]');
126} {{[0,1,3,4]}}
drhe98b6fa2015-09-11 15:32:33 +0000127do_execsql_test json102-450 {
drh852944e2015-09-10 03:29:11 +0000128 SELECT json_remove('[0,1,2,3,4]','$[2]','$[0]');
129} {{[1,3,4]}}
drhe98b6fa2015-09-11 15:32:33 +0000130do_execsql_test json102-460 {
drh852944e2015-09-10 03:29:11 +0000131 SELECT json_remove('[0,1,2,3,4]','$[0]','$[2]');
132} {{[1,2,4]}}
drhe98b6fa2015-09-11 15:32:33 +0000133do_execsql_test json102-470 {
drh852944e2015-09-10 03:29:11 +0000134 SELECT json_remove('{"x":25,"y":42}');
135} {{{"x":25,"y":42}}}
drhe98b6fa2015-09-11 15:32:33 +0000136do_execsql_test json102-480 {
drh852944e2015-09-10 03:29:11 +0000137 SELECT json_remove('{"x":25,"y":42}','$.z');
138} {{{"x":25,"y":42}}}
drhe98b6fa2015-09-11 15:32:33 +0000139do_execsql_test json102-490 {
drh852944e2015-09-10 03:29:11 +0000140 SELECT json_remove('{"x":25,"y":42}','$.y');
141} {{{"x":25}}}
drhe98b6fa2015-09-11 15:32:33 +0000142do_execsql_test json102-500 {
drh852944e2015-09-10 03:29:11 +0000143 SELECT json_remove('{"x":25,"y":42}','$');
144} {{}}
drhe98b6fa2015-09-11 15:32:33 +0000145do_execsql_test json102-510 {
drh852944e2015-09-10 03:29:11 +0000146 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}');
147} {{object}}
drhe98b6fa2015-09-11 15:32:33 +0000148do_execsql_test json102-520 {
drh852944e2015-09-10 03:29:11 +0000149 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$');
150} {{object}}
drhe98b6fa2015-09-11 15:32:33 +0000151do_execsql_test json102-530 {
drh852944e2015-09-10 03:29:11 +0000152 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a');
153} {{array}}
drhe98b6fa2015-09-11 15:32:33 +0000154do_execsql_test json102-540 {
drh852944e2015-09-10 03:29:11 +0000155 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[0]');
156} {{integer}}
drhe98b6fa2015-09-11 15:32:33 +0000157do_execsql_test json102-550 {
drh852944e2015-09-10 03:29:11 +0000158 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[1]');
159} {{real}}
drhe98b6fa2015-09-11 15:32:33 +0000160do_execsql_test json102-560 {
drh852944e2015-09-10 03:29:11 +0000161 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[2]');
162} {{true}}
drhe98b6fa2015-09-11 15:32:33 +0000163do_execsql_test json102-570 {
drh852944e2015-09-10 03:29:11 +0000164 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[3]');
165} {{false}}
drhe98b6fa2015-09-11 15:32:33 +0000166do_execsql_test json102-580 {
drh852944e2015-09-10 03:29:11 +0000167 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[4]');
168} {{null}}
drhe98b6fa2015-09-11 15:32:33 +0000169do_execsql_test json102-590 {
drh852944e2015-09-10 03:29:11 +0000170 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[5]');
171} {{text}}
drhe98b6fa2015-09-11 15:32:33 +0000172do_execsql_test json102-600 {
drh852944e2015-09-10 03:29:11 +0000173 SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[6]');
174} {{}}
drhe98b6fa2015-09-11 15:32:33 +0000175do_execsql_test json102-610 {
drh12b3b892015-09-11 01:22:41 +0000176 SELECT json_valid(char(123)||'"x":35'||char(125));
drh852944e2015-09-10 03:29:11 +0000177} {{1}}
drhe98b6fa2015-09-11 15:32:33 +0000178do_execsql_test json102-620 {
drh12b3b892015-09-11 01:22:41 +0000179 SELECT json_valid(char(123)||'"x":35');
drh852944e2015-09-10 03:29:11 +0000180} {{0}}
181
drhe6f52782015-09-10 15:22:35 +0000182ifcapable vtab {
drh12b3b892015-09-11 01:22:41 +0000183do_execsql_test json102-1000 {
drh852944e2015-09-10 03:29:11 +0000184 CREATE TABLE user(name,phone);
185 INSERT INTO user(name,phone) VALUES
186 ('Alice','["919-555-2345","804-555-3621"]'),
187 ('Bob','["201-555-8872"]'),
188 ('Cindy','["704-555-9983"]'),
189 ('Dave','["336-555-8421","704-555-4321","803-911-4421"]');
190 SELECT DISTINCT user.name
191 FROM user, json_each(user.phone)
192 WHERE json_each.value LIKE '704-%'
193 ORDER BY 1;
194} {Cindy Dave}
195
drh12b3b892015-09-11 01:22:41 +0000196do_execsql_test json102-1010 {
drh852944e2015-09-10 03:29:11 +0000197 UPDATE user
198 SET phone=json_extract(phone,'$[0]')
199 WHERE json_array_length(phone)<2;
200 SELECT name, substr(phone,1,5) FROM user ORDER BY name;
201} {Alice {["919} Bob 201-5 Cindy 704-5 Dave {["336}}
drh12b3b892015-09-11 01:22:41 +0000202do_execsql_test json102-1011 {
drh852944e2015-09-10 03:29:11 +0000203 SELECT name FROM user WHERE phone LIKE '704-%'
204 UNION
205 SELECT user.name
206 FROM user, json_each(user.phone)
207 WHERE json_valid(user.phone)
208 AND json_each.value LIKE '704-%';
209} {Cindy Dave}
210
drh12b3b892015-09-11 01:22:41 +0000211do_execsql_test json102-1100 {
drh852944e2015-09-10 03:29:11 +0000212 CREATE TABLE big(json JSON);
213 INSERT INTO big(json) VALUES('{
214 "id":123,
215 "stuff":[1,2,3,4],
216 "partlist":[
217 {"uuid":"bb108722-572e-11e5-9320-7f3b63a4ca74"},
218 {"uuid":"c690dc14-572e-11e5-95f9-dfc8861fd535"},
219 {"subassembly":[
220 {"uuid":"6fa5181e-5721-11e5-a04e-57f3d7b32808"}
221 ]}
222 ]
223 }');
224 INSERT INTO big(json) VALUES('{
225 "id":456,
226 "stuff":["hello","world","xyzzy"],
227 "partlist":[
228 {"uuid":false},
229 {"uuid":"c690dc14-572e-11e5-95f9-dfc8861fd535"}
230 ]
231 }');
232} {}
233set correct_answer [list \
234 1 {$.id} 123 \
235 1 {$.stuff[0]} 1 \
236 1 {$.stuff[1]} 2 \
237 1 {$.stuff[2]} 3 \
238 1 {$.stuff[3]} 4 \
239 1 {$.partlist[0].uuid} bb108722-572e-11e5-9320-7f3b63a4ca74 \
240 1 {$.partlist[1].uuid} c690dc14-572e-11e5-95f9-dfc8861fd535 \
241 1 {$.partlist[2].subassembly[0].uuid} 6fa5181e-5721-11e5-a04e-57f3d7b32808 \
242 2 {$.id} 456 \
243 2 {$.stuff[0]} hello \
244 2 {$.stuff[1]} world \
245 2 {$.stuff[2]} xyzzy \
246 2 {$.partlist[0].uuid} 0 \
247 2 {$.partlist[1].uuid} c690dc14-572e-11e5-95f9-dfc8861fd535]
drh12b3b892015-09-11 01:22:41 +0000248do_execsql_test json102-1110 {
drh852944e2015-09-10 03:29:11 +0000249 SELECT big.rowid, fullkey, value
250 FROM big, json_tree(big.json)
251 WHERE json_tree.type NOT IN ('object','array')
252 ORDER BY +big.rowid, +json_tree.id
253} $correct_answer
drh12b3b892015-09-11 01:22:41 +0000254do_execsql_test json102-1120 {
drh852944e2015-09-10 03:29:11 +0000255 SELECT big.rowid, fullkey, atom
256 FROM big, json_tree(big.json)
257 WHERE atom IS NOT NULL
258 ORDER BY +big.rowid, +json_tree.id
259} $correct_answer
260
drh12b3b892015-09-11 01:22:41 +0000261do_execsql_test json102-1130 {
drh852944e2015-09-10 03:29:11 +0000262 SELECT DISTINCT json_extract(big.json,'$.id')
263 FROM big, json_tree(big.json,'$.partlist')
264 WHERE json_tree.key='uuid'
265 AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808';
266} {123}
drh12b3b892015-09-11 01:22:41 +0000267do_execsql_test json102-1131 {
drh852944e2015-09-10 03:29:11 +0000268 SELECT DISTINCT json_extract(big.json,'$.id')
269 FROM big, json_tree(big.json,'$')
270 WHERE json_tree.key='uuid'
271 AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808';
272} {123}
drh12b3b892015-09-11 01:22:41 +0000273do_execsql_test json102-1132 {
drh852944e2015-09-10 03:29:11 +0000274 SELECT DISTINCT json_extract(big.json,'$.id')
275 FROM big, json_tree(big.json)
276 WHERE json_tree.key='uuid'
277 AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808';
278} {123}
drhe6f52782015-09-10 15:22:35 +0000279} ;# end ifcapable vtab
drh852944e2015-09-10 03:29:11 +0000280
281finish_test