Skip to content

Commit

Permalink
Merge pull request #97 from fac-13/issue80
Browse files Browse the repository at this point in the history
SQL query to get all members and basic information from db - Issue80
  • Loading branch information
LawEKS authored May 15, 2018
2 parents 3372a09 + 831a02b commit b3faed0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/model/database/db_build_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ INSERT INTO members
(github_id, full_name, github_handle, github_avatar_url, fac_campus, fac_code_id, linkedin_url, twitter_handle, member_type, job_search_status, min_years_exp, max_years_exp, github_cv_url, cv_url, job_view_pref)
VALUES
(1, 'Helen', 'helenzhou6', 'https://uk.linkedin.com/dbsmith', 'london', 1, 'https://uk.linkedin.com/', 'hel_zhou', 'admin', 'red', 0, 1, 'https://github.com/helenzhou6/CV', 'https://github.com/helenzhou6/CV', 'private'),
(2, 'Deborah', 'dsmith', 'https://uk.linkedin.com/dbsmith', 'gaza', 2, 'https://uk.linkedin.com/dbsmith', 'dbsmith', 'member', 'yellow', 2, 5, NULL, NULL, 'public');
(2, 'Deborah', 'dsmith', 'https://uk.linkedin.com/dbsmith', 'gaza', 2, 'https://uk.linkedin.com/dbsmith', 'dbsmith', 'member', 'orange', 2, 5, NULL, NULL, 'public');

INSERT INTO member_tech_stack
(member_id, stack_id, order_num)
Expand Down
21 changes: 21 additions & 0 deletions src/model/queries/getAllMemberData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const dbConnection = require('../database/db_connection.js');

const allMembersQuery =
`SELECT mem.id, mem.github_id, mem.full_name, mem.github_handle, github_avatar_url, code.code AS fac_cohort,
(SELECT array_agg(tech.tech ORDER BY stack.order_num)
FROM tech_stack tech
INNER JOIN member_tech_stack stack
ON stack.stack_id = tech.id
WHERE stack.member_id = mem.id
) AS tech_stack, job_search_status
FROM members mem
INNER JOIN fac_code code
ON mem.fac_code_id = code.id`;

const getAllMemberData = () =>
dbConnection
.query(allMembersQuery)
.then(allMemberDetails => allMemberDetails);

module.exports = getAllMemberData;

50 changes: 50 additions & 0 deletions src/test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const runDbBuild = require('../model/database/db_build_test');
const dbConnection = require('../model/database/db_connection');
const postMemberInfo = require('../model/queries/postMemberInfo.js');
const getMemberData = require('../model/queries/getMemberData.js');
const getAllMemberData = require('../model/queries/getAllMemberData.js');


const selectAllMembers = 'SELECT * FROM members';

Expand Down Expand Up @@ -112,6 +114,54 @@ test('Test postMemberInfo adds a row', (t) => {
});
});

// GET ALL USERS MEMBER DATA TEST
test('Test getAllMemberData query returns the correct format and number of rows', (t) => {
const correctResult =
[{
id: 1,
github_id: 1,
full_name: 'Helen',
github_handle: 'helenzhou6',
github_avatar_url: 'https://uk.linkedin.com/dbsmith',
fac_cohort: 'FAC0',
tech_stack: ['JavaScript', 'Node.js'],
job_search_status: 'red',
},
{
id: 2,
github_id: 2,
full_name: 'Deborah',
github_handle: 'dsmith',
github_avatar_url: 'https://uk.linkedin.com/dbsmith',
fac_cohort: 'FAC1',
tech_stack: ['Node.js', 'JavaScript'],
job_search_status: 'orange',
}];


runDbBuild().then(() => {
dbConnection.query(selectAllMembers)
.then((res1) => {
const testQuantity = res1.length;
getAllMemberData()
.then((res2) => {
if (typeof res2 === 'object') {
t.pass('getAllMemberData returns an object');
}
const newQuantity = res2.length;
t.equal(testQuantity, newQuantity, 'getAllMemberData returns expected number of rows');
t.deepEqual(res2, correctResult, 'deepEquals of all member info');
t.end();
});
}).catch((error) => {
console.log(error);
t.error(error, 'getAllMemberData test error');
t.end();
});
});
});


test.onFinish(() => {
dbConnection.$pool.end();
});

0 comments on commit b3faed0

Please sign in to comment.