Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMSBIOHUB-457: Taxonomy Endpoints #235

Merged
merged 23 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"git.ignoreLimitWarning": true
}
"git.ignoreLimitWarning": true
}
6 changes: 3 additions & 3 deletions api/package-lock.json

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

12 changes: 12 additions & 0 deletions api/src/__mocks__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ export class MockRes {
return this;
});

/**
* The value of the last `.setHeader(<value>)` call.
*
* @memberof MockRes
*/
headerValue: any;
setHeader = sinon.fake((header: any) => {
this.headerValue = header;

return this;
});

/**
* The value of the last `.json(<value>)` call.
*
Expand Down
2 changes: 1 addition & 1 deletion api/src/paths/submission/intake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function submissionIntake(): RequestHandler {
const searchIndexService = new SearchIndexService(connection);
const regionService = new RegionService(connection);

// validate theubmission
// validate the submission
if (!(await validationService.validateSubmissionFeatures([submissionFeature]))) {
throw new HTTP400('Invalid submission'); // TODO return details on why the submission is invalid
}
Expand Down
87 changes: 0 additions & 87 deletions api/src/paths/taxonomy/species/list.ts

This file was deleted.

88 changes: 0 additions & 88 deletions api/src/paths/taxonomy/species/search.ts

This file was deleted.

97 changes: 97 additions & 0 deletions api/src/paths/taxonomy/taxon/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Ajv from 'ajv';
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { findTaxonBySearchTerms, GET } from '.';
import * as db from '../../../database/db';
import { HTTPError } from '../../../errors/http-error';
import { ItisService } from '../../../services/itis-service';
import { getMockDBConnection, getRequestHandlerMocks } from '../../../__mocks__/db';

chai.use(sinonChai);

describe('taxon', () => {
describe('openapi schema', () => {
const ajv = new Ajv();

it('is valid openapi v3 schema', () => {
expect(ajv.validateSchema(GET.apiDoc as unknown as object)).to.be.true;
});
});

describe('findTaxonBySearchTerms', () => {
afterEach(() => {
sinon.restore();
});

it('returns an empty array if no species are found', async () => {
const dbConnectionObj = getMockDBConnection();

sinon.stub(db, 'getDBConnection').returns(dbConnectionObj);

const getSpeciesFromIdsStub = sinon.stub(ItisService.prototype, 'searchItisByTerm').resolves([]);

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
mockReq.query = {
terms: ''
};

const requestHandler = findTaxonBySearchTerms();

await requestHandler(mockReq, mockRes, mockNext);

expect(getSpeciesFromIdsStub).to.have.been.calledWith('');

expect(mockRes.statusValue).to.equal(200);
expect(mockRes.jsonValue).to.eql({ searchResponse: [] });
});

it('returns an array of species', async () => {
const dbConnectionObj = getMockDBConnection();

sinon.stub(db, 'getDBConnection').returns(dbConnectionObj);

const mock1 = { id: '1', commonName: 'something', scientificName: 'string' } as unknown as any;
const mock2 = { id: '2', commonName: null, scientificName: 'string' } as unknown as any;

const getSpeciesFromIdsStub = sinon.stub(ItisService.prototype, 'searchItisByTerm').resolves([mock1, mock2]);

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
mockReq.query = {
terms: 't'
};

const requestHandler = findTaxonBySearchTerms();

await requestHandler(mockReq, mockRes, mockNext);

expect(getSpeciesFromIdsStub).to.have.been.calledWith('t');

expect(mockRes.jsonValue).to.eql({ searchResponse: [mock1, mock2] });
expect(mockRes.statusValue).to.equal(200);
});

it('catches error, and re-throws error', async () => {
const dbConnectionObj = getMockDBConnection({ rollback: sinon.stub(), release: sinon.stub() });

sinon.stub(db, 'getDBConnection').returns(dbConnectionObj);

sinon.stub(ItisService.prototype, 'searchItisByTerm').rejects(new Error('a test error'));

const { mockReq, mockRes, mockNext } = getRequestHandlerMocks();
mockReq.query = {
ids: 'a'
};

try {
const requestHandler = findTaxonBySearchTerms();

await requestHandler(mockReq, mockRes, mockNext);
expect.fail();
} catch (actualError) {
expect((actualError as HTTPError).message).to.equal('a test error');
}
});
});
});
Loading
Loading