Skip to content

Commit

Permalink
feat(CB2-11360): cherished transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Searle committed May 24, 2024
1 parent d8462a9 commit 3538e1c
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 196 deletions.
1 change: 1 addition & 0 deletions src/handler/mot-update-vrm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const processRecord = async (cherishedTransfer: SQSRecord): Promise<SNSMessageBo
completeTechRecord,
);
await updateVehicle(recordsToArchive, recordsToUpdate);
logger.info(`Updated systemNumber ${currentRecord.systemNumber} with VRM ${parsedRecord.vrm}`);

const newAdrCertificate = createAdrCertificate(currentRecord.systemNumber);
const adrCertificateDetailsErrors = validateAdrCertificateDetails(newAdrCertificate);
Expand Down
343 changes: 147 additions & 196 deletions tests/unit/handler/mot-update-vrm.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,198 +1,149 @@
// /* eslint-disable import/first */
// const mockSearchByCriteria = jest.fn();
// const mockUpdateVehicle = jest.fn();
// const mockPublish = jest.fn();
// const mockSqs = jest.fn();
//
// import { handler } from '../../../src/handler/mot-update-vrm';
// import { StatusCode } from '../../../src/util/enum';
// import logger from '../../../src/util/logger';
// import updateEventMultiple from '../../resources/mot-vrm-update-event-multiple.json';
// import updateEvent from '../../resources/mot-vrm-update-event.json';
//
// jest.mock('../../../src/services/database.ts', () => ({
// searchByCriteria: mockSearchByCriteria,
// updateVehicle: mockUpdateVehicle,
// }));
//
// jest.mock('../../../src/services/sns', () => ({
// publish: mockPublish,
// }));
//
// jest.mock('../../../src/services/sqs', () => ({
// addToSqs: mockSqs,
// }));
//
// describe('Test Mot Update Vrm Lambda Function', () => {
// beforeEach(() => {
// jest.resetAllMocks();
// jest.resetModules();
// });
//
// it('should log when no records are found', async () => {
// mockSearchByCriteria.mockReturnValue([]);
//
// const loggerSpy = jest.spyOn(logger, 'info');
//
// updateEvent.Records[0].body = JSON.stringify({
// vin: '1',
// vrm: '3',
// });
//
// await handler(updateEvent);
//
// expect(loggerSpy).toHaveBeenCalledWith('No record found for VIN: 1');
// expect(mockPublish).not.toHaveBeenCalled();
// });
//
// it('should log when no current records are found', async () => {
// mockSearchByCriteria.mockReturnValue([
// {
// primaryVrm: '1',
// vin: '2',
// techRecord_statusCode: StatusCode.ARCHIVED,
// systemNumber: '15',
// },
// ]);
//
// const loggerSpy = jest.spyOn(logger, 'info');
//
// updateEvent.Records[0].body = JSON.stringify({
// vin: '1',
// vrm: '3',
// });
//
// await handler(updateEvent);
//
// expect(loggerSpy).toHaveBeenCalledWith('No current record found for VIN: 1');
// expect(mockPublish).not.toHaveBeenCalled();
// });
//
// it('should log when a current record with a duplicate VIN is found', async () => {
// mockSearchByCriteria.mockReturnValue([{
// primaryVrm: '1',
// vin: '2',
// techRecord_statusCode: StatusCode.CURRENT,
// systemNumber: '15',
// },
// {
// primaryVrm: '1',
// vin: '2',
// techRecord_statusCode: StatusCode.CURRENT,
// systemNumber: '16',
// }]);
//
// const loggerSpy = jest.spyOn(logger, 'info');
//
// updateEvent.Records[0].body = JSON.stringify({
// vin: '1',
// vrm: '3',
// });
//
// await handler(updateEvent);
//
// expect(loggerSpy).toHaveBeenCalledWith('Duplicate current records found for VIN 1');
// expect(mockPublish).not.toHaveBeenCalled();
// });
//
// it('should log when a current record with a duplicate VIN and VRM is found', async () => {
// mockSearchByCriteria.mockReturnValue([{
// primaryVrm: '1',
// vin: '2',
// techRecord_statusCode: StatusCode.CURRENT,
// systemNumber: '15',
// },
// ]);
//
// const loggerSpy = jest.spyOn(logger, 'info');
//
// updateEvent.Records[0].body = JSON.stringify({
// vin: '2',
// vrm: '1',
// });
//
// await handler(updateEvent);
//
// expect(loggerSpy).toHaveBeenCalledWith('No update needed for VRM 1 and VIN 2');
// expect(mockPublish).not.toHaveBeenCalled();
// });
//
// it('should log when there is a current record with a matching VIN and no matching VRM', async () => {
// mockSqs.mockResolvedValueOnce(undefined);
// mockSearchByCriteria.mockReturnValue([{
// primaryVrm: '1',
// vin: '2',
// techRecord_statusCode: StatusCode.CURRENT,
// systemNumber: '15',
// },
// ]);
//
// mockUpdateVehicle.mockResolvedValue(true);
//
// const loggerSpy = jest.spyOn(logger, 'info');
//
// updateEvent.Records[0].body = JSON.stringify({
// vin: '2',
// vrm: '3',
// });
//
// await handler(updateEvent);
//
// expect(loggerSpy).toHaveBeenCalledWith('Updated systemNumber 15 with VRM 3');
// expect(mockUpdateVehicle).toHaveBeenCalled();
// expect(mockPublish).toHaveBeenCalled();
// });
//
// it('should run three events, pass one, fail one, pass the third one', async () => {
// mockSqs.mockResolvedValueOnce(undefined);
//
// mockSearchByCriteria.mockReturnValueOnce([{
// primaryVrm: '1',
// vin: '2',
// techRecord_statusCode: StatusCode.CURRENT,
// systemNumber: '15',
// },
// ]).mockReturnValueOnce([]).mockReturnValue([
// {
// primaryVrm: '10',
// vin: '5',
// techRecord_statusCode: StatusCode.CURRENT,
// systemNumber: '16',
// },
// ]);
//
// mockUpdateVehicle.mockResolvedValue(true);
//
// const loggerSpy = jest.spyOn(logger, 'info');
//
// updateEventMultiple.Records[0].body = JSON.stringify({
// vin: '2',
// vrm: '3',
// });
//
// updateEventMultiple.Records[1].body = JSON.stringify({
// vin: '3',
// vrm: '4',
// });
//
// updateEventMultiple.Records[2].body = JSON.stringify({
// vin: '5',
// vrm: '6',
// });
//
// await handler(updateEventMultiple);
//
// expect(loggerSpy).toHaveBeenCalledWith('Updated systemNumber 15 with VRM 3');
// expect(loggerSpy).toHaveBeenCalledWith('No record found for VIN: 3');
// expect(loggerSpy).toHaveBeenCalledWith('Updated systemNumber 16 with VRM 6');
// expect(mockUpdateVehicle).toHaveBeenCalledTimes(2);
// expect(mockPublish).toHaveBeenCalled();
// expect(loggerSpy).toHaveBeenLastCalledWith('All records processed in SQS event');
// });
// });

describe('Mot update vrm', () => {
test('it should update the vrm', () => {
console.log('test');
/* eslint-disable import/first */

const mockGetBySystemNumberAndCreatedTimestamp = jest.fn();
const mockSearchByCriteria = jest.fn();
const mockUpdateVehicle = jest.fn();
const mockPublish = jest.fn();
const mockSqs = jest.fn();

import { handler } from '../../../src/handler/mot-update-vrm';
import { StatusCode } from '../../../src/util/enum';
import logger from '../../../src/util/logger';
import updateEventMultiple from '../../resources/mot-vrm-update-event-multiple.json';

Check failure on line 12 in tests/unit/handler/mot-update-vrm.unit.test.ts

View workflow job for this annotation

GitHub Actions / tests

'updateEventMultiple' is defined but never used
import updateEvent from '../../resources/mot-vrm-update-event.json';

jest.mock('../../../src/services/database.ts', () => ({
searchByCriteria: mockSearchByCriteria,
updateVehicle: mockUpdateVehicle,
getBySystemNumberAndCreatedTimestamp: mockGetBySystemNumberAndCreatedTimestamp,
}));

jest.mock('../../../src/services/sns', () => ({
publish: mockPublish,
}));

jest.mock('../../../src/services/sqs', () => ({
addToSqs: mockSqs,
}));

describe('Test Mot Update Vrm Lambda Function', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
});

it('should log when no records are found', async () => {
mockSearchByCriteria.mockReturnValue([]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '1',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('No record found for VIN: 1');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when no current records are found', async () => {
mockSearchByCriteria.mockReturnValue([
{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.ARCHIVED,
systemNumber: '15',
},
]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '1',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('No current record found for VIN: 1');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when a current record with a duplicate VIN is found', async () => {
mockSearchByCriteria.mockReturnValue([{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '15',
},
{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '16',
}]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '1',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('Duplicate current records found for VIN 1');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when a current record with a duplicate VIN and VRM is found', async () => {
mockSearchByCriteria.mockReturnValue([{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '15',
},
]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '2',
vrm: '1',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('No update needed for VRM 1 and VIN 2');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when there is a current record with a matching VIN and no matching VRM', async () => {
mockSqs.mockResolvedValueOnce(undefined);
mockSearchByCriteria.mockReturnValue([{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '15',
},
]);

mockGetBySystemNumberAndCreatedTimestamp.mockResolvedValueOnce({});
mockUpdateVehicle.mockResolvedValue(true);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '2',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('Updated systemNumber 15 with VRM 3');
expect(mockUpdateVehicle).toHaveBeenCalled();
expect(mockPublish).toHaveBeenCalled();
});
});

0 comments on commit 3538e1c

Please sign in to comment.