From 76ddaaf8fba149d41526c97627f4189c52c018d8 Mon Sep 17 00:00:00 2001 From: Mael Date: Thu, 11 Apr 2019 18:23:05 +0200 Subject: [PATCH] :bug: :white_check_mark: Ajout d'un test qui casse Est-ce le tryptique inversion + double filtre ?? --- source/engine/traverse.js | 42 +++++++++++++++----- source/engine/treatVariable.js | 2 - test/bug-cotisations.test.js | 71 ++++++++++++++++++++++++++++++---- 3 files changed, 96 insertions(+), 19 deletions(-) diff --git a/source/engine/traverse.js b/source/engine/traverse.js index 614d10420..f5cf1243e 100644 --- a/source/engine/traverse.js +++ b/source/engine/traverse.js @@ -1,13 +1,35 @@ -import { ShowValuesConsumer } from 'Components/rule/ShowValuesContext'; -import { evaluateControls } from 'Engine/controls'; -import { chain, cond, evolve, is, keys, map, mergeAll, path, pick, pipe, T } from 'ramda'; -import React from 'react'; -import { bonus, evaluateNode, makeJsx, mergeMissing, rewriteNode } from './evaluation'; -import { Node } from './mecanismViews/common'; -import { disambiguateRuleReference, findParentDependency, findRule, findRuleByDottedName } from './rules'; -import { anyNull, undefOrTrue, val } from './traverse-common-functions'; -import { treatNumber, treatObject, treatOther, treatString } from './treat'; - +import { ShowValuesConsumer } from 'Components/rule/ShowValuesContext' +import { evaluateControls } from 'Engine/controls' +import { + chain, + cond, + evolve, + is, + keys, + map, + mergeAll, + path, + pick, + pipe, + T +} from 'ramda' +import React from 'react' +import { + bonus, + evaluateNode, + makeJsx, + mergeMissing, + rewriteNode +} from './evaluation' +import { Node } from './mecanismViews/common' +import { + disambiguateRuleReference, + findParentDependency, + findRule, + findRuleByDottedName +} from './rules' +import { anyNull, undefOrTrue, val } from './traverse-common-functions' +import { treatNumber, treatObject, treatOther, treatString } from './treat' /* Dans ce fichier, les règles YAML sont parsées. diff --git a/source/engine/treatVariable.js b/source/engine/treatVariable.js index 8d51b1558..e7d3d9bec 100644 --- a/source/engine/treatVariable.js +++ b/source/engine/treatVariable.js @@ -108,9 +108,7 @@ export let treatVariable = (rules, rule, filter) => parseResult => { // TODO - the implementations of filters is really bad. It injects a hack in the situation to make the composante mecanism compute only one of its branch. It is then stored in the cache under a new key, dottedName.filter. This mecanism should just query the variable tree to get the active composante's value... // -window.count = 0 export let treatVariableTransforms = (rules, rule) => parseResult => { - window.count += 1 let evaluateTransforms = originalEval => ( cache, situation, diff --git a/test/bug-cotisations.test.js b/test/bug-cotisations.test.js index 3c6d13275..6e01412ea 100644 --- a/test/bug-cotisations.test.js +++ b/test/bug-cotisations.test.js @@ -2,12 +2,66 @@ import { expect } from 'chai' import { enrichRule } from 'Engine/rules' import { rules as realRules } from '../source/engine/rules' import { analyse, analyseMany, parseAll } from '../source/engine/traverse' +import dedent from 'dedent-js' +import { safeLoad } from 'js-yaml' describe('bug-analyse-many', function() { - it.only('should compute the same contributions if asked with analyseMany or analyse', function() { + it('complex inversion with composantes', () => { + let rawRules = dedent` + - nom: net + formule: brut - cotisations + - nom: cotisations + formule: + somme: + - cotisation a (salarié) + - cotisation b + + - nom: cotisation a + formule: + multiplication: + assiette: brut + composantes: + - attributs: + dû par: employeur + taux: 10% + - attributs: + dû par: salarié + taux: 10% + + - nom: cotisation b + formule: + multiplication: + assiette: brut + composantes: + - attributs: + impôt sur le revenu: x + taux: 10% + - attributs: + impôt sur le revenu: y + taux: 10% + + + - nom: brut + format: euro + formule: + inversion numérique: + avec: + - net + `, + rules = parseAll(safeLoad(rawRules).map(enrichRule)), + stateSelector = name => ({ net: 700 }[name]) + const targets = ['brut', 'cotisations'] + const many = analyseMany(rules, targets)(stateSelector).targets + + const one = analyse(rules, 'cotisations')(stateSelector).targets[0] + + //console.log(many[0].nodeValue, many[1].nodeValue, one.nodeValue) + expect(many[1].nodeValue).to.be.closeTo(one.nodeValue, 0.1) + }) + it('should compute the same contributions if asked with analyseMany or analyse', function() { const situationSelector = dottedName => ({ - 'contrat salarié . salaire . net après impôt': 10000, + 'contrat salarié . rémunération . net de cotisations': 3500, 'auto entrepreneur': 'non', 'contrat salarié': 'oui', 'contrat salarié . assimilé salarié': 'oui', @@ -25,14 +79,17 @@ describe('bug-analyse-many', function() { }[dottedName]) const rules = parseAll(realRules.map(enrichRule)) const targets = [ - "entreprise . chiffre d'affaires", - 'contrat salarié . cotisations' + 'contrat salarié . salaire . brut de base', + 'contrat salarié . cotisations . salariales' ] const analyseManyValue = analyseMany(rules, targets)(situationSelector) .targets[1] - const analyseValue = analyse(rules, 'contrat salarié . cotisations')( - situationSelector - ).targets[0] + const analyseValue = analyse( + rules, + 'contrat salarié . cotisations . salariales' + )(situationSelector).targets[0] + + console.log(analyseManyValue.nodeValue, analyseValue.nodeValue) expect(analyseManyValue.nodeValue).to.equal(analyseValue.nodeValue) }) })