Skip to content

Commit

Permalink
feat(cb2-9560): removed testing logs and increased coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-cs committed Oct 17, 2023
1 parent fdb3d79 commit 9f6e2a4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 23 deletions.
22 changes: 0 additions & 22 deletions src/services/CertificateGenerationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,10 @@ class CertificateGenerationService {
vehicleTestRes = "adr_pass";
} else if (WELSH_CERT_VEHICLES.TYPES.includes(testResult.vehicleType) && testType.testResult === "pass" && isTestStationWelsh) {
vehicleTestRes = testResult.vehicleType + "_" + testType.testResult + "_bilingual";
console.log("** THIS IS THE vehicleTestRes in the else if " + vehicleTestRes);
} else {
vehicleTestRes = testResult.vehicleType + "_" + testType.testResult;
}

console.log("** THIS IS THE vehicleType after the else if: " + testResult.vehicleType);
console.log("** THIS IS THE testResult after the else if: " + testType.testResult);
console.log("** THIS IS THE isTestStationWelsh in the else if: " + isTestStationWelsh);
console.log("** THIS IS THE vehicleTestRes after the else if: " + vehicleTestRes);

const invokeParams: any = {
FunctionName: iConfig.functions.certGen.name,
InvocationType: "RequestResponse",
Expand Down Expand Up @@ -971,7 +965,6 @@ class CertificateGenerationService {
case "minor":
defects.MinorDefects.push(this.formatDefect(defect));
if (testTypes.testResult === TEST_RESULTS.PASS && isWelsh && WELSH_CERT_VEHICLES.TYPES.includes(vehicleType)) {
// TODO - add logic to only push to array if not null
defects.MinorDefectsWelsh.push(
this.formatDefectWelsh(defect, vehicleType, flattenedDefects)
);
Expand All @@ -980,7 +973,6 @@ class CertificateGenerationService {
case "advisory":
defects.AdvisoryDefects.push(this.formatDefect(defect));
if (testTypes.testResult === TEST_RESULTS.PASS && isWelsh && WELSH_CERT_VEHICLES.TYPES.includes(vehicleType)) {
// TODO - add logic to only push to array if not null
defects.AdvisoryDefectsWelsh.push(this.formatDefect(defect));
}
break;
Expand Down Expand Up @@ -1069,7 +1061,6 @@ class CertificateGenerationService {
vehicleType
);

// TODO - handle if there are no matching defects and remove this if
if (filteredFlatDefect !== null) {
let defectString = `${defect.deficiencyRef} ${filteredFlatDefect.itemDescriptionWelsh}`;

Expand Down Expand Up @@ -1110,9 +1101,6 @@ class CertificateGenerationService {
if (defect.additionalInformation.notes) {
defectString += ` ${defect.additionalInformation.notes}`;
}
// TODO - remove this once tested
console.log(`Defect: ${JSON.stringify(defect)}`);
console.log(`Welsh Defect String Generated: ${defectString}`);
return defectString;
} else {
console.log(`ERROR: Unable to find a filtered defect`);
Expand Down Expand Up @@ -1161,20 +1149,12 @@ class CertificateGenerationService {
if (filteredFlatDefects.length === 0) {
return null;
} else if (filteredFlatDefects.length === 1) {
// TODO - remove this once tested
console.log(
`Filtered to one defect on def ref id: ${JSON.stringify(filteredFlatDefects[0])}`
);
return filteredFlatDefects[0];
} else {
const filteredWelshDefectsOnVehicleType = filteredFlatDefects.filter(
(flatDefect: IFlatDefect) =>
flatDefect.forVehicleType!.includes(vehicleType)
);
// TODO - remove this once tested
console.log(
`Filtered to one defect on def ref id and vehicle type: ${JSON.stringify(filteredWelshDefectsOnVehicleType[0])}`
);
return filteredWelshDefectsOnVehicleType[0];
}
}
Expand Down Expand Up @@ -1228,8 +1208,6 @@ class CertificateGenerationService {
});
}
});
// TODO - remove this once tested
console.log("Flattened defect array length: " + flatDefects.length);
} catch (e) {
console.error(`Error flattening defects: ${e}`);
}
Expand Down
71 changes: 70 additions & 1 deletion tests/unit/CertificateGenerationService.unitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import defectsMock from "../../tests/resources/defects_mock.json";
import flatDefectsMock from "../../tests/resources/flattened-defects.json";
import testStationsMock from "../../tests/resources/testStationsMock.json";
import { LOCATION_ENGLISH, LOCATION_WELSH } from "../../src/models/Enums";
import {Configuration} from "../../src/utils/Configuration";

describe("Certificate Generation Service", () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -565,6 +566,28 @@ describe("Certificate Generation Service", () => {
"74.1 Diffyg na ddisgrifir mewn man arall yn y llawlyfr fel: byddai defnyddio'r cerbyd ar y ffordd yn golygu perygl uniongyrchol o anaf i unrhyw berson arall. Blaen. None"
);
});
it("should return null if filteredFlatDefect array is empty", () => {
// @ts-ignore
const certGenSvc = new CertificateGenerationService(
null as any,
new LambdaService(new Lambda())
);

const filterFlatDefectsStub = sandbox
.stub(certGenSvc, "filterFlatDefects").returns(null);

// get mock of defect or test result
const testResultWithDefect = cloneDeep(mockTestResult);
console.log(testResultWithDefect.testTypes[0].defects[0]);
const format = certGenSvc.formatDefectWelsh(
testResultWithDefect.testTypes[0].defects[0],
"hgv",
[]
);
console.log(format);
expect(format).toBeNull();
filterFlatDefectsStub.restore();
});
});

context("test convertLocationWelsh method", () => {
Expand Down Expand Up @@ -655,6 +678,19 @@ describe("Certificate Generation Service", () => {
);
expect(filterFlatDefect).toEqual(flatDefect);
});
it("should return null if array is empty", () => {
// @ts-ignore
const certGenSvc = new CertificateGenerationService(
null as any,
new LambdaService(new Lambda())
);
const flatDefect = flatDefectsMock[0];
const filterFlatDefect = certGenSvc.filterFlatDefects(
[],
"hgv"
);
expect(filterFlatDefect).toBeNull();
});
});

context("test flattenDefectsFromApi method", () => {
Expand All @@ -665,10 +701,20 @@ describe("Certificate Generation Service", () => {
new LambdaService(new Lambda())
);
const flattenedArray = certGenSvc.flattenDefectsFromApi(defectsMock);
console.log(flattenedArray);
expect(flattenedArray).toEqual(flatDefectsMock);
expect(flattenedArray).toHaveLength(7);
});
// it("should catch any exceptions", () => {
// const certGenSvc = new CertificateGenerationService(
// null as any,
// new LambdaService(new Lambda())
// );
//
// const pushMock = jest.fn().mockRejectedValue(new Error);
// const arrayPushStub = Array.prototype.push = pushMock;
//
// const flattenedArray = certGenSvc.flattenDefectsFromApi(defectsMock);
// });
});
});

Expand Down Expand Up @@ -701,6 +747,7 @@ describe("Certificate Generation Service", () => {
expect(logSpy).toHaveBeenCalledWith(
"Test station details could not be found for 445567"
);
logSpy.mockClear();
});
it("should return a null and message if the list of test stations is empty", () => {
const logSpy = jest.spyOn(console, "log");
Expand All @@ -712,6 +759,7 @@ describe("Certificate Generation Service", () => {
const postCode = certGenSvc.getThisTestStation([], "P50742");
expect(postCode).toBeNull();
expect(logSpy).toHaveBeenCalledWith("Test stations data is empty");
logSpy.mockClear();
});
});

Expand All @@ -728,10 +776,31 @@ describe("Certificate Generation Service", () => {
expect(logSpy).toHaveBeenCalledWith(
"Welsh certificate generation deactivated via environment variable set to TRUE"
);
logSpy.mockClear();
});
});
});

context("test postcode lookup method", () => {
context("when the SECRET_KEY environment variable does not exist", () => {
it("should log the the errors", async () => {
Configuration.prototype.getWelshSecretKey = jest.fn().mockReturnValue(null);
const logSpy = jest.spyOn(console, "log");

const certGenSvc = new CertificateGenerationService(
null as any,
new LambdaService(new Lambda())
);

await certGenSvc.lookupPostcode("some_postcode");
expect(logSpy.mock.calls[0][0]).toBe("SECRET_KEY environment variable does not exist.");
expect(logSpy.mock.calls[1][0]).toBe("SMC Postcode lookup details not found. Return value for isWelsh for some_postcode is false");

logSpy.mockClear();
});
});
});

// TODO: url and api key need to be populated in lookupPostcode method for this test to pass
// TODO: need to mock secret values as tests fail with security token expired
// context("test postcode lookup method", () => {
Expand Down

0 comments on commit 9f6e2a4

Please sign in to comment.