Skip to content
Snippets Groups Projects
Commit cb69e651 authored by David Smadja's avatar David Smadja
Browse files

update generate yaml script

parent 7f75acc7
No related branches found
No related tags found
No related merge requests found
...@@ -34,6 +34,12 @@ const optionsDefinitions = [ ...@@ -34,6 +34,12 @@ const optionsDefinitions = [
name: "year", name: "year",
type: Number, type: Number,
}, },
{
alias: "t",
help: "Single Test-Case to run",
name: "testcase",
type: String,
},
{ {
defaultOption: true, defaultOption: true,
help: "Directory to write OpenFisca test YAML files", help: "Directory to write OpenFisca test YAML files",
...@@ -202,14 +208,22 @@ function cleanSimulatedJson(jsonData: any): any { ...@@ -202,14 +208,22 @@ function cleanSimulatedJson(jsonData: any): any {
} }
function splitVariablesByCategory( function splitVariablesByCategory(
section: string,
entityKey: string,
entityData: JsonObject, entityData: JsonObject,
outputVariablesList: string[], initialTestCase: Situation,
) { ) {
const input: JsonObject = {} const input: JsonObject = {}
const output: JsonObject = {} const output: JsonObject = {}
for (const [variable, value] of Object.entries(entityData)) { 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 ( if (
variableSummaryByName[variable].value_type === "Enum" && variableSummaryByName[variable].value_type === "Enum" &&
variableSummaryByName[variable].possible_values !== undefined variableSummaryByName[variable].possible_values !== undefined
...@@ -233,16 +247,19 @@ function splitVariablesByCategory( ...@@ -233,16 +247,19 @@ function splitVariablesByCategory(
} }
function processSection( function processSection(
section: string,
sectionData: JsonObject, sectionData: JsonObject,
outputVariablesList: string[], initialTestCase: Situation,
) { ) {
const inputSection: JsonObject = {} const inputSection: JsonObject = {}
const outputSection: JsonObject = {} const outputSection: JsonObject = {}
for (const [entityKey, entityData] of Object.entries(sectionData)) { for (const [entityKey, entityData] of Object.entries(sectionData)) {
const { input, output } = splitVariablesByCategory( const { input, output } = splitVariablesByCategory(
section,
entityKey,
entityData, entityData,
outputVariablesList, initialTestCase,
) )
if (Object.keys(input).length > 0) { if (Object.keys(input).length > 0) {
...@@ -260,7 +277,7 @@ function buildYamlOutput( ...@@ -260,7 +277,7 @@ function buildYamlOutput(
name: string, name: string,
year: string, year: string,
simulatedTestCase: JsonObject, simulatedTestCase: JsonObject,
outputVariablesList: string[], testCase: Situation,
) { ) {
const sections = ["familles", "foyers_fiscaux", "menages", "individus"] const sections = ["familles", "foyers_fiscaux", "menages", "individus"]
const jsonForYamlOutput: { const jsonForYamlOutput: {
...@@ -280,8 +297,9 @@ function buildYamlOutput( ...@@ -280,8 +297,9 @@ function buildYamlOutput(
for (const section of sections) { for (const section of sections) {
if (simulatedTestCase[section]) { if (simulatedTestCase[section]) {
const { input, output } = processSection( const { input, output } = processSection(
section,
simulatedTestCase[section], simulatedTestCase[section],
outputVariablesList, testCase,
) )
if (Object.keys(input).length > 0) { if (Object.keys(input).length > 0) {
...@@ -367,12 +385,14 @@ function isKeyOrPlural(str: string): boolean { ...@@ -367,12 +385,14 @@ function isKeyOrPlural(str: string): boolean {
async function main() { async function main() {
console.info("Initializing...") console.info("Initializing...")
const personsEntityKey = Object.entries(entityByKey) const personsEntityKey = Object.entries(entityByKey)
.filter(([, entity]) => entity.is_person) .filter(([, entity]) => entity.is_person)
.map(([key]) => key)[0] .map(([key]) => key)[0]
const year = options.year const year = options.year
const outdir = options.outdir const outdir = options.outdir
const singleTestCase = options.testcase
const variablesFromDecompositions = [ const variablesFromDecompositions = [
...summaryCalculatedVariablesName, ...summaryCalculatedVariablesName,
...@@ -402,12 +422,19 @@ async function main() { ...@@ -402,12 +422,19 @@ async function main() {
) )
const testCasesCore = testCasesCoreUnknown as unknown as Situation[] const testCasesCore = testCasesCoreUnknown as unknown as Situation[]
const testCaseCount = testCasesCore.length
let processedCounter = 0
for (const testCase of testCasesCore) { for (const testCase of testCasesCore) {
if (singleTestCase !== undefined && testCase.id !== singleTestCase) {
continue
}
console.info("Processing ", testCase.id) console.info("Processing ", testCase.id)
processedCounter++
let testCaseContainsMissingVariable = false let testCaseContainsMissingVariable = false
let jsonForApiCall: JsonObject = {} let jsonForApiCall: JsonObject = {}
const outputVariablesList: Array<string> = []
jsonForApiCall.input = {} jsonForApiCall.input = {}
...@@ -463,7 +490,6 @@ async function main() { ...@@ -463,7 +490,6 @@ async function main() {
getEntityKeyFromKeyPlural(section, entityByKey) && getEntityKeyFromKeyPlural(section, entityByKey) &&
jsonForApiCall.input[section][entityKey][variable] === undefined jsonForApiCall.input[section][entityKey][variable] === undefined
) { ) {
outputVariablesList.push(variable)
switch (variableSummaryByName[variable].definition_period) { switch (variableSummaryByName[variable].definition_period) {
case "year": { case "year": {
jsonForApiCall.input[section][entityKey][variable] = { jsonForApiCall.input[section][entityKey][variable] = {
...@@ -497,8 +523,13 @@ async function main() { ...@@ -497,8 +523,13 @@ async function main() {
} }
jsonForApiCall = replaceValuesFromReplacedKeys(jsonForApiCall, replacedKeys) 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...") console.info("Launching simulation on OpenFisca France API...")
const openFiscaApiUrl = "https://api.fr.openfisca.org/latest/calculate" const openFiscaApiUrl = "https://api.fr.openfisca.org/latest/calculate"
// const openFiscaApiUrl = "http://localhost:5000/calculate"
const openFiscaApiOptions: RequestInit = { const openFiscaApiOptions: RequestInit = {
body: JSON.stringify(jsonForApiCall.input), body: JSON.stringify(jsonForApiCall.input),
headers: { headers: {
...@@ -533,7 +564,7 @@ async function main() { ...@@ -533,7 +564,7 @@ async function main() {
testCase.id!, testCase.id!,
year, year,
simulatedTestCase, simulatedTestCase,
outputVariablesList, testCase,
) )
const yamlOutput = YAML.dump(jsonForYamlOutput, { const yamlOutput = YAML.dump(jsonForYamlOutput, {
...@@ -545,6 +576,7 @@ async function main() { ...@@ -545,6 +576,7 @@ async function main() {
const cleanedYaml = yamlOutput.replace(/'(\d{4})':/g, "$1:") const cleanedYaml = yamlOutput.replace(/'(\d{4})':/g, "$1:")
await fs.writeFile(path.join(outdir, testCase.id + ".yml"), cleanedYaml) await fs.writeFile(path.join(outdir, testCase.id + ".yml"), cleanedYaml)
console.info("YAML file written :", outdir, testCase.id + ".yml") console.info("YAML file written :", outdir, testCase.id + ".yml")
console.info(processedCounter, "files processed over", testCaseCount)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment