-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
570 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
api/src/paths/public/project/{projectId}/attachments/list.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
Oops, something went wrong.