-
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.
chore: updated tests for critter capture row validator
- Loading branch information
Showing
4 changed files
with
211 additions
and
22 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
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
194 changes: 194 additions & 0 deletions
194
api/src/utils/csv-utils/row-validators/critter-capture-row-validator.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,194 @@ | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { CSVRowState } from '../csv-config-validation.interface'; | ||
import { getCritterCaptureRowValidator } from './critter-capture-row-validator'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe.only('getCritterCaptureRowValidator', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should return an error if the critter alias is not found in the survey alias map', () => { | ||
const getCellValueStub = sinon.stub(); | ||
const getWorksheetHeaderStub = sinon.stub().returns('HEADER'); | ||
|
||
const utils = { | ||
getCellValue: getCellValueStub, | ||
getWorksheetHeader: getWorksheetHeaderStub | ||
}; | ||
|
||
getCellValueStub.onCall(0).returns('BAD_ALIAS'); | ||
|
||
const surveyAliasMap = new Map<string, any>(); | ||
|
||
const rowValidator = getCritterCaptureRowValidator(surveyAliasMap, utils as any); | ||
|
||
const errors = rowValidator({ row: {} } as any); | ||
|
||
expect(errors).to.have.lengthOf(1); | ||
expect(errors[0].error).to.contain('matching survey animal'); | ||
expect(errors[0].cell).to.equal('BAD_ALIAS'); | ||
expect(errors[0].header).to.be.equal('HEADER'); | ||
}); | ||
|
||
it('should return an error if the critter has no captures', () => { | ||
const getCellValueStub = sinon.stub(); | ||
const getWorksheetHeaderStub = sinon.stub().returns('HEADER'); | ||
|
||
const utils = { | ||
getCellValue: getCellValueStub, | ||
getWorksheetHeader: getWorksheetHeaderStub | ||
}; | ||
|
||
getCellValueStub.onCall(0).returns('critter'); | ||
|
||
const surveyAliasMap = new Map<string, any>([['critter', { captures: [] }]]); | ||
|
||
const rowValidator = getCritterCaptureRowValidator(surveyAliasMap, utils as any); | ||
|
||
const errors = rowValidator({ row: {} } as any); | ||
|
||
expect(errors).to.have.lengthOf(1); | ||
expect(errors[0].error).to.contain('no captures'); | ||
expect(errors[0].cell).to.equal('critter'); | ||
expect(errors[0].header).to.be.equal('HEADER'); | ||
}); | ||
|
||
it('should return an error if the capture date is not found in the critter captures', () => { | ||
const getCellValueStub = sinon.stub(); | ||
const getWorksheetHeaderStub = sinon.stub().returns('HEADER'); | ||
|
||
const utils = { | ||
getCellValue: getCellValueStub, | ||
getWorksheetHeader: getWorksheetHeaderStub | ||
}; | ||
|
||
getCellValueStub.onCall(0).returns('critter'); | ||
getCellValueStub.onCall(1).returns('2024-01-01'); | ||
getCellValueStub.onCall(2).returns('10:10:00'); | ||
|
||
const surveyAliasMap = new Map<string, any>([['critter', { captures: [{ capture_date: '2025-01-01' }] }]]); | ||
const rowValidator = getCritterCaptureRowValidator(surveyAliasMap, utils as any); | ||
|
||
const errors = rowValidator({ row: {} } as any); | ||
|
||
expect(errors).to.have.lengthOf(1); | ||
expect(errors[0].error).to.contain('Capture not found'); | ||
expect(errors[0].cell).to.equal('2024-01-01'); | ||
expect(errors[0].header).to.be.equal('HEADER'); | ||
}); | ||
|
||
it('should return an error if multiple captures are found for the critter', () => { | ||
const getCellValueStub = sinon.stub(); | ||
const getWorksheetHeaderStub = sinon.stub().returns('HEADER'); | ||
|
||
const utils = { | ||
getCellValue: getCellValueStub, | ||
getWorksheetHeader: getWorksheetHeaderStub | ||
}; | ||
|
||
getCellValueStub.onCall(0).returns('critter'); | ||
getCellValueStub.onCall(1).returns('2024-01-01'); | ||
getCellValueStub.onCall(2).returns('10:10:00'); | ||
|
||
const surveyAliasMap = new Map<string, any>([ | ||
[ | ||
'critter', | ||
{ | ||
captures: [ | ||
{ | ||
capture_date: '2024-01-01', | ||
capture_time: '10:10:00' | ||
}, | ||
{ | ||
capture_date: '2024-01-01', | ||
capture_time: '10:10:00' | ||
} | ||
] | ||
} | ||
] | ||
]); | ||
const rowValidator = getCritterCaptureRowValidator(surveyAliasMap, utils as any); | ||
|
||
const errors = rowValidator({ row: {} } as any); | ||
|
||
expect(errors).to.have.lengthOf(1); | ||
expect(errors[0].error).to.contain('Multiple captures found'); | ||
expect(errors[0].cell).to.equal('2024-01-01'); | ||
expect(errors[0].header).to.be.equal('HEADER'); | ||
}); | ||
|
||
it('should return an empty array if the critter capture is valid', () => { | ||
const getCellValueStub = sinon.stub(); | ||
const getWorksheetHeaderStub = sinon.stub().returns('HEADER'); | ||
|
||
const utils = { | ||
getCellValue: getCellValueStub, | ||
getWorksheetHeader: getWorksheetHeaderStub | ||
}; | ||
|
||
getCellValueStub.onCall(0).returns('critter'); | ||
getCellValueStub.onCall(1).returns('2024-01-01'); | ||
getCellValueStub.onCall(2).returns('10:10:00'); | ||
|
||
const surveyAliasMap = new Map<string, any>([ | ||
[ | ||
'critter', | ||
{ | ||
captures: [ | ||
{ | ||
capture_date: '2024-01-01', | ||
capture_time: '10:10:00' | ||
} | ||
] | ||
} | ||
] | ||
]); | ||
const rowValidator = getCritterCaptureRowValidator(surveyAliasMap, utils as any); | ||
|
||
const errors = rowValidator({ row: {} } as any); | ||
|
||
expect(errors).to.have.lengthOf(0); | ||
}); | ||
|
||
it('should update the row state with the critter_id and capture_id', () => { | ||
const getCellValueStub = sinon.stub(); | ||
const getWorksheetHeaderStub = sinon.stub().returns('HEADER'); | ||
|
||
const utils = { | ||
getCellValue: getCellValueStub, | ||
getWorksheetHeader: getWorksheetHeaderStub | ||
}; | ||
|
||
getCellValueStub.onCall(0).returns('critter'); | ||
getCellValueStub.onCall(1).returns('2024-01-01'); | ||
getCellValueStub.onCall(2).returns('10:10:00'); | ||
|
||
const surveyAliasMap = new Map<string, any>([ | ||
[ | ||
'critter', | ||
{ | ||
critter_id: 'critter_id', | ||
captures: [ | ||
{ | ||
capture_id: 'capture_id', | ||
capture_date: '2024-01-01', | ||
capture_time: '10:10:00' | ||
} | ||
] | ||
} | ||
] | ||
]); | ||
const rowValidator = getCritterCaptureRowValidator(surveyAliasMap, utils as any); | ||
|
||
const row = {}; | ||
|
||
rowValidator({ row } as any); | ||
|
||
expect(row[CSVRowState].critter_id).to.equal('critter_id'); | ||
expect(row[CSVRowState].capture_id).to.equal('capture_id'); | ||
}); | ||
}); |
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