From e31ba3b13309b7be3be4579ba7efbfe3eae270bc Mon Sep 17 00:00:00 2001 From: Doug Latornell Date: Fri, 26 Jul 2024 12:10:10 -0700 Subject: [PATCH] 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. --- tests/workers/test_make_live_ocean_files.py | 41 ++++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/tests/workers/test_make_live_ocean_files.py b/tests/workers/test_make_live_ocean_files.py index 0d9b692b..9f722501 100644 --- a/tests/workers/test_make_live_ocean_files.py +++ b/tests/workers/test_make_live_ocean_files.py @@ -22,7 +22,6 @@ import textwrap from pathlib import Path from types import SimpleNamespace -from unittest.mock import patch import arrow import nemo_nowcast @@ -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) @@ -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}" )