Skip to content

Commit

Permalink
(#33) - Implemented 'createTestingDatabase' function for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos8896 committed Sep 7, 2018
1 parent dabac00 commit d160305
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
5 changes: 4 additions & 1 deletion common/services/__tests__/integration/body-area.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
const {
getFreePort,
getBaseURLWithPort,
createTestingDatabase,
} = require( '../../../../dev/testing/environment-utils' );

const app = require( '../../../../server/server' );
Expand Down Expand Up @@ -50,6 +51,8 @@ beforeEach( () => server = app.listen( port ) );

afterEach( async () => {

await createTestingDatabase();

await resetTables(
app.dataSources.mysql_ds,
['BodyArea', 'BodyArea_Exercise_Detail', 'Administrator', 'Customer']
Expand Down Expand Up @@ -81,7 +84,7 @@ describe( 'ACLs from BodyArea model', () => {
expect( registeredBodyArea.description ).toBe( bodyArea.description );
expect( bodyAreaCount ).toBe( 1 );

// const message = { port: server.address().port };
// const msessage = { port: server.address().port };
// throw message;


Expand Down
27 changes: 27 additions & 0 deletions dev/testing/environment-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,38 @@ const getFreePort = () => portfinder.getPortPromise();
const getBaseURLWithPort = ( port ) => `http://${process.env.TEST_API_HOST}:${port}/api`;


const createTestingDatabase = () => {

const mysql = require( 'mysql' );
const pool = mysql.createPool({
host: process.env.TEST_API_HOST,
user: process.env.TEST_DB_USER,
password: process.env.TEST_DB_PASSWORD,
});

return new Promise( ( resolve, reject ) => {

pool.query( `
CREATE DATABASE IF NOT EXISTS
${process.env.TEST_DB_NAME}_${process.env.JEST_WORKER_ID}`,
function( error, results ) {

if ( error ) return reject( error );

return resolve( results );

});

});

};

if ( process.env.NODE_ENV === 'test' ) {

module.exports = {
getFreePort,
getBaseURLWithPort,
createTestingDatabase,
};

}
58 changes: 18 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"loopback-ds-readonly-mixin": "^2.0.4",
"loopback-ds-timestamp-mixin": "^3.4.1",
"mkdirp": "^0.5.1",
"mysql": "^2.16.0",
"node-readfiles": "^0.2.0",
"prompt": "^1.0.0",
"serve-favicon": "^2.0.1",
Expand Down

0 comments on commit d160305

Please sign in to comment.