Skip to content

Commit

Permalink
Merge pull request #21 from ebenezerdon/setup-travis-database-161624846
Browse files Browse the repository at this point in the history
#161624846 Setup Travis Database
  • Loading branch information
ebenezerdon committed Oct 31, 2018
2 parents 7bee282 + 12ff972 commit c7c65ea
Show file tree
Hide file tree
Showing 14 changed files with 803 additions and 236 deletions.
15 changes: 9 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ language: node_js
node_js:
- "stable"

install:
- npm install
install: npm install

test:
- npm test
services:
- postgresql

after_success:
- npm run coveralls
before_script:
- psql -c 'create database storemanagertest;' -U postgres

script: npm test

after_success: npm run coveralls
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import router from './server/routes/index';

const app = express();

app.set('port', process.env.PORT || 5000);
app.set('port', process.env.PORT || 5002);
app.set('json spaces', 2);

app.use(logger('dev'));
Expand Down
101 changes: 94 additions & 7 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"private": true,
"scripts": {
"start": "nodemon --exec babel-node app.js",
"test": "nyc mocha ./server/tests --compilers js:babel-core/register --timeout 1000 --exit",
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
"migration": "babel-node server/models/migration.js"
"migration": "babel-node server/models/migration.js dropTables && babel-node server/models/migration.js createTables",
"seed": "babel-node server/models/seed.js seedProducts && babel-node server/models/seed.js seedSales && babel-node server/models/seed.js seedUsers",
"test": "npm run migration && npm run seed && nyc mocha ./server/tests --compilers js:babel-core/register --timeout 1000 --exit"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand Down Expand Up @@ -47,6 +48,8 @@
"nyc": "^13.1.0",
"path": "^0.12.7",
"pg": "^7.5.0",
"superagent": "^4.0.0-beta.5",
"supertest": "^3.3.0",
"uuid": "^3.3.2"
},
"standard": {
Expand Down
7 changes: 5 additions & 2 deletions server/models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { config } from 'dotenv';
config();
let ssl = false;
if (process.env.NODE_ENV === 'production') ssl = true;

let connectionString = process.env.DATABASE_URL;
if (process.env.NODE_ENV === 'test') {
connectionString = process.env.DATABASE_URL_TEST;
}
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
connectionString,
ssl,
});

Expand Down
81 changes: 81 additions & 0 deletions server/models/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import pool from './db';

const seedProducts = () => {
const text1 = `INSERT INTO
products(productname, description, price, quantity, min)
VALUES($1, $2, $3, $4, $5)
returning *`;
const values1 = [
'Long Sleeve T shirt',
'Really cool stuff',
'52000',
41,
25,
];
const text2 = `INSERT INTO
products(productname, description, price, quantity, min)
VALUES($1, $2, $3, $4, $5)
returning *`;
const values2 = [
'Black Long Sleeve T shirt',
'Another Long sleeve',
'34512',
45,
21,
];
pool.query(text1, values1);
pool.query(text2, values2);
};

const seedSales = () => {
const text1 = `INSERT INTO
sales(productname, productId, price, attendant_id, quantity)
VALUES($1, $2, $3, $4, $5)
returning *`;
const values1 = [
'Long Sleeve T shirt',
2,
'52000',
41,
25,
];
const text2 = `INSERT INTO
sales(productname, productId, price, attendant_id, quantity)
VALUES($1, $2, $3, $4, $5)`;
const values2 = [
'Black Long Sleeve T shirt',
2,
'52000',
42,
21,
];
pool.query(text1, values1);
pool.query(text2, values2);
};

const seedUsers = () => {
const text1 = `INSERT INTO
users(fullname, emailaddress, password, type)
VALUES($1, $2, $3, $4)`;
const values1 = [
'Admin',
'admin@gmail.com',
'adminpassword',
'admin',
];
const text2 = `INSERT INTO
users(fullname, emailaddress, password, type)
VALUES($1, $2, $3, $4)`;
const values2 = [
'attendant',
'attendant@gmail.com',
'attendantpassword',
'attendant',
];

pool.query(text1, values1);
pool.query(text2, values2);
};

export { seedProducts, seedSales, seedUsers };
require('make-runnable');
22 changes: 17 additions & 5 deletions server/routes/controllers/productsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const getOneProduct = (req, res) => {
const text = 'SELECT * FROM products WHERE id = $1';
pool.query(text, [req.params.id], (err, data) => {
if (!data.rowCount) {
return res.status(404).json('Hi! There\'s no product with that id');
return res.status(404).json({
message: 'Hi! There\'s no product with that id',
success: false,
});
}
if (err) {
throw err;
Expand Down Expand Up @@ -60,7 +63,10 @@ const updateProduct = (req, res) => {
];
pool.query(text, values, (err, data) => {
if (!data.rowCount) {
return res.status(404).json('Hi! There\'s no product with that id');
return res.status(404).json({
message: 'Hi! There\'s no product with that id',
success: false,
});
}
if (err) {
throw err;
Expand All @@ -73,12 +79,18 @@ const deleteProduct = (req, res) => {
const text = 'DELETE FROM products WHERE id=$1 returning *';
pool.query(text, [req.params.id], (err, data) => {
if (!data.rowCount) {
return res.status(404).json('Hi! There\'s no product with that id');
return res.status(404).json({
message: 'Hi! There\'s no product with that id',
success: false,
});
}
if (err) {
throw err;
}
return res.status(204).json('Cool. Deleted!');
return res.status(200).json({
message: 'Deleted!',
success: true,
});
});
};

Expand All @@ -88,4 +100,4 @@ export {
addProduct,
updateProduct,
deleteProduct,
};
};
Loading

0 comments on commit c7c65ea

Please sign in to comment.