forked from WiseLibs/better-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb2b267
commit ab69a58
Showing
9 changed files
with
154 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <sqlite3ext.h> | ||
SQLITE_EXTENSION_INIT1 | ||
|
||
static void TestExtensionFunction(sqlite3_context* pCtx, int nVal, sqlite3_value** _) { | ||
sqlite3_result_double(pCtx, (double)nVal); | ||
} | ||
|
||
int sqlite3_extension_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi) { | ||
SQLITE_EXTENSION_INIT2(pApi) | ||
if (pzErrMsg != 0) *pzErrMsg = 0; | ||
sqlite3_create_function(db, "testExtensionFunction", -1, SQLITE_UTF8, 0, TestExtensionFunction, 0, 0); | ||
return SQLITE_OK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
var expect = require('chai').expect; | ||
var Database = require('../.'); | ||
var util = (function () { | ||
var path = require('path'); | ||
var dbId = 0; | ||
var obj; | ||
return obj = { | ||
current: function () { | ||
return 'temp/' + path.basename(__filename).split('.')[0] + '.' + dbId + '.db'; | ||
}, | ||
next: function () {++dbId; return obj.current();} | ||
}; | ||
}()); | ||
var filepath = (function () { | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
function exists(loc) {try {fs.readFileSync(loc); return true;} catch (_) {return false;}} | ||
var attemps = [ | ||
'../build/Debug/test_extension.node', | ||
'../build/Release/test_extension.node' | ||
].map(function (loc) {return path.join(__dirname, loc);}); | ||
for (var i=0; i<attemps.length; ++i) { | ||
if (exists(attemps[i])) return attemps[i]; | ||
} | ||
throw new TypeError('Could not find test_extension.node'); | ||
}()); | ||
|
||
describe('Database#loadExtension()', function () { | ||
it('should throw if a string argument is not given', function () { | ||
var db = new Database(util.next()); | ||
expect(function () {db.loadExtension();}).to.throw(TypeError); | ||
expect(function () {db.loadExtension(undefined);}).to.throw(TypeError); | ||
expect(function () {db.loadExtension(null);}).to.throw(TypeError); | ||
expect(function () {db.loadExtension(123);}).to.throw(TypeError); | ||
expect(function () {db.loadExtension(new String(filepath));}).to.throw(TypeError); | ||
expect(function () {db.loadExtension([filepath]);}).to.throw(TypeError); | ||
}); | ||
it('should throw if the database is closed', function () { | ||
var db = new Database(util.next()); | ||
db.close(); | ||
expect(function () {db.loadExtension(filepath);}).to.throw(TypeError); | ||
}); | ||
it('should throw if the database is busy', function () { | ||
var db = new Database(util.next()); | ||
var invoked = false; | ||
db.prepare('select 555').pluck().each(function (value) { | ||
expect(value).to.equal(555); | ||
expect(function () {db.loadExtension(filepath);}).to.throw(TypeError); | ||
invoked = true; | ||
}); | ||
expect(invoked).to.be.true; | ||
}); | ||
it('should throw if the extension is not found', function () { | ||
var db = new Database(util.next()); | ||
try { | ||
db.loadExtension(filepath + 'x'); | ||
} catch (err) { | ||
expect(err).to.be.an.instanceof(Database.SqliteError); | ||
expect(err.message).to.be.a('string'); | ||
expect(err.message.length).to.be.above(0); | ||
expect(err.message).to.not.equal('not an error'); | ||
expect(err.code).to.equal('SQLITE_ERROR'); | ||
return; | ||
} | ||
throw new Error('This code should not have been reached'); | ||
}); | ||
it('should register the specified extension', function () { | ||
var db = new Database(util.next()); | ||
expect(db.loadExtension(filepath)).to.equal(db); | ||
expect(db.prepare('SELECT testExtensionFunction(NULL, 123, 99, 2)').pluck().get()).to.equal(4); | ||
expect(db.prepare('SELECT testExtensionFunction(NULL, 2)').pluck().get()).to.equal(2); | ||
}); | ||
it('should not allow registering extensions with SQL', function () { | ||
var db1 = new Database(util.next()); | ||
expect(function () {db1.prepare('SELECT load_extension(?)').get(filepath);}).to.throw(Database.SqliteError); | ||
expect(db1.loadExtension(filepath)).to.equal(db1); | ||
expect(function () {db1.prepare('SELECT load_extension(?)').get(filepath);}).to.throw(Database.SqliteError); | ||
var db2 = new Database(util.next()); | ||
try { | ||
db2.loadExtension(filepath + 'x'); | ||
} catch (err) { | ||
expect(function () {db2.prepare('SELECT load_extension(?)').get(filepath);}).to.throw(Database.SqliteError); | ||
return; | ||
} | ||
throw new Error('This code should not have been reached'); | ||
}); | ||
}); |