Skip to content
Snippets Groups Projects
Commit df0ed4e7 authored by Lloyd Hilaiel's avatar Lloyd Hilaiel
Browse files

(mysql driver) when unit_test is specified at open time, we use a database...

(mysql driver) when unit_test is specified at open time, we use a database with a random name, and clean it up upon cdb connection lose.
parent e4c421f8
No related branches found
No related tags found
No related merge requests found
...@@ -18,9 +18,13 @@ ...@@ -18,9 +18,13 @@
*/ */
const const
mysql = require('mysql'); mysql = require('mysql'),
secrets = require('./secrets');
var client = new mysql.Client(); var client = undefined;
// may get defined at open() time causing a database to be dropped upon connection closing.
var drop_on_close = undefined;
const schemas = [ const schemas = [
"CREATE TABLE IF NOT EXISTS user ( id INTEGER PRIMARY KEY, password TEXT );", "CREATE TABLE IF NOT EXISTS user ( id INTEGER PRIMARY KEY, password TEXT );",
...@@ -30,19 +34,28 @@ const schemas = [ ...@@ -30,19 +34,28 @@ const schemas = [
// open & create the mysql database // open & create the mysql database
exports.open = function(cfg, cb) { exports.open = function(cfg, cb) {
if (client) throw "database is already open!";
client = new mysql.Client();
// mysql config requires // mysql config requires
const defParams = { const defParams = {
host: '127.0.0.1', host: '127.0.0.1',
port: "3306", port: "3306",
user: 'test', user: 'test',
password: 'pass' password: 'pass',
unit_test: false
}; };
Object.keys(defParams).forEach(function(param) { Object.keys(defParams).forEach(function(param) {
client[param] = cfg[param] ? config.param : defParams[param]; client[param] = cfg[param] ? cfg.param : defParams[param];
}); });
var database = cfg.database ? cfg.database : 'browserid'; // let's figure out the database name
var database = cfg.database;
if (!database) database = "browserid";
if (cfg.unit_test) {
database += "_" + secrets.generate(8);
drop_on_close = database;
}
client.connect(function(error) { client.connect(function(error) {
if (error) cb(error); if (error) cb(error);
...@@ -78,9 +91,21 @@ exports.open = function(cfg, cb) { ...@@ -78,9 +91,21 @@ exports.open = function(cfg, cb) {
}; };
exports.close = function(cb) { exports.close = function(cb) {
client.end(function(err) { function endConn() {
if (cb) cb(err); client.end(function(err) {
}); client = undefined;
if (cb) cb(err);
});
}
// when unit_test is specified at open time, we use a temporary database,
// and clean it up upon close.
if (drop_on_close) {
client.query("DROP DATABASE " + drop_on_close, function() {
endConn();
});
} else {
endConn();
}
}; };
exports.emailKnown = function() { exports.emailKnown = function() {
......
...@@ -22,7 +22,7 @@ function addTestsForDriver(driver) { ...@@ -22,7 +22,7 @@ function addTestsForDriver(driver) {
// set up mysql. // set up mysql.
suite.addBatch({ suite.addBatch({
"mysql server": { "mysql server": {
topic: function() { db.open({driver: driver}, this.callback) }, topic: function() { db.open({driver: driver, unit_test: true}, this.callback) },
"accepting connections": function(err) { "accepting connections": function(err) {
if (err) { if (err) {
console.log("SKIPPING MYSQL TESTING: I cannot connect to the mysql database (" + err.message + ")"); console.log("SKIPPING MYSQL TESTING: I cannot connect to the mysql database (" + err.message + ")");
...@@ -49,7 +49,7 @@ function addTestsForDriver(driver) { ...@@ -49,7 +49,7 @@ function addTestsForDriver(driver) {
}, },
"opening the database": { "opening the database": {
topic: function() { topic: function() {
db.open({ driver: driver, path: dbPath }, this.callback); db.open({ driver: driver, unit_test: true, path: dbPath }, this.callback);
}, },
"and its ready": function(r) { "and its ready": function(r) {
assert.isUndefined(r); assert.isUndefined(r);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment