Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test improvements #6

Merged
merged 19 commits into from
Jul 15, 2019
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
- run:
name: 'Running Unit Tests'
command: |
npm run test:ci
# npm run test:ci
npm test -- --user=$POSTGRES_USER --host=$POSTGRES_HOST --port=$POSTGRES_PORT --db=$POSTGRES_DATABASE --password=$POSTGRES_PASSWORD
- store_test_results:
path: test-results
- run:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.env
node_modules
.DS_Store
migrations
./migrations/
.npgmrc
mochawesome-report
coverage
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
2 changes: 1 addition & 1 deletion db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
* Get client
* @param {*} args
*/
function getDbClient(args) {
function getDbClient(args={}) {
return knex({
client: 'pg',
connection: args.connection || {
Expand Down
35 changes: 35 additions & 0 deletions migrations/01.ordered.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

'use strict';

/***********************************************************************************************************************************************
* NODE DB MIGRATE - ORDERED
***********************************************************************************************************************************************
* @author File generated by @liveaxle/node-pg-migrate
* @description
*
*/

/**
* [exports description]
* @type {Object}
*/
module.exports = {
up, down
};

/**
* [up description]
* @return {[type]} [description]
*/
async function up(client) {

}

/**
* [down description]
* @return {[type]} [description]
*/
async function down(client) {

}

35 changes: 35 additions & 0 deletions migrations/02.ordered.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

'use strict';

/***********************************************************************************************************************************************
* NODE DB MIGRATE - ORDERED
***********************************************************************************************************************************************
* @author File generated by @liveaxle/node-pg-migrate
* @description
*
*/

/**
* [exports description]
* @type {Object}
*/
module.exports = {
up, down
};

/**
* [up description]
* @return {[type]} [description]
*/
async function up(client) {

}

/**
* [down description]
* @return {[type]} [description]
*/
async function down(client) {

}

3 changes: 2 additions & 1 deletion tasks/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ module.exports = async function(args={}, client, task) {

// Build migrations path
let dir = path.join(process.cwd(), value.directory);

console.log(value, process.cwd(), dir);

// Check if dir exists - if not make it
let exists = fs.existsSync(dir);

Expand Down
76 changes: 71 additions & 5 deletions test/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
*/
const chai = require('chai');
const path = require('path');
const dotenv = require('dotenv').config({path: path.join(process.cwd(), '.env')});
const fs = require('fs');
const {exec} = require('child_process');
const {client} = require('../db');
const args = require('minimist')(process.argv.slice(2));

console.log(args)
//
// Chai constants
//------------------------------------------------------------------------------------------//
Expand Down Expand Up @@ -67,15 +71,77 @@ describe('Migrate', () => {
});
});

it('Should run created migrations' ,(done) => {
exec('node ./ up', (err, stdout, stderr) => {
done();
it('Should run up created migrations - sequential' , (done) => {
exec('node ./ up --directory=test/migrations/sequential --ordering=sequential', (err, stdout, stderr) => {
let db = client(args);

db('users').select('*').then(rows => {
assert.lengthOf(rows, 3);
}).then(() => {
return db('books').select('*');
}).then(rows => {
assert.lengthOf(rows, 3);
}).then(() => {
return db('authors').select('*');
}).then(rows => {
assert.lengthOf(rows, 3);
}).then(done).catch(e => {
console.log(e.message);
});;
})
});

it('Should should skip any migrations that have already been executed - sequential' , (done) => {
exec('node ./ up --directory=test/migrations/sequential --ordering=sequential', (err, stdout, stderr) => {
let db = client(args);

db('users').select('*').then(rows => {
assert.lengthOf(rows, 3);
}).then(() => {
return db('books').select('*');
}).then(rows => {
assert.lengthOf(rows, 3);
}).then(() => {
return db('authors').select('*');
}).then(rows => {
assert.lengthOf(rows, 3);
}).then(done).catch(e => {
console.log(e.message);

});
})
});

it('Should list migrations' ,(done) => {
it('Should run down migrations - sequential' , (done) => {
exec('node ./ down --directory=test/migrations/sequential --ordering=sequential', (err, stdout, stderr) => {
let db = client(args);

db('users').select('*').then(rows => {
if(rows.length) throw new Error('Users Table was not dropped - down migration unsuccessful');
}, (e) => {
assert.isNotNull(e);
});

db('books').select('*').then(rows => {
if(rows.length) throw new Error('Users Table was not dropped - down migration unsuccessful');
}, (e) => {
assert.isNotNull(e);
});

db('authors').select('*').then(rows => {
if(rows.length) throw new Error('Users Table was not dropped - down migration unsuccessful');
}, (e) => {
assert.isNotNull(e);
}).then(done);
})
});

it('Should list migrations' ,() => {
exec('node ./ list', (err, stdout, stderr) => {
done();
assert.isNotNull(stdout.match(/users/gi))
assert.isNotNull(stdout.match(/books/gi))
assert.isNotNull(stdout.match(/authors/gi));
process.exit(0)
})
});
});
Expand Down
45 changes: 45 additions & 0 deletions test/migrations/sequential/01.users.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

'use strict';

/***********************************************************************************************************************************************
* NODE DB MIGRATE - USERS
***********************************************************************************************************************************************
* @author File generated by @liveaxle/node-pg-migrate
* @description
*
*/

/**
* [exports description]
* @type {Object}
*/
module.exports = {
up, down
};

/**
* [up description]
* @return {[type]} [description]
*/
async function up(client) {
return await (client.schema.raw(`

CREATE TABLE IF NOT EXISTS users (
id serial PRIMARY KEY NOT NULL,
name text
);

INSERT into users (name) VALUES ('Foo');
INSERT into users (name) VALUES ('Bar');
INSERT into users (name) VALUES ('Baz');
`))
}

/**
* [down description]
* @return {[type]} [description]
*/
async function down(client) {
return await client.schema.dropTable('users');
}

45 changes: 45 additions & 0 deletions test/migrations/sequential/02.books.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

'use strict';

/***********************************************************************************************************************************************
* NODE DB MIGRATE - BOOKS
***********************************************************************************************************************************************
* @author File generated by @liveaxle/node-pg-migrate
* @description
*
*/

/**
* [exports description]
* @type {Object}
*/
module.exports = {
up, down
};

/**
* [up description]
* @return {[type]} [description]
*/
async function up(client) {
return await (client.schema.raw(`
CREATE TABLE IF NOT EXISTS books (
id serial PRIMARY KEY,
title text
);


INSERT into books (title) VALUES ('FooBook');
INSERT into books (title) VALUES ('BarBook');
INSERT into books (title) VALUES ('BazBook');
`))
}

/**
* [down description]
* @return {[type]} [description]
*/
async function down(client) {
return await client.schema.dropTable('books');
}

45 changes: 45 additions & 0 deletions test/migrations/sequential/03.authors.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

'use strict';

/***********************************************************************************************************************************************
* NODE DB MIGRATE - AUTHORS
***********************************************************************************************************************************************
* @author File generated by @liveaxle/node-pg-migrate
* @description
*
*/

/**
* [exports description]
* @type {Object}
*/
module.exports = {
up, down
};

/**
* [up description]
* @return {[type]} [description]
*/
async function up(client) {
return await (client.schema.raw(`
CREATE TABLE IF NOT EXISTS authors (
id serial PRIMARY KEY,
user_id serial REFERENCES users,
book_id serial REFERENCES books
);

INSERT into authors (user_id, book_id) VALUES (1, 1);
INSERT into authors (user_id, book_id) VALUES (2, 2);
INSERT into authors (user_id, book_id) VALUES (3, 3);
`))
}

/**
* [down description]
* @return {[type]} [description]
*/
async function down(client) {
return await client.schema.dropTable('authors');
}

35 changes: 35 additions & 0 deletions test/migrations/timestamp/1562967306603.users.migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

'use strict';

/***********************************************************************************************************************************************
* NODE DB MIGRATE - USERS
***********************************************************************************************************************************************
* @author File generated by @liveaxle/node-pg-migrate
* @description
*
*/

/**
* [exports description]
* @type {Object}
*/
module.exports = {
up, down
};

/**
* [up description]
* @return {[type]} [description]
*/
async function up(client) {

}

/**
* [down description]
* @return {[type]} [description]
*/
async function down(client) {

}

Loading