diff --git a/.travis.yml b/.travis.yml index 7b4a99c..3d4ac4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,8 @@ language: node_js node_js: - "node" notifications: - - email: "false" + - email: false services: - postgresql before_script: - - psql -c 'create database travis_ci_test;' -U postgres \ No newline at end of file + - psql -c 'create database travis;' -U postgres \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/package.json b/package.json index da2df3a..e3b3014 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "description": "an application that allows users to track symptoms and potentially contributing factors and view data that represents these data points over time", "main": "app.js", "scripts": { - "test": "NODE_ENV=test ./node_modules/mocha/bin/mocha ./src/tests/*.js", - "querytest": "tape src/tests/queries.test.js | tap-spec", + "test": "ENV=test tape src/tests/*.js | tap-spec", "start": "nodemon ./src/index.js", "build": "node src/model/database/db_build.js" }, diff --git a/src/model/database/db_build.js b/src/model/database/db_build.js index 7701dd1..0b671ac 100644 --- a/src/model/database/db_build.js +++ b/src/model/database/db_build.js @@ -4,15 +4,9 @@ const dbConnect = require('./db_connect.js'); const sql = file => QueryFile(path.join(__dirname, file), { minify: true }); -let build; - -build = sql('./db_build.sql'); +const build = sql('./db_build.sql'); const runDbBuild = () => dbConnect - .query(build) - .then() - .catch(e => console.error('error', e)); - -runDbBuild(); + .query(build); module.exports = runDbBuild; diff --git a/src/model/database/db_build.sql b/src/model/database/db_build.sql index 2c118a1..ff6b127 100644 --- a/src/model/database/db_build.sql +++ b/src/model/database/db_build.sql @@ -83,14 +83,30 @@ INSERT into users (username, password) VALUES ('eade', 'passworddd'); +INSERT into users + (username, password) +VALUES + ('stanley', 'passworddd'); INSERT into symptoms (symptom, user_id) VALUES ('rash', 1); +INSERT into symptoms + (symptom, user_id) +VALUES + ('itch', 1); +INSERT into symptoms + (symptom, user_id) +VALUES + ('hyper', 2); INSERT into factors (factor, user_id) VALUES ('water', 1); +INSERT into factors + (factor, user_id) +VALUES + ('sleep', 1); INSERT into symptom_scale (symptom_id, user_id, comment_1, comment_6) VALUES @@ -103,4 +119,20 @@ INSERT into factor_data (factor_id, user_id, rating) VALUES (1, 1, 8); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (1, 1, 8); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (1, 1, 6); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (2, 1, 6); +INSERT into symptom_data + (symptom_id, user_id, rating) +VALUES + (3, 2, 8); COMMIT; \ No newline at end of file diff --git a/src/model/database/db_connect.js b/src/model/database/db_connect.js index 87c04bd..b21044e 100644 --- a/src/model/database/db_connect.js +++ b/src/model/database/db_connect.js @@ -1,38 +1,16 @@ const pgp = require('pg-promise')(); -const url = require('url'); -require('env2')('./config.env'); - -let options = {}; - -if (process.env.TRAVIS === 'true') { - options = { - database: 'travis_ci_test', - user: 'postgres', - }; -} else { - let DB_URL = process.env.DB_URL; - - if (process.env.NODE_ENV === 'test') { - DB_URL = process.env.TEST_DB_URL; - } +require('env2')('./config.env'); - if (!DB_URL) { - throw new Error('Environment variable DB_URL must be set!'); - } +const DB_URL = + process.env.ENV === 'test' + ? process.env.TEST_DB_URL + : process.env.DB_URL; - const params = url.parse(DB_URL); - const [username, password] = params.auth.split(':'); - options = { - host: params.hostname, - port: params.port, - database: params.pathname.split('/')[1], - max: process.env.DB_MAX_CONNECTIONS || 2, - user: username, - password, - ssl: params.hostname !== 'localhost', - }; +if (!DB_URL) { + throw new Error('Environment variable DB_URL must be set!'); } - -module.exports = pgp(options); +module.exports = pgp({ + DB_URL, +}); diff --git a/src/model/queries/getQueries.js b/src/model/queries/getQueries.js index 157168a..3aee5cb 100644 --- a/src/model/queries/getQueries.js +++ b/src/model/queries/getQueries.js @@ -1,23 +1,27 @@ const dbConnect = require('../database/db_connect.js'); + // get username and password from users table -const getUserData = username => - dbConnect.query('SELECT password FROM users WHERE username=$1', [username]) - .then((res) => { - console.log('res0', res[0]); - return res[0]; - }); -// .catch(err => console.log(err)); -// get a symptom from symptoms table +const getUserData = username => dbConnect.query('SELECT password FROM users WHERE username=$1', [username]) + .then(res => res[0]); -// get scale comments for symptom scale -// get a factor from factors table +// get all user symptoms from symptoms table +const getSymptoms = username => dbConnect.query('select symptom from symptoms where user_id = (select id from users where username=$1)', [username]); +// get all user factors from factor table -// get scale comment for factor scale +const getFactors = username => dbConnect.query('select factor from factors where user_id = (select id from users where username=$1)', [username]); -// get specific symptom ratings +// get scale comments for symptom scale +const getSymptomScale = (symptom, username) => dbConnect.query('select * from symptom_scale where symptom_id = (select id from symptoms where symptom=$1) and user_id = (select id from users where username=$2);)', [symptom, username]); -// get specific factor ratings +// get scale comment for factor scale +const getFactorScale = (factor, username) => dbConnect.query('select * from factor_scale where factor_id = (select id from factors where factor=$1) and user_id = (select id from users where username=$2);)', [factor, username]); +// get specific symptom data +const getSymptomRatings = username => dbConnect.query('select symptom_data.rating, symptom_data.date_entered, symptoms.symptom from symptom_data left join symptoms on symptoms.id = symptom_data.symptom_id where symptom_data.user_id = (select id from users where username=$1)', [username]); +// get specific factor data +const getFactorRatings = username => dbConnect.query('select factor_data.rating, factor_data.date_entered, factors.factor from factor_data left join factors on factors.id = factor_data.factor_id where factor_data.user_id = (select id from users where username=$1)', [username]); -module.exports = { getUserData }; +module.exports = { + getUserData, getSymptoms, getFactors, getSymptomScale, getFactorScale, getSymptomRatings, getFactorRatings, +}; diff --git a/src/model/queries/postQueries.js b/src/model/queries/postQueries.js index 7b4be04..43889e1 100644 --- a/src/model/queries/postQueries.js +++ b/src/model/queries/postQueries.js @@ -1,13 +1,23 @@ -// post username and password to users table +const dbConnect = require('../database/db_connect.js'); +// post username and password to users table +const postUserData = (username, password) => dbConnect.query('insert into users (username, password) values ($1, $2)', [username, password]); // post a symptom to symptom - -// post scale comments for symptom scale +const postSymptom = (symptom, username) => dbConnect.query('insert into symptoms (symptom, user_id) values ($1, (select id from users where username=$2))', [symptom, username]); // post a factor to factor table +const postFactor = (factor, username) => dbConnect.query('insert into factors (factor, user_id) values ($1, (select id from users where username=$2))', [factor, username]); +// post scale comments for symptom scale +const postSymptomScale = (symptom, username, comment_1, comment_2, comment_3, comment_4, comment_5, comment_6, comment_7, comment_8, comment_9, comment_10) => dbConnect.query('insert into symptom_scale (symptom_id, user_id, comment_1, comment_2, comment_3, comment_4, comment_5, comment_6, comment_7, comment_8, comment_9, comment_10) values ((select id from symptoms where symptom = $1 and user_id=(select id from users where username=$2)), (select id from users where username = $2), $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)', [symptom, username, comment_1, comment_2, comment_3, comment_4, comment_5, comment_6, comment_7, comment_8, comment_9, comment_10]); // post scale comment for factor scale +const postFactorScale = (factor, username, comment_1, comment_2, comment_3, comment_4, comment_5, comment_6, comment_7, comment_8, comment_9, comment_10) => dbConnect.query('insert into factor_scale (factor_id, user_id, comment_1, comment_2, comment_3, comment_4, comment_5, comment_6, comment_7, comment_8, comment_9, comment_10) values ((select id from factors where factor = $1 and user_id=(select id from users where username=$2)), (select id from users where username = $2), $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)', [factor, username, comment_1, comment_2, comment_3, comment_4, comment_5, comment_6, comment_7, comment_8, comment_9, comment_10]); // post symptom ratings +const postSymptomRating = (symptom, username, rating) => dbConnect.query('insert into symptom_data (symptom_id, user_id, rating) values((select id from symptoms where symptom = $1 and user_id=(select id from users where username=$2)), (select id from users where username=$2),$3)', [symptom, username, rating]); +// post factor ratings +const postFactorRating = (factor, username, rating) => dbConnect.query('insert into factor_data (factor_id, user_id, rating) values((select id from factors where factor = $1 and user_id=(select id from users where username=$2)), (select id from users where username=$2),$3)', [factor, username, rating]); -// post factor ratings \ No newline at end of file +module.exports = { + postUserData, postSymptom, postFactor, postSymptomScale, postFactorScale, postSymptomRating, postFactorRating, +}; diff --git a/src/tests/queries.test.js b/src/tests/queries.test.js index 4c6a6d5..598e429 100644 --- a/src/tests/queries.test.js +++ b/src/tests/queries.test.js @@ -1,48 +1,71 @@ const runDbBuild = require('../model/database/db_build.js'); -const { getUserData } = require('../model/queries/getQueries'); -const dbConnect = require('../model/database/db_connect'); +const dbConnection = require('../model/database/db_connect'); +const { getUserData, getSymptoms, getFactors } = require('../model/queries/getQueries'); // const { postUserData } = require('../model/queries/postQueries'); const test = require('tape'); -runDbBuild(); - -test('simple test', (t) => { - dbConnect.query('SELECT * FROM users') - .then(res => res[0]).then((queryResult) => { - t.deepEqual( - Object.keys(queryResult), - ['id', 'username', 'password'], - 'three keys', +// test query to get username and password from users table +test('testing that getUserData returns an object with user password', (t) => { + runDbBuild() + .then((res) => { + t.ok(res); + return getUserData('eade'); + }) + .catch((e) => { + t.error(e, 'error building'); + t.end(); + }) + .then((queryResult) => { + t.equal( + queryResult.password, + 'passworddd', + 'queryResult should contain passworddd', ); - t.equal(queryResult.username, 'eade', 'returns eade'); - t.equal(queryResult.password, 'passworddd', 'should return passworddd'); + t.end(); + }) + .catch((e) => { + t.error(e, 'error with getting user data'); t.end(); }); }); -// test query to get username and password from users table -test('testing that getUserData returns an object with user password', (t) => { - getUserData('eade').then((queryResult) => { - t.equal( - queryResult.password, - 'passworddd', - 'queryResult should contain passworddd', - ); - t.end(); - }) - .catch(console.log); -}); - - // test query to get a symptom from symptoms table +test('testing symptoms query returns something', (t) => { + runDbBuild() + .then(() => getSymptoms('eade')) + .then((queryResult) => { + t.ok(queryResult); + t.end(); + }) + .catch((e) => { + t.error(e, 'error with symptoms query'); + t.end(); + }); +}); // test query to get scale comments for symptom scale // test query to get a factor from factors table +test('testing factors query returns something', (t) => { + runDbBuild() + .then(() => getFactors('eade')) + .then((queryResult) => { + t.ok(queryResult); + t.end(); + }) + .catch((e) => { + t.error(e, 'error with factors query'); + t.end(); + }); +}); // test query to get scale comment for factor scale // test query to get specific symptom ratings + // test query to get specific factor ratings +test.onFinish(() => { + dbConnection.$pool.end(); +}); diff --git a/src/tests/routes.test.js b/src/tests/routes.test.js index a747134..d49bdbe 100644 --- a/src/tests/routes.test.js +++ b/src/tests/routes.test.js @@ -1,127 +1,180 @@ const request = require('supertest'); -const mocha = require('mocha'); -const chai = require('chai'); -const assert = require('chai').assert; -const expect = require('chai').expect; -const express = require('express'); - +const test = require('tape'); const app = require('./../app.js'); + // test home route -it('tests home returns html file with status code of 200', (done) => { +test('tests home returns html file with status code of 200', (t) => { request(app) .get('/') .expect(200) .expect('Content-Type', /html/) - .end(done); + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); }); // // test login route -it('tests login returns html file with status code of 200', (done) => { +test('tests login returns html file with status code of 200', (t) => { request(app) .get('/logIn') .expect(200) .expect('Content-Type', /html/) - .end(done); + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); }); // // test register route -it('tests register returns html file with status code of 200', (done) => { +test('tests register returns html file with status code of 200', (t) => { request(app) .get('/register') .expect(200) .expect('Content-Type', /html/) - .end(done); + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); }); // test error route -it('tests errors return html file with status code of 404', (done) => { +test('tests errors return html file with status code of 404', (t) => { request(app) .get('/asdf') .expect(404) .expect('Content-Type', /html/) - .end(done); + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); }); // test symptom route -describe('testing the symptom routes', () => { - it('tests symptoms/home return html file with status code of 200', (done) => { - request(app) - .get('/symptoms/home') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); +test('tests symptoms/home return html file with status code of 200', (t) => { + request(app) + .get('/symptoms/home') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); +}); - it('tests symptoms/new return html file with status code of 200', (done) => { - request(app) - .get('/symptoms/add') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); - it('tests symptoms/scaleInfo return html file with status code of 200', (done) => { - request(app) - .get('/symptoms/scaleInfo') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); - it('tests symptoms/scaleSetup return html file with status code of 200', (done) => { - request(app) - .get('/symptoms/scaleSetup') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); +test('tests symptoms/new return html file with status code of 200', (t) => { + request(app) + .get('/symptoms/add') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); +}); + +test('tests symptoms/scaleInfo return html file with status code of 200', (t) => { + request(app) + .get('/symptoms/scaleInfo') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); +}); + +test('tests symptoms/scaleSetup return html file with status code of 200', (t) => { + request(app) + .get('/symptoms/scaleSetup') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); }); // testing factor routes -describe('testing the factor routes', () => { - it('tests factor/home return html file with status code of 200', (done) => { - request(app) - .get('/factors/home') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); +test('tests factor/home return html file with status code of 200', (t) => { + request(app) + .get('/factors/home') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); +}); + +test('tests factor/new return html file with status code of 200', (t) => { + request(app) + .get('/factors/add') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); +}); - it('tests factor/new return html file with status code of 200', (done) => { - request(app) - .get('/factors/add') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); - it('tests factor/scaleInfo return html file with status code of 200', (done) => { - request(app) - .get('/factors/scaleInfo') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); - it('tests factor/scaleSetup return html file with status code of 200', (done) => { - request(app) - .get('/factors/scaleSetup') - .expect(200) - .expect('Content-Type', /html/) - .end(done); - }); +test('tests factor/scaleInfo return html file with status code of 200', (t) => { + request(app) + .get('/factors/scaleInfo') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); }); + +test('tests factor/scaleSetup return html file with status code of 200', (t) => { + request(app) + .get('/factors/scaleSetup') + .expect(200) + .expect('Content-Type', /html/) + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); +}); + // testing add data route -it('tests adddata return html file with status code of 200', (done) => { +test('tests adddata return html file with status code of 200', (t) => { request(app) .get('/adddata') .expect(200) .expect('Content-Type', /html/) - .end(done); + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); }); // testing see data route -it('tests seedata return html file with status code of 200', (done) => { +test('tests seedata return html file with status code of 200', (t) => { request(app) .get('/seedata') .expect(200) .expect('Content-Type', /html/) - .end(done); + .end((err, res) => { + t.error(err); + t.ok(res); + t.end(); + }); });