Skip to content

Commit

Permalink
Merge branch 'master' into scales
Browse files Browse the repository at this point in the history
  • Loading branch information
tspeed90 committed May 11, 2018
2 parents 3dd68aa + be94dda commit 39418a3
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 165 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- psql -c 'create database travis;' -U postgres
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
10 changes: 2 additions & 8 deletions src/model/database/db_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
32 changes: 32 additions & 0 deletions src/model/database/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
42 changes: 10 additions & 32 deletions src/model/database/db_connect.js
Original file line number Diff line number Diff line change
@@ -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,
});
32 changes: 18 additions & 14 deletions src/model/queries/getQueries.js
Original file line number Diff line number Diff line change
@@ -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,
};
18 changes: 14 additions & 4 deletions src/model/queries/postQueries.js

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

77 changes: 50 additions & 27 deletions src/tests/queries.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
Loading

0 comments on commit 39418a3

Please sign in to comment.