Skip to content

Commit

Permalink
Adding the ConfigValidationError (#281)
Browse files Browse the repository at this point in the history
* checkpoint, making better error logs

* adding test coverage, changing tests to expect the new ConfigValidationError class
  • Loading branch information
arik-shurygin authored Nov 6, 2024
1 parent b73a87a commit 25b3c58
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 49 deletions.
60 changes: 52 additions & 8 deletions src/resp_ode/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,31 @@ def assert_valid_configuration(self):
validator_funcs = make_list_if_not(validator_funcs)
vals = [getattr(self, k) for k in key]
# val_func() throws assert errors if incongruence arrises
[
(
val_func(key[0], vals[0])
if len(key) == 1 # convert back to floats if needed
else val_func(key, vals)
)
for val_func in validator_funcs
]
try:
[
(
val_func(key[0], vals[0])
if len(key)
== 1 # convert back to floats if needed
else val_func(key, vals)
)
for val_func in validator_funcs
]
except Exception as e:
if len(key) > 1:
err_text = """There was an issue validating your Config object.
The error was caused by the intersection of the following parameters: %s.
%s""" % (
key,
e,
)
else:
err_text = """The following error occured while validating the %s
parameter in your configuration file: %s""" % (
key[0],
e,
)
raise ConfigValidationError(err_text)


def make_list_if_not(obj):
Expand Down Expand Up @@ -291,6 +308,16 @@ def test_positive(key, value):
)


def test_enum_len(key, enum, expected_len):
assert (
len(enum) == expected_len
), "Expected %s to have %s entries, got %s" % (
key,
expected_len,
len(enum),
)


def test_not_negative(key, value):
"""
checks if a value is not negative.
Expand Down Expand Up @@ -654,6 +681,13 @@ class is accepted to modify/create the downstream parameters.
key, [(vals[0], 2 ** vals[0]), vals[1]]
),
},
{
"name": ["NUM_STRAINS", "STRAIN_IDX"],
# check that len(STRAIN_IDX)==NUM_STRAINS
"validate": lambda keys, vals: test_enum_len(
keys[1], vals[1], vals[0]
),
},
{
"name": "MAX_VACCINATION_COUNT",
"validate": test_not_negative,
Expand Down Expand Up @@ -852,4 +886,14 @@ class is accepted to modify/create the downstream parameters.


class ConfigParserError(Exception):
"""A basic class meant to denote when the Config
class is having an issue parsing a configuration file"""

pass


class ConfigValidationError(Exception):
"""A basic class meant to denote when the Config
class is having an issue validating a configuration file"""

pass
Loading

0 comments on commit 25b3c58

Please sign in to comment.