Skip to content

Commit

Permalink
Merge pull request #2524 from chusloj/read_json_error
Browse files Browse the repository at this point in the history
Check for existence of reform and assumption json files
  • Loading branch information
MattHJensen authored Jan 8, 2021
2 parents 565158d + 119a946 commit f7e5920
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion taxcalc/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,19 @@ def convert_year_to_int(syr_dict):
req.raise_for_status()
txt = req.text
else:
txt = obj
if isinstance(topkey, str) and (topkey == ''):
raise ValueError("topkey string is empty.")
if isinstance(obj, str):
if obj == '':
raise ValueError("obj string is empty.")
elif obj.endswith('.json') and not os.path.isfile(obj):
raise FileNotFoundError("The .json file does not exist.")
elif ("{" and "}") in obj:
txt = obj
else:
raise ValueError("The JSON variable is misspecified.")
else:
raise ValueError("The JSON variable is misspecified.")
# strip out //-comments without changing line numbers
json_txt = re.sub('//.*', ' ', txt)
# convert JSON text into a Python dictionary
Expand Down
10 changes: 10 additions & 0 deletions taxcalc/tests/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,16 @@ def test_read_bad_json_assump_file():
Calculator.read_json_param_objects(None, list())


def test_json_doesnt_exist():
"""
Test JSON file which doesn't exist
"""
with pytest.raises(FileNotFoundError):
Calculator.read_json_param_objects(None, './reforms/doesnt_exist.json')
with pytest.raises(FileNotFoundError):
Calculator.read_json_param_objects('./reforms/doesnt_exist.json', None)


def test_calc_all():
"""
Test calc_all method.
Expand Down

0 comments on commit f7e5920

Please sign in to comment.