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

Add database test and initial istanbul setup #40

Merged
merged 3 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"description": "",
"main": "index.js",
"scripts": {
"test-express": "node ./tests/express-test | tap-spec",
"test-database": "node ./tests/database.test | tap-spec",
"start:watch": "nodemon ./src/index.js",
"coverage": "istanbul cover ./tests/database.test.js && istanbul cover ./tests/express-test.js",
"test": "node ./tests/express-test.js | tap-spec",
"start:watch": "nodemon ./src/index.js"
},
"repository": {
"type": "git",
Expand All @@ -29,6 +32,7 @@
"eslint": "^4.4.1",
"istanbul": "^0.4.5",
"nodemon": "^1.11.0",
"shot": "^3.4.2",
"supertest": "^3.0.0",
"tap-diff": "^0.1.1",
"tap-spec": "^4.1.1",
Expand Down
3 changes: 0 additions & 3 deletions src/controllers/browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ exports.get = (req, res, next) => {
}
showData()
.then ((stuff, text)=>{
// console.log(stuff,text);
res.render('browse',{
activePage:{
browse: true
},
data: stuff.rows
});
// console.log('this is out stuff: ', stuff);
// res.rows??
})
.catch((stuff, text)=>{
res.status(500);
Expand Down
96 changes: 96 additions & 0 deletions tests/database.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const tape = require('tape');
const shot = require('shot');
const fs = require('fs');
const path = require('path');
const buildDataBase = require('../src/model/db_build');
const dbConnection = require('../src/model/db_connection');
const app = require('../src/app');

// Select all firstnames from users
tape("Select all firstnames from users", t => {
buildDataBase();
const expected = [
{ firstname: 'Abdullah'},
{ firstname: 'Max'},
{ firstname: 'Jen'},
{ firstname: 'Rebeca'}
];

dbConnection.query('SELECT firstname FROM users;', (err, res) => {
if (err) {
t.error(err, 'This is an error');
} else {
const actual = res.rows;
t.deepEquals(actual, expected, 'db query should get firstnames');
t.end();
}
})
})

// test ideas database
tape("get idea titles from ideas database", t => {
buildDataBase();
const expected = [
{ ideatitle: 'Movie recommendation app' },
{ ideatitle: 'travel giffy app' },
{ ideatitle: 'holiday inspiration app' },
{ ideatitle: 'mario cv app' }
];

dbConnection.query('SELECT ideatitle FROM ideas;', (err, res) => {
if (err) {
t.error(err, 'This is an error');
} else {
const actual = res.rows;
t.deepEquals(actual, expected, 'db query should get titles');
t.end();
}
})
})

tape("check if post adds a new user to user table", t => {
buildDataBase();
const insertUserSql = "INSERT INTO users (firstname, email) VALUES ($1, $2)";
const expected = [
{ firstname: 'Amelie', email: '[email protected]'}
];

dbConnection.query(insertUserSql, [expected[0].firstname, expected[0].email], (err, res) => {
if (err) {
t.error(err, 'This is an error');
} else {
dbConnection.query("SELECT firstname, email FROM users WHERE firstname='Amelie';",
(err, res) => {
if (err) {
t.error('This is an error');
} else {
const actual = res.rows;
t.deepEquals(actual, expected, 'firstname should be Amelie');
t.end();
}
})
}
})
})




// inserting a new idea
// tape("check if a post adds a new idea to ideatable", t => {
// buildDataBase();
// const insertNewIdeaSql = "INSERT INTO ideas (userid, dataadded, ideatitle, ideadesc) VALUES ($1, $2, $3, $4)";
// const expected = [
// { userid: 5, dataadded: 2017-08-17, }
// ];
//
// dbConnection.query('SELECT ideatitle FROM ideas;', (err, res) => {
// if (err) {
// t.error(err, 'This is an error');
// } else {
// const actual = res.rows;
// t.deepEquals(actual, expected, 'db query should get titles');
// t.end();
// }
// })
// })
11 changes: 6 additions & 5 deletions tests/express-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ test('1 equals 1', (t) => {
t.end();
});

// check we get 404 on /error endpoint
test('404 on "/error" endpoint', (t) => {
// check if supetest works
test('check if supertest works', (t) => {
supertest(app)
.get('/error')
.expect(404)
.get('/')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
t.same(res.statusCode, 404, 'Status code is 404');
t.same(res.statusCode, 200, 'Status code is 200');
t.end();
})
})
Expand Down Expand Up @@ -46,3 +46,4 @@ for(let i=0; i<endpoints.length; i++){
})
})
}