|
| 1 | +exports.test = function(sql, assert) { |
| 2 | + var db = new sql.Database(); |
| 3 | + // tests taken from https://www.sqlite.org/json1.html#jmini |
| 4 | + [ |
| 5 | + // The json() function |
| 6 | + `json(' { "this" : "is", "a": [ "test" ] } ') = '{"this":"is","a":["test"]}'`, |
| 7 | + |
| 8 | + // The json_array_length() function |
| 9 | + `json_array(1,2,'3',4) = '[1,2,"3",4]'`, |
| 10 | + `json_array('[1,2]') = '["[1,2]"]'`, |
| 11 | + `json_array(json_array(1,2)) = '[[1,2]]'`, |
| 12 | + `json_array(1,null,'3','[4,5]','{"six":7.7}') = '[1,null,"3","[4,5]","{\\"six\\":7.7}"]'`, |
| 13 | + `json_array(1,null,'3',json('[4,5]'),json('{"six":7.7}')) = '[1,null,"3",[4,5],{"six":7.7}]'`, |
| 14 | + `json_array_length('[1,2,3,4]') = 4`, |
| 15 | + `json_array_length('[1,2,3,4]', '$') = 4`, |
| 16 | + `json_array_length('[1,2,3,4]', '$[2]') = 0`, |
| 17 | + `json_array_length('{"one":[1,2,3]}') = 0`, |
| 18 | + `json_array_length('{"one":[1,2,3]}', '$.one') = 3`, |
| 19 | + `json_array_length('{"one":[1,2,3]}', '$.two') = null`, |
| 20 | + |
| 21 | + // The json_extract() function |
| 22 | + `json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$') = '{"a":2,"c":[4,5,{"f":7}]}'`, |
| 23 | + `json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c') = '[4,5,{"f":7}]'`, |
| 24 | + `json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]') = '{"f":7}'`, |
| 25 | + `json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f') = 7`, |
| 26 | + `json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a') = '[[4,5],2]'`, |
| 27 | + `json_extract('{"a":2,"c":[4,5],"f":7}','$.c[#-1]') = 5`, |
| 28 | + `json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x') = null`, |
| 29 | + `json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a') = '[null,2]'`, |
| 30 | + |
| 31 | + // The json_insert(), json_replace, and json_set() functions |
| 32 | + `json_insert('[1,2,3,4]','$[#]',99) = '[1,2,3,4,99]'`, |
| 33 | + `json_insert('[1,[2,3],4]','$[1][#]',99) = '[1,[2,3,99],4]'`, |
| 34 | + `json_insert('{"a":2,"c":4}', '$.a', 99) = '{"a":2,"c":4}'`, |
| 35 | + `json_insert('{"a":2,"c":4}', '$.e', 99) = '{"a":2,"c":4,"e":99}'`, |
| 36 | + `json_replace('{"a":2,"c":4}', '$.a', 99) = '{"a":99,"c":4}'`, |
| 37 | + `json_replace('{"a":2,"c":4}', '$.e', 99) = '{"a":2,"c":4}'`, |
| 38 | + `json_set('{"a":2,"c":4}', '$.a', 99) = '{"a":99,"c":4}'`, |
| 39 | + `json_set('{"a":2,"c":4}', '$.e', 99) = '{"a":2,"c":4,"e":99}'`, |
| 40 | + `json_set('{"a":2,"c":4}', '$.c', '[97,96]') = '{"a":2,"c":"[97,96]"}'`, |
| 41 | + `json_set('{"a":2,"c":4}', '$.c', json('[97,96]')) = '{"a":2,"c":[97,96]}'`, |
| 42 | + `json_set('{"a":2,"c":4}', '$.c', json_array(97,96)) = '{"a":2,"c":[97,96]}'`, |
| 43 | + |
| 44 | + // The json_object() function |
| 45 | + `json_object('a',2,'c',4) = '{"a":2,"c":4}'`, |
| 46 | + `json_object('a',2,'c','{e:5}') = '{"a":2,"c":"{e:5}"}'`, |
| 47 | + `json_object('a',2,'c',json_object('e',5)) = '{"a":2,"c":{"e":5}}'`, |
| 48 | + |
| 49 | + // The json_patch() function |
| 50 | + `json_patch('{"a":1,"b":2}','{"c":3,"d":4}') = '{"a":1,"b":2,"c":3,"d":4}'`, |
| 51 | + `json_patch('{"a":[1,2],"b":2}','{"a":9}') = '{"a":9,"b":2}'`, |
| 52 | + `json_patch('{"a":[1,2],"b":2}','{"a":null}') = '{"b":2}'`, |
| 53 | + `json_patch('{"a":1,"b":2}','{"a":9,"b":null,"c":8}') = '{"a":9,"c":8}'`, |
| 54 | + `json_patch('{"a":{"x":1,"y":2},"b":3}','{"a":{"y":9},"c":8}') = '{"a":{"x":1,"y":9},"b":3,"c":8}'`, |
| 55 | + |
| 56 | + // The json_remove() function |
| 57 | + `json_remove('[0,1,2,3,4]','$[2]') = '[0,1,3,4]'`, |
| 58 | + `json_remove('[0,1,2,3,4]','$[2]','$[0]') = '[1,3,4]'`, |
| 59 | + `json_remove('[0,1,2,3,4]','$[0]','$[2]') = '[1,2,4]'`, |
| 60 | + `json_remove('[0,1,2,3,4]','$[#-1]','$[0]') = '[1,2,3]'`, |
| 61 | + `json_remove('{"x":25,"y":42}') = '{"x":25,"y":42}'`, |
| 62 | + `json_remove('{"x":25,"y":42}','$.z') = '{"x":25,"y":42}'`, |
| 63 | + `json_remove('{"x":25,"y":42}','$.y') = '{"x":25}'`, |
| 64 | + `json_remove('{"x":25,"y":42}','$') = null`, |
| 65 | + |
| 66 | + // The json_type() function |
| 67 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}') = 'object'`, |
| 68 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$') = 'object'`, |
| 69 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a') = 'array'`, |
| 70 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[0]') = 'integer'`, |
| 71 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[1]') = 'real'`, |
| 72 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[2]') = 'true'`, |
| 73 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[3]') = 'false'`, |
| 74 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[4]') = 'null'`, |
| 75 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[5]') = 'text'`, |
| 76 | + `json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[6]') = null`, |
| 77 | + |
| 78 | + // The json_valid() function |
| 79 | + `json_valid('{"x":35}') = 1`, |
| 80 | + `json_valid('{"x":35') = 0`, |
| 81 | + |
| 82 | + // The json_quote() function |
| 83 | + `json_quote(3.14159) = 3.14159`, |
| 84 | + `json_quote('verdant') = "verdant"` |
| 85 | + ].forEach(function (sql) { |
| 86 | + assert.equal( |
| 87 | + String(db.exec( |
| 88 | + "SELECT " + sql.split(" = ")[0] + " AS val;" |
| 89 | + )[0].values[0][0]), |
| 90 | + String(sql.split(" = ")[1].replace(/'/g, "")) |
| 91 | + ); |
| 92 | + }); |
| 93 | +}; |
| 94 | + |
| 95 | +if (module == require.main) { |
| 96 | + const target_file = process.argv[2]; |
| 97 | + const sql_loader = require('./load_sql_lib'); |
| 98 | + sql_loader(target_file).then((sql)=>{ |
| 99 | + require('test').run({ |
| 100 | + 'test extension functions': function(assert){ |
| 101 | + exports.test(sql, assert); |
| 102 | + } |
| 103 | + }); |
| 104 | + }) |
| 105 | + .catch((e)=>{ |
| 106 | + console.error(e); |
| 107 | + assert.fail(e); |
| 108 | + }); |
| 109 | +} |
0 commit comments