|
1 | 1 | exports.test = function(SQL, assert){ |
2 | | - var db = new SQL.Database(); |
3 | | - db.exec("CREATE TABLE test (data); INSERT INTO test VALUES ('Hello World');"); |
4 | | - |
5 | | - // Simple function, appends extra text on a string. |
6 | | - function test_function(string_arg) { |
7 | | - return "Function called with: " + string_arg; |
8 | | - }; |
9 | | - |
10 | | - // Register with SQLite. |
11 | | - db.create_function("TestFunction", 1, test_function); |
12 | | - |
13 | | - // Use in a query, check expected result. |
14 | | - var result = db.exec("SELECT TestFunction(data) FROM test;"); |
15 | | - var result_str = result[0]["values"][0][0]; |
16 | | - assert.equal(result_str, "Function called with: Hello World"); |
17 | | - |
18 | | - // 2 arg function, adds two ints together. |
19 | | - db.exec("CREATE TABLE test2 (int1, int2); INSERT INTO test2 VALUES (456, 789);"); |
20 | | - |
21 | | - function test_add(int1, int2) { |
22 | | - return int1 + int2; |
23 | | - }; |
24 | | - |
25 | | - db.create_function("TestAdd", 2, test_add); |
26 | | - result = db.exec("SELECT TestAdd(int1, int2) FROM test2;"); |
27 | | - result_int = result[0]["values"][0][0]; |
28 | | - assert.equal(result_int, 1245); |
29 | | - |
30 | | - // Binary data function, tests which byte in a column is set to 0 |
31 | | - db.exec("CREATE TABLE test3 (data); INSERT INTO test3 VALUES (x'6100ff'), (x'ffffff00ffff');"); |
32 | | - |
33 | | - function test_zero_byte_index(data) { |
34 | | - // Data is a Uint8Array |
35 | | - for (var i=0; i<data.length; i++) { |
36 | | - if (data[i] === 0) { |
37 | | - return i; |
38 | | - } |
39 | | - } |
40 | | - return -1; |
41 | | - }; |
42 | | - |
43 | | - db.create_function("TestZeroByteIndex", 1, test_zero_byte_index); |
44 | | - result = db.exec("SELECT TestZeroByteIndex(data) FROM test3;"); |
45 | | - result_int0 = result[0]["values"][0][0]; |
46 | | - result_int1 = result[0]["values"][1][0]; |
47 | | - assert.equal(result_int0, 1); |
48 | | - assert.equal(result_int1, 3); |
49 | | - |
50 | | - db.close(); |
| 2 | + var db = new SQL.Database(); |
| 3 | + db.exec("CREATE TABLE test (data); INSERT INTO test VALUES ('Hello World');"); |
| 4 | + |
| 5 | + // Simple function, appends extra text on a string. |
| 6 | + function test_function(string_arg) { |
| 7 | + return "Function called with: " + string_arg; |
| 8 | + }; |
| 9 | + |
| 10 | + // Register with SQLite. |
| 11 | + db.create_function("TestFunction", test_function); |
| 12 | + |
| 13 | + // Use in a query, check expected result. |
| 14 | + var result = db.exec("SELECT TestFunction(data) FROM test;"); |
| 15 | + var result_str = result[0]["values"][0][0]; |
| 16 | + assert.equal(result_str, "Function called with: Hello World", "Named functions can be registered"); |
| 17 | + |
| 18 | + // 2 arg function, adds two ints together. |
| 19 | + db.exec("CREATE TABLE test2 (int1, int2); INSERT INTO test2 VALUES (456, 789);"); |
| 20 | + |
| 21 | + function test_add(int1, int2) { |
| 22 | + return int1 + int2; |
| 23 | + }; |
| 24 | + |
| 25 | + db.create_function("TestAdd", test_add); |
| 26 | + result = db.exec("SELECT TestAdd(int1, int2) FROM test2;"); |
| 27 | + result_int = result[0]["values"][0][0]; |
| 28 | + assert.equal(result_int, 1245, "Multiple argument functions can be registered"); |
| 29 | + |
| 30 | + // Binary data function, tests which byte in a column is set to 0 |
| 31 | + db.exec("CREATE TABLE test3 (data); INSERT INTO test3 VALUES (x'6100ff'), (x'ffffff00ffff');"); |
| 32 | + |
| 33 | + function test_zero_byte_index(data) { |
| 34 | + // Data is a Uint8Array |
| 35 | + for (var i=0; i<data.length; i++) { |
| 36 | + if (data[i] === 0) { |
| 37 | + return i; |
| 38 | + } |
| 39 | + } |
| 40 | + return -1; |
| 41 | + }; |
| 42 | + |
| 43 | + db.create_function("TestZeroByteIndex", test_zero_byte_index); |
| 44 | + result = db.exec("SELECT TestZeroByteIndex(data) FROM test3;"); |
| 45 | + result_int0 = result[0]["values"][0][0]; |
| 46 | + result_int1 = result[0]["values"][1][0]; |
| 47 | + assert.equal(result_int0, 1, "Binary data works inside functions"); |
| 48 | + assert.equal(result_int1, 3, "Binary data works inside functions"); |
| 49 | + |
| 50 | + db.create_function("addOne", function (x) { return x + 1;} ); |
| 51 | + result = db.exec("SELECT addOne(1);"); |
| 52 | + assert.equal(result[0]["values"][0][0], 2, "Accepts anonymous functions"); |
| 53 | + |
| 54 | + db.close(); |
51 | 55 | }; |
52 | 56 |
|
53 | 57 | if (module == require.main) { |
54 | | - var sql = require('../js/sql.js'); |
55 | | - var assert = require("assert"); |
56 | | - exports.test(sql, assert); |
| 58 | + var sql = require('../js/sql.js'); |
| 59 | + var assert = require("assert"); |
| 60 | + exports.test(sql, assert); |
57 | 61 | } |
0 commit comments