Skip to content

Commit

Permalink
test: update tests for current code
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Jan 3, 2022
1 parent 28ee409 commit a5f10e8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 50 deletions.
40 changes: 21 additions & 19 deletions __tests__/api-tests/seed-data-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,46 @@ const { v4: uuid } = require('uuid');
const knex = require('../../database/connection');

const stakeholderOne = Object.freeze({
id: 5000000,
id: uuid(),
type: 'type',
org_name: 'name',
first_name: 'first_name',
last_name: 'last_name',
email: 'email',
phone: 'phone',
// pwd_reset_required: true,
pwd_reset_required: true,
website: 'website',
// wallet: 'wallet@@#',
// password: 'password',
// salt: 'salt',
// active_contract_id: 10,
// offering_pay_to_plant: true,
// tree_validation_contract_id: 11,
wallet: 'wallet@@#',
password: 'password',
salt: 'salt',
active_contract_id: uuid(),
offering_pay_to_plant: true,
tree_validation_contract_id: uuid(),
logo_url: 'url',
map: 'ma,e',
stakeholder_uuid: uuid(),
owner_id: uuid(),
organization_id: 5000000,
});
const stakeholderTwo = Object.freeze({
id: 5000001,
id: uuid(),
type: 'type',
org_name: 'name',
first_name: 'first_name',
last_name: 'last_name',
email: 'email',
phone: 'phone',
// pwd_reset_required: true,
pwd_reset_required: true,
website: 'website',
// wallet: 'wallet@!#',
// password: 'password',
// salt: 'salt',
// active_contract_id: 10,
// offering_pay_to_plant: true,
// tree_validation_contract_id: 11,
wallet: 'wallet@!#',
password: 'password',
salt: 'salt',
active_contract_id: uuid(),
offering_pay_to_plant: true,
tree_validation_contract_id: uuid(),
logo_url: 'url',
map: 'ma,e',
stakeholder_uuid: uuid(),
owner_id: uuid(),
organization_id: 5000001,
});

before(async () => {
Expand All @@ -56,7 +58,7 @@ before(async () => {
after(async () => {
await knex.raw(`
DELETE FROM stakeholder
WHERE id = '${5000000}' OR id = '${5000001}';
WHERE organization_id = '${5000000}' OR organization_id = '${5000001}';
`);
});

Expand Down
2 changes: 2 additions & 0 deletions server/models/Stakeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const Stakeholder = ({

const FilterCriteria = ({
id = null,
owner_id = null,
organization_id = null,
type = null,
orgName = null,
Expand All @@ -154,6 +155,7 @@ const FilterCriteria = ({
}) => {
return Object.entries({
id,
owner_id,
organization_id,
type,
orgName,
Expand Down
28 changes: 14 additions & 14 deletions server/models/Stakeholder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ describe('Stakeholder Model', () => {
'last_name',
'email',
'phone',
// 'pwd_reset_required',
'pwd_reset_required',
'website',
// 'wallet',
// 'password',
// 'salt',
// 'active_contract_id',
// 'offering_pay_to_plant',
// 'tree_validation_contract_id',
'wallet',
'password',
'salt',
'active_contract_id',
'offering_pay_to_plant',
'tree_validation_contract_id',
'logo_url',
'map',
'stakeholder_uuid',
// 'organization_id',
'owner_id',
'organization_id',
]);
});

describe('FilterCriteria', () => {
it('filterCriteria should not return results other than id, stakeholder_uuid, organization_id, type, orgName, firstName, lastName, imageUrl, email, phone, website, logoUrl, map', () => {
it('filterCriteria should not return results other than id, owner_id, organization_id, type, orgName, firstName, lastName, imageUrl, email, phone, website, logoUrl, map', () => {
const filter = FilterCriteria({ check: true });
expect(filter).to.be.empty;
});

it('filterCriteria should not return undefined fields', () => {
const filter = FilterCriteria({
id: undefined,
stakeholder_uuid: undefined,
owner_id: undefined,
organization_id: undefined,
});
expect(filter).to.be.empty;
});

it('filterCriteria should return id, stakeholder_uuid', () => {
it('filterCriteria should return id, owner_id', () => {
const filter = FilterCriteria({
id: 'undefined',
stakeholder_uuid: 'undefined',
owner_id: 'undefined',
organization_id: undefined,
});
expect(filter).to.have.keys(['id', 'stakeholder_uuid']);
expect(filter).to.have.keys(['id', 'owner_id']);
});
});

Expand Down
13 changes: 8 additions & 5 deletions server/repositories/StakeholderRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,20 @@ class StakeholderRepository extends BaseRepository {
.count('*')
.where('id', id);

return { stakeholders: [stakeholder], count: +count[0].count };
return {
stakeholders: [stakeholder],
count: count ? +count[0].count : 0,
};
}

async getParentIds(id) {
const parents = await this._session
.getDB()('stakeholder as s')
.select('stakeholder_relations.parent_id')
.join('stakeholder_relations', 's.id', 'stakeholder_relations.child_id')
.join('stakeholder_relations as sr', 's.id', 'sr.child_id')
.where('s.id', id);

return parents ? parents.map((parent) => parent.parent_id) : [];
return parents.length ? parents.map((parent) => parent.parent_id) : [];
}

async getParents(id) {
Expand All @@ -160,10 +163,10 @@ class StakeholderRepository extends BaseRepository {
const children = await this._session
.getDB()('stakeholder as s')
.select('stakeholder_relations.child_id')
.join('stakeholder_relations', 's.id', 'stakeholder_relations.parent_id')
.join('stakeholder_relations as sr', 's.id', 'sr.parent_id')
.where('s.id', id);

return children ? children.map((child) => child.child_id) : [];
return children.length ? children.map((child) => child.child_id) : [];
}

async getChildren(parent, options) {
Expand Down
46 changes: 34 additions & 12 deletions server/repositories/StakeholderRepository.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,46 @@ describe('StakeholderRepository', () => {
mockKnex.unmock(knex);
});

it('getStakeholderById', async () => {
it('getStakeholderTreeById', async () => {
tracker.uninstall();
tracker.install();
tracker.on('query', (query) => {
let bool = query.sql.match(/select.*.*id.*.*stakeholder_uuid.*/);
if (!bool)
bool = query.sql.match(
/select.*stakeholder.*id.*or.*stakeholder_uuid.*limit.*offset/,
);
expect(bool);
tracker.on('query', (query, step) => {
const stakeholder = { id: 1 };
query.response(stakeholder);
const count = { count: 1 };
[
function firstQuery() {
const bool = query.sql.match(/select.*.*id.*/);
expect(bool);
query.response(stakeholder);
},
function firstQuery() {
const bool = query.sql.match(/select.*.*id.*/);
expect(bool);
query.response([]);
},
function firstQuery() {
const bool = query.sql.match(/select.*.*id.*/);
expect(bool);
query.response([]);
},
function secondQuery() {
const bool = query.sql.match(/count.*.*id.*/);
expect(bool);
query.response([count]);
},
function finalQuery() {
query.response({ stakeholders: [stakeholder], count });
},
][step - 1]();
});
const { stakeholder } = await stakeholderRepository.getStakeholderById(1);
expect(stakeholder).property('id').eq(1);

const { stakeholders, count } =
await stakeholderRepository.getStakeholderTreeById(1);
expect(stakeholders[0]).property('id').eq(1);
expect(count).eq(1);
});

it.skip('getStakeholderByOrganizationId', async () => {
it('getStakeholderByOrganizationId', async () => {
tracker.uninstall();
tracker.install();
tracker.on('query', (query) => {
Expand Down

0 comments on commit a5f10e8

Please sign in to comment.