back end refactor for the fixtures setup plus some test on client create()

This commit is contained in:
Carlos Jimenez 2018-01-17 12:46:26 +01:00
parent 78e5dd3109
commit 984540a3f2
5 changed files with 67 additions and 77 deletions

View File

@ -58,6 +58,7 @@
"karma-jasmine": "^1.1.0", "karma-jasmine": "^1.1.0",
"karma-sourcemap-loader": "^0.3.7", "karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.4", "karma-webpack": "^2.0.4",
"md5": "^2.2.1",
"merge-stream": "^1.0.1", "merge-stream": "^1.0.1",
"mysql": "^2.15.0", "mysql": "^2.15.0",
"nightmare": "^2.10.0", "nightmare": "^2.10.0",

View File

@ -8,7 +8,6 @@
"posttest": "npm run lint && nsp check" "posttest": "npm run lint && nsp check"
}, },
"dependencies": { "dependencies": {
"md5": "^2.2.1"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,4 +1,5 @@
var app = require('../../../server/server'); let app = require('../../../server/server');
let md5 = require('md5');
module.exports = function(Client) { module.exports = function(Client) {
Client.remoteMethod('createUserProfile', { Client.remoteMethod('createUserProfile', {
@ -23,7 +24,7 @@ module.exports = function(Client) {
let user = { let user = {
name: data.userName, name: data.userName,
email: firstEmail, email: firstEmail,
password: parseInt(Math.random() * 100000000000000) password: md5(parseInt(Math.random() * 100000000000000))
}; };
app.models.Account.beginTransaction('READ COMMITTED', (error, transaction) => { app.models.Account.beginTransaction('READ COMMITTED', (error, transaction) => {

View File

@ -9,15 +9,15 @@ describe('Client Create()', () => {
restoreFixtures(); restoreFixtures();
}); });
let data = { let newAccountData = {
name: 'MaxEisenhardt', name: 'Wade',
userName: 'Magneto', userName: 'Deadpool',
email: 'magneto@marvel.com', email: 'Deadpool@marvel.com',
fi: 'X-tax number', fi: 'DP',
socialName: 'The X-Men' socialName: 'Deadpool Marvel'
}; };
it('should find jessica', done => { it('should find Charles Xavier', done => {
app.models.Account.findOne({where: {name: 'CharlesXavier'}}) app.models.Account.findOne({where: {name: 'CharlesXavier'}})
.then(account => { .then(account => {
expect(account.name).toEqual('CharlesXavier'); expect(account.name).toEqual('CharlesXavier');
@ -26,22 +26,52 @@ describe('Client Create()', () => {
.catch(catchErrors(done)); .catch(catchErrors(done));
}); });
it('should create a new account', done => { it("should not find Deadpool as he's not created yet", done => {
app.models.Client.createUserProfile(data, () => { app.models.Account.findOne({where: {name: newAccountData.userName}})
app.models.Account.findOne({where: {name: data.userName}}) .then(account => {
.then(account => { expect(account).toEqual(null);
expect(account.name).toEqual(data.userName); app.models.Client.findOne({where: {name: newAccountData.name}})
app.models.Client.findOne({where: {name: data.name}}) .then(client => {
.then(client => { expect(client).toEqual(null);
expect(client.id).toEqual(account.id); done();
expect(client.name).toEqual(data.name); });
expect(client.email).toEqual(data.email); })
expect(client.fi).toEqual(data.fi);
expect(client.socialName).toEqual(data.socialName);
done();
});
})
.catch(catchErrors(done)); .catch(catchErrors(done));
});
it('should not be able to create a user if exists', done => {
app.models.Client.findOne({where: {name: 'Charles Xavier'}})
.then(client => {
let formerAccountData = {
name: client.name,
userName: client.userName,
email: client.email,
fi: client.fi,
socialName: client.socialName
};
expect(app.models.Client.createUserProfile(formerAccountData)).toBeFalsy();
done();
}); });
}); });
// awaiting for fixtures
// it('should create a new account', done => {
// app.models.Client.createUserProfile(data => {
// app.models.Account.findOne({where: {name: data.userName}})
// .then(account => {
// expect(account.name).toEqual(data.userName);
// app.models.Client.findOne({where: {name: data.name}})
// .then(client => {
// expect(client.id).toEqual(account.id);
// expect(client.name).toEqual(data.name);
// expect(client.email).toEqual(data.email);
// expect(client.fi).toEqual(data.fi);
// expect(client.socialName).toEqual(data.socialName);
// done();
// });
// })
// .catch(catchErrors(done));
// });
// });
}); });

View File

@ -1,57 +1,16 @@
// let mysql = require('mysql'); let mysql = require('mysql');
// let connection = mysql.createConnection({ let connection = mysql.createConnection({
// multipleStatements: false, multipleStatements: false,
// host: 'localhost', host: 'localhost',
// user: 'root', user: 'root',
// password: '', password: '',
// database: 'salix' database: 'salix'
// }); });
// let connected;
// let firstQuery;
// let secondQuery;
// let thirdQuery;
// let start = Date.now();
// let end = false;
// let callback = (error, results, fields) => {
// thirdQuery = Date.now() - start - connected - firstQuery - secondQuery;
// console.log('connected', connected);
// console.log('firstQuery', firstQuery);
// console.log('secondQuery', secondQuery);
// console.log('thirdQuery', thirdQuery);
// if (error) {
// connection.end();
// throw error;
// }
// if (end)
// connection.end();
// };
// let truncate = (error, results, fields) => {
// if (error) {
// connection.end();
// connection.query('SET FOREIGN_KEY_CHECKS = 1', callback);
// throw error;
// }
// let truncatesSQL = results.reduce((accumulator, currentValue, i) => {
// let sql = 'TRUNCATE TABLE `' + currentValue.table_schema + '`.`' + currentValue.table_name + '`; ';
// return `${accumulator}${sql}`;
// }, '');
// end = true;
// connection.query(truncatesSQL.slice(0, -2), callback);
// };
// connection.connect();
// connected = Date.now() - start;
// connection.query('SET FOREIGN_KEY_CHECKS = 0', callback);
// firstQuery = Date.now() - start - connected;
// connection.query(`SELECT table_schema, table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema IN ('account', 'bi','bs', 'cache', 'edi', 'hedera', 'pbx', 'salix', 'util', 'vn', 'vn2008', 'vncontrol') AND table_rows < 1;`, truncate);
// secondQuery = Date.now() - start - connected - firstQuery;
export function restoreFixtures() { export function restoreFixtures() {
// connection.connect(); // connection.connect();
// connection.query('CALL truncateDatabase'); calls the MySQL procedure // connection.query('CALL truncateDatabase');
console.log('fixtures restored'); // connection.disconect();
// console.log('fixtures restored');
} }