Skip to content

Commit

Permalink
Change NowcastWorker mock to pytest fixture
Browse files Browse the repository at this point in the history
re: issue #81
  • Loading branch information
douglatornell committed Oct 5, 2022
1 parent 104c834 commit d386e82
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 51 deletions.
7 changes: 4 additions & 3 deletions nowcast/workers/update_forecast_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""SalishSeaCast worker that builds a new directory of symlinks to model
results files for the rolling forecast datasets and replaces the previous
rolling forecast directory with the new one.
"""SalishSeaCast worker that builds a new directory of symlinks to model results files
for the rolling forecast datasets and replaces the previous rolling forecast directory
with the new one.
"""
import logging
import os
Expand Down Expand Up @@ -64,6 +64,7 @@ def main():
help="Date of the run to update rolling forecast datasets for.",
)
worker.run(update_forecast_datasets, success, failure)
return worker


def success(parsed_args):
Expand Down
85 changes: 37 additions & 48 deletions tests/workers/test_update_forecast_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import textwrap
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import call, Mock, patch
from unittest.mock import call, patch

import arrow
import nemo_nowcast
Expand Down Expand Up @@ -73,56 +73,45 @@ def config(base_config):
return config_


@patch("nowcast.workers.update_forecast_datasets.NowcastWorker", spec=True)
@pytest.fixture
def mock_worker(mock_nowcast_worker, monkeypatch):
monkeypatch.setattr(update_forecast_datasets, "NowcastWorker", mock_nowcast_worker)


class TestMain:
"""Unit tests for main() function."""

def test_instantiate_worker(self, m_worker):
m_worker().cli = Mock(name="cli")
update_forecast_datasets.main()
args, kwargs = m_worker.call_args
assert args == ("update_forecast_datasets",)
assert "description" in kwargs

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

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

def test_add_run_type_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
update_forecast_datasets.main()
args, kwargs = m_worker().cli.add_argument.call_args_list[1]
assert args == ("run_type",)
assert kwargs["choices"] == {"forecast", "forecast2"}
assert "help" in kwargs

def test_add_data_date_arg(self, m_worker):
m_worker().cli = Mock(name="cli")
update_forecast_datasets.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_run_worker(self, m_worker):
m_worker().cli = Mock(name="cli")
update_forecast_datasets.main()
args, kwargs = m_worker().run.call_args
expected = (
update_forecast_datasets.update_forecast_datasets,
update_forecast_datasets.success,
update_forecast_datasets.failure,
)
assert args == expected
def test_instantiate_worker(self, mock_worker):
worker = update_forecast_datasets.main()
assert worker.name == "update_forecast_datasets"
assert worker.description.startswith(
"SalishSeaCast worker that builds a new directory of symlinks to model results files"
)

def test_add_model_arg(self, mock_worker):
worker = update_forecast_datasets.main()
assert worker.cli.parser._actions[3].dest == "model"
assert worker.cli.parser._actions[3].choices == {"fvcom", "nemo", "wwatch3"}
assert worker.cli.parser._actions[3].help

def test_add_run_type_arg(self, mock_worker):
worker = update_forecast_datasets.main()
assert worker.cli.parser._actions[4].dest == "run_type"
assert worker.cli.parser._actions[4].choices == {"forecast", "forecast2"}
assert worker.cli.parser._actions[4].help

def test_add_data_date_arg(self, mock_worker, monkeypatch):
def mock_now():
return arrow.get("2022-10-04 14:50:43")

monkeypatch.setattr(update_forecast_datasets.arrow, "now", mock_now)

worker = update_forecast_datasets.main()
assert worker.cli.parser._actions[5].dest == "run_date"
expected = nemo_nowcast.cli.CommandLineInterface.arrow_date
assert worker.cli.parser._actions[5].type == expected
assert worker.cli.parser._actions[5].default == arrow.get("2022-10-04")
assert worker.cli.parser._actions[5].help


class TestConfig:
Expand Down

0 comments on commit d386e82

Please sign in to comment.