Skip to content

Commit

Permalink
Fix regression on YAML tests runner cache for tax benefit systems wit…
Browse files Browse the repository at this point in the history
…h reforms or extensions

Merge pull request #825 from openfisca/yaml-reform-format
  • Loading branch information
sandcha authored Mar 5, 2019
2 parents 273e17b + 93ce495 commit aa37fc1
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 26.0.4 [#825](https://github.com/openfisca/openfisca-core/pull/825)

- Fixes regression introduced by Core v25 when running YAML tests with reforms or extensions

### 26.0.3 [828](https://github.com/openfisca/openfisca-core/pull/828)

- Remove `__future__` statements
Expand Down
13 changes: 8 additions & 5 deletions openfisca_core/tools/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,21 @@ def _parse_test_file(tax_benefit_system, yaml_path, options):


def _get_tax_benefit_system(baseline, reforms, extensions):
key = hash((id(baseline), frozenset(reforms), frozenset(extensions)))
if not isinstance(reforms, list):
reforms = [reforms]
if not isinstance(extensions, list):
extensions = [extensions]

# keep reforms order in cache, ignore extensions order
key = hash((id(baseline), ':'.join(reforms), frozenset(extensions)))
if _tax_benefit_system_cache.get(key):
return _tax_benefit_system_cache.get(key)

current_tax_benefit_system = baseline

if not isinstance(reforms, list):
reforms = [reforms]
for reform_path in reforms:
current_tax_benefit_system = current_tax_benefit_system.apply_reform(reform_path)

if not isinstance(extensions, list):
extensions = [extensions]
for extension in extensions:
current_tax_benefit_system = current_tax_benefit_system.clone()
current_tax_benefit_system.load_extension(extension)
Expand All @@ -217,6 +219,7 @@ def _parse_test(tax_benefit_system, test, options, yaml_path):
name = test.get('name', '')
if not test.get('output'):
raise ValueError("Missing key 'output' in test '{}' in file '{}'".format(name, yaml_path))

if not TEST_KEYWORDS.issuperset(test.keys()):
unexpected_keys = set(test.keys()).difference(TEST_KEYWORDS)
raise ValueError("Unexpected keys {} in test '{}' in file '{}'".format(unexpected_keys, name, yaml_path))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

setup(
name = 'OpenFisca-Core',
version = '26.0.3',
version = '26.0.4',
author = 'OpenFisca Team',
author_email = 'contact@openfisca.org',
classifiers = [
Expand Down
29 changes: 0 additions & 29 deletions tests/core/test_runner/test_yaml_runner.py

This file was deleted.

91 changes: 91 additions & 0 deletions tests/core/tools/test_runner/test_yaml_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from openfisca_core.tools.test_runner import _run_test, _get_tax_benefit_system
from openfisca_core.errors import VariableNotFound


import pytest


class TaxBenefitSystem:
def __init__(self):
self.variables = {}

def get_package_metadata(self):
return {"name": "Test", "version": "Test"}

def apply_reform(self, path):
return Reform(self)

def load_extension(self, extension):
pass

def clone(self):
return TaxBenefitSystem()


class Reform(TaxBenefitSystem):
def __init__(self, baseline):
self.baseline = baseline


class Simulation:
def __init__(self):
self.tax_benefit_system = TaxBenefitSystem()
self.entities = {}

def get_entity(self, plural = None):
return None


def test_variable_not_found():
test = {"output": {"unknown_variable": 0}}
with pytest.raises(VariableNotFound) as excinfo:
_run_test(Simulation(), test)
assert excinfo.value.variable_name == "unknown_variable"


def test_tax_benefit_systems_with_reform_cache():
baseline = TaxBenefitSystem()

ab_tax_benefit_system = _get_tax_benefit_system(baseline, 'ab', [])
ba_tax_benefit_system = _get_tax_benefit_system(baseline, 'ba', [])
assert ab_tax_benefit_system != ba_tax_benefit_system


def test_reforms_formats():
baseline = TaxBenefitSystem()

lonely_reform_tbs = _get_tax_benefit_system(baseline, 'lonely_reform', [])
list_lonely_reform_tbs = _get_tax_benefit_system(baseline, ['lonely_reform'], [])
assert lonely_reform_tbs == list_lonely_reform_tbs


def test_reforms_order():
baseline = TaxBenefitSystem()

abba_tax_benefit_system = _get_tax_benefit_system(baseline, ['ab', 'ba'], [])
baab_tax_benefit_system = _get_tax_benefit_system(baseline, ['ba', 'ab'], [])
assert abba_tax_benefit_system != baab_tax_benefit_system # keep reforms order in cache


def test_tax_benefit_systems_with_extensions_cache():
baseline = TaxBenefitSystem()

xy_tax_benefit_system = _get_tax_benefit_system(baseline, [], 'xy')
yx_tax_benefit_system = _get_tax_benefit_system(baseline, [], 'yx')
assert xy_tax_benefit_system != yx_tax_benefit_system


def test_extensions_formats():
baseline = TaxBenefitSystem()

lonely_extension_tbs = _get_tax_benefit_system(baseline, [], 'lonely_extension')
list_lonely_extension_tbs = _get_tax_benefit_system(baseline, [], ['lonely_extension'])
assert lonely_extension_tbs == list_lonely_extension_tbs


def test_extensions_order():
baseline = TaxBenefitSystem()

xy_tax_benefit_system = _get_tax_benefit_system(baseline, [], ['x', 'y'])
yx_tax_benefit_system = _get_tax_benefit_system(baseline, [], ['y', 'x'])
assert xy_tax_benefit_system == yx_tax_benefit_system # extensions order is ignored in cache

0 comments on commit aa37fc1

Please sign in to comment.