Skip to content

Commit

Permalink
Merge branch 'dev' into BHBC-1367
Browse files Browse the repository at this point in the history
  • Loading branch information
sdevalapurkar authored Sep 24, 2021
2 parents 0cd87ab + 506a697 commit ca28790
Show file tree
Hide file tree
Showing 6 changed files with 570 additions and 3 deletions.
185 changes: 185 additions & 0 deletions api/src/paths/dwc/view-occurrences.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as view_occurrences from './view-occurrences';
import * as db from '../../database/db';
import * as occurrence_view_queries from '../../queries/occurrence/occurrence-view-queries';
import SQL from 'sql-template-strings';

chai.use(sinonChai);

describe('getOccurrencesForView', () => {
const dbConnectionObj = {
systemUserId: () => {
return null;
},
open: async () => {
// do nothing
},
release: () => {
// do nothing
},
commit: async () => {
// do nothing
},
rollback: async () => {
// do nothing
},
query: async () => {
// do nothing
}
};

const sampleReq = {
keycloak_token: {},
body: {
occurrence_submission_id: null
}
} as any;

let actualResult: any = null;

const sampleRes = {
status: () => {
return {
json: (result: any) => {
actualResult = result;
}
};
}
};

afterEach(() => {
sinon.restore();
});

it('should throw a 400 error when no occurrence submission id in request body', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

try {
const result = view_occurrences.getOccurrencesForView();

await result(sampleReq, (null as unknown) as any, (null as unknown) as any);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Missing required request body param `occurrence_submission_id`');
}
});

it('should throw an error when failed to build SQL get occurrences for view statement', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

sinon.stub(occurrence_view_queries, 'getOccurrencesForViewSQL').returns(null);

try {
const result = view_occurrences.getOccurrencesForView();

await result(
{ ...sampleReq, body: { occurrence_submission_id: 1 } },
(null as unknown) as any,
(null as unknown) as any
);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Failed to build SQL get occurrences for view statement');
}
});

it('should throw an error when failed to get occurrences view data', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({
rows: null
});

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(occurrence_view_queries, 'getOccurrencesForViewSQL').returns(SQL`something`);

try {
const result = view_occurrences.getOccurrencesForView();

await result(
{ ...sampleReq, body: { occurrence_submission_id: 1 } },
(null as unknown) as any,
(null as unknown) as any
);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Failed to get occurrences view data');
}
});

it('should return the occurrences view data on success', async () => {
const data = {
geometry: '{"type":"Point","coordinates":[50.7,60.9]}',
taxonid: 'M-LAL',
occurrence_id: 1,
lifestage: 'Adult',
vernacularname: 'V-name',
individualcount: 2,
organismquantity: 2,
organismquantitytype: 'Q-type'
};

const mockQuery = sinon.stub();

mockQuery.resolves({
rows: [data]
});

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(occurrence_view_queries, 'getOccurrencesForViewSQL').returns(SQL`something`);

const result = view_occurrences.getOccurrencesForView();

await result({ ...sampleReq, body: { occurrence_submission_id: 1 } }, sampleRes as any, (null as unknown) as any);

expect(actualResult).to.be.eql([
{
geometry: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [50.7, 60.9]
},
properties: {}
},
taxonId: data.taxonid,
occurrenceId: data.occurrence_id,
individualCount: Number(data.individualcount),
lifeStage: data.lifestage,
organismQuantity: Number(data.organismquantity),
organismQuantityType: data.organismquantitytype,
vernacularName: data.vernacularname
}
]);
});
});
6 changes: 3 additions & 3 deletions api/src/paths/dwc/view-occurrences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export function getOccurrencesForView(): RequestHandler {

const response = await connection.query(sqlStatement.text, sqlStatement.values);

const result = (response && response.rows && new GetOccurrencesViewData(response.rows)) || null;

if (!result || !result.occurrences) {
if (!response || !response.rows) {
throw new HTTP400('Failed to get occurrences view data');
}

const result = new GetOccurrencesViewData(response.rows);

await connection.commit();

return res.status(200).json(result.occurrences);
Expand Down
185 changes: 185 additions & 0 deletions api/src/paths/public/project/{projectId}/attachments/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as list from './list';
import * as db from '../../../../../database/db';
import * as project_queries from '../../../../../queries/public/project-queries';
import SQL from 'sql-template-strings';

chai.use(sinonChai);

describe('getPublicProjectAttachments', () => {
const dbConnectionObj = {
systemUserId: () => {
return null;
},
open: async () => {
// do nothing
},
release: () => {
// do nothing
},
commit: async () => {
// do nothing
},
rollback: async () => {
// do nothing
},
query: async () => {
// do nothing
}
};

const sampleReq = {
keycloak_token: {},
body: {},
params: {
projectId: 1
}
} as any;

let actualResult: any = null;

const sampleRes = {
status: () => {
return {
json: (result: any) => {
actualResult = result;
}
};
}
};

afterEach(() => {
sinon.restore();
});

it('should throw a 400 error when no projectId is provided', async () => {
sinon.stub(db, 'getDBConnection').returns(dbConnectionObj);

try {
const result = list.getPublicProjectAttachments();
await result(
{ ...sampleReq, params: { ...sampleReq.params, projectId: null } },
(null as unknown) as any,
(null as unknown) as any
);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Missing required path param `projectId`');
}
});

it('should throw a 400 error when no sql statement returned for getProjectAttachmentsSQL', async () => {
sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
}
});

sinon.stub(project_queries, 'getPublicProjectAttachmentsSQL').returns(null);

try {
const result = list.getPublicProjectAttachments();

await result(sampleReq, (null as unknown) as any, (null as unknown) as any);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Failed to build SQL get statement');
}
});

it('should return a list of project attachments where the lastModified is the create_date', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({
rows: [
{
id: 13,
file_name: 'name1',
create_date: '2020-01-01',
update_date: '',
file_size: 50
}
]
});

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(project_queries, 'getPublicProjectAttachmentsSQL').returns(SQL`something`);

const result = list.getPublicProjectAttachments();

await result(sampleReq, sampleRes as any, (null as unknown) as any);

expect(actualResult).to.be.eql({
attachmentsList: [{ fileName: 'name1', id: 13, lastModified: '2020-01-01', size: 50 }]
});
});

it('should return a list of project attachments where the lastModified is the update_date', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({
rows: [
{
id: 13,
file_name: 'name1',
create_date: '2020-01-01',
update_date: '2020-01-02',
file_size: 50
}
]
});

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(project_queries, 'getPublicProjectAttachmentsSQL').returns(SQL`something`);

const result = list.getPublicProjectAttachments();

await result(sampleReq, sampleRes as any, (null as unknown) as any);

expect(actualResult).to.be.eql({
attachmentsList: [{ fileName: 'name1', id: 13, lastModified: '2020-01-02', size: 50 }]
});
});

it('should return null if the project has no attachments, on success', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({ rows: undefined });

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(project_queries, 'getPublicProjectAttachmentsSQL').returns(SQL`something`);

const result = list.getPublicProjectAttachments();

await result(sampleReq, sampleRes as any, (null as unknown) as any);

expect(actualResult).to.be.null;
});
});
Loading

0 comments on commit ca28790

Please sign in to comment.