Skip to content

Commit

Permalink
Modernize test_make_live_ocean_files (#288)
Browse files Browse the repository at this point in the history
* Change NowcastWorker mock to pytest fixture

Test suite maintenance.

re: issue #81

* Replace patch w/ pytest.fixture in test_make_live_ocean_files

Replaced `patch` decorators with a pytest fixture to mock
`create_LiveOcean_TS_BCs`. This improves test clarity and ensures the mock is
applied only where necessary. Also adjusted test assertions to use the correct
filepath.
  • Loading branch information
douglatornell authored Jul 26, 2024
1 parent f0cf475 commit 189a9e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
1 change: 1 addition & 0 deletions nowcast/workers/make_live_ocean_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def main():
""",
)
worker.run(make_live_ocean_files, success, failure)
return worker


def success(parsed_args):
Expand Down
90 changes: 46 additions & 44 deletions tests/workers/test_make_live_ocean_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import textwrap
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock, patch

import arrow
import nemo_nowcast
Expand Down Expand Up @@ -54,40 +53,29 @@ def config(base_config):
return config_


@patch("nowcast.workers.make_live_ocean_files.NowcastWorker", spec=True)
@pytest.fixture
def mock_worker(mock_nowcast_worker, monkeypatch):
monkeypatch.setattr(make_live_ocean_files, "NowcastWorker", mock_nowcast_worker)


class TestMain:
"""Unit tests for main() function."""

def test_instantiate_worker(self, m_worker):
m_worker().cli = Mock(name="cli")
make_live_ocean_files.main()
args, kwargs = m_worker.call_args
assert args == ("make_live_ocean_files",)
assert "description" in kwargs

def test_init_cli(self, m_worker):
m_worker().cli = Mock(name="cli")
make_live_ocean_files.main()
m_worker().init_cli.assert_called_once_with()

def test_add_run_date_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
make_live_ocean_files.main()
args, kwargs = m_worker().cli.add_date_option.call_args_list[0]
assert args == ("--run-date",)
assert kwargs["default"] == arrow.now().floor("day")
assert "help" in kwargs

def test_run_worker(self, m_worker):
m_worker().cli = Mock(name="cli")
make_live_ocean_files.main()
args, kwargs = m_worker().run.call_args
expected = (
make_live_ocean_files.make_live_ocean_files,
make_live_ocean_files.success,
make_live_ocean_files.failure,
def test_instantiate_worker(self, mock_worker):
worker = make_live_ocean_files.main()
assert worker.name == "make_live_ocean_files"
assert worker.description.startswith(
"SalishSeaCast nowcast worker that produces hourly temperature and salinity"
)
assert args == expected

def test_add_run_date_option(self, mock_worker):
worker = make_live_ocean_files.main()
assert worker.cli.parser._actions[3].dest == "run_date"
expected = nemo_nowcast.cli.CommandLineInterface.arrow_date
assert worker.cli.parser._actions[3].type == expected
expected = arrow.now().floor("day")
assert worker.cli.parser._actions[3].default == expected
assert worker.cli.parser._actions[3].help


class TestConfig:
Expand Down Expand Up @@ -166,32 +154,46 @@ def test_failure(self, caplog):
assert msg_type == "failure"


@patch(
"nowcast.workers.make_live_ocean_files.LiveOcean_parameters.set_parameters",
autospec=True,
)
@patch("nowcast.workers.make_live_ocean_files.create_LiveOcean_TS_BCs", spec=True)
class TestMakeLiveOceanFiles:
"""Unit test for make_live_ocean_files() function."""

def test_checklist(self, m_create_ts, m_set_params, config, caplog):
run_date = arrow.get("2020-02-15")
@staticmethod
@pytest.fixture
def mock_create_LiveOcean_TS_BCs(config, monkeypatch):
def _mock_create_LiveOcean_TS_BCs(
date, file_template, meshfilename, bc_dir, LO_dir, LO_to_SSC_parameters
):
return (
"forcing/LiveOcean/boundary_conditions/LiveOcean_v201905_y2024m07d26.nc"
)

monkeypatch.setattr(
make_live_ocean_files,
"create_LiveOcean_TS_BCs",
_mock_create_LiveOcean_TS_BCs,
)

def test_checklist(self, mock_create_LiveOcean_TS_BCs, config, caplog):
run_date = arrow.get("2024-07-26")
parsed_args = SimpleNamespace(run_date=run_date)
filename = config["temperature salinity"]["file template"].format(
run_date.datetime
)
m_create_ts.return_value = filename
bc_dir = config["temperature salinity"]["bc dir"]
filepath = f"{bc_dir}/{filename}"
caplog.set_level(logging.DEBUG)

checklist = make_live_ocean_files.make_live_ocean_files(parsed_args, config)
assert checklist == {"temperature & salinity": filename}
assert checklist == {"temperature & salinity": filepath}

def test_log_messages(self, m_create_ts, m_set_params, config, caplog):
run_date = arrow.get("2019-02-15")
def test_log_messages(self, mock_create_LiveOcean_TS_BCs, config, caplog):
run_date = arrow.get("2024-07-26")
parsed_args = SimpleNamespace(run_date=run_date)
filename = config["temperature salinity"]["file template"].format(
run_date.datetime
)
m_create_ts.return_value = filename
bc_dir = config["temperature salinity"]["bc dir"]
filepath = f"{bc_dir}/{filename}"
caplog.set_level(logging.DEBUG)

make_live_ocean_files.make_live_ocean_files(parsed_args, config)
Expand All @@ -204,5 +206,5 @@ def test_log_messages(self, m_create_ts, m_set_params, config, caplog):
assert caplog.records[1].levelname == "INFO"
assert (
caplog.messages[1]
== f"Stored T&S western boundary conditions file: {filename}"
== f"Stored T&S western boundary conditions file: {filepath}"
)

0 comments on commit 189a9e3

Please sign in to comment.