2018-06-13 16:50:51 +00:00
|
|
|
import { AssertionError } from 'chai'
|
2020-11-04 17:05:46 +00:00
|
|
|
import Engine, { parsePublicodes } from 'publicodes'
|
2020-12-15 11:46:57 +00:00
|
|
|
import { disambiguateRuleReference } from '../../publicodes/core/source/ruleUtils'
|
2020-12-09 11:41:58 +00:00
|
|
|
import rules from 'modele-social'
|
2018-06-13 16:50:51 +00:00
|
|
|
|
|
|
|
// les variables dans les tests peuvent être exprimées relativement à l'espace de nom de la règle,
|
|
|
|
// comme dans sa formule
|
2020-11-04 17:05:46 +00:00
|
|
|
let parsedRules = parsePublicodes(rules)
|
2020-04-23 07:30:03 +00:00
|
|
|
const engine = new Engine(parsedRules)
|
2018-06-13 16:50:51 +00:00
|
|
|
let runExamples = (examples, rule) =>
|
2020-12-01 09:17:27 +00:00
|
|
|
examples.map((ex) => {
|
2020-03-30 17:14:03 +00:00
|
|
|
const expected = ex['valeur attendue']
|
|
|
|
const situation = Object.entries(ex.situation).reduce(
|
|
|
|
(acc, [name, value]) => ({
|
|
|
|
...acc,
|
2020-12-01 09:17:27 +00:00
|
|
|
[disambiguateRuleReference(
|
|
|
|
engine.parsedRules,
|
|
|
|
rule.dottedName,
|
|
|
|
name
|
|
|
|
)]: value,
|
2020-03-30 17:14:03 +00:00
|
|
|
}),
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
const evaluation = engine
|
|
|
|
.setSituation(situation)
|
2020-04-23 07:30:03 +00:00
|
|
|
.evaluate(rule.dottedName, {
|
2020-12-01 09:17:27 +00:00
|
|
|
unit: rule['unité par défaut'],
|
2020-04-23 07:30:03 +00:00
|
|
|
})
|
2020-03-30 17:14:03 +00:00
|
|
|
const ok =
|
|
|
|
evaluation.nodeValue === expected
|
|
|
|
? true
|
|
|
|
: typeof expected === 'number'
|
|
|
|
? Math.abs((evaluation.nodeValue - expected) / expected) < 0.001
|
|
|
|
: false
|
2018-06-13 16:50:51 +00:00
|
|
|
|
2020-03-30 17:14:03 +00:00
|
|
|
return { ...ex, ok, rule: evaluation }
|
2018-06-13 16:50:51 +00:00
|
|
|
})
|
2017-12-18 19:56:23 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
describe('Tests des règles de notre base de règles', () =>
|
2020-12-01 09:17:27 +00:00
|
|
|
Object.values(parsedRules).map((rule) => {
|
2020-03-30 17:14:03 +00:00
|
|
|
if (!rule?.exemples) return null
|
2018-09-20 15:14:11 +00:00
|
|
|
describe(rule.dottedName, () => {
|
2018-06-13 16:50:51 +00:00
|
|
|
let examples = runExamples(rule.exemples, rule)
|
2020-12-01 09:17:27 +00:00
|
|
|
examples.map((example) =>
|
2018-01-03 15:54:19 +00:00
|
|
|
it(example.nom + '', () => {
|
2020-03-30 17:14:03 +00:00
|
|
|
if (!example.ok) {
|
2018-01-03 15:54:19 +00:00
|
|
|
throw new AssertionError(`
|
2020-03-30 17:14:03 +00:00
|
|
|
Valeur attendue : ${example['valeur attendue']}
|
2020-11-18 17:53:37 +00:00
|
|
|
Valeur obtenue : ${example.rule.nodeValue}
|
2020-03-30 17:14:03 +00:00
|
|
|
${
|
|
|
|
example.rule.nodeValue === null
|
|
|
|
? 'Variables manquantes : ' +
|
|
|
|
JSON.stringify(example.rule.missingVariables, null, 4)
|
|
|
|
: ''
|
|
|
|
}
|
2017-12-18 19:56:23 +00:00
|
|
|
`)
|
2020-03-30 17:14:03 +00:00
|
|
|
}
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}))
|