Skip to content

Commit

Permalink
feat(api): #3121 scdl integration script
Browse files Browse the repository at this point in the history
  • Loading branch information
461OceanBd committed Jan 30, 2025
1 parent 841bc3f commit e1c2b95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
23 changes: 16 additions & 7 deletions packages/api/src/interfaces/cli/ScdlBatch.cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe("scdl data integration script", () => {
describe("loadConfig method", () => {
it("should load config file successfully", () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(validConfigData));
// @ts-expect-error: protected
const result = scdlBatchCli.loadConfig();

expect(result).toEqual(validConfigData);
Expand All @@ -89,7 +90,7 @@ describe("scdl data integration script", () => {
jest.spyOn(fs, "readFileSync").mockImplementationOnce(() => {
throw new Error("Unexpected token i in JSON at position 2");
});

// @ts-expect-error: protected
expect(() => scdlBatchCli.loadConfig()).toThrowError(new Error("Unexpected token i in JSON at position 2"));
});
});
Expand All @@ -112,6 +113,7 @@ describe("scdl data integration script", () => {
};

it("should process file methods correctly with addProducer called with correct params", async () => {
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();

expect(addProducerMock).toHaveBeenCalledWith(
Expand All @@ -122,6 +124,7 @@ describe("scdl data integration script", () => {
});

it("should process file methods correctly with parse method called with correct params", async () => {
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();

expect(parseMock).toHaveBeenCalledWith(
Expand All @@ -134,6 +137,7 @@ describe("scdl data integration script", () => {
});

it("should process file methods correctly with parseXls method not to have been called", async () => {
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
expect(parseXlsMock).not.toHaveBeenCalled();
});
Expand All @@ -142,27 +146,32 @@ describe("scdl data integration script", () => {
addProducerMock = jest
.spyOn(ScdlCli.prototype, "addProducer")
.mockRejectedValue(new Error("Mocked addProducer error"));
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
});

it("should not call parse method when addProducer error", async () => {
addProducerMock = jest
.spyOn(ScdlCli.prototype, "addProducer")
.mockRejectedValue(new Error("Mocked addProducer error"));
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
expect(parseMock).not.toHaveBeenCalled();
});

it("should catch Unsupported file type error", async () => {
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfigWrongType)).resolves.toBeUndefined();
});

it("should not call parse method when wrong file type", async () => {
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfigWrongType)).resolves.toBeUndefined();
expect(parseMock).not.toHaveBeenCalled();
});

it("should not call parseXls method when wrong file type", async () => {
// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfigWrongType)).resolves.toBeUndefined();
expect(parseXlsMock).not.toHaveBeenCalled();
});
Expand All @@ -171,15 +180,15 @@ describe("scdl data integration script", () => {
addProducerMock = jest
.spyOn(ScdlCli.prototype, "addProducer")
.mockRejectedValue(new Error("Producer already exists"));

// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
});

it("should call parse method when producer already exists", async () => {
addProducerMock = jest
.spyOn(ScdlCli.prototype, "addProducer")
.mockRejectedValue(new Error("Producer already exists"));

// @ts-expect-error: protected
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
expect(parseMock).toHaveBeenCalledTimes(1);
});
Expand All @@ -189,7 +198,7 @@ describe("scdl data integration script", () => {
it("should call ScdlCli methods with correct arguments", async () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(validConfigData));

await expect(scdlBatchCli.main()).resolves.toBeUndefined();
await expect(scdlBatchCli.import()).resolves.toBeUndefined();

expect(addProducerMock).toHaveBeenCalledTimes(2);
expect(addProducerMock).toHaveBeenCalledWith("producerSlug1", "Test Producer 1", "12345678901");
Expand Down Expand Up @@ -224,7 +233,7 @@ describe("scdl data integration script", () => {
jest.spyOn(fs, "readFileSync").mockImplementationOnce(() => {
throw new Error("Unexpected token i in JSON at position 2");
});
await expect(scdlBatchCli.main()).rejects.toThrow("Unexpected token i in JSON at position 2");
await expect(scdlBatchCli.import()).rejects.toThrow("Unexpected token i in JSON at position 2");
});

it("should throw Invalid configuration file error", async () => {
Expand All @@ -241,7 +250,7 @@ describe("scdl data integration script", () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(invalidConfigData));
fs.writeFileSync(testFilePath, JSON.stringify(invalidConfigData));

await expect(scdlBatchCli.main()).rejects.toThrow(
await expect(scdlBatchCli.import()).rejects.toThrow(
"Invalid configuration file: The config does not match the expected structure.",
);
});
Expand Down Expand Up @@ -277,7 +286,7 @@ describe("scdl data integration script", () => {
.spyOn(ScdlCli.prototype, "parseXls")
.mockRejectedValue(new Error("Mocked addProducer error"));

await expect(scdlBatchCli.main()).resolves.toBeUndefined();
await expect(scdlBatchCli.import()).resolves.toBeUndefined();
expect(addProducerMock).toHaveBeenCalledTimes(3);
expect(parseMock).toHaveBeenCalledTimes(2);
expect(parseXlsMock).toHaveBeenCalledTimes(1);
Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/interfaces/cli/ScdlBatch.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export default class ScdlBatchCli {
);
}

public loadConfig(): ScdlFileProcessingConfigList {
private loadConfig(): ScdlFileProcessingConfigList {
const filePath = path.resolve(SCDL_FILE_PROCESSING_PATH, SCDL_FILE_PROCESSING_CONFIG_FILENAME);
const data = fs.readFileSync(filePath, "utf8");
return JSON.parse(data) as ScdlFileProcessingConfigList;
}

public async processFile(fileInfo: ScdlFileProcessingConfig): Promise<void> {
protected async processFile(fileInfo: ScdlFileProcessingConfig): Promise<void> {
const { name, parseParams, addProducer, producerName, producerSiret } = fileInfo;
const dirPath = path.resolve(SCDL_FILE_PROCESSING_PATH);
const { producerSlug, exportDate, ...optionalParams } = parseParams;
Expand Down Expand Up @@ -92,7 +92,7 @@ export default class ScdlBatchCli {
/**
* main function to load the file listing .json and launch data integration
*/
public async main() {
public async import() {
try {
const config: ScdlFileProcessingConfigList = this.loadConfig();

Expand Down Expand Up @@ -121,7 +121,6 @@ export default class ScdlBatchCli {
console.log("⚠️ List of Errors :");
this.errorList.forEach(desc => console.log(`❌ ${desc}`));
}
process.exit(0);
}
}
}

0 comments on commit e1c2b95

Please sign in to comment.