Skip to content

Commit

Permalink
Replace patch w/ pytest.fixture in test_make_live_ocean_files
Browse files Browse the repository at this point in the history
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 committed Jul 26, 2024
1 parent 747676f commit e31ba3b
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 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 patch

import arrow
import nemo_nowcast
Expand Down Expand Up @@ -155,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 @@ -193,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 e31ba3b

Please sign in to comment.