Skip to content

Commit

Permalink
added support to drop the tables, we make use of it in unit tests in …
Browse files Browse the repository at this point in the history
…an afterEach method
  • Loading branch information
christophertrudel committed Sep 26, 2014
1 parent 0d366e3 commit e502606
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ eg: node setup.js 'travis_ci_test', 'postgres', '12345', 'acl_', 192.168.56.10,
eg: node setup.js null, null, null, 'acl_', null, null, 'postgres://postgres:[email protected]:5432/travis_ci_test'
typically passing db is for use within code (we use it for rebuilding acl in unit tests)
var createTables = require('node_modules/acl-knex/lib/createTables').createTables;
var createTables = require('node_modules/acl-knex/lib/databaseTasks').createTables;
createTables([
null,
null,
Expand Down
33 changes: 27 additions & 6 deletions lib/createTables.js → lib/databaseTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function tmpl(str, ctx) {
return sql.replace(/\?/g, function() { return '$' + n++; });
}

function createTables(args, callback) {
function getDB(args, callback) {
var connection = null;
var db_name = args[0], username = args[1], password = args[2], prefix = args[3], db_host = args[4], db_port = args[5], db_url = args[6], db = args[7];
var db_name = args[0], username = args[1], password = args[2], db_host = args[4], db_port = args[5], db_url = args[6], db = args[7];
if (!db && !db_url) {
if (!db_name) throw Error('no db_name (1st arg) supplied');
if (!username) throw Error('no username (2nd arg) supplied');
Expand All @@ -41,14 +41,34 @@ function createTables(args, callback) {
connection = db_url;
}

return db = knex({
client: 'postgres',
connection: connection
});
}

function dropTables(args, callback) {
var prefix = args[3], db = args[7];

if (!db) {
db = getDB(args);
}
if (!prefix) prefix = 'acl_';

db.raw(tmpl(downSql, {'prefix': prefix}))
.then(function() {
callback(null, db);
})
;
}

function createTables(args, callback) {
var prefix = args[3], db = args[7];

if (!db) {
db = knex({
client: 'postgres',
connection: connection
});
db = getDB(args);
}
if (!prefix) prefix = 'acl_';

db.raw(tmpl(downSql+upSql, {'prefix': prefix}))
.then(function() {
Expand All @@ -58,3 +78,4 @@ function createTables(args, callback) {
}

exports.createTables = createTables;
exports.dropTables = dropTables;
5 changes: 3 additions & 2 deletions lib/knex-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var contract = require('./contract');
var async = require('async');
var _ = require('lodash');
var createTables = require('../lib/createTables').createTables;
var createTables = require('../lib/databaseTasks').createTables;

function KnexDBBackend(db, client, prefix){
this.db = db;
Expand Down Expand Up @@ -349,6 +349,7 @@ KnexDBBackend.prototype = {
}
};

KnexDBBackend.prototype.setup = require('../lib/createTables').createTables;
KnexDBBackend.prototype.setup = createTables;
KnexDBBackend.prototype.teardown = require('../lib/databaseTasks').dropTables;

exports = module.exports = KnexDBBackend;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acl-knex",
"version": "0.2.0",
"version": "0.2.1",
"description": "A Knex.js backend for node_acl",
"keywords": [
"middleware",
Expand Down
2 changes: 1 addition & 1 deletion setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var createTables = require('./lib/createTables').createTables;
var createTables = require('./lib/databaseTasks').createTables;

createTables(process.argv.slice(2), function() {
process.exit();
Expand Down
130 changes: 107 additions & 23 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var knex = require('knex');
var KnexBackend = require('../');
var tests = require('../node_modules/acl/test/tests');
var assert = require('chai').assert;
var error = null;

function run() {
Object.keys(tests).forEach(function (test) {
Expand All @@ -11,40 +13,122 @@ function run() {
}

describe('Postgres', function () {
describe('with passing db', function () {
before(function (done) {
var self = this;
var db = knex({
client: 'postgres',
connection: 'postgres://[email protected]:5432/travis_ci_test'
describe('testing setup method', function () {
before(function () {
error = null;
});

describe('with passing db', function () {
before(function (done) {
var self = this;
var db = knex({
client: 'postgres',
connection: 'postgres://[email protected]:5432/travis_ci_test'
});
new KnexBackend().setup([null, null, null, null, null, null, null, db], function(err, db) {
error = err;
if (err) return done(err);
done();
});
});
new KnexBackend().setup([null, null, null, null, null, null, null, db], function(err, db) {
if (err) return done(err);
self.backend = new KnexBackend(db, 'postgres', 'acl_');
done();

it('should create tables in database', function () {
assert(!error);
});

describe('and then using teardown method', function () {
before(function (done) {
var self = this;
var db = knex({
client: 'postgres',
connection: 'postgres://[email protected]:5432/travis_ci_test'
});
new KnexBackend().teardown([null, null, null, null, null, null, null, db], function(err, db) {
error = err;
if (err) return done(err);
done();
});
});

it('should drop tables in database', function () {
assert(!error);
});
});
});

run();
});
describe('with connection string', function () {
before(function (done) {
var self = this;
describe('with connection string', function () {
before(function (done) {
var self = this;

new KnexBackend().setup([null, null, null, null, null, null, 'postgres://[email protected]:5432/travis_ci_test'], function(err, db) {
error = err;
if (err) return done(err);
done();
});
});

new KnexBackend().setup([null, null, null, null, null, null, 'postgres://[email protected]:5432/travis_ci_test'], function(err, db) {
if (err) return done(err);
self.backend = new KnexBackend(db, 'postgres', 'acl_');
done();
it('should create tables in database', function () {
assert(!error);
});

describe('and then using teardown method', function () {
before(function (done) {
var self = this;

new KnexBackend().teardown([null, null, null, null, null, null, 'postgres://[email protected]:5432/travis_ci_test'], function(err, db) {
error = err;
if (err) return done(err);
done();
});
});

it('should drop tables in database', function () {
assert(!error);
});
});
});

run();
describe('without connection string', function () {
before(function (done) {
var self = this;

new KnexBackend().setup(['travis_ci_test', 'postgres'], function(err, db) {
error = err;
if (err) return done(err);
done();
});
});

it('should create tables in database', function () {
assert(!error);
});

describe('and then using teardown method', function () {
before(function (done) {
var self = this;

new KnexBackend().teardown(['travis_ci_test', 'postgres'], function(err, db) {
error = err;
if (err) return done(err);
done();
});
});

it('should drop tables in database', function () {
assert(!error);
});
});
});
});
describe('without connection string', function () {

describe('Acl Test', function () {
before(function (done) {
var self = this;

new KnexBackend().setup(['travis_ci_test', 'postgres'], function(err, db) {
var db = knex({
client: 'postgres',
connection: 'postgres://[email protected]:5432/travis_ci_test'
});
new KnexBackend().setup([null, null, null, null, null, null, null, db], function(err, db) {
if (err) return done(err);
self.backend = new KnexBackend(db, 'postgres', 'acl_');
done();
Expand Down

0 comments on commit e502606

Please sign in to comment.