Skip to content

Commit

Permalink
test(app): create and destroy test data on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
chidimo committed May 21, 2019
1 parent 8b2a0a8 commit 94751f8
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 47 deletions.
36 changes: 11 additions & 25 deletions test/loans-spec.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
// should is not used directly in the file but is added as a mocha requirement

import { exec } from 'child_process';

import supertest from 'supertest';
import assert from 'assert';
import app from '../app';

import { test_logger } from '../utils/loggers';
import createDB from '../utils/createDB';
import clearDB from '../utils/clearDB';

const server = supertest.agent(app);

describe('/loans', () => {
const dump = 'psql -h localhost -d testdb -U postgres -f test/testdb.sql';
const connect = 'psql -h localhost -d testdb -U postgres -c';
const query = 'delete from loans;delete from repayments';
const clear = `${connect} "${query}"`;

before(done => {

exec(dump, err => {
if (err) {
test_logger(`dump error: ${ err }`);
}
test_logger('****Database populated successfully.****');
done();
});
before(async () => {
test_logger('Creating DB in loans-spec');
await createDB();
});

after(done => {
test_logger('After hook start');
exec(clear, err => {
if (err) {
test_logger(`Error clearing db ${err}`);
}
});
test_logger('****Database cleared successfully.****');
done();

after(async () => {
test_logger('Clearing DB in loans-spec');
await clearDB();
});

describe('/loans: Get all loans', () => {
Expand Down
31 changes: 9 additions & 22 deletions test/users-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,22 @@

import supertest from 'supertest';
import app from '../app';
import { exec } from 'child_process';
import { test_logger } from '../utils/loggers';
import createDB from '../utils/createDB';
import clearDB from '../utils/clearDB';

const server = supertest.agent(app);

describe('/users', () => {
const dump = 'psql -h localhost -d testdb -U postgres -f test/testdb.sql';
const connect = 'psql -h localhost -d testdb -U postgres -c';
const query = 'delete from users';
const clear = `${connect} "${query}"`;

before(done => {
exec(dump, err => {
if (err) {
test_logger(`dump error: ${ err }`);
}
test_logger('****Database populated successfully.****');
done();
});
before(async () => {
test_logger('Creating DB in users-spec');
await createDB();
});

after(done => {
exec(clear, err => {
if (err) {
test_logger(`Error clearing db ${err}`);
}
});
test_logger('****Database cleared successfully.****');
done();

after(async () => {
test_logger('Clearing DB in users-spec');
await clearDB();
});

describe('/auth/signup', () => {
Expand Down
36 changes: 36 additions & 0 deletions utils/clearDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Pool } from 'pg';
import dotenv from 'dotenv';

import Settings from '../settings';
import { dev_logger } from './loggers';

dotenv.config();

const config = {
user: Settings.dbSettings().dbUser,
host: Settings.dbSettings().dbHost,
port: Settings.dbSettings().dbPort,
database: Settings.dbSettings().dbName,
password: Settings.dbSettings().dbPassword
};

const pool = new Pool(config);

const dropUsers = `
DROP TABLE users
`;
const dropLoans = `
DROP TABLE loans
`;
const dropRepayments = `
DROP TABLE repayments
`;
const queries = [ dropUsers, dropLoans, dropRepayments ];
const clearDB = async () => {
for (const query of queries) {
dev_logger(query);
await pool.query(query);
}
};

export default clearDB;
115 changes: 115 additions & 0 deletions utils/createDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import bcrypt from 'bcrypt';
import { Pool } from 'pg';
import dotenv from 'dotenv';

import Settings from '../settings';
import { dev_logger, test_logger } from './loggers';

dotenv.config();

const config = {
user: Settings.dbSettings().dbUser,
host: Settings.dbSettings().dbHost,
port: Settings.dbSettings().dbPort,
database: Settings.dbSettings().dbName,
password: Settings.dbSettings().dbPassword
};

const hashPassword = password => (bcrypt.hashSync(password, 8));
const pool = new Pool(config);

const createUserTable = `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email VARCHAR NOT NULL UNIQUE,
password VARCHAR NOT NULL,
firstname VARCHAR DEFAULT '',
lastname VARCHAR DEFAULT '',
phone VARCHAR DEFAULT '',
photo VARCHAR NULL,
address jsonb DEFAULT '{"home": "", "office": ""}',
status VARCHAR DEFAULT 'unverified',
isAdmin BOOLEAN DEFAULT false
)
`;

const populateUserTable = `
INSERT INTO users(email, password, firstname, lastname, phone, address)
VALUES ('a@b.com', '${hashPassword('password')}', 'first', 'men', '080121515', '{"home": "iyaba", "office": "ring road"}'),
('c@d.go', '${hashPassword('password')}', 'name', 'cat', '08151584151', '{"home": "london", "office": "NYC"}'),
('me@yahoo.com', '${hashPassword('password')}', 'tayo', 'dele', '08012345678', '{"home": "ijebu","office": "ijegun"}'),
('abc@gmail.com', '${hashPassword('password')}', 'what', 'is', '08012345678','{"home": "must","office": "not"}'),
('name@chat.co', '${hashPassword('password')}', 'niger', 'tornadoes', '08012345678', '{"home": "niger","office": "niger"}'),
('bcc@gmail.com', '${hashPassword('password')}', 'bcc', 'lions', '08012345678', '{"home": "gboko","office": "gboko"}'),
('bbc@bbc.uk', '${hashPassword('password')}', 'bbc', 'broadcast', '08012345678', '{"home": "london","office": "uk"}'),
('c@g.move', '${hashPassword('password')}', 'abc', 'def', '08012345678', '{"home": "shop","office": "home"}'),
('an@dela.ng', '${hashPassword('password')}', 'and', 'ela', '08012345678', '{"home": "ikorodu","office": "lagos"}'),
('soft@ware.eng', '${hashPassword('password')}', 'soft', 'eng', '08012345678', '{"home": "remote","office": "on-site"}');
`;

const createLoansTable = `
CREATE TABLE IF NOT EXISTS loans (
id SERIAL PRIMARY KEY,
userid INT NOT NULL,
createdon TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
status VARCHAR DEFAULT 'pending',
repaid BOOLEAN DEFAULT false,
amount FLOAT NOT NULL,
tenor INT NOT NULL,
interest FLOAT NOT NULL,
balance FLOAT NOT NULL,
paymentinstallment FLOAT NOT NULL
);
`;

const populateLoansTable = `
INSERT INTO loans(userid, status, repaid, amount, tenor, interest, balance, paymentinstallment)
VALUES (1, 'approved', false, 50000, 12, 2500, 36999.35, 4375),
(2, 'approved', true, 100000, 12, 5000, 0, 8750),
(3, 'approved', false, 200000, 8, 10000, 200000, 26250),
(4, 'approved', false, 25000, 12, 1250, 24500, 2187.5),
(5, 'approved', false, 45000, 6, 2250, 26250, 7875),
(6, 'pending', false, 80000, 12, 4000, 8000, 7000),
(7, 'rejected', false, 60000, 6, 3000, 6000, 10500),
(8, 'approved', false, 125000, 12, 6250, 20000, 10937.5),
(9, 'rejected', false, 190000, 12, 9500, 19000, 16625),
(10, 'pending', false, 1000000, 12, 50000, 0, 87500);
`;

const createRepaymentsTable = `
CREATE TABLE IF NOT EXISTS repayments (
id SERIAL PRIMARY KEY,
loanid INT NOT NULL,
adminid INT NOT NULL,
createdon TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
amount FLOAT NOT NULL
);
`;
const populateRepaymentsTable = `
INSERT INTO repayments(loanid, adminid, amount)
VALUES (1, 3, 4375),
(1, 3, 4375),
(2, 1, 26250),
(1, 2, 4375),
(3, 4, 2875),
(5, 8, 10500),
(4, 3, 4375),
(8, 1, 4375),
(8, 4, 4375),
(10, 8, 4375)
`;

const queries = [
createUserTable, createLoansTable, createRepaymentsTable,
populateUserTable, populateLoansTable, populateRepaymentsTable
];

const createDB = async () => {
for (const query of queries) {
dev_logger(query);
test_logger(query);
await pool.query(query);
}
};

export default createDB;

0 comments on commit 94751f8

Please sign in to comment.