Skip to content

Commit

Permalink
add data validation tests
Browse files Browse the repository at this point in the history
- invalid timesteps
- empty or non-dict top-level config
  • Loading branch information
tomalrussell committed Mar 15, 2017
1 parent 62ddd3f commit 9dfcc83
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions smif/data_layer/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class ValidationError(Exception):
def validate_sos_model_config(data):
"""Check expected values for data loaded from master config file
"""
if not isinstance(data, dict):
msg = "Main config file should contain setup data, instead found: {}"
err = ValidationError(msg.format(data))
VALIDATION_ERRORS.append(err)
return

# check timesteps
if "timesteps" not in data:
VALIDATION_ERRORS.append(
Expand Down
40 changes: 39 additions & 1 deletion tests/data_layer/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
validate_interventions,
validate_planning_config,
validate_sector_model_initial_config,
validate_sos_model_config)
validate_sos_model_config,
validate_timesteps)


def get_sos_model_config():
Expand Down Expand Up @@ -97,6 +98,23 @@ def test_modelrun_config_validate():
validate_sos_model_config(data)


def test_modelrun_config_invalid():
"""Expect an error if not a dict
"""
invalid_possibilities = [
0,
[],
"just a string",
3.1415
]

for invalid_data in invalid_possibilities:
validate_sos_model_config(invalid_data)
ex = VALIDATION_ERRORS.pop()
msg = "Main config file should contain setup data"
assert msg in str(ex)


def test_missing_timestep():
"""Expect an error if missing timesteps
"""
Expand All @@ -121,6 +139,26 @@ def test_invalid_timesteps_file():
assert msg in str(ex)


def test_invalid_timesteps():
"""Expect a list of timesteps, else error
"""
data = 2010
validate_timesteps(data, "timestep.yaml")
ex = VALIDATION_ERRORS.pop()
msg = "expected a list of timesteps"
assert msg in str(ex)


def test_invalid_single_timestep():
"""Expect an error for non-integer timesteps
"""
data = [2010, "January 2015"]
validate_timesteps(data, "timestep.yaml")
ex = VALIDATION_ERRORS.pop()
msg = "timesteps should be integer years"
assert msg in str(ex)


def test_missing_sector_models():
"""Expect an error if missing sector_models
"""
Expand Down

0 comments on commit 9dfcc83

Please sign in to comment.