Skip to content

Commit

Permalink
refactor(test): fix bugs after rewrite and make tests running
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
  • Loading branch information
wzrdtales committed May 14, 2019
1 parent 265de5a commit 1013c84
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
22 changes: 11 additions & 11 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ dbmigrate.prototype = {
}
}

return Promise.resolve(executeUp(this.internals, this.config)).asCallback(
return Promise.resolve(executeUp(this.internals, this.config)).nodeify(
callback
);
},
Expand Down Expand Up @@ -226,9 +226,9 @@ dbmigrate.prototype = {
}
}

return Promise.promisify(
executeDown(this.internals, this.config)
).asCallback(callback);
return Promise.resolve(executeDown(this.internals, this.config)).asCallback(
callback
);
},

check: function (specification, opts, callback) {
Expand All @@ -249,7 +249,7 @@ dbmigrate.prototype = {
}
}

return Promise.promisify(
return Promise.resolve(
executeCheck(this.internals, this.config)
).asCallback(callback);
},
Expand All @@ -275,9 +275,9 @@ dbmigrate.prototype = {
}
}

return Promise.promisify(
executeSync(this.internals, this.config)
).asCallback(callback);
return Promise.resolve(executeSync(this.internals, this.config)).asCallback(
callback
);
},

/**
Expand All @@ -294,9 +294,9 @@ dbmigrate.prototype = {
}

this.internals.argv.count = Number.MAX_VALUE;
return Promise.promisify(
executeDown(this.internals, this.config)
).asCallback(callback);
return Promise.resolve(executeDown(this.internals, this.config)).asCallback(
callback
);
},

/**
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ var log = require('db-migrate-shared').log;
exports.dataType = require('db-migrate-shared').dataType;

function loadPluginList (options) {
try {
fs.accessSync(path.join(options.cwd, 'package.json'), fs.constants.R_OK);
} catch (err) {
return {};
}
var plugins = JSON.parse(
fs.readFileSync(path.join(options.cwd, 'package.json'), 'utf-8')
);
Expand Down
8 changes: 6 additions & 2 deletions lib/commands/create-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ const optimist = require('optimist');
const util = require('util');

async function createMigrationDir (dir) {
await stat(dir);
return mkdirp(dir);
const res = await stat(dir).catch(err => ({ err: true }));
if (res && res.err === true) {
return mkdirp(dir);
}

return Promise.resolve();
}

async function executeCreateMigration (internals, config) {
Expand Down
19 changes: 2 additions & 17 deletions test/integration/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,9 @@ lab.experiment('api', function () {
function () {
return Promise.resolve([
[], // promise
[sinon.spy()],
['nameatargetmigration', sinon.spy()], // targeted migration
['nameatargetmigration'], // promise targeted migration
[1, sinon.spy()], // targeted migration
[1], // promise targeted migration
['nameatargetmigration', 'testscope', sinon.spy()], // scoped target
['nameatargetmigration', 'testscope'], // promise scope target
[1, 'testscope', sinon.spy()], // scoped target
[1, 'testscope'] // promise scope target
])
.each(defaultExecParams('up'))
Expand All @@ -157,10 +152,7 @@ lab.experiment('api', function () {
function () {
return Promise.resolve([
[], // promise
[sinon.spy()],
[1, sinon.spy()], // targeted migration
[1], // promise targeted migration
[1, 'testscope', sinon.spy()], // scoped target
[1, 'testscope'] // promise scope target
])
.each(defaultExecParams('down'))
Expand All @@ -174,10 +166,7 @@ lab.experiment('api', function () {
function () {
return Promise.resolve([
[], // promise
[sinon.spy()],
[1, sinon.spy()], // targeted migration
[1], // promise targeted migration
[1, 'testscope', sinon.spy()], // scoped target
[1, 'testscope'] // promise scope target
])
.each(defaultExecParams('check'))
Expand All @@ -191,8 +180,6 @@ lab.experiment('api', function () {
function () {
return Promise.resolve([
[], // promise
[sinon.spy()],
['testscope', sinon.spy()], // scoped target
['testscope'] // promise scope target
])
.each(defaultExecParams('reset'))
Expand All @@ -206,9 +193,7 @@ lab.experiment('api', function () {
function () {
return Promise.resolve([
[],
['nameatargetmigration', sinon.spy()], // targeted migration
['nameatargetmigration'], // promise targeted migration
['nameatargetmigration', 'testscope', sinon.spy()], // scoped target
['nameatargetmigration', 'testscope'] // promise scope target
])
.each(defaultExecParams('sync'))
Expand All @@ -226,7 +211,7 @@ function defaultExecParams (method) {

return [api[method].apply(api, args), args];

function stub (internals, config, callback) {
async function stub (internals, config) {
if (typeof args[0] === 'string') {
Code.expect(internals.argv.destination).to.equal(args[0]);
} else if (typeof args[0] === 'number') {
Expand All @@ -238,7 +223,7 @@ function defaultExecParams (method) {
Code.expect(internals.matching).to.equal(args[1]);
}

callback();
return Promise.resolve();
}
};
}
Expand Down
6 changes: 5 additions & 1 deletion test/integration/create_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ lab.experiment('create', function () {
lab.before(function (done) {
wipeMigrations(function (err) {
Code.expect(err).to.be.null();
dbMigrate('create', 'first migration').on('exit', function (code) {
const db = dbMigrate('create', 'first migration');
// db.stderr.on('data', data => console.log(data.toString()));
// db.stdout.on('data', data => console.log(data.toString()));

db.on('exit', function (code) {
exitCode = code;
done();
});
Expand Down
21 changes: 13 additions & 8 deletions test/on_complete_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var lab = (exports.lab = Lab.script());
var onComplete = require('../lib/commands/on-complete');

lab.experiment('on-complete', function () {
lab.test('should invoke the callback with the results', function (done) {
lab.test('should return a promise with the results', async function () {
var migratorMock = {
driver: {
close: function (cb) {
Expand All @@ -13,15 +13,20 @@ lab.experiment('on-complete', function () {
}
};
var internalsMock = {
argv: { }
argv: {}
};

var resultsPassedToCallback = 'callback should be invoked with results';
var testCallback = function (err, res) {
Code.expect(err).to.equal(null);
Code.expect(res).to.equal(resultsPassedToCallback);
done();
};
onComplete(migratorMock, internalsMock, testCallback, null, resultsPassedToCallback);
const { err, res } = await onComplete(
migratorMock,
internalsMock,
null,
resultsPassedToCallback
)
.then(res => ({ res }))
.catch(err => ({ err }));

Code.expect(err).to.equal(undefined);
Code.expect(res).to.equal(resultsPassedToCallback);
});
});

0 comments on commit 1013c84

Please sign in to comment.