|
1 | 1 | var sqlite3 = require('..');
|
2 | 2 | var assert = require('assert');
|
| 3 | +var path = require('path'); |
3 | 4 |
|
4 | 5 | describe('user functions', function() {
|
5 | 6 | var db;
|
6 | 7 | before(function(done) { db = new sqlite3.Database(':memory:', done); });
|
7 | 8 |
|
8 | 9 | it('should allow registration of user functions', function() {
|
9 |
| - db.registerFunction('MY_UPPERCASE', function(value) { |
10 |
| - return value.toUpperCase(); |
11 |
| - }); |
12 |
| - db.registerFunction('MY_STRING_JOIN', function(value1, value2) { |
13 |
| - return [value1, value2].join(' '); |
14 |
| - }); |
15 |
| - db.registerFunction('MY_Add', function(value1, value2) { |
16 |
| - return value1 + value2; |
17 |
| - }); |
18 |
| - db.registerFunction('MY_REGEX', function(regex, value) { |
19 |
| - return !!value.match(new RegExp(regex)); |
20 |
| - }); |
21 |
| - db.registerFunction('MY_REGEX_VALUE', function(regex, value) { |
22 |
| - return /match things/i; |
23 |
| - }); |
24 |
| - db.registerFunction('MY_ERROR', function(value) { |
25 |
| - throw new Error('This function always throws'); |
26 |
| - }); |
27 |
| - db.registerFunction('MY_UNHANDLED_TYPE', function(value) { |
28 |
| - return {}; |
29 |
| - }); |
30 |
| - db.registerFunction('MY_NOTHING', function(value) { |
31 |
| - |
32 |
| - }); |
| 10 | + db.registerFunction('MY_UPPERCASE', path.join(__dirname, 'support/user_functions.js')); |
| 11 | + db.registerFunction('MY_STRING_JOIN', path.join(__dirname, 'support/user_functions.js')); |
| 12 | + db.registerFunction('MY_Add', path.join(__dirname, 'support/user_functions.js')); |
| 13 | + db.registerFunction('MY_REGEX', path.join(__dirname, 'support/user_functions.js')); |
| 14 | + db.registerFunction('MY_REGEX_VALUE', path.join(__dirname, 'support/user_functions.js')); |
| 15 | + db.registerFunction('MY_ERROR', path.join(__dirname, 'support/user_functions.js')); |
| 16 | + db.registerFunction('MY_UNHANDLED_TYPE', path.join(__dirname, 'support/user_functions.js')); |
| 17 | + db.registerFunction('MY_NOTHING', path.join(__dirname, 'support/user_functions.js')); |
| 18 | + db.registerFunction('MY_INVALID_SCOPING', path.join(__dirname, 'support/user_functions.js')); |
| 19 | + db.registerFunction('MY_REQUIRE', path.join(__dirname, 'support/user_functions.js')); |
33 | 20 | });
|
34 | 21 |
|
35 | 22 | it('should process user functions with one arg', function(done) {
|
@@ -103,5 +90,22 @@ describe('user functions', function() {
|
103 | 90 | });
|
104 | 91 | });
|
105 | 92 |
|
| 93 | + it('does not allow access to external scope', function(done) { |
| 94 | + db.all('SELECT MY_INVALID_SCOPING() AS val', function(err, rows) { |
| 95 | + assert.equal(err.message, 'SQLITE_ERROR: Uncaught ReferenceError: db is not defined'); |
| 96 | + assert.equal(rows, undefined); |
| 97 | + done(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('allows use of require', function(done) { |
| 102 | + db.all('SELECT MY_REQUIRE() AS val', function(err, rows) { |
| 103 | + if (err) throw err; |
| 104 | + assert.equal(rows.length, 1); |
| 105 | + assert.equal(rows[0].val, undefined); |
| 106 | + done(); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
106 | 110 | after(function(done) { db.close(done); });
|
107 | 111 | });
|
0 commit comments