From 8902fa91cd7063353b594eb6d2930ff25c852cc2 Mon Sep 17 00:00:00 2001 From: Drew Oldag <47493171+drewoldag@users.noreply.github.com> Date: Wed, 8 May 2024 20:10:21 -0700 Subject: [PATCH] Close test gap on `runner.py` (#121) * 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. --- src/lephare/__init__.py | 1 + src/lephare/runner.py | 2 +- tests/lephare/test_runner.py | 92 ++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/lephare/test_runner.py diff --git a/src/lephare/__init__.py b/src/lephare/__init__.py index be8c9c79..ea51e4ba 100644 --- a/src/lephare/__init__.py +++ b/src/lephare/__init__.py @@ -28,5 +28,6 @@ from .magSvc import * from .prepare import * from .process import * +from .runner import * from .sedtolib import * from .zphota import * diff --git a/src/lephare/runner.py b/src/lephare/runner.py index c2a84201..8bf49c0a 100644 --- a/src/lephare/runner.py +++ b/src/lephare/runner.py @@ -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] != "#": diff --git a/tests/lephare/test_runner.py b/tests/lephare/test_runner.py new file mode 100644 index 00000000..dc2ec8c8 --- /dev/null +++ b/tests/lephare/test_runner.py @@ -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"