-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2f75c13
feat(api): #3121 scdl integration script
461OceanBd 3944c13
feat(api): #3121 scdl integration script
461OceanBd 45ac075
feat(api): #3121 scdl integration script
461OceanBd 068ab91
feat(api): #3121 scdl integration script
461OceanBd 744fb66
feat(api): #3121 scdl integration script
461OceanBd 208be39
feat(api): #3121 scdl integration script
461OceanBd d54200c
feat(api): #3121 scdl integration script
461OceanBd 9581c11
feat(api): #3121 scdl integration script
461OceanBd 304b155
feat(api): #3121 scdl integration script
461OceanBd e22f88e
feat(api): #3121 scdl integration script
461OceanBd 2387084
feat(api): #3121 scdl integration script
461OceanBd ecd6dfe
feat(api): #3121 scdl integration script
461OceanBd 11101b0
feat(api): #3121 scdl integration script
461OceanBd 4ffaf5a
feat(api): #3121 scdl integration script
461OceanBd cf533e0
feat(api): #3121 scdl integration script
461OceanBd 33ab374
feat(api): #3121 scdl integration script
461OceanBd 6e9e94b
feat(api): #3121 scdl integration script
461OceanBd d9baa46
feat(api): #3121 scdl integration script
461OceanBd eee623b
feat(api): #3121 scdl integration script
461OceanBd 8b4adb8
feat(api): #3121 scdl integration script
461OceanBd 841bc3f
feat(api): #3121 scdl integration script
461OceanBd e1c2b95
feat(api): #3121 scdl integration script
461OceanBd ac85d10
Merge branch 'develop' into 3121-scdl-integration-script
alice-telescoop 9788a96
Merge branch 'develop' into 3121-scdl-integration-script
alice-telescoop 729b6e2
Merge branch 'develop' into 3121-scdl-integration-script
alice-telescoop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum FileExtensionEnum { | ||
CSV = "csv", | ||
XLS = "xls", | ||
XLSX = "xlsx", | ||
} |
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,15 @@ | ||
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; | ||
parseParams: ScdlParseArgs | ScdlParseXlsArgs; | ||
addProducer: boolean; | ||
producerName?: string; | ||
producerSiret?: string; | ||
} | ||
|
||
export interface ScdlFileProcessingConfigList { | ||
files: ScdlFileProcessingConfig[]; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const SCDL_FILE_PROCESSING_PATH = process.env.SCDL_FILE_PROCESSING_PATH || "./data-integration"; | ||
export const SCDL_FILE_PROCESSING_CONFIG_FILENAME = "scdl-file-processing.config.json"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
import path from "path"; | ||
import ScdlBatchCli from "./ScdlBatch.cli"; | ||
import fs from "fs"; | ||
|
||
import { | ||
SCDL_FILE_PROCESSING_PATH, | ||
SCDL_FILE_PROCESSING_CONFIG_FILENAME, | ||
} from "../../configurations/scdlIntegration.conf"; | ||
import ScdlCli from "../cli/Scdl.cli"; | ||
import { ScdlFileProcessingConfigList, ScdlParseArgs, ScdlParseXlsArgs } from "../../@types/ScdlDataIntegration"; | ||
|
||
jest.mock("../../configurations/scdlIntegration.conf", () => ({ | ||
SCDL_FILE_PROCESSING_PATH: path.resolve(__dirname, "test-integration"), | ||
SCDL_FILE_PROCESSING_CONFIG_FILENAME: "test-scdl-file-processing.config.json", | ||
})); | ||
jest.mock("../cli/Scdl.cli"); | ||
jest.mock("fs"); | ||
|
||
const validConfigData: ScdlFileProcessingConfigList = { | ||
files: [ | ||
{ | ||
name: "donnees-a-integrer1.csv", | ||
parseParams: { producerSlug: "producerSlug1", exportDate: "2025-01-13" }, | ||
addProducer: true, | ||
producerName: "Test Producer 1", | ||
producerSiret: "12345678901", | ||
}, | ||
{ | ||
name: "donnees-a-integrer2.csv", | ||
parseParams: { producerSlug: "producerSlug2", exportDate: "2025-01-14" }, | ||
addProducer: true, | ||
producerName: "Test Producer 2", | ||
producerSiret: "12345678902", | ||
}, | ||
{ | ||
name: "donnees-a-integrer3.csv", | ||
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]>; | ||
let parseMock: jest.SpyInstance< | ||
Promise<void>, | ||
[ | ||
file: string, | ||
producerSlug: string, | ||
exportDate: string, | ||
delimiter?: string | undefined, | ||
quote?: string | undefined, | ||
] | ||
>; | ||
let parseXlsMock: jest.SpyInstance< | ||
Promise<void>, | ||
[ | ||
file: string, | ||
producerSlug: string, | ||
exportDate: string, | ||
pageName?: string | undefined, | ||
rowOffset?: string | number | undefined, | ||
] | ||
>; | ||
|
||
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(); | ||
}); | ||
|
||
describe("loadConfig method", () => { | ||
it("should load config file successfully", () => { | ||
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(validConfigData)); | ||
const result = scdlBatchCli.loadConfig(); | ||
|
||
expect(result).toEqual(validConfigData); | ||
}); | ||
|
||
it("should throw an error when the JSON file is invalid", () => { | ||
jest.spyOn(fs, "readFileSync").mockImplementationOnce(() => { | ||
throw new Error("Unexpected token i in JSON at position 2"); | ||
}); | ||
|
||
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: { producerSlug: "producerSlug1", exportDate: "2025-01-13" } as ScdlParseArgs, | ||
addProducer: true, | ||
producerName: "Test Producer 1", | ||
producerSiret: "12345678901", | ||
}; | ||
|
||
const fileConfigWrongType = { | ||
name: "donnees-a-integrer1.doc", | ||
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(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined(); | ||
|
||
expect(addProducerMock).toHaveBeenCalledWith( | ||
fileConfig.parseParams.producerSlug, | ||
fileConfig.producerName, | ||
fileConfig.producerSiret, | ||
); | ||
}); | ||
|
||
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.producerSlug, | ||
fileConfig.parseParams.exportDate, | ||
undefined, | ||
undefined, | ||
); | ||
}); | ||
|
||
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(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(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined(); | ||
expect(parseMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should catch Unsupported file type error", async () => { | ||
await expect(scdlBatchCli.processFile(fileConfigWrongType)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it("should not call parse method when wrong file type", async () => { | ||
await expect(scdlBatchCli.processFile(fileConfigWrongType)).resolves.toBeUndefined(); | ||
expect(parseMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should not call parseXls method when wrong file type", async () => { | ||
await expect(scdlBatchCli.processFile(fileConfigWrongType)).resolves.toBeUndefined(); | ||
expect(parseXlsMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should catch Producer already exists error", async () => { | ||
addProducerMock = jest | ||
.spyOn(ScdlCli.prototype, "addProducer") | ||
.mockRejectedValue(new Error("Producer already exists")); | ||
|
||
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(scdlBatchCli.processFile(fileConfig)).resolves.toBeUndefined(); | ||
expect(parseMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe("main", () => { | ||
it("should call ScdlCli methods with correct arguments", async () => { | ||
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(validConfigData)); | ||
|
||
await expect(scdlBatchCli.main()).resolves.toBeUndefined(); | ||
|
||
expect(addProducerMock).toHaveBeenCalledTimes(2); | ||
expect(addProducerMock).toHaveBeenCalledWith("producerSlug1", "Test Producer 1", "12345678901"); | ||
expect(addProducerMock).toHaveBeenCalledWith("producerSlug2", "Test Producer 2", "12345678902"); | ||
|
||
expect(parseMock).toHaveBeenCalledTimes(3); | ||
expect(parseMock).toHaveBeenCalledWith( | ||
path.join(SCDL_FILE_PROCESSING_PATH, "donnees-a-integrer1.csv"), | ||
"producerSlug1", | ||
"2025-01-13", | ||
undefined, | ||
undefined, | ||
); | ||
expect(parseMock).toHaveBeenCalledWith( | ||
path.join(SCDL_FILE_PROCESSING_PATH, "donnees-a-integrer2.csv"), | ||
"producerSlug2", | ||
"2025-01-14", | ||
undefined, | ||
undefined, | ||
); | ||
expect(parseMock).toHaveBeenCalledWith( | ||
path.join(SCDL_FILE_PROCESSING_PATH, "donnees-a-integrer3.csv"), | ||
"producerSlug2", | ||
"2025-01-15", | ||
undefined, | ||
undefined, | ||
); | ||
expect(parseXlsMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should throw Unexpected token error", async () => { | ||
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"); | ||
}); | ||
|
||
it("should throw Invalid configuration file error", async () => { | ||
const invalidConfigData = { | ||
files: [ | ||
{ | ||
name: "donnees-a-integrer1.csv", | ||
addProducer: true, | ||
producerName: "Test Producer 1", | ||
producerSiret: "12345678901", | ||
}, | ||
], | ||
}; | ||
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(invalidConfigData)); | ||
fs.writeFileSync(testFilePath, JSON.stringify(invalidConfigData)); | ||
|
||
await expect(scdlBatchCli.main()).rejects.toThrow( | ||
"Invalid configuration file: The config does not match the expected structure.", | ||
); | ||
}); | ||
|
||
it("when error with one file, should process continue for other files", async () => { | ||
const configData = { | ||
files: [ | ||
{ | ||
name: "donnees-a-integrer1.xlsx", | ||
parseParams: { producerSlug: "producerSlug1", exportDate: "2025-01-13" } as ScdlParseXlsArgs, | ||
addProducer: true, | ||
producerName: "Test Producer 1", | ||
producerSiret: "12345678901", | ||
}, | ||
{ | ||
name: "donnees-a-integrer2.csv", | ||
parseParams: { producerSlug: "producerSlug2", exportDate: "2025-01-14" } as ScdlParseArgs, | ||
addProducer: true, | ||
producerName: "Test Producer 2", | ||
producerSiret: "12345678902", | ||
}, | ||
{ | ||
name: "donnees-a-integrer3.csv", | ||
parseParams: { producerSlug: "producerSlug3", exportDate: "2025-01-15" } as ScdlParseArgs, | ||
addProducer: true, | ||
producerName: "Test Producer 3", | ||
producerSiret: "12345678903", | ||
}, | ||
], | ||
}; | ||
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify(configData)); | ||
parseXlsMock = jest | ||
.spyOn(ScdlCli.prototype, "parseXls") | ||
.mockRejectedValue(new Error("Mocked addProducer error")); | ||
|
||
await expect(scdlBatchCli.main()).resolves.toBeUndefined(); | ||
expect(addProducerMock).toHaveBeenCalledTimes(3); | ||
expect(parseMock).toHaveBeenCalledTimes(2); | ||
expect(parseXlsMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ils étaient pas bien les noms de variables ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C'était pour répondre à la remarque de Maxime sur le fait que c'est bizarre que la variable rowOffsetStr s'appelle comme ça alors qu'on peut aussi mettre un number