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 Mar 29, 2024
1 parent 2bdd7ca commit 3c3bd4d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 58 deletions.
1 change: 1 addition & 0 deletions nowcast/workers/make_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def main():
""",
)
worker.run(make_plots, success, failure)
return worker


def success(parsed_args):
Expand Down
112 changes: 54 additions & 58 deletions tests/workers/test_make_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"""Unit tests for SalishSeaCast make_plots worker.
"""
from types import SimpleNamespace
from unittest.mock import Mock, patch
from unittest.mock import patch

import arrow
import nemo_nowcast
import pytest

from nowcast.workers import make_plots
Expand All @@ -33,36 +34,34 @@ def config(base_config):
return base_config


@patch("nowcast.workers.make_plots.NowcastWorker", spec=True)
@pytest.fixture
def mock_worker(mock_nowcast_worker, monkeypatch):
monkeypatch.setattr(make_plots, "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_plots.main()
args, kwargs = m_worker.call_args
assert args == ("make_plots",)
assert list(kwargs.keys()) == ["description"]

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

def test_add_model_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
make_plots.main()
args, kwargs = m_worker().cli.add_argument.call_args_list[0]
assert args == ("model",)
assert kwargs["choices"] == {"nemo", "fvcom", "wwatch3"}
assert "help" in kwargs

def test_add_run_type_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
make_plots.main()
args, kwargs = m_worker().cli.add_argument.call_args_list[1]
assert args == ("run_type",)
assert kwargs["choices"] == {
def test_instantiate_worker(self, mock_worker):
worker = make_plots.main()

assert worker.name == "make_plots"
assert worker.description.startswith(
"SalishSeaCast worker that produces visualization images for"
)

def test_add_model_arg(self, mock_worker):
worker = make_plots.main()

assert worker.cli.parser._actions[3].dest == "model"
assert worker.cli.parser._actions[3].choices == {"nemo", "fvcom", "wwatch3"}
assert worker.cli.parser._actions[3].help

def test_add_run_type_arg(self, mock_worker):
worker = make_plots.main()

assert worker.cli.parser._actions[4].dest == "run_type"
assert worker.cli.parser._actions[4].choices == {
"nowcast",
"nowcast-green",
"nowcast-agrif",
Expand All @@ -72,36 +71,33 @@ def test_add_run_type_arg(self, m_worker):
"forecast2",
"forecast-x2",
}
assert "help" in kwargs

def test_add_plot_type_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
make_plots.main()
args, kwargs = m_worker().cli.add_argument.call_args_list[2]
assert args == ("plot_type",)
assert kwargs["choices"] == {"publish", "research", "comparison"}
assert "help" in kwargs

def test_add_run_date_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
make_plots.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_add_test_figure_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
make_plots.main()
args, kwargs = m_worker().cli.add_argument.call_args_list[3]
assert args == ("--test-figure",)
assert "help" in kwargs

def test_run_worker(self, m_worker):
m_worker().cli = Mock(name="cli")
make_plots.main()
args, kwargs = m_worker().run.call_args
assert args == (make_plots.make_plots, make_plots.success, make_plots.failure)
assert worker.cli.parser._actions[4].help

def test_add_plot_type_arg(self, mock_worker):
worker = make_plots.main()

assert worker.cli.parser._actions[5].dest == "plot_type"
assert worker.cli.parser._actions[5].choices == {
"publish",
"research",
"comparison",
}
assert worker.cli.parser._actions[5].help

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

def test_add_test_figure_arg(self, mock_worker):
worker = make_plots.main()

assert worker.cli.parser._actions[7].dest == "test_figure"
assert worker.cli.parser._actions[7].default is None
assert worker.cli.parser._actions[7].help


class TestConfig:
Expand Down

0 comments on commit 3c3bd4d

Please sign in to comment.