Skip to content

Commit

Permalink
♻️ Adapt cli and main tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dalonsoa committed Oct 25, 2024
1 parent 05ff05b commit 5d3a200
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 67 deletions.
88 changes: 39 additions & 49 deletions tests/test_cli_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,55 +26,45 @@ def test_ve_run(capsys, mocker):
"""

# import virtual_ecosystem.core #F401
from tests.conftest import patch_bypass_setup, patch_run_update
from virtual_ecosystem.core.logger import remove_file_logger
from virtual_ecosystem.entry_points import ve_run_cli

with (
patch_run_update("soil"),
patch_bypass_setup("soil") as mock_bypass_setup_soil,
patch_run_update("litter"),
patch_bypass_setup("litter") as mock_bypass_setup_litter,
):
mock_bypass_setup_soil.return_value = False
mock_bypass_setup_litter.return_value = False

with TemporaryDirectory() as tempdir:
try:
# Install the example directory to run it - tested above - and consume
# the resulting stdout
ve_run_cli(args_list=["--install-example", tempdir])
_ = capsys.readouterr()

example_dir = Path(tempdir) / "ve_example"
configs = example_dir / "config"
outdir = example_dir / "out"
logfile = outdir / "ve_example.log"
ve_run_cli(
args_list=[
str(configs),
"--outpath",
str(outdir),
"--logfile",
str(logfile),
"--progress",
]
)

# Test the requested --progress output ends as expected
captured = capsys.readouterr()
expected = "Virtual Ecosystem run complete.\n"
assert captured.out.endswith(expected)

# Check the logfile has been populated as expected
assert logfile.exists()
with open(logfile) as logfile_io:
contents = logfile_io.readlines()
assert "Virtual Ecosystem model run completed!" in contents[-1]

except Exception as excep:
# If the code above fails then tidy up the logger to restore normal
# stream logging rather than leaving all other tests logging to the file
# and then fail the test.
remove_file_logger()
pytest.fail(reason=str(excep))
with TemporaryDirectory() as tempdir:
try:
# Install the example directory to run it - tested above - and consume
# the resulting stdout
ve_run_cli(args_list=["--install-example", tempdir])
_ = capsys.readouterr()

example_dir = Path(tempdir) / "ve_example"
configs = example_dir / "config"
outdir = example_dir / "out"
logfile = outdir / "ve_example.log"
ve_run_cli(
args_list=[
str(configs),
"--outpath",
str(outdir),
"--logfile",
str(logfile),
"--progress",
]
)

# Test the requested --progress output ends as expected
captured = capsys.readouterr()
expected = "Virtual Ecosystem run complete.\n"
assert captured.out.endswith(expected)

# Check the logfile has been populated as expected
assert logfile.exists()
with open(logfile) as logfile_io:
contents = logfile_io.readlines()
assert "Virtual Ecosystem model run completed!" in contents[-1]

except Exception as excep:
# If the code above fails then tidy up the logger to restore normal
# stream logging rather than leaving all other tests logging to the file
# and then fail the test.
remove_file_logger()
pytest.fail(reason=str(excep))
31 changes: 13 additions & 18 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from virtual_ecosystem.core.exceptions import ConfigurationError, InitialisationError
from virtual_ecosystem.main import ve_run

from .conftest import log_check, patch_bypass_setup, patch_run_update
from .conftest import log_check

INITIALISATION_LOG = [
(INFO, "Initialising models: soil"),
Expand Down Expand Up @@ -100,23 +100,18 @@ def test_initialise_models(
core_components = CoreComponents(config)
caplog.clear()

with (
patch_run_update("soil"),
patch_bypass_setup("soil") as mock_bypass_setup,
):
mock_bypass_setup.return_value = False
with raises:
models = initialise_models(
config=config,
data=dummy_carbon_data,
core_components=core_components,
models=config.model_classes,
)

if output is None:
assert models == [None]
else:
assert repr(models["soil"]) == output
with raises:
models = initialise_models(
config=config,
data=dummy_carbon_data,
core_components=core_components,
models=config.model_classes,
)

if output is None:
assert models == [None]
else:
assert repr(models["soil"]) == output

log_check(caplog, expected_log_entries)

Expand Down

0 comments on commit 5d3a200

Please sign in to comment.