Skip to content

Commit

Permalink
Add walk_node unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Nov 14, 2018
1 parent 3fbd2c2 commit 245b7e1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
2 changes: 2 additions & 0 deletions openfisca_web_api/loader/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def walk_node(node, parameters, path_fragments, country_package_metadata):
walk_node(child, parameters, path_fragments + [child_name], country_package_metadata)
parameters.append(api_parameter)

return parameters


def build_parameters(tax_benefit_system, country_package_metadata):
original_parameters = tax_benefit_system.parameters
Expand Down
60 changes: 40 additions & 20 deletions tests/web_api/loader/test_parameters.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
# -*- coding: utf-8 -*-

from openfisca_web_api.loader.parameters import walk_node
import pytest

from openfisca_core.parameters import (
ParameterNode,
Scale,
)
from openfisca_core.parameters import ParameterNode, Scale

from openfisca_web_api.loader.parameters import build_api_scale, walk_node

def test_rate_tax_scale_serialization():

def test_build_rate_scale():
'''Extracts a 'rate' children from a bracket collection'''
data = {'brackets': [{'rate': {'2014-01-01': {'value': 0.5}}, 'threshold': {'2014-01-01': {'value': 1}}}]}
rate = Scale('this rate', data, None)
assert build_api_scale(rate, 'rate') == {'2014-01-01': {1: 0.5}}


def test_build_amount_scale():
'''Extracts an 'amount' children from a bracket collection'''
data = {'brackets': [{'amount': {'2014-01-01': {'value': 0}}, 'threshold': {'2014-01-01': {'value': 1}}}]}
rate = Scale('that amount', data, None)
assert build_api_scale(rate, 'amount') == {'2014-01-01': {1: 0}}


@pytest.fixture
def walk_node_args():
parameters = []
metadata = {'location': 'foo', 'version': '1', 'repository_url': 'foo'}
path_fragments = []
country_package_metadata = {'location': 'foo', 'version': '1', 'repository_url': 'foo'}

return parameters, path_fragments, country_package_metadata


def test_walk_node_rate_scale(walk_node_args):
'''Serializes a 'rate' parameter node'''
root_node = ParameterNode(data = {})
amount_rate_data = {'brackets': [{'rate': {'2014-01-01': {'value': 0.5}}, 'threshold': {'2014-01-01': {'value': 1}}}]}
rate = Scale('scale', amount_rate_data, 'foo')
root_node.children['rate'] = rate
walk_node(root_node, parameters, [], metadata)
assert parameters == [{'description': None, 'id': 'rate', 'metadata': {}, 'source': 'foo/blob/1', 'brackets': {'2014-01-01': {1: 0.5}}}]
data = {'brackets': [{'rate': {'2014-01-01': {'value': 0.5}}, 'threshold': {'2014-01-01': {'value': 1}}}]}
scale = Scale('this rate', data, None)
root_node.children['rate'] = scale
parameters = walk_node(root_node, *walk_node_args)
assert parameters == [{'description': None, 'id': 'rate', 'metadata': {}, 'brackets': {'2014-01-01': {1: 0.5}}}]


def test_amount_tax_scale_serialization():
parameters = []
metadata = {'location': 'foo', 'version': '1', 'repository_url': 'foo'}
def test_walk_node_amount_scale(walk_node_args):
'''Serializes an 'amount' parameter node'''
root_node = ParameterNode(data = {})
amount_scale_data = {'brackets': [{'amount': {'2014-01-01': {'value': 0}}, 'threshold': {'2014-01-01': {'value': 1}}}]}
amount = Scale('scale', amount_scale_data, 'foo')
root_node.children['amount'] = amount
walk_node(root_node, parameters, [], metadata)
assert parameters == [{'description': None, 'id': 'amount', 'metadata': {}, 'source': 'foo/blob/1', 'brackets': {'2014-01-01': {1: 0}}}]
data = {'brackets': [{'amount': {'2014-01-01': {'value': 0}}, 'threshold': {'2014-01-01': {'value': 1}}}]}
scale = Scale('that amount', data, None)
root_node.children['amount'] = scale
parameters = walk_node(root_node, *walk_node_args)
assert parameters == [{'description': None, 'id': 'amount', 'metadata': {}, 'brackets': {'2014-01-01': {1: 0}}}]

0 comments on commit 245b7e1

Please sign in to comment.