From d92c383fa43f522326d24b10f961d5933aaab9b7 Mon Sep 17 00:00:00 2001 From: Mael Date: Tue, 23 Oct 2018 10:58:27 +0000 Subject: [PATCH] =?UTF-8?q?:bug:=20Mise=20en=20=C3=A9vidence=20de=20la=20m?= =?UTF-8?q?auvaise=20impl=C3=A9mentation=20des=20p=C3=A9riodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/treatVariable.js | 20 +++++++++++++------- test/library.test.js | 9 +++++---- test/mécanismes/période.yaml | 28 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/source/engine/treatVariable.js b/source/engine/treatVariable.js index 445a40887..2a972ea42 100644 --- a/source/engine/treatVariable.js +++ b/source/engine/treatVariable.js @@ -131,18 +131,24 @@ export let treatVariableTransforms = (rules, rule) => parseResult => { let calledPeriod = ruleToTransform['période'] || environmentPeriod let transformedNodeValue = - callingPeriod === 'mois' && calledPeriod === 'année' - ? nodeValue / 12 - : callingPeriod === 'année' && calledPeriod === 'mois' - ? nodeValue * 12 - : nodeValue + callingPeriod === 'mois' && calledPeriod === 'année' + ? nodeValue / 12 + : callingPeriod === 'année' && calledPeriod === 'mois' + ? nodeValue * 12 + : nodeValue, + periodTransform = nodeValue !== transformedNodeValue - return rewriteNode( - filteredNode, + let result = rewriteNode( + { + ...filteredNode, + periodTransform: periodTransform, + ...(periodTransform ? { originPeriodValue: nodeValue } : {}) + }, transformedNodeValue, filteredNode.explanation, filteredNode.missingVariables ) + return result } let node = treatVariable(rules, rule, parseResult.filter)( parseResult.variable || parseResult diff --git a/test/library.test.js b/test/library.test.js index 7459e960e..1346facaf 100644 --- a/test/library.test.js +++ b/test/library.test.js @@ -128,17 +128,18 @@ describe('library', function() { expect(value).to.equal(7253.26) }) it('should let let user define a rule base on a completely different subject', function() { - let target = 'impact' + let targets = ['impact'] let value = Syso.evaluate( - target, + targets, { 'nombre de douches': 30, 'chauffage . type': 'gaz', - 'durée de la douche': 7 + 'durée de la douche': 10 }, - { base: co2 } + { base: co2, debug: true } ) + console.log(JSON.stringify(value.targets[0], null, 4)) expect(value).to.equal(7253.26) }) }) diff --git a/test/mécanismes/période.yaml b/test/mécanismes/période.yaml index c106a251d..7414f4028 100644 --- a/test/mécanismes/période.yaml +++ b/test/mécanismes/période.yaml @@ -1,6 +1,34 @@ # This is not a mecanism test, but we make use of the simplicity of declaring tests in YAML, only available for mecanisms for now +- test: impact des douches + période: année + formule: 1 * nombre de douches + + exemples: + - situation: + nombre de douches: 30 + valeur attendue: 360 + +- nom: nombre de douches + période: mois + question: Combien prenez-vous de douches par mois ? + format: nombre + suggestions: + - 30 + +- nom: impact par douche + formule: 1 + unité: kgCO2e + +- test: impact des douches erroné + période: année + formule: impact par douche * nombre de douches + exemples: + - situation: + nombre de douches: 30 + valeur attendue: 360 + - nom: assiette mensuelle période: mois format: €