Skip to content

Commit

Permalink
Add Policy.PROHIBIT_PARAM_CODE class attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholmer committed Dec 6, 2016
1 parent 0052470 commit 984f1ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
17 changes: 11 additions & 6 deletions taxcalc/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class instance: Policy

VALID_PARAM_CODE_NAMES = set(['ALD_Investment_ec_base_code'])

PROHIBIT_PARAM_CODE = False

@staticmethod
def default_inflation_rates():
"""
Expand Down Expand Up @@ -213,7 +215,7 @@ def read_json_reform_text(text_string):
json_without_comments = re.sub('//.*', ' ', text_string)
# convert JSON text into a dictionary with year skeys as strings
try:
reform_dict_raw = json.loads(json_without_comments)
raw_dict = json.loads(json_without_comments)
except ValueError as valerr:
msg = 'Policy reform text below contains invalid JSON:\n'
msg += str(valerr) + '\n'
Expand All @@ -228,13 +230,16 @@ def read_json_reform_text(text_string):
msg += '{:02d}{}'.format(linenum, line) + '\n'
msg += bline + '\n'
raise ValueError(msg)
# handle special param_code key in reform_dict_raw
paramcode = reform_dict_raw.pop('param_code', None)
# handle special param_code key in raw_dict
paramcode = raw_dict.pop('param_code', None)
if paramcode:
if Policy.PROHIBIT_PARAM_CODE:
msg = 'JSON reform file containing "param_code" is not allowed'
raise ValueError(msg)
for param, code in paramcode.items():
reform_dict_raw[param] = {'0': code}
# convert reform_dict_raw
return Policy.convert_reform_dictionary(reform_dict_raw)
raw_dict[param] = {'0': code}
# convert raw_dict
return Policy.convert_reform_dictionary(raw_dict)

def implement_reform(self, reform):
"""
Expand Down
17 changes: 12 additions & 5 deletions taxcalc/tests/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,13 @@ def reform_file():
pass # sometimes we can't remove a generated temporary file


def test_prohibit_param_code(reform_file):

This comment has been minimized.

Copy link
@talumbau

talumbau Dec 7, 2016

Member

looks good!

Policy.PROHIBIT_PARAM_CODE = True
with pytest.raises(ValueError):
Policy.read_json_reform_file(reform_file.name)
Policy.PROHIBIT_PARAM_CODE = False


@pytest.mark.parametrize("set_year", [False, True])
def test_read_json_reform_file_and_implement_reform(reform_file, set_year):
"""
Expand Down Expand Up @@ -690,18 +697,18 @@ def test_read_bad_json_reform_file(badreformfile):

def test_convert_reform_dictionary():
with pytest.raises(ValueError):
rdict = Policy.convert_reform_dictionary({2013: {'2013': [40000]}})
Policy.convert_reform_dictionary({2013: {'2013': [40000]}})
with pytest.raises(ValueError):
rdict = Policy.convert_reform_dictionary({'_II_em': {2013: [40000]}})
Policy.convert_reform_dictionary({'_II_em': {2013: [40000]}})


def test_reform_pkey_year():
with pytest.raises(ValueError):
rdict = Policy._reform_pkey_year({4567: {2013: [40000]}})
Policy._reform_pkey_year({4567: {2013: [40000]}})
with pytest.raises(ValueError):
rdict = Policy._reform_pkey_year({'_II_em': 40000})
Policy._reform_pkey_year({'_II_em': 40000})
with pytest.raises(ValueError):
rdict = Policy._reform_pkey_year({'_II_em': {'2013': [40000]}})
Policy._reform_pkey_year({'_II_em': {'2013': [40000]}})


def test_current_law_version():
Expand Down

0 comments on commit 984f1ca

Please sign in to comment.