Skip to content

Commit

Permalink
Adding tests specific for what is added in this branch/pull_request. …
Browse files Browse the repository at this point in the history
…#Codecoverage
  • Loading branch information
VGPReys committed Jun 6, 2023
1 parent af97e51 commit 934d32b
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 5 deletions.
41 changes: 38 additions & 3 deletions tests/test_gear_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ def test_get_module_name(header, name):
def test_load(config_example, expected):
"""Test read config."""
r = config.loads(config_example)
assert r == expected
assert r['final_cfg'] == expected


def test_load_nan_vlaue():
"""Test read config."""
r = config.loads("param=nan")
assert isnan(r["param"])
assert isnan(r['final_cfg']["param"])


@pytest.mark.parametrize(
Expand All @@ -232,7 +232,7 @@ def test_save_config(conf):
target = Path("dummy_config.cfg")
config.save(conf, target)
loaded = config.load(target)
assert loaded == conf
assert loaded['final_cfg'] == conf
target.unlink()


Expand All @@ -248,3 +248,38 @@ def test_save_config_toml(conf):
target = Path("dummy_config.toml")
config.save(conf, target, pure_toml=True)
target.unlink()


@pytest.mark.parametrize(
"paramline",
[
'param=true',
'sampling_factor=false',
'sym3 =true',
'sym4 = false',
]
)
def test_not_uppercase_bool(paramline):
"""Test lower case regex."""
assert config._uppercase_bool_re.match(paramline) is None


@pytest.mark.parametrize(
"paramline,expectedparam,expectedbool",
[
('param=True', 'param=', 'true'),
('param = False', 'param = ', 'false'),
('sampling_factor =False', 'sampling_factor =', 'false'),
('sym3=True', 'sym3=', 'true'),
('_strange_1_param_= False', '_strange_1_param_= ', 'false'),
('_strange1_2_3param_ = True', '_strange1_2_3param_ = ', 'true'),
]
)
def test_uppercase_bool(paramline, expectedparam, expectedbool):
"""Test lower case regex."""
groups = config._uppercase_bool_re.match(paramline)
assert groups is not None # Able to catch line
paramgroup = groups[1]
assert paramgroup == expectedparam # Able to catch parameter
lowercase_bool = groups[4].lower()
assert lowercase_bool == expectedbool # Able to lowercase boolean
140 changes: 140 additions & 0 deletions tests/test_gear_prepare_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from haddock.core.exceptions import ConfigurationError
from haddock.gear.prepare_run import (
check_if_path_exists,
copy_molecules_to_topology,
Expand All @@ -14,7 +15,10 @@
populate_topology_molecule_params,
update_step_contents_to_step_names,
validate_module_names_are_not_misspelled,
validate_param_range,
validate_param_type,
validate_parameters_are_not_misspelled,
validate_value,
)
from haddock.gear.yaml2cfg import read_from_yaml_config
from haddock.modules import modules_names
Expand Down Expand Up @@ -241,3 +245,139 @@ def test_update_step_folders_from_restart():
assert '1_dummystep' in file2

shutil.rmtree(output_tmp)


@pytest.mark.parametrize(
"defaultparam,inputvalue",
[
({'type': 'integer'}, 1),
({'type': 'float'}, 0.99),
({'type': 'list'}, [1, 2]),
({'type': 'boolean'}, False),
({'type': 'boolean'}, True),
({'type': 'string'}, 'Hello world'),
]
)
def test_accepted_value_type(defaultparam, inputvalue):
"""Test if the provided value type is correct.
Should not return anything
"""
assert validate_param_type(defaultparam, inputvalue) is None


@pytest.mark.parametrize(
"defaultparam,inputvalue",
[
({'type': 'integer', 'default': 0}, False),
({'type': 'float', 'default': 1.1}, 'Hello world'),
({'type': 'list', 'default': [1, 2]}, {1: 1, 2: 2}),
({'type': 'boolean', 'default': True}, 1),
({'type': 'boolean', 'default': False}, 0),
({'type': 'str', 'default': 'test'}, 2.1),
]
)
def test_value_type_error(defaultparam, inputvalue):
"""Test if the provided value type is wrong.
Should return a formated string describing the error.
"""
assert validate_param_type(defaultparam, inputvalue) is not None


@pytest.mark.parametrize(
"defaultparam,inputvalue",
[
({'min': 0, 'max': 1}, 0.5),
({'min': 1, 'max': 1000}, 1),
({'min': 0, 'max': 1000}, 1000),
({'min': 0, 'max': 1000}, -0),
({'choices': ['ok', 'fine']}, 'fine'),
({'choices': ['super']}, 'super'),
]
)
def test_accepted_value_range(defaultparam, inputvalue):
"""Test if the provided value range/choices is correct.
Should not return anything
"""
assert validate_param_range(defaultparam, inputvalue) is None


@pytest.mark.parametrize(
"defaultparam,inputvalue",
[
({'min': 0, 'max': 1, 'default': 0}, 2),
({'min': 1.1, 'max': 1.3, 'default': 1.2}, 1.4),
({'min': 0.1, 'max': 1.0, 'default': 0.5}, -0.1),
({'choices': ['not', 'good'], 'default': 'good'}, ['not', 'good']),
({'choices': ['super'], 'default': 'super'}, 'wrong'),
]
)
def test_value_range_error(defaultparam, inputvalue):
"""Test if the provided value range/choices is erronnated.
Should return formated string describing the error.
"""
assert validate_param_range(defaultparam, inputvalue) is not None


@pytest.mark.parametrize(
"defaultparams,key,value",
[
({'key': {'min': 0, 'max': 1, 'type': 'integer'}}, 'key', 1),
({'key': {'min': 0.5, 'max': 2.2, 'type': 'float'}}, 'key', 1.1),
({'key': {'min': 0, 'max': 1, 'type': 'integer'}}, 'key', 0),
({'key': {'choices': ['ok', 'fine'], 'type': 'string'}},
'key', 'ok'),
({'key': {'choices': ['ok', 'fine'], 'type': 'string'}},
'key', 'fine'),
({'key': {'type': 'boolean'}}, 'key', True),
({'key': {'type': 'list'}}, 'key', ['mol1', 'mol2']),
({'mol': {'group': 'molecules', 'type': 'list'}}, 'mol', ['pdb']),
({'molecule': {'type': 'list'}}, 'molecules', ['mol1', 'pdb']),
]
)
def test_accepted_param_value(defaultparams, key, value):
"""Test if the provided parameter = value is correct.
Should not return anything
"""
assert validate_value(defaultparams, key, value) is None


@pytest.mark.parametrize(
"defaultparams,key,value",
[
({'key': {'min': 0, 'max': 1, 'type': 'integer',
'default': 0}}, 'key', 1.1),
({'key': {'min': 0.5, 'max': 2.2,
'type': 'float', 'default': 1.1}}, 'key', 0),
({'key': {'choices': ['not', 'good'],
'type': 'string',
'default': 'good'}},
'key', 'ok'),
({'key': {'choices': ['False', 'True'],
'type': 'string',
'default': 'True'}},
'key', True),
({'key': {'choices': ['False', 'True'], 'type': 'string',
'default': 'False'}},
'key', False),
({'key': {'type': 'boolean', 'default': False}}, 'key', 0),
({'key': {'type': 'boolean', 'default': True}}, 'key', 1),
({'key': {'type': 'boolean', 'default': False}}, 'key', []),
({'key': {'type': 'boolean', 'default': False}}, 'key', ''),
({'key': {'type': 'list', 'default': ['m1', 'm2']}}, 'key', 1),
({'key': {'type': 'list', 'default': ['m1', 'm2']}}, 'key', 'list'),
({'key': {'type': 'list', 'default': ['m1', 'm2']}},
'key', {'hello': 'world'}),
]
)
def test_param_value_error(defaultparams, key, value):
"""Test if the provided parameter = value is not correct.
Should raise ConfigurationError
"""
with pytest.raises(ConfigurationError):
validate_value(defaultparams, key, value)
14 changes: 12 additions & 2 deletions tests/test_modules_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from haddock import modules_defaults_path
from haddock.core.exceptions import ConfigurationError
from haddock.gear.prepare_run import validate_param_range
from haddock.gear.yaml2cfg import read_from_yaml_config
from haddock.libs.libio import read_from_yaml
from haddock.modules import (
Expand Down Expand Up @@ -91,8 +92,8 @@ def keys_inspect(keys, d, param, module):

# global dictionaries helping functions below
yaml_params_types = {
"integer": (int, float),
"float": (float,),
"integer": (int, float,),
"float": (float, int,),
"boolean": (bool,),
"string": (str,),
"list": (list,),
Expand Down Expand Up @@ -144,6 +145,13 @@ def test_general_config():
assert read_from_yaml_config(modules_defaults_path)


def inspect_default_value(defaultval, param, paramname, module):
"""Test if default value is acceptable."""
error_msg = validate_param_range(param, defaultval)
assert error_msg is None, \
f"In module {module} parameter {paramname}, the default {error_msg}"


def test_yaml_keys(module_yaml_pure):
"""
Test keys in each parameter.
Expand All @@ -169,6 +177,8 @@ def inspect_types(d, module):
module,
)
yaml_types_to_keys[_type](value, param, module)
# Validate default value
inspect_default_value(value["default"], value, param, module)

elif isinstance(value, dict):
inspect_commons(value, param, module)
Expand Down

0 comments on commit 934d32b

Please sign in to comment.