Skip to content

Commit

Permalink
Close test gap on runner.py (#121)
Browse files Browse the repository at this point in the history
* WIP - initial commit of test suite for `runner.py`.

* Added a few more tests to cover edge cases. The rest should be covered by other test suites.
  • Loading branch information
drewoldag authored May 9, 2024
1 parent afe58d2 commit 8902fa9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lephare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
from .magSvc import *
from .prepare import *
from .process import *
from .runner import *
from .sedtolib import *
from .zphota import *
2 changes: 1 addition & 1 deletion src/lephare/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def config_reader(filename):

config = config_reader(filename)
for line in config:
if line[0] != "#" and line != "\n":
if line[0] != "#" and line != "\n" and not line.isspace():
splits = line.split()
splits[0].lstrip() # remove leading spaces
if splits[0] != "#":
Expand Down
92 changes: 92 additions & 0 deletions tests/lephare/test_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import os

import pytest

import lephare as lp
from lephare._lephare import keyword

TESTDIR = os.path.abspath(os.path.dirname(__file__))
TESTDATADIR = os.path.join(TESTDIR, "../data")


def test_runner_base():
"""This is the most rudimentary test of the Runner class. We have to provide
both a list of config_keys and a path to a config file. We want to check only
that the class can be instantiated and that the resulting keymap isn't empty."""

test_keys = ["key1", "key2", "key3"]
config_file_path = os.path.join(TESTDATADIR, "examples/COSMOS.para")
runner = lp.Runner(config_keys=test_keys, config_file=config_file_path)
assert len(runner.keymap)


def test_runner_no_config_keys():
"""Expect that a RuntimeError is raised when no config_keys or
config_keys=None is passed to Runner."""
with pytest.raises(RuntimeError) as excinfo:
lp.Runner()
assert excinfo.value == "Runner is a base class and cannot be initialized"

with pytest.raises(RuntimeError) as excinfo:
lp.Runner(config_keys=None)
assert excinfo.value == "Runner is a base class and cannot be initialized"


def test_runner_config_keymap_updates():
"""This test checks that the keymap is updated when a config_keymap is passed
to the Runner class."""
test_keys = ["key1", "key2", "key3"]
config_file_path = os.path.join(TESTDATADIR, "examples/COSMOS.para")
config_keymap = {
"STAR_SED": keyword("STAR_SED", "foo"),
"LIMITS_MAPP_CUT": keyword("LIMITS_MAPP_CUT", "42"),
}
runner = lp.Runner(config_keys=test_keys, config_file=config_file_path, config_keymap=config_keymap)

resulting_keymap = runner.keymap
assert resulting_keymap["STAR_SED"].value == "foo"
assert resulting_keymap["LIMITS_MAPP_CUT"].value == "42"


def test_runner_cannot_run():
"""Since the runner class is an abstract class, we expect that it cannot call
the run method directly."""

test_keys = ["key1", "key2", "key3"]
config_file_path = os.path.join(TESTDATADIR, "examples/COSMOS.para")
runner = lp.Runner(config_keys=test_keys, config_file=config_file_path)

with pytest.raises(Exception) as excinfo:
runner.run()
assert excinfo.value == "runner.py is an abstract class"


def test_runner_verbosity():
"""Check to make sure that verbosity is set correctly via the config_keymap"""
test_keys = ["key1", "key2", "key3"]
config_file_path = os.path.join(TESTDATADIR, "examples/COSMOS.para")
config_keymap = {}
runner = lp.Runner(config_keys=test_keys, config_file=config_file_path, config_keymap=config_keymap)

assert not runner.verbose

config_keymap = {"VERBOSE": keyword("VERBOSE", "NO")}
runner = lp.Runner(config_keys=test_keys, config_file=config_file_path, config_keymap=config_keymap)

assert not runner.verbose

config_keymap = {"VERBOSE": keyword("VERBOSE", "YES")}
runner = lp.Runner(config_keys=test_keys, config_file=config_file_path, config_keymap=config_keymap)

assert runner.verbose


def test_runner_config_file_not_found():
"""Pass in a config file that does not exist and expect a RuntimeError."""

test_keys = ["key1", "key2", "key3"]
config_file_path = os.path.join(TESTDATADIR, "foo/bar.para")

with pytest.raises(RuntimeError) as excinfo:
_ = lp.Runner(config_keys=test_keys, config_file=config_file_path)
assert excinfo.value == f"File {config_file_path} not found"

0 comments on commit 8902fa9

Please sign in to comment.