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

feat(api): #3121 scdl integration script #3124

Merged
merged 25 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2f75c13
feat(api): #3121 scdl integration script
461OceanBd Jan 15, 2025
3944c13
feat(api): #3121 scdl integration script
461OceanBd Jan 16, 2025
45ac075
feat(api): #3121 scdl integration script
461OceanBd Jan 16, 2025
068ab91
feat(api): #3121 scdl integration script
461OceanBd Jan 16, 2025
744fb66
feat(api): #3121 scdl integration script
461OceanBd Jan 16, 2025
208be39
feat(api): #3121 scdl integration script
461OceanBd Jan 16, 2025
d54200c
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
9581c11
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
304b155
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
e22f88e
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
2387084
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
ecd6dfe
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
11101b0
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
4ffaf5a
feat(api): #3121 scdl integration script
461OceanBd Jan 28, 2025
cf533e0
feat(api): #3121 scdl integration script
461OceanBd Jan 29, 2025
33ab374
feat(api): #3121 scdl integration script
461OceanBd Jan 29, 2025
6e9e94b
feat(api): #3121 scdl integration script
461OceanBd Jan 29, 2025
d9baa46
feat(api): #3121 scdl integration script
461OceanBd Jan 29, 2025
eee623b
feat(api): #3121 scdl integration script
461OceanBd Jan 29, 2025
8b4adb8
feat(api): #3121 scdl integration script
461OceanBd Jan 29, 2025
841bc3f
feat(api): #3121 scdl integration script
461OceanBd Jan 29, 2025
e1c2b95
feat(api): #3121 scdl integration script
461OceanBd Jan 30, 2025
ac85d10
Merge branch 'develop' into 3121-scdl-integration-script
alice-telescoop Feb 3, 2025
9788a96
Merge branch 'develop' into 3121-scdl-integration-script
alice-telescoop Feb 3, 2025
729b6e2
Merge branch 'develop' into 3121-scdl-integration-script
alice-telescoop Feb 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"lint:all": "eslint --fix --ext .js,.ts ./{migrations,src,tests,tools}/**/*.{js,ts} ./{migrations,src,tests,tools}/*.{js,ts}",
"prettier": "prettier --write",
"prettier:all": "prettier --write ./{migrations,src,tests,tools}/**/*.{js,ts}",
"lint-format": "lint-staged",
"scdl:import": "ts-node src/interfaces/scripts/ScdlDataIntegration.node.ts"
"lint-format": "lint-staged"
},
"lint-staged": {
"./{migrations,src,tests,tools}/**/*.{js,ts}": [
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/@types/ScdlDataIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type BaseScdlParseArgs = [producerSlug: string, exportDate: string];
export type ScdlParseArgs = BaseScdlParseArgs & [delimiter?: string, quote?: string];
export type ScdlParseXlsArgs = BaseScdlParseArgs & [pageName?: string, rowOffset?: number | string];
type BaseScdlParseArgs = { producerSlug: string; exportDate: string };
export type ScdlParseArgs = BaseScdlParseArgs & { delimiter?: string; quote?: string };
export type ScdlParseXlsArgs = BaseScdlParseArgs & { pageName?: string; rowOffset?: number | string };

export interface ScdlFileProcessingConfig {
name: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { initIndexes } from "./shared/MongoInit";
import GeoCli from "./interfaces/cli/Geo.cli";
import DataBretagneCli from "./interfaces/cli/DataBretagne.cli";
import PaymentFlatCli from "./interfaces/cli/PaymentFlat.cli";
import ScdlBatchCli from "./interfaces/cli/ScdlBatch.cli";
async function main() {
await connectDB();
await initIndexes();
Expand Down Expand Up @@ -52,6 +53,7 @@ async function main() {
GeoCli,
DataBretagneCli,
PaymentFlatCli,
ScdlBatchCli,
];

const args = process.argv.slice(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import path from "path";
import { main, loadConfig, processFile } from "./ScdlDataIntegration.node";
import ScdlBatchCli from "./ScdlBatch.cli";
import fs from "fs";

import {
SCDL_FILE_PROCESSING_PATH,
SCDL_FILE_PROCESSING_CONFIG_FILENAME,
Expand All @@ -19,27 +20,28 @@ const validConfigData: ScdlFileProcessingConfigList = {
files: [
{
name: "donnees-a-integrer1.csv",
parseParams: ["producerSlug1", "2025-01-13"],
parseParams: { producerSlug: "producerSlug1", exportDate: "2025-01-13" },
addProducer: true,
producerName: "Test Producer 1",
producerSiret: "12345678901",
},
{
name: "donnees-a-integrer2.csv",
parseParams: ["producerSlug2", "2025-01-14"],
parseParams: { producerSlug: "producerSlug2", exportDate: "2025-01-14" },
addProducer: true,
producerName: "Test Producer 2",
producerSiret: "12345678902",
},
{
name: "donnees-a-integrer3.csv",
parseParams: ["producerSlug2", "2025-01-15"],
parseParams: { producerSlug: "producerSlug2", exportDate: "2025-01-15" },
addProducer: false,
},
],
};

describe("scdl data integration script", () => {
let scdlBatchCli: ScdlBatchCli;
const testFilePath = path.join(SCDL_FILE_PROCESSING_PATH, SCDL_FILE_PROCESSING_CONFIG_FILENAME);

let addProducerMock: jest.SpyInstance<Promise<void>, [slug: string, name: string, siret: string]>;
Expand All @@ -64,21 +66,21 @@ describe("scdl data integration script", () => {
]
>;

beforeAll(() => {
scdlBatchCli = new ScdlBatchCli();
});

beforeEach(() => {
jest.spyOn(process, "exit").mockImplementation((() => {}) as (code?: any) => never);
addProducerMock = jest.spyOn(ScdlCli.prototype, "addProducer").mockResolvedValue();
parseMock = jest.spyOn(ScdlCli.prototype, "parse").mockResolvedValue();
parseXlsMock = jest.spyOn(ScdlCli.prototype, "parseXls").mockResolvedValue();
});

afterEach(() => {
jest.restoreAllMocks();
});

describe("loadConfig method", () => {
it("should load config file successfully", () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(validConfigData));
const result = loadConfig();
const result = scdlBatchCli.loadConfig();

expect(result).toEqual(validConfigData);
});
Expand All @@ -88,87 +90,80 @@ describe("scdl data integration script", () => {
throw new Error("Unexpected token i in JSON at position 2");
});

expect(() => loadConfig()).toThrowError(new Error("Unexpected token i in JSON at position 2"));
});

it("should throw an error when the JSON file does not exist", () => {
jest.spyOn(fs, "readFileSync").mockImplementationOnce(() => {
throw new Error("ENOENT: no such file or directory");
});
expect(() => loadConfig()).toThrowError("ENOENT: no such file or directory");
expect(() => scdlBatchCli.loadConfig()).toThrowError(new Error("Unexpected token i in JSON at position 2"));
});
});

describe("Test for processFile method", () => {
const fileConfig = {
name: "donnees-a-integrer1.csv",
parseParams: ["producerSlug1", "2025-01-13"] as ScdlParseArgs,
parseParams: { producerSlug: "producerSlug1", exportDate: "2025-01-13" } as ScdlParseArgs,
addProducer: true,
producerName: "Test Producer 1",
producerSiret: "12345678901",
};

const fileConfigWrongType = {
name: "donnees-a-integrer1.doc",
parseParams: ["producerSlug1", "2025-01-13"] as ScdlParseXlsArgs,
parseParams: { producerSlug: "producerSlug1", exportDate: "2025-01-13" } as ScdlParseXlsArgs,
addProducer: true,
producerName: "Test Producer 1",
producerSiret: "12345678901",
};

it("should process file methods correctly with addProducer Called with correct params", async () => {
await expect(processFile(fileConfig)).resolves.toBeUndefined();
it("should process file methods correctly with addProducer called with correct params", async () => {
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();

expect(addProducerMock).toHaveBeenCalledWith(
fileConfig.parseParams[0],
fileConfig.parseParams.producerSlug,
fileConfig.producerName,
fileConfig.producerSiret,
);
});

it("should process file methods correctly with parse method Called with correct params", async () => {
await expect(processFile(fileConfig)).resolves.toBeUndefined();
it("should process file methods correctly with parse method called with correct params", async () => {
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();

expect(parseMock).toHaveBeenCalledWith(
expect.stringContaining(fileConfig.name),
fileConfig.parseParams[0],
fileConfig.parseParams[1],
fileConfig.parseParams.producerSlug,
fileConfig.parseParams.exportDate,
undefined,
undefined,
);
});

it("should process file methods correctly with parseXls method not to have been Called", async () => {
await expect(processFile(fileConfig)).resolves.toBeUndefined();
it("should process file methods correctly with parseXls method not to have been called", async () => {
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
expect(parseXlsMock).not.toHaveBeenCalled();
});

it("should catch error", async () => {
addProducerMock = jest
.spyOn(ScdlCli.prototype, "addProducer")
.mockRejectedValue(new Error("Mocked addProducer error"));
await expect(processFile(fileConfig)).resolves.toBeUndefined();
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"));
await expect(processFile(fileConfig)).resolves.toBeUndefined();
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
expect(parseMock).not.toHaveBeenCalled();
});

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

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

it("should not call parseXls method when wrong file type", async () => {
await expect(processFile(fileConfigWrongType)).resolves.toBeUndefined();
await expect(scdlBatchCli.processFile(fileConfigWrongType)).resolves.toBeUndefined();
expect(parseXlsMock).not.toHaveBeenCalled();
});

Expand All @@ -177,24 +172,24 @@ describe("scdl data integration script", () => {
.spyOn(ScdlCli.prototype, "addProducer")
.mockRejectedValue(new Error("Producer already exists"));

await expect(processFile(fileConfig)).resolves.toBeUndefined();
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"));

await expect(processFile(fileConfig)).resolves.toBeUndefined();
await expect(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined();
expect(parseMock).toHaveBeenCalledTimes(1);
});
});

describe("Test for main script", () => {
describe("main", () => {
it("should call ScdlCli methods with correct arguments", async () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(validConfigData));

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

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

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

await expect(main()).rejects.toThrow(
await expect(scdlBatchCli.main()).rejects.toThrow(
"Invalid configuration file: The config does not match the expected structure.",
);
});
Expand All @@ -256,21 +251,21 @@ describe("scdl data integration script", () => {
files: [
{
name: "donnees-a-integrer1.xlsx",
parseParams: ["producerSlug1", "2025-01-13"] as ScdlParseXlsArgs,
parseParams: { producerSlug: "producerSlug1", exportDate: "2025-01-13" } as ScdlParseXlsArgs,
addProducer: true,
producerName: "Test Producer 1",
producerSiret: "12345678901",
},
{
name: "donnees-a-integrer2.csv",
parseParams: ["producerSlug2", "2025-01-14"] as ScdlParseArgs,
parseParams: { producerSlug: "producerSlug2", exportDate: "2025-01-14" } as ScdlParseArgs,
addProducer: true,
producerName: "Test Producer 2",
producerSiret: "12345678902",
},
{
name: "donnees-a-integrer3.csv",
parseParams: ["producerSlug3", "2025-01-15"] as ScdlParseArgs,
parseParams: { producerSlug: "producerSlug3", exportDate: "2025-01-15" } as ScdlParseArgs,
addProducer: true,
producerName: "Test Producer 3",
producerSiret: "12345678903",
Expand All @@ -282,7 +277,7 @@ describe("scdl data integration script", () => {
.spyOn(ScdlCli.prototype, "parseXls")
.mockRejectedValue(new Error("Mocked addProducer error"));

await expect(main()).resolves.toBeUndefined();
await expect(scdlBatchCli.main()).resolves.toBeUndefined();
expect(addProducerMock).toHaveBeenCalledTimes(3);
expect(parseMock).toHaveBeenCalledTimes(2);
expect(parseXlsMock).toHaveBeenCalledTimes(1);
Expand Down
Loading
Loading