Skip to content

Commit

Permalink
tests and value checks for batch and launch settings
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaputko committed Oct 9, 2024
1 parent 2cbd3be commit 6aa9991
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
19 changes: 19 additions & 0 deletions smartsim/settings/batch_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def __init__(
"""The scheduler type"""
except ValueError:
raise ValueError(f"Invalid scheduler type: {batch_scheduler}") from None

if batch_args:
if not (

Check warning on line 122 in smartsim/settings/batch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/batch_settings.py#L121-L122

Added lines #L121 - L122 were not covered by tests
isinstance(batch_args, t.Mapping)
and all(isinstance(key, str) for key, val in batch_args.items())
):
raise TypeError(

Check warning on line 126 in smartsim/settings/batch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/batch_settings.py#L126

Added line #L126 was not covered by tests
"batch_args argument was not of type mapping of str and str"
)
self._arguments = self._get_arguments(batch_args)
"""The BatchSettings child class based on scheduler type"""
self.env_vars = env_vars or {}
Expand All @@ -140,6 +149,16 @@ def env_vars(self) -> StringArgument:
@env_vars.setter
def env_vars(self, value: t.Dict[str, str | None]) -> None:
"""Set the environment variables."""

if not (

Check warning on line 153 in smartsim/settings/batch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/batch_settings.py#L153

Added line #L153 was not covered by tests
isinstance(value, t.Mapping)
and all(
isinstance(key, str) and isinstance(val, str)
for key, val in value.items()
)
):
raise TypeError("env_vars argument was not of type dic of str and str")

Check warning on line 160 in smartsim/settings/batch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/batch_settings.py#L160

Added line #L160 was not covered by tests

self._env_vars = copy.deepcopy(value)

def _get_arguments(self, batch_args: StringArgument | None) -> BatchArguments:
Expand Down
27 changes: 27 additions & 0 deletions smartsim/settings/launch_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ def __init__(
"""The launcher type"""
except ValueError:
raise ValueError(f"Invalid launcher type: {launcher}")

if launch_args:
if not (

Check warning on line 131 in smartsim/settings/launch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/launch_settings.py#L131

Added line #L131 was not covered by tests
isinstance(launch_args, t.Mapping)
and all(isinstance(key, str) for key, val in launch_args.items())
):
raise TypeError(

Check warning on line 135 in smartsim/settings/launch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/launch_settings.py#L135

Added line #L135 was not covered by tests
"batch_args argument was not of type mapping of str and str"
)
self._arguments = self._get_arguments(launch_args)
"""The LaunchSettings child class based on launcher type"""
self.env_vars = env_vars or {}
Expand Down Expand Up @@ -165,6 +174,15 @@ def env_vars(self, value: dict[str, str | None]) -> None:
:param value: The new environment mapping
"""
if not (
isinstance(value, t.Mapping)
and all(
isinstance(key, str) and isinstance(val, str)
for key, val in value.items()
)
):
raise TypeError("env_vars argument was not of type dic of str and str")

Check warning on line 184 in smartsim/settings/launch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/launch_settings.py#L184

Added line #L184 was not covered by tests

self._env_vars = copy.deepcopy(value)

def _get_arguments(self, launch_args: StringArgument | None) -> LaunchArguments:
Expand Down Expand Up @@ -209,6 +227,15 @@ def update_env(self, env_vars: t.Dict[str, str | None]) -> None:
:param env_vars: environment variables to update or add
:raises TypeError: if env_vars values cannot be coerced to strings
"""
if not (

Check warning on line 230 in smartsim/settings/launch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/launch_settings.py#L230

Added line #L230 was not covered by tests
isinstance(env_vars, t.Mapping)
and all(
isinstance(key, str) and isinstance(val, str)
for key, val in env_vars.items()
)
):
raise TypeError("env_vars argument was not of type dic of str and str")

Check warning on line 237 in smartsim/settings/launch_settings.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/launch_settings.py#L237

Added line #L237 was not covered by tests

# Coerce env_vars values to str as a convenience to user
for env, val in env_vars.items():
if not isinstance(env, str):
Expand Down
30 changes: 30 additions & 0 deletions tests/temp_tests/test_settings/test_batchSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,33 @@ def test_env_vars_property():
assert bs.env_vars == {"ENV": "VAR"}
ref = bs.env_vars
assert ref is bs.env_vars


def test_type_batch_scheduler():
batch_scheduler = "invalid"
with pytest.raises(ValueError, match="Invalid scheduler type: invalid"):
BatchSettings(
batch_scheduler=batch_scheduler,
batch_args={"launch": "var"},
env_vars={"ENV": "VAR"},
)


def test_type_batch_args():
batch_args = "invalid"
with pytest.raises(
TypeError, match="batch_args argument was not of type mapping of str and str"
):
BatchSettings(
batch_scheduler="slurm",
batch_args=batch_args,
env_vars={"ENV": "VAR"},
)


def test_type_env_vars():
env_vars = "invalid"
with pytest.raises(
TypeError, match="env_vars argument was not of type dic of str and str"
):
BatchSettings(batch_scheduler="slurm", env_vars=env_vars)
26 changes: 26 additions & 0 deletions tests/temp_tests/test_settings/test_launchSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,29 @@ def test_update_env_vars_errors():
# and that the function is atomic
ls.update_env({"test": "test", "test": 1})
assert ls.env_vars == {"ENV": "VAR"}


def test_type_launcher():
launcher = "invalid"
with pytest.raises(ValueError, match="Invalid launcher type: invalid"):
LaunchSettings(
launcher=launcher, launch_args={"launch": "var"}, env_vars={"ENV": "VAR"}
)


def test_type_launch_args():
launch_args = "invalid"
with pytest.raises(
TypeError, match="batch_args argument was not of type mapping of str and str"
):
LaunchSettings(
launcher="local", launch_args=launch_args, env_vars={"ENV": "VAR"}
)


def test_type_env_vars():
env_vars = "invalid"
with pytest.raises(
TypeError, match="env_vars argument was not of type dic of str and str"
):
LaunchSettings(launcher="local", env_vars=env_vars)

0 comments on commit 6aa9991

Please sign in to comment.