From cb69e6511642c70d98a16517c7a5d9e5461824e1 Mon Sep 17 00:00:00 2001 From: David Smadja <david.smadja@assemblee-nationale.fr> Date: Thu, 13 Mar 2025 16:56:13 +0100 Subject: [PATCH] update generate yaml script --- src/scripts/generate_openfisca_tests_yaml.ts | 50 ++++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/scripts/generate_openfisca_tests_yaml.ts b/src/scripts/generate_openfisca_tests_yaml.ts index 6adf7e5d9..afa393f60 100644 --- a/src/scripts/generate_openfisca_tests_yaml.ts +++ b/src/scripts/generate_openfisca_tests_yaml.ts @@ -34,6 +34,12 @@ const optionsDefinitions = [ name: "year", type: Number, }, + { + alias: "t", + help: "Single Test-Case to run", + name: "testcase", + type: String, + }, { defaultOption: true, help: "Directory to write OpenFisca test YAML files", @@ -202,14 +208,22 @@ function cleanSimulatedJson(jsonData: any): any { } function splitVariablesByCategory( + section: string, + entityKey: string, entityData: JsonObject, - outputVariablesList: string[], + initialTestCase: Situation, ) { const input: JsonObject = {} const output: JsonObject = {} for (const [variable, value] of Object.entries(entityData)) { - if (outputVariablesList.includes(variable)) { + const replacedKeys: { [key: string]: string } = {} + + initialTestCase = removeSpacesFromKeys(initialTestCase, replacedKeys) + if ( + initialTestCase[section][entityKey] !== undefined && + initialTestCase[section][entityKey][variable] === undefined + ) { if ( variableSummaryByName[variable].value_type === "Enum" && variableSummaryByName[variable].possible_values !== undefined @@ -233,16 +247,19 @@ function splitVariablesByCategory( } function processSection( + section: string, sectionData: JsonObject, - outputVariablesList: string[], + initialTestCase: Situation, ) { const inputSection: JsonObject = {} const outputSection: JsonObject = {} for (const [entityKey, entityData] of Object.entries(sectionData)) { const { input, output } = splitVariablesByCategory( + section, + entityKey, entityData, - outputVariablesList, + initialTestCase, ) if (Object.keys(input).length > 0) { @@ -260,7 +277,7 @@ function buildYamlOutput( name: string, year: string, simulatedTestCase: JsonObject, - outputVariablesList: string[], + testCase: Situation, ) { const sections = ["familles", "foyers_fiscaux", "menages", "individus"] const jsonForYamlOutput: { @@ -280,8 +297,9 @@ function buildYamlOutput( for (const section of sections) { if (simulatedTestCase[section]) { const { input, output } = processSection( + section, simulatedTestCase[section], - outputVariablesList, + testCase, ) if (Object.keys(input).length > 0) { @@ -367,12 +385,14 @@ function isKeyOrPlural(str: string): boolean { async function main() { console.info("Initializing...") + const personsEntityKey = Object.entries(entityByKey) .filter(([, entity]) => entity.is_person) .map(([key]) => key)[0] const year = options.year const outdir = options.outdir + const singleTestCase = options.testcase const variablesFromDecompositions = [ ...summaryCalculatedVariablesName, @@ -402,12 +422,19 @@ async function main() { ) const testCasesCore = testCasesCoreUnknown as unknown as Situation[] + const testCaseCount = testCasesCore.length + + let processedCounter = 0 for (const testCase of testCasesCore) { + if (singleTestCase !== undefined && testCase.id !== singleTestCase) { + continue + } + console.info("Processing ", testCase.id) + processedCounter++ let testCaseContainsMissingVariable = false let jsonForApiCall: JsonObject = {} - const outputVariablesList: Array<string> = [] jsonForApiCall.input = {} @@ -463,7 +490,6 @@ async function main() { getEntityKeyFromKeyPlural(section, entityByKey) && jsonForApiCall.input[section][entityKey][variable] === undefined ) { - outputVariablesList.push(variable) switch (variableSummaryByName[variable].definition_period) { case "year": { jsonForApiCall.input[section][entityKey][variable] = { @@ -497,8 +523,13 @@ async function main() { } jsonForApiCall = replaceValuesFromReplacedKeys(jsonForApiCall, replacedKeys) + // await fs.writeFile( + // path.join(".", testCase.id + ".json"), + // JSON.stringify(jsonForApiCall.input, null, 2), + // ) console.info("Launching simulation on OpenFisca France API...") const openFiscaApiUrl = "https://api.fr.openfisca.org/latest/calculate" + // const openFiscaApiUrl = "http://localhost:5000/calculate" const openFiscaApiOptions: RequestInit = { body: JSON.stringify(jsonForApiCall.input), headers: { @@ -533,7 +564,7 @@ async function main() { testCase.id!, year, simulatedTestCase, - outputVariablesList, + testCase, ) const yamlOutput = YAML.dump(jsonForYamlOutput, { @@ -545,6 +576,7 @@ async function main() { const cleanedYaml = yamlOutput.replace(/'(\d{4})':/g, "$1:") await fs.writeFile(path.join(outdir, testCase.id + ".yml"), cleanedYaml) console.info("YAML file written :", outdir, testCase.id + ".yml") + console.info(processedCounter, "files processed over", testCaseCount) } } -- GitLab