Skip to content

Commit

Permalink
fix tests/bugs introduced by merging in develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bgunnar5 committed Apr 25, 2024
1 parent 3099d4c commit 37839f6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
10 changes: 8 additions & 2 deletions merlin/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ def get_priority(priority: Priority) -> int:
:param priority: The priority value that we want
:returns: The priority value as an integer
"""
if priority not in Priority:
raise ValueError(f"Invalid priority: {priority}")
priority_err_msg = f"Invalid priority: {priority}"
try:
# In python 3.12+ if something is not in the enum it will just return False
if priority not in Priority:
raise ValueError(priority_err_msg)
# In python 3.11 and below, a TypeError is raised when looking for something in an enum that is not there
except TypeError:
raise ValueError(priority_err_msg)

priority_map = determine_priority_map(CONFIG.broker.name.lower())
return priority_map.get(priority, priority_map[Priority.MID]) # Default to MID priority for unknown priorities
1 change: 1 addition & 0 deletions merlin/examples/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,5 @@ def setup_example(name, outdir):

LOG.info(f"Copying example '{name}' to {outdir}")
write_example(src_path, outdir)
print(f'example: {example}')
return example
50 changes: 42 additions & 8 deletions tests/unit/config/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from merlin.config.configfile import CONFIG
from merlin.config.utils import Priority, get_priority, is_rabbit_broker, is_redis_broker
from merlin.config.utils import Priority, determine_priority_map, get_priority, is_rabbit_broker, is_redis_broker


def test_is_rabbit_broker():
Expand Down Expand Up @@ -37,25 +37,27 @@ def test_is_redis_broker_invalid():
def test_get_priority_rabbit_broker(rabbit_broker_config: "fixture"): # noqa: F821
"""
Test the `get_priority` function with rabbit as the broker.
Low priority for rabbit is 1 and high is 10.
Low priority for rabbit is 1 and high is 9.
:param rabbit_broker_config: A fixture to set the CONFIG object to a test configuration that we'll use here
"""
assert get_priority(Priority.LOW) == 1
assert get_priority(Priority.MID) == 5
assert get_priority(Priority.HIGH) == 10
assert get_priority(Priority.HIGH) == 9
assert get_priority(Priority.RETRY) == 10


def test_get_priority_redis_broker(redis_broker_config: "fixture"): # noqa: F821
"""
Test the `get_priority` function with redis as the broker.
Low priority for redis is 10 and high is 1.
Low priority for redis is 10 and high is 2.
:param redis_broker_config: A fixture to set the CONFIG object to a test configuration that we'll use here
"""
assert get_priority(Priority.LOW) == 10
assert get_priority(Priority.MID) == 5
assert get_priority(Priority.HIGH) == 1
assert get_priority(Priority.HIGH) == 2
assert get_priority(Priority.RETRY) == 1


def test_get_priority_invalid_broker(redis_broker_config: "fixture"): # noqa: F821
Expand All @@ -68,7 +70,7 @@ def test_get_priority_invalid_broker(redis_broker_config: "fixture"): # noqa: F
CONFIG.broker.name = "invalid"
with pytest.raises(ValueError) as excinfo:
get_priority(Priority.LOW)
assert "Function get_priority has reached unknown state! Maybe unsupported broker invalid?" in str(excinfo.value)
assert "Unsupported broker name: invalid" in str(excinfo.value)


def test_get_priority_invalid_priority(redis_broker_config: "fixture"): # noqa: F821
Expand All @@ -78,6 +80,38 @@ def test_get_priority_invalid_priority(redis_broker_config: "fixture"): # noqa:
:param redis_broker_config: A fixture to set the CONFIG object to a test configuration that we'll use here
"""
with pytest.raises(TypeError) as excinfo:
with pytest.raises(ValueError) as excinfo:
get_priority("invalid_priority")
assert "Unrecognized priority 'invalid_priority'!" in str(excinfo.value)
assert "Invalid priority: invalid_priority" in str(excinfo.value)


def test_determine_priority_map_rabbit():
"""
Test the `determine_priority_map` function with rabbit as the broker.
This should return the following map:
{Priority.LOW: 1, Priority.MID: 5, Priority.HIGH: 9, Priority.RETRY: 10}
"""
expected = {Priority.LOW: 1, Priority.MID: 5, Priority.HIGH: 9, Priority.RETRY: 10}
actual = determine_priority_map("rabbitmq")
assert actual == expected


def test_determine_priority_map_redis():
"""
Test the `determine_priority_map` function with redis as the broker.
This should return the following map:
{Priority.LOW: 10, Priority.MID: 5, Priority.HIGH: 2, Priority.RETRY: 1}
"""
expected = {Priority.LOW: 10, Priority.MID: 5, Priority.HIGH: 2, Priority.RETRY: 1}
actual = determine_priority_map("redis")
assert actual == expected


def test_determine_priority_map_invalid():
"""
Test the `determine_priority_map` function with an invalid broker.
This should raise a ValueError.
"""
with pytest.raises(ValueError) as excinfo:
determine_priority_map("invalid_broker")
assert "Unsupported broker name: invalid_broker" in str(excinfo.value)
6 changes: 4 additions & 2 deletions tests/unit/test_examples_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ def test_gather_all_examples():
f"{EXAMPLES_DIR}/lsf/lsf_par.yaml",
f"{EXAMPLES_DIR}/null_spec/null_chain.yaml",
f"{EXAMPLES_DIR}/null_spec/null_spec.yaml",
f"{EXAMPLES_DIR}/openfoam_wf/openfoam_wf_template.yaml",
f"{EXAMPLES_DIR}/openfoam_wf/openfoam_wf_docker_template.yaml",
f"{EXAMPLES_DIR}/openfoam_wf/openfoam_wf.yaml",
f"{EXAMPLES_DIR}/openfoam_wf_no_docker/openfoam_wf_no_docker_template.yaml",
f"{EXAMPLES_DIR}/openfoam_wf_no_docker/openfoam_wf_no_docker.yaml",
f"{EXAMPLES_DIR}/openfoam_wf_singularity/openfoam_wf_singularity.yaml",
f"{EXAMPLES_DIR}/openfoam_wf_singularity/openfoam_wf_singularity_template.yaml",
f"{EXAMPLES_DIR}/optimization/optimization_basic.yaml",
f"{EXAMPLES_DIR}/remote_feature_demo/remote_feature_demo.yaml",
f"{EXAMPLES_DIR}/restart/restart.yaml",
Expand Down Expand Up @@ -445,7 +446,7 @@ def test_setup_example_openfoam(temp_output_dir: str):
example_name = "openfoam_wf"
example_files = [
"openfoam_wf.yaml",
"openfoam_wf_template.yaml",
"openfoam_wf_docker_template.yaml",
"README.md",
"requirements.txt",
"scripts/make_samples.py",
Expand Down Expand Up @@ -492,6 +493,7 @@ def test_setup_example_openfoam_singularity(temp_output_dir: str):
example_name = "openfoam_wf_singularity"
example_files = [
"openfoam_wf_singularity.yaml",
"openfoam_wf_singularity_template.yaml",
"requirements.txt",
"scripts/make_samples.py",
"scripts/blockMesh_template.txt",
Expand Down

0 comments on commit 37839f6

Please sign in to comment.