Skip to content

Commit

Permalink
refactor teardown to not require adapter identity, update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Aug 1, 2015
1 parent a27bea3 commit 865edf9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
27 changes: 15 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ exports.register = function (server, options, next) {

}

// Now init using the proper config and expose the model to Hapi
// Now init using the proper config and expose the model to hapi
waterline.initialize({
connections: options.connections,
adapters: options.adapters
Expand All @@ -84,30 +84,33 @@ exports.register = function (server, options, next) {
// Decorate request with collections so they can be used in extensions easily
server.decorate('request', 'collections', ontology.collections);

// Expose a connection teardown method
// Expose an all-connections teardown method
server.expose('teardown', function (cb) {

var teardowns = {};
var teardowns = [];

var connection;
var identity;
var teardown;
var connectionNames = Object.keys(ontology.connections);
for (var k = 0; k < connectionNames.length; ++k) {

connection = ontology.connections[connectionNames[k]];
identity = connection._adapter.identity;
if (identity) {
teardown = connection._adapter.teardown;

teardowns[identity] = connection._adapter.teardown;

if (typeof teardowns[identity] !== 'function') {
delete teardowns[identity];
}
if (typeof teardown === 'function' &&
teardowns.indexOf(teardown) === -1) {

teardowns.push(teardown);
}

}

Items.parallel.execute(teardowns, cb);
var run = function (item, done) {

return item(done);
};

Items.parallel(teardowns, run, cb);

});

Expand Down
16 changes: 8 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ experiment('Dogwater', function () {

});

test('takes its models option as an array.', function (done) {
test('takes its models option as an array of functions and objects.', function (done) {

var options = {
connections: connections,
Expand All @@ -186,7 +186,7 @@ experiment('Dogwater', function () {

});

test('errors if the models option is not an array or string.', function (done) {
test('throws if the models option is not an array or string.', function (done) {

var options = {
connections: connections,
Expand Down Expand Up @@ -215,6 +215,7 @@ experiment('Dogwater', function () {


test('exposes Waterline collections, connections, and schema.', function (done) {

// Via model definitions, this verifies that a definition can be a function
// to which waterline is passed and from which a definition is returned.

Expand Down Expand Up @@ -256,8 +257,6 @@ experiment('Dogwater', function () {
});

test('exposes connection teardown method, skips when method missing.', function (done) {
// Via model definitions, this verifies that a definition can be a function
// to which waterline is passed and from which a definition is returned.

var options = {
connections: connections,
Expand All @@ -279,16 +278,15 @@ experiment('Dogwater', function () {
expect(toreDown).to.equal(false);
teardown(function (err) {

expect(err).to.not.exist();
expect(toreDown).to.equal(false);
done();
});
});

});

test('exposes connection teardown method, skips when adapter identity missing.', function (done) {
// Via model definitions, this verifies that a definition can be a function
// to which waterline is passed and from which a definition is returned.
test('exposes connection teardown method, succeeds when adapter identity missing.', function (done) {

var options = {
connections: connections,
Expand All @@ -310,7 +308,8 @@ experiment('Dogwater', function () {
expect(toreDown).to.equal(false);
teardown(function (err) {

expect(toreDown).to.equal(false);
expect(err).to.not.exist();
expect(toreDown).to.equal(true);
done();
});
});
Expand Down Expand Up @@ -341,6 +340,7 @@ experiment('Dogwater', function () {
expect(toreDown).to.equal(false);
teardown(function (err) {

expect(err).to.not.exist();
expect(toreDown).to.equal(true);
done();
});
Expand Down

0 comments on commit 865edf9

Please sign in to comment.