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

TECHDEBT: Deprecate old CSV patterns #1487

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
8 changes: 4 additions & 4 deletions api/src/services/critterbase-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ describe('CritterbaseService', () => {
describe('getTaxonMeasurements', () => {
it('should retrieve taxon measurements', async () => {
const axiosStub = sinon.stub(cb.axiosInstance, 'get').resolves({ data: [] });
await cb.getTaxonMeasurements('123456');
expect(axiosStub).to.have.been.calledOnceWith('/xref/taxon-measurements', { params: { tsn: '123456' } });
await cb.getTaxonMeasurements(123456);
expect(axiosStub).to.have.been.calledOnceWith('/xref/taxon-measurements', { params: { tsn: 123456 } });
});
});

describe('getTaxonBodyLocations', () => {
it('should retrieve taxon body locations', async () => {
const axiosStub = sinon.stub(cb.axiosInstance, 'get').resolves({ data: [] });
await cb.getTaxonBodyLocations('asdf');
await cb.getTaxonBodyLocations(1234);
expect(axiosStub).to.have.been.calledOnceWith('/xref/taxon-marking-body-locations', {
params: {
tsn: 'asdf',
tsn: 1234,
format: 'asSelect'
}
});
Expand Down
16 changes: 8 additions & 8 deletions api/src/services/critterbase-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ export class CritterbaseService {
/**
* Fetches qualitative and quantitative measurements for the specified taxon.
*
* @param {string} tsn - The taxon serial number (TSN).
* @param {number} tsn - The taxon serial number (TSN).
* @returns {Promise<{ qualitative: CBQualitativeMeasurementTypeDefinition[], quantitative: CBQuantitativeMeasurementTypeDefinition[] }>} - The response data containing qualitative and quantitative measurements.
*/
async getTaxonMeasurements(tsn: string): Promise<{
async getTaxonMeasurements(tsn: number): Promise<{
qualitative: CBQualitativeMeasurementTypeDefinition[];
quantitative: CBQuantitativeMeasurementTypeDefinition[];
}> {
Expand All @@ -507,10 +507,10 @@ export class CritterbaseService {
/**
* Fetches body location information for the specified taxon.
*
* @param {string} tsn - The taxon serial number (TSN).
* @param {number} tsn - The taxon serial number (TSN).
* @returns {Promise<IAsSelectLookup[]>} - The response data containing body location information.
*/
async getTaxonBodyLocations(tsn: string): Promise<IAsSelectLookup[]> {
async getTaxonBodyLocations(tsn: number): Promise<IAsSelectLookup[]> {
const response = await this.axiosInstance.get('/xref/taxon-marking-body-locations', {
params: { tsn, format: CritterbaseFormatEnum.AS_SELECT }
});
Expand Down Expand Up @@ -704,10 +704,10 @@ export class CritterbaseService {
* Find collection categories by tsn. Includes hierarchies.
*
* @async
* @param {string} tsn - ITIS TSN
* @param {number} tsn - ITIS TSN
* @returns {Promise<ICollectionCategory[]>} Collection categories
*/
async findTaxonCollectionCategories(tsn: string): Promise<ICollectionCategory[]> {
async findTaxonCollectionCategories(tsn: number): Promise<ICollectionCategory[]> {
const response = await this.axiosInstance.get(`/xref/taxon-collection-categories`, { params: { tsn } });

return response.data;
Expand All @@ -717,10 +717,10 @@ export class CritterbaseService {
* Find collection units by tsn. Includes hierarchies.
*
* @async
* @param {string} tsn - ITIS TSN
* @param {number} tsn - ITIS TSN
* @returns {Promise<ICollectionUnitWithCategory[]>} Collection units
*/
async findTaxonCollectionUnits(tsn: string): Promise<ICollectionUnitWithCategory[]> {
async findTaxonCollectionUnits(tsn: number): Promise<ICollectionUnitWithCategory[]> {
const response = await this.axiosInstance.get(`/xref/taxon-collection-units`, { params: { tsn } });

return response.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { NestedRecord } from '../../../utils/nested-record';
import { getMockDBConnection } from '../../../__mocks__/db';
import { CritterbaseService } from '../../critterbase-service';
import { SurveyCritterService } from '../../survey-critter-service';
import * as critterConfig from './critter-header-configs';
import { ImportCrittersService } from './import-critters-service';
import * as critterConfig from './utils/critter-header-configs';

chai.use(sinonChai);

Expand All @@ -31,12 +31,12 @@ describe('ImportCrittersService', () => {
expect(service).to.have.property('worksheet', worksheet);
expect(service).to.have.property('surveyId', 1);

expect(service.configUtils).to.be.instanceof(CSVConfigUtils);
expect(service.utils).to.be.instanceof(CSVConfigUtils);
expect(service.surveyCritterService).to.be.instanceof(SurveyCritterService);
expect(service.critterbaseService).to.be.instanceof(CritterbaseService);

expect(Object.keys(service._config.staticHeadersConfig)).to.deep.equal([
'ITIS_TSN',
expect(Object.keys(service.utils.config.staticHeadersConfig)).to.deep.equal([
'SPECIES',
'ALIAS',
'SEX',
'WLH_ID',
Expand All @@ -45,14 +45,13 @@ describe('ImportCrittersService', () => {
});
});

describe('_getCSVConfig', () => {
it('should return a valid CSVConfig object (no errors thrown)', async () => {
describe('getCSVConfig', () => {
it('should return a valid CSVConfig object', async () => {
const mockConnection = getMockDBConnection();
const worksheet = xlsx.utils.json_to_sheet([]);

const service = new ImportCrittersService(mockConnection, worksheet, 1);

sinon.stub(service, '_getTsnHeaderConfig').resolves({ validateCell: () => [] });
sinon.stub(service, '_getAliasHeaderConfig').resolves({ validateCell: () => [] });
sinon.stub(service, '_getSexHeaderConfig').resolves({ validateCell: () => [], setCellValue: () => 'A' });
sinon
Expand All @@ -64,7 +63,7 @@ describe('ImportCrittersService', () => {

const config = await service.getCSVConfig();

expect(config.staticHeadersConfig.ITIS_TSN.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.SPECIES.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.ALIAS.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.SEX.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.SEX.setCellValue).to.be.a('function');
Expand All @@ -75,55 +74,6 @@ describe('ImportCrittersService', () => {

expect(config.ignoreDynamicHeaders).to.be.false;
});

it('should return a valid CSVConfig object (when errors thrown)', async () => {
const mockConnection = getMockDBConnection();
const worksheet = xlsx.utils.json_to_sheet([]);

const service = new ImportCrittersService(mockConnection, worksheet, 1);

sinon.stub(service, '_getTsnHeaderConfig').resolves({ validateCell: () => [] });
sinon.stub(service, '_getAliasHeaderConfig').resolves({ validateCell: () => [] });
sinon.stub(service, '_getSexHeaderConfig').resolves({ validateCell: () => [], setCellValue: () => 'A' });
sinon.stub(service, '_getCollectionUnitDynamicHeaderConfig').rejects(new Error('Dynamic header error'));

sinon.stub(headerConfig, 'getDescriptionCellValidator').returns(() => []);
sinon.stub(critterConfig, 'getWlhIDCellValidator').returns(() => []);

const config = await service.getCSVConfig();

expect(config.staticHeadersConfig.ITIS_TSN.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.ALIAS.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.SEX.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.SEX.setCellValue).to.be.a('function');
expect(config.staticHeadersConfig.WLH_ID.validateCell).to.be.a('function');
expect(config.staticHeadersConfig.DESCRIPTION.validateCell).to.be.a('function');

expect(config.dynamicHeadersConfig).to.be.undefined;
expect(config.ignoreDynamicHeaders).to.be.true;
});
});

describe('_getTsnHeaderConfig', () => {
it('should return a valid header config object', async () => {
const mockConnection = getMockDBConnection();
const worksheet = xlsx.utils.json_to_sheet([{ ITIS_TSN: '1234' }]);

const service = new ImportCrittersService(mockConnection, worksheet, 1);

const getTaxonomyByTsnsStub = sinon
.stub(service.platformService, 'getTaxonomyByTsns')
.resolves([{ tsn: 1234, scientificName: 'test' }]);
const getTsnCellValidatorStub = sinon.stub(headerConfig, 'getTsnCellValidator').returns(() => []);

const tsnHeaderConfig = await service._getTsnHeaderConfig();

expect(getTaxonomyByTsnsStub).to.have.been.calledOnceWithExactly(['1234']);
expect(getTsnCellValidatorStub).to.have.been.calledOnceWithExactly(new Set([1234]));

expect(tsnHeaderConfig.validateCell).to.be.a('function');
expect(tsnHeaderConfig.setCellValue).to.be.a('function');
});
});

describe('_getAliasHeaderConfig', () => {
Expand All @@ -143,10 +93,7 @@ describe('ImportCrittersService', () => {
const aliasHeaderConfig = await service._getAliasHeaderConfig();

expect(getSurveyCritterAliasesStub).to.have.been.calledOnceWithExactly(1);
expect(getCritterAliasCellValidatorStub).to.have.been.calledOnceWithExactly(
new Set(['test']),
service.configUtils
);
expect(getCritterAliasCellValidatorStub).to.have.been.calledOnceWithExactly(new Set(['test']), service.utils);

expect(aliasHeaderConfig.validateCell).to.be.a('function');
expect(aliasHeaderConfig.setCellValue).to.be.a('function');
Expand All @@ -156,7 +103,7 @@ describe('ImportCrittersService', () => {
describe('_getSexHeaderConfig', () => {
it('should return a valid header config object', async () => {
const mockConnection = getMockDBConnection();
const worksheet = xlsx.utils.json_to_sheet([{ ITIS_TSN: 1234 }]);
const worksheet = xlsx.utils.json_to_sheet([{ SPECIES: 'species' }]);

const service = new ImportCrittersService(mockConnection, worksheet, 1);

Expand All @@ -181,14 +128,13 @@ describe('ImportCrittersService', () => {

const getSexCellValidatorStub = sinon.stub(critterConfig, 'getCritterSexCellValidator').returns(() => []);

const sexHeaderConfig = await service._getSexHeaderConfig();
const sexHeaderConfig = await service._getSexHeaderConfig([1234]);

expect(getTaxonMeasurementsStub).to.have.been.calledWithExactly('1234');
expect(getTaxonMeasurementsStub).to.have.been.calledWithExactly(1234);
expect(getSexCellValidatorStub).to.have.been.calledWithExactly(
new NestedRecord({
1234: { male: 'maleUUID', female: 'femaleUUID' }
}),
service.configUtils
})
);

expect(sexHeaderConfig.validateCell).to.be.a('function');
Expand All @@ -199,7 +145,7 @@ describe('ImportCrittersService', () => {
describe('_getCollectionUnitDynamicHeaderConfig', () => {
it('should return a valid header config object', async () => {
const mockConnection = getMockDBConnection();
const worksheet = xlsx.utils.json_to_sheet([{ UNIT: 'unit', ITIS_TSN: 1234 }]);
const worksheet = xlsx.utils.json_to_sheet([{ UNIT: 'unit', SPECIES: 'species' }]);

const service = new ImportCrittersService(mockConnection, worksheet, 1);

Expand All @@ -211,26 +157,16 @@ describe('ImportCrittersService', () => {
.stub(critterConfig, 'getCritterCollectionUnitCellValidator')
.returns(() => []);

const getCollectionUnitCellSetterStub = sinon
.stub(critterConfig, 'getCritterCollectionUnitCellSetter')
.returns(() => 'value');
const config = await service._getCollectionUnitDynamicHeaderConfig([1234]);

const config = await service._getCollectionUnitDynamicHeaderConfig();

expect(findTaxonCollectionUnitsStub).to.have.been.calledOnceWithExactly('1234');
expect(findTaxonCollectionUnitsStub).to.have.been.calledOnceWithExactly(1234);

expect(getCollectionUnitCellValidatorStub).to.have.been.calledWithExactly(
new NestedRecord({ 1234: { category: { unit: 'uuid' } } }),
service.configUtils
);

expect(getCollectionUnitCellSetterStub).to.have.been.calledWithExactly(
new NestedRecord({ 1234: { category: { unit: 'uuid' } } }),
service.configUtils
new NestedRecord({ 1234: { category: { unit: 'uuid' } } })
);

expect(config.validateCell).to.be.a('function');
expect(config.setCellValue).to.be.a('function');
expect(config.setCellValue).to.be.undefined;
});
});

Expand All @@ -239,15 +175,16 @@ describe('ImportCrittersService', () => {
const mockConnection = getMockDBConnection();
const rows = [
{
ITIS_TSN: '1234',
SPECIES: 1234,
ALIAS: 'test',
SEX: 'male',
SEX: 'sexId',
WLH_ID: '12-2222',
DESCRIPTION: 'comment',
POPULATION_UNIT: 'unit',
COLLECTION_UNIT: 'collection',
[CSVRowState]: {
sexId: 'sexId'
itis_tsn: 1234,
itis_scientific_name: 'species'
}
}
];
Expand All @@ -259,7 +196,7 @@ describe('ImportCrittersService', () => {

expect(payloads.simsPayload[0]).to.be.a('string');

expect(payloads.critterbasePayload.critters?.[0].itis_tsn).to.be.equal('1234');
expect(payloads.critterbasePayload.critters?.[0].itis_tsn).to.be.equal(1234);
expect(payloads.critterbasePayload.critters?.[0].animal_id).to.be.equal('test');
expect(payloads.critterbasePayload.critters?.[0].sex_qualitative_option_id).to.be.equal('sexId');
expect(payloads.critterbasePayload.critters?.[0].wlh_id).to.be.equal('12-2222');
Expand Down
Loading
Loading