Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
KjartanE committed Feb 1, 2024
1 parent 7359346 commit 616d045
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
128 changes: 128 additions & 0 deletions api/src/repositories/taxonomy-repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import chai, { expect } from 'chai';
import { describe } from 'mocha';
import { QueryResult } from 'pg';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { getMockDBConnection } from '../__mocks__/db';
import { TaxonomyRepository, TaxonRecord } from './taxonomy-repository';

chai.use(sinonChai);

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

describe('getTaxonByTsnIds', () => {
it('should return array of system constants', async () => {
const TaxonRecord = {
taxon_id: 1,
itis_tsn: 1,
bc_taxon_code: 'string',
itis_scientific_name: 'string',
common_name: 'string',
itis_data: {},
record_effective_date: 'string',
record_end_date: 'string',
create_date: 'string',
create_user: 1,
update_date: 'string',
update_user: 1,
revision_count: 1
};

const mockQueryResponse = {
rowCount: 1,
rows: [TaxonRecord] as unknown as TaxonRecord[]
} as unknown as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({ knex: () => mockQueryResponse });

const taxonomyRepository = new TaxonomyRepository(mockDBConnection);

const response = await taxonomyRepository.getTaxonByTsnIds([1]);

expect(response).to.be.eql([TaxonRecord]);
});
});

describe('addItisTaxonRecord', () => {
it('should return a new taxon record', async () => {
const mockQueryResponse = {
rowCount: 1,
rows: [
{
taxon_id: 1,
itis_tsn: 1,
bc_taxon_code: 'string',
itis_scientific_name: 'string',
common_name: 'string',
itis_data: {},
record_effective_date: 'string',
record_end_date: 'string',
create_date: 'string',
create_user: 1,
update_date: 'string',
update_user: 1,
revision_count: 1
}
]
} as unknown as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({ sql: () => mockQueryResponse });

const taxonomyRepository = new TaxonomyRepository(mockDBConnection);

const response = await taxonomyRepository.addItisTaxonRecord(1, 'string', 'string', {}, 'string');

expect(response).to.be.eql({
taxon_id: 1,
itis_tsn: 1,
bc_taxon_code: 'string',
itis_scientific_name: 'string',
common_name: 'string',
itis_data: {},
record_effective_date: 'string',
record_end_date: 'string',
create_date: 'string',
create_user: 1,
update_date: 'string',
update_user: 1,
revision_count: 1
});
});
});

describe('deleteTaxonRecord', () => {
it('should return a deleted taxon record', async () => {
const mockQueryResponse = {
rowCount: 1,
rows: [
{
taxon_id: 1,
itis_tsn: 1,
bc_taxon_code: 'string',
itis_scientific_name: 'string',
common_name: 'string',
itis_data: {},
record_effective_date: 'string',
record_end_date: 'string',
create_date: 'string',
create_user: 1,
update_date: 'string',
update_user: 1,
revision_count: 1
}
]
} as unknown as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({ sql: () => mockQueryResponse });

const taxonomyRepository = new TaxonomyRepository(mockDBConnection);

const response = await taxonomyRepository.deleteTaxonRecord(1);

expect(response).to.be.eql(undefined);
});
});
});
1 change: 0 additions & 1 deletion api/src/services/es-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const ElasticSearchIndices = {
TAXONOMY: process.env.ELASTICSEARCH_TAXONOMY_INDEX || 'taxonomy_3.0.0'
};


/**
* Base class for services that require a elastic search connection.
*
Expand Down

0 comments on commit 616d045

Please sign in to comment.