Skip to content

Commit

Permalink
Change logging mocks to pytest caplog fixture
Browse files Browse the repository at this point in the history
Replace unittest.mock.patch decorator with pytest caplog fixture for tests of
logging.

Test suite maintenance re: issue #82.
  • Loading branch information
douglatornell committed Mar 29, 2024
1 parent 5f166c8 commit 459d677
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
3 changes: 1 addition & 2 deletions nowcast/workers/make_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ def success(parsed_args):
def failure(parsed_args):
logger.critical(
f"{parsed_args.model} {parsed_args.plot_type} plots failed for "
f'{parsed_args.run_date.format("YYYY-MM-DD")} {parsed_args.run_type} '
f"failed"
f'{parsed_args.run_date.format("YYYY-MM-DD")} {parsed_args.run_type}'
)
msg_type = (
f"failure {parsed_args.model} {parsed_args.run_type} "
Expand Down
29 changes: 22 additions & 7 deletions tests/workers/test_make_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

"""Unit tests for SalishSeaCast make_plots worker.
"""
import logging
from types import SimpleNamespace
from unittest.mock import patch

import arrow
import nemo_nowcast
Expand Down Expand Up @@ -162,19 +162,27 @@ def test_message_types(self, msg, prod_config):
("wwatch3", "forecast2", "publish"),
],
)
@patch("nowcast.workers.make_plots.logger", autospec=True)
class TestSuccess:
"""Unit tests for success() function."""

def test_success(self, m_logger, model, run_type, plot_type):
def test_success(self, model, run_type, plot_type, caplog):
parsed_args = SimpleNamespace(
model=model,
run_type=run_type,
plot_type=plot_type,
run_date=arrow.get("2017-01-02"),
)
caplog.set_level(logging.DEBUG)

msg_type = make_plots.success(parsed_args)
assert m_logger.info.called

assert caplog.records[0].levelname == "INFO"
expected = (
f"{parsed_args.model} {parsed_args.plot_type} plots for "
f'{parsed_args.run_date.format("YYYY-MM-DD")} '
f"{parsed_args.run_type} completed"
)
assert caplog.messages[0] == expected
assert msg_type == f"success {model} {run_type} {plot_type}"


Expand All @@ -195,17 +203,24 @@ def test_success(self, m_logger, model, run_type, plot_type):
("wwatch3", "forecast2", "publish"),
],
)
@patch("nowcast.workers.make_plots.logger", autospec=True)
class TestFailure:
"""Unit tests for failure() function."""

def test_failure(self, m_logger, model, run_type, plot_type):
def test_failure(self, model, run_type, plot_type, caplog):
parsed_args = SimpleNamespace(
model=model,
run_type=run_type,
plot_type=plot_type,
run_date=arrow.get("2017-01-02"),
)
caplog.set_level(logging.DEBUG)

msg_type = make_plots.failure(parsed_args)
assert m_logger.critical.called

assert caplog.records[0].levelname == "CRITICAL"
expected = (
f"{parsed_args.model} {parsed_args.plot_type} plots failed for "
f'{parsed_args.run_date.format("YYYY-MM-DD")} {parsed_args.run_type}'
)
assert caplog.messages[0] == expected
assert msg_type == f"failure {model} {run_type} {plot_type}"

0 comments on commit 459d677

Please sign in to comment.