From 81c90c9632220ba7f2111b43655cd85bf6fca46f Mon Sep 17 00:00:00 2001 From: Doug Latornell Date: Fri, 17 May 2024 13:00:53 -0700 Subject: [PATCH] Change NowcastWorker mock to pytest fixture Test suite maintenance. re: issue #81 --- nowcast/workers/make_surface_current_tiles.py | 5 +- .../test_make_surface_current_tiles.py | 101 +++++++++--------- 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/nowcast/workers/make_surface_current_tiles.py b/nowcast/workers/make_surface_current_tiles.py index 9c592d30..e30731df 100644 --- a/nowcast/workers/make_surface_current_tiles.py +++ b/nowcast/workers/make_surface_current_tiles.py @@ -13,9 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. """SalishSeaCast worker that produces tiles of surface current visualization -images for the web site from run results. +images for the website from run results. -The tile specifications and initial code implementation were provided by IOS. +IOS provided the tile specifications and initial code implementation. """ import datetime import logging @@ -77,6 +77,7 @@ def main(): ) worker.run(make_surface_current_tiles, success, failure) + return worker def success(parsed_args): diff --git a/tests/workers/test_make_surface_current_tiles.py b/tests/workers/test_make_surface_current_tiles.py index 1fd15f76..84c3c051 100644 --- a/tests/workers/test_make_surface_current_tiles.py +++ b/tests/workers/test_make_surface_current_tiles.py @@ -21,7 +21,7 @@ from pathlib import Path import textwrap from types import SimpleNamespace -from unittest.mock import Mock, patch +from unittest.mock import patch import arrow import nemo_nowcast @@ -72,62 +72,59 @@ def config(base_config): return config_ -@patch("nowcast.workers.make_surface_current_tiles.NowcastWorker", spec=True) +@pytest.fixture +def mock_worker(mock_nowcast_worker, monkeypatch): + monkeypatch.setattr( + make_surface_current_tiles, "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_surface_current_tiles.main() - args, kwargs = m_worker.call_args - assert args == ("make_surface_current_tiles",) - assert list(kwargs.keys()) == ["description"] - - def test_init_cli(self, m_worker): - m_worker().cli = Mock(name="cli") - make_surface_current_tiles.main() - m_worker().init_cli.assert_called_once_with() - - def test_add_run_type_arg(self, m_worker): - m_worker().cli = Mock(name="cli") - make_surface_current_tiles.main() - args, kwargs = m_worker().cli.add_argument.call_args_list[0] - assert args == ("run_type",) - assert kwargs["choices"] == {"nowcast-green", "forecast", "forecast2"} - assert "help" in kwargs - - def test_add_run_date_option(self, m_worker): - m_worker().cli = Mock(name="cli") - make_surface_current_tiles.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 - - @patch( - "nowcast.workers.make_surface_current_tiles.multiprocessing.cpu_count", - return_value=12, - autospec=True, - ) - def test_add_nprocs_arg(self, m_cpu_count, m_worker): - m_worker().cli = Mock(name="cli") - make_surface_current_tiles.main() - args, kwargs = m_worker().cli.add_argument.call_args_list[1] - assert args == ("--nprocs",) - assert kwargs["type"] == int - assert kwargs["default"] == 6 - assert "help" in kwargs - - def test_run_worker(self, m_worker): - m_worker().cli = Mock(name="cli") - make_surface_current_tiles.main() - args, kwargs = m_worker().run.call_args - assert args == ( - make_surface_current_tiles.make_surface_current_tiles, - make_surface_current_tiles.success, - make_surface_current_tiles.failure, + def test_instantiate_worker(self, mock_worker): + worker = make_surface_current_tiles.main() + + assert worker.name == "make_surface_current_tiles" + assert worker.description.startswith( + "SalishSeaCast worker that produces tiles of surface current visualization" + ) + + def test_add_run_type_arg(self, mock_worker): + worker = make_surface_current_tiles.main() + + assert worker.cli.parser._actions[3].dest == "run_type" + assert worker.cli.parser._actions[3].choices == { + "nowcast-green", + "forecast", + "forecast2", + } + assert worker.cli.parser._actions[3].help + + def test_add_run_date_option(self, mock_worker): + worker = make_surface_current_tiles.main() + + assert worker.cli.parser._actions[4].dest == "run_date" + expected = nemo_nowcast.cli.CommandLineInterface.arrow_date + assert worker.cli.parser._actions[4].type == expected + assert worker.cli.parser._actions[4].default == arrow.now().floor("day") + assert worker.cli.parser._actions[4].help + + def test_add_nprocs_option(self, mock_worker, monkeypatch): + def mock_cpu_count(): + return 12 + + monkeypatch.setattr( + make_surface_current_tiles.multiprocessing, "cpu_count", mock_cpu_count ) + worker = make_surface_current_tiles.main() + + assert worker.cli.parser._actions[5].dest == "nprocs" + assert worker.cli.parser._actions[5].type == int + assert worker.cli.parser._actions[5].default == 6 + assert worker.cli.parser._actions[5].help + class TestConfig: """Unit tests for production YAML config file elements related to worker."""