Skip to content

Commit

Permalink
Change NowcastWorker mock to pytest fixture
Browse files Browse the repository at this point in the history
Test suite maintenance.

re: issue #81
  • Loading branch information
douglatornell committed May 17, 2024
1 parent 32d3a94 commit 81c90c9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 54 deletions.
5 changes: 3 additions & 2 deletions nowcast/workers/make_surface_current_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,6 +77,7 @@ def main():
)

worker.run(make_surface_current_tiles, success, failure)
return worker


def success(parsed_args):
Expand Down
101 changes: 49 additions & 52 deletions tests/workers/test_make_surface_current_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 81c90c9

Please sign in to comment.