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