drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1 | # 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 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | |
drh | c306e08 | 2015-10-08 23:37:00 +0000 | [diff] [blame] | 18 | ifcapable !json1 { |
| 19 | finish_test |
| 20 | return |
| 21 | } |
| 22 | |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 23 | do_execsql_test json101-1.1.00 { |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 24 | SELECT json_array(1,2.5,null,'hello'); |
| 25 | } {[1,2.5,null,"hello"]} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 26 | do_execsql_test json101-1.1.01 { |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 27 | SELECT json_array(1,'{"abc":2.5,"def":null,"ghi":hello}',99); |
| 28 | -- the second term goes in as a string: |
| 29 | } {[1,"{\\"abc\\":2.5,\\"def\\":null,\\"ghi\\":hello}",99]} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 30 | do_execsql_test json101-1.1.02 { |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 31 | SELECT json_array(1,json('{"abc":2.5,"def":null,"ghi":"hello"}'),99); |
| 32 | -- the second term goes in as JSON |
| 33 | } {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 34 | do_execsql_test json101-1.1.03 { |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 35 | SELECT json_array(1,json_object('abc',2.5,'def',null,'ghi','hello'),99); |
| 36 | -- the second term goes in as JSON |
| 37 | } {[1,{"abc":2.5,"def":null,"ghi":"hello"},99]} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 38 | do_execsql_test json101-1.2 { |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 39 | SELECT hex(json_array('String "\ Test')); |
| 40 | } {5B22537472696E67205C225C5C2054657374225D} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 41 | do_catchsql_test json101-1.3 { |
drh | dc38495 | 2015-09-19 18:54:39 +0000 | [diff] [blame] | 42 | SELECT json_array(1,printf('%.1000c','x'),x'abcd',3); |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 43 | } {1 {JSON cannot hold BLOB values}} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 44 | do_execsql_test json101-1.4 { |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 45 | SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1, |
| 46 | 0.0, 1.0, -1.0, -1e99, +2e100, |
| 47 | 'one','two','three', |
| 48 | 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
| 49 | 19, NULL, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 50 | 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 51 | 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 52 | 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 53 | 99); |
| 54 | } {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]} |
| 55 | |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 56 | do_execsql_test json101-2.1 { |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 57 | SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test'); |
| 58 | } {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 59 | do_catchsql_test json101-2.2 { |
drh | dc38495 | 2015-09-19 18:54:39 +0000 | [diff] [blame] | 60 | SELECT json_object('a',printf('%.1000c','x'),2,2.5); |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 61 | } {1 {json_object() labels must be TEXT}} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 62 | do_catchsql_test json101-2.3 { |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 63 | SELECT json_object('a',1,'b'); |
| 64 | } {1 {json_object() requires an even number of arguments}} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 65 | do_catchsql_test json101-2.4 { |
drh | dc38495 | 2015-09-19 18:54:39 +0000 | [diff] [blame] | 66 | SELECT json_object('a',printf('%.1000c','x'),'b',x'abcd'); |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 67 | } {1 {JSON cannot hold BLOB values}} |
| 68 | |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 69 | do_execsql_test json101-3.1 { |
drh | ecb5fed | 2015-08-28 03:33:50 +0000 | [diff] [blame] | 70 | SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]'); |
| 71 | } {{{"a":"[3,4,5]","b":2}}} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 72 | do_execsql_test json101-3.2 { |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 73 | SELECT json_replace('{"a":1,"b":2}','$.a',json('[3,4,5]')); |
drh | ecb5fed | 2015-08-28 03:33:50 +0000 | [diff] [blame] | 74 | } {{{"a":[3,4,5],"b":2}}} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 75 | do_execsql_test json101-3.3 { |
drh | ecb5fed | 2015-08-28 03:33:50 +0000 | [diff] [blame] | 76 | SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b'); |
| 77 | } {text} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 78 | do_execsql_test json101-3.4 { |
drh | f5ddb9c | 2015-09-11 00:06:41 +0000 | [diff] [blame] | 79 | SELECT json_type(json_set('{"a":1,"b":2}','$.b',json('{"x":3,"y":4}')),'$.b'); |
drh | ecb5fed | 2015-08-28 03:33:50 +0000 | [diff] [blame] | 80 | } {object} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 81 | ifcapable vtab { |
| 82 | do_execsql_test json101-3.5 { |
drh | 8cb0c83 | 2015-09-22 00:21:03 +0000 | [diff] [blame] | 83 | SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456)); |
| 84 | } {{$} {} | {$.x} 456 |} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 85 | } |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 86 | |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 87 | # Per rfc7159, any JSON value is allowed at the top level, and whitespace |
| 88 | # is permitting before and/or after that value. |
| 89 | # |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 90 | do_execsql_test json101-4.1 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 91 | CREATE TABLE j1(x); |
| 92 | INSERT INTO j1(x) |
| 93 | VALUES('true'),('false'),('null'),('123'),('-234'),('34.5e+6'), |
| 94 | ('""'),('"\""'),('"\\"'),('"abcdefghijlmnopqrstuvwxyz"'), |
| 95 | ('[]'),('{}'),('[true,false,null,123,-234,34.5e+6,{},[]]'), |
| 96 | ('{"a":true,"b":{"c":false}}'); |
| 97 | SELECT * FROM j1 WHERE NOT json_valid(x); |
| 98 | } {} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 99 | do_execsql_test json101-4.2 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 100 | SELECT * FROM j1 WHERE NOT json_valid(char(0x20,0x09,0x0a,0x0d)||x); |
| 101 | } {} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 102 | do_execsql_test json101-4.3 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 103 | SELECT * FROM j1 WHERE NOT json_valid(x||char(0x20,0x09,0x0a,0x0d)); |
| 104 | } {} |
| 105 | |
| 106 | # But an empty string, or a string of pure whitespace is not valid JSON. |
| 107 | # |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 108 | do_execsql_test json101-4.4 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 109 | SELECT json_valid(''), json_valid(char(0x20,0x09,0x0a,0x0d)); |
| 110 | } {0 0} |
| 111 | |
| 112 | # json_remove() and similar functions with no edit operations return their |
| 113 | # input unchanged. |
| 114 | # |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 115 | do_execsql_test json101-4.5 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 116 | SELECT x FROM j1 WHERE json_remove(x)<>x; |
| 117 | } {} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 118 | do_execsql_test json101-4.6 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 119 | SELECT x FROM j1 WHERE json_replace(x)<>x; |
| 120 | } {} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 121 | do_execsql_test json101-4.7 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 122 | SELECT x FROM j1 WHERE json_set(x)<>x; |
| 123 | } {} |
drh | b760146 | 2015-09-24 11:06:26 +0000 | [diff] [blame] | 124 | do_execsql_test json101-4.8 { |
drh | d1f0068 | 2015-08-29 16:02:37 +0000 | [diff] [blame] | 125 | SELECT x FROM j1 WHERE json_insert(x)<>x; |
| 126 | } {} |
| 127 | |
| 128 | # json_extract(JSON,'$') will return objects and arrays without change. |
| 129 | # |
| 130 | do_execsql_test json-4.10 { |
| 131 | SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array'); |
| 132 | SELECT x FROM j1 |
| 133 | WHERE json_extract(x,'$')<>x |
| 134 | AND json_type(x) IN ('object','array'); |
| 135 | } {4} |
| 136 | |
drh | 20b3b61 | 2015-08-29 18:30:30 +0000 | [diff] [blame] | 137 | do_execsql_test json-5.1 { |
| 138 | CREATE TABLE j2(id INTEGER PRIMARY KEY, json, src); |
| 139 | INSERT INTO j2(id,json,src) |
| 140 | VALUES(1,'{ |
| 141 | "firstName": "John", |
| 142 | "lastName": "Smith", |
| 143 | "isAlive": true, |
| 144 | "age": 25, |
| 145 | "address": { |
| 146 | "streetAddress": "21 2nd Street", |
| 147 | "city": "New York", |
| 148 | "state": "NY", |
| 149 | "postalCode": "10021-3100" |
| 150 | }, |
| 151 | "phoneNumbers": [ |
| 152 | { |
| 153 | "type": "home", |
| 154 | "number": "212 555-1234" |
| 155 | }, |
| 156 | { |
| 157 | "type": "office", |
| 158 | "number": "646 555-4567" |
| 159 | } |
| 160 | ], |
| 161 | "children": [], |
| 162 | "spouse": null |
| 163 | }','https://en.wikipedia.org/wiki/JSON'); |
| 164 | INSERT INTO j2(id,json,src) |
| 165 | VALUES(2, '{ |
| 166 | "id": "0001", |
| 167 | "type": "donut", |
| 168 | "name": "Cake", |
| 169 | "ppu": 0.55, |
| 170 | "batters": |
| 171 | { |
| 172 | "batter": |
| 173 | [ |
| 174 | { "id": "1001", "type": "Regular" }, |
| 175 | { "id": "1002", "type": "Chocolate" }, |
| 176 | { "id": "1003", "type": "Blueberry" }, |
| 177 | { "id": "1004", "type": "Devil''s Food" } |
| 178 | ] |
| 179 | }, |
| 180 | "topping": |
| 181 | [ |
| 182 | { "id": "5001", "type": "None" }, |
| 183 | { "id": "5002", "type": "Glazed" }, |
| 184 | { "id": "5005", "type": "Sugar" }, |
| 185 | { "id": "5007", "type": "Powdered Sugar" }, |
| 186 | { "id": "5006", "type": "Chocolate with Sprinkles" }, |
| 187 | { "id": "5003", "type": "Chocolate" }, |
| 188 | { "id": "5004", "type": "Maple" } |
| 189 | ] |
| 190 | }','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html'); |
| 191 | INSERT INTO j2(id,json,src) |
| 192 | VALUES(3,'[ |
| 193 | { |
| 194 | "id": "0001", |
| 195 | "type": "donut", |
| 196 | "name": "Cake", |
| 197 | "ppu": 0.55, |
| 198 | "batters": |
| 199 | { |
| 200 | "batter": |
| 201 | [ |
| 202 | { "id": "1001", "type": "Regular" }, |
| 203 | { "id": "1002", "type": "Chocolate" }, |
| 204 | { "id": "1003", "type": "Blueberry" }, |
| 205 | { "id": "1004", "type": "Devil''s Food" } |
| 206 | ] |
| 207 | }, |
| 208 | "topping": |
| 209 | [ |
| 210 | { "id": "5001", "type": "None" }, |
| 211 | { "id": "5002", "type": "Glazed" }, |
| 212 | { "id": "5005", "type": "Sugar" }, |
| 213 | { "id": "5007", "type": "Powdered Sugar" }, |
| 214 | { "id": "5006", "type": "Chocolate with Sprinkles" }, |
| 215 | { "id": "5003", "type": "Chocolate" }, |
| 216 | { "id": "5004", "type": "Maple" } |
| 217 | ] |
| 218 | }, |
| 219 | { |
| 220 | "id": "0002", |
| 221 | "type": "donut", |
| 222 | "name": "Raised", |
| 223 | "ppu": 0.55, |
| 224 | "batters": |
| 225 | { |
| 226 | "batter": |
| 227 | [ |
| 228 | { "id": "1001", "type": "Regular" } |
| 229 | ] |
| 230 | }, |
| 231 | "topping": |
| 232 | [ |
| 233 | { "id": "5001", "type": "None" }, |
| 234 | { "id": "5002", "type": "Glazed" }, |
| 235 | { "id": "5005", "type": "Sugar" }, |
| 236 | { "id": "5003", "type": "Chocolate" }, |
| 237 | { "id": "5004", "type": "Maple" } |
| 238 | ] |
| 239 | }, |
| 240 | { |
| 241 | "id": "0003", |
| 242 | "type": "donut", |
| 243 | "name": "Old Fashioned", |
| 244 | "ppu": 0.55, |
| 245 | "batters": |
| 246 | { |
| 247 | "batter": |
| 248 | [ |
| 249 | { "id": "1001", "type": "Regular" }, |
| 250 | { "id": "1002", "type": "Chocolate" } |
| 251 | ] |
| 252 | }, |
| 253 | "topping": |
| 254 | [ |
| 255 | { "id": "5001", "type": "None" }, |
| 256 | { "id": "5002", "type": "Glazed" }, |
| 257 | { "id": "5003", "type": "Chocolate" }, |
| 258 | { "id": "5004", "type": "Maple" } |
| 259 | ] |
| 260 | } |
| 261 | ]','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html'); |
| 262 | SELECT count(*) FROM j2; |
| 263 | } {3} |
| 264 | |
| 265 | do_execsql_test json-5.2 { |
| 266 | SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id; |
| 267 | } {1 1 object | 2 1 object | 3 1 array |} |
| 268 | |
| 269 | ifcapable !vtab { |
| 270 | finish_test |
| 271 | return |
| 272 | } |
| 273 | |
| 274 | # fullkey is always the same as path+key (with appropriate formatting) |
| 275 | # |
| 276 | do_execsql_test json-5.3 { |
| 277 | SELECT j2.rowid, jx.rowid, fullkey, path, key |
| 278 | FROM j2, json_tree(j2.json) AS jx |
| 279 | WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']' |
| 280 | ELSE '.'||key END); |
| 281 | } {} |
| 282 | do_execsql_test json-5.4 { |
| 283 | SELECT j2.rowid, jx.rowid, fullkey, path, key |
| 284 | FROM j2, json_each(j2.json) AS jx |
| 285 | WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']' |
| 286 | ELSE '.'||key END); |
| 287 | } {} |
| 288 | |
| 289 | |
| 290 | # Verify that the json_each.json and json_tree.json output is always the |
| 291 | # same as input. |
| 292 | # |
| 293 | do_execsql_test json-5.5 { |
| 294 | SELECT j2.rowid, jx.rowid, fullkey, path, key |
| 295 | FROM j2, json_each(j2.json) AS jx |
| 296 | WHERE jx.json<>j2.json; |
| 297 | } {} |
| 298 | do_execsql_test json-5.6 { |
| 299 | SELECT j2.rowid, jx.rowid, fullkey, path, key |
| 300 | FROM j2, json_tree(j2.json) AS jx |
| 301 | WHERE jx.json<>j2.json; |
| 302 | } {} |
| 303 | do_execsql_test json-5.7 { |
| 304 | SELECT j2.rowid, jx.rowid, fullkey, path, key |
| 305 | FROM j2, json_each(j2.json) AS jx |
| 306 | WHERE jx.value<>jx.atom AND type NOT IN ('array','object'); |
| 307 | } {} |
| 308 | do_execsql_test json-5.8 { |
| 309 | SELECT j2.rowid, jx.rowid, fullkey, path, key |
| 310 | FROM j2, json_tree(j2.json) AS jx |
| 311 | WHERE jx.value<>jx.atom AND type NOT IN ('array','object'); |
| 312 | } {} |
| 313 | |
drh | f27cd1f | 2015-09-23 01:10:29 +0000 | [diff] [blame] | 314 | do_execsql_test json-6.1 { |
| 315 | SELECT json_valid('{"a":55,"b":72,}'); |
| 316 | } {0} |
| 317 | do_execsql_test json-6.2 { |
| 318 | SELECT json_valid('{"a":55,"b":72}'); |
| 319 | } {1} |
| 320 | do_execsql_test json-6.3 { |
| 321 | SELECT json_valid('["a",55,"b",72,]'); |
| 322 | } {0} |
| 323 | do_execsql_test json-6.4 { |
| 324 | SELECT json_valid('["a",55,"b",72]'); |
| 325 | } {1} |
drh | 20b3b61 | 2015-08-29 18:30:30 +0000 | [diff] [blame] | 326 | |
| 327 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 328 | finish_test |