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 Sep 2, 2024
1 parent c79afb8 commit 5613d98
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions tests/workers/test_make_CHS_currents_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

"""Unit tests for SalishSeaCast make_CHS_currents_file worker.
"""
import logging
import textwrap
from pathlib import Path
from types import SimpleNamespace
Expand Down Expand Up @@ -148,30 +149,38 @@ def test_file_group(self, prod_config):


@pytest.mark.parametrize("run_type", ["nowcast", "forecast", "forecast2"])
@patch("nowcast.workers.make_CHS_currents_file.logger", autospec=True)
class TestSuccess:
"""Unit tests for success() function."""

def test_success(self, m_logger, run_type):
parsed_args = SimpleNamespace(
run_type=run_type, run_date=arrow.get("2018-09-01")
)
def test_success(self, run_type, caplog):
run_date = arrow.get("2018-09-01")
parsed_args = SimpleNamespace(run_type=run_type, run_date=run_date)
caplog.set_level(logging.DEBUG)

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

assert caplog.records[0].levelname == "INFO"
expected = (
f"Made CHS currents file for {run_date.format("YYYY-MM-DD")} for {run_type}"
)
assert caplog.records[0].message == expected
assert msg_type == f"success {run_type}"


@pytest.mark.parametrize("run_type", ["nowcast", "forecast", "forecast2"])
@patch("nowcast.workers.make_CHS_currents_file.logger", autospec=True)
class TestFailure:
"""Unit tests for failure() function."""

def test_failure(self, m_logger, run_type):
parsed_args = SimpleNamespace(
run_type=run_type, run_date=arrow.get("2018-09-01")
)
def test_failure(self, run_type, caplog):
run_date = arrow.get("2018-09-01")
parsed_args = SimpleNamespace(run_type=run_type, run_date=run_date)
caplog.set_level(logging.DEBUG)

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

assert caplog.records[0].levelname == "CRITICAL"
expected = f"Making CHS currents file for {run_date.format("YYYY-MM-DD")} failed for {run_type}"
assert caplog.records[0].message == expected
assert msg_type == f"failure {run_type}"


Expand All @@ -189,7 +198,9 @@ class TestMakeCHSCurrentsFile:
"""Unit tests for make_CHS_currents_function."""

@pytest.mark.parametrize("run_type", ["nowcast", "forecast", "forecast2"])
def test_checklist(self, m_fix_perms, m_write_ncdf, m_read_aur, run_type, config):
def test_checklist(
self, m_fix_perms, m_write_ncdf, m_read_aur, run_type, config, caplog
):
parsed_args = SimpleNamespace(
run_type=run_type, run_date=arrow.get("2018-09-01")
)
Expand Down Expand Up @@ -230,6 +241,7 @@ def test_read_avg_unstagger_rotatehecklist(
ufile,
vfile,
config,
caplog,
):
parsed_args = SimpleNamespace(
run_type=run_type, run_date=arrow.get("2018-09-01")
Expand Down

0 comments on commit 5613d98

Please sign in to comment.