Skip to content

Commit

Permalink
💚 Ensure tests have the appropriate log level enforced
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Mar 26, 2024
1 parent d99fca5 commit 4595a48
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/openforms/registrations/tests/test_pre_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.tasks.registration import pre_registration
from openforms.submissions.tests.factories import SubmissionFactory
from openforms.utils.tests.logging import ensure_logger_level


class PreRegistrationTests(TestCase):
Expand Down Expand Up @@ -272,6 +273,7 @@ def test_retry_after_too_many_attempts_skips(self, m_get_solo):
patch(
"openforms.registrations.contrib.zgw_apis.plugin.ZGWRegistration.pre_register_submission"
) as mock_pre_register,
ensure_logger_level("DEBUG"),
self.assertLogs(level="DEBUG") as logs,
):
pre_registration(submission.id, PostSubmissionEvents.on_retry)
Expand Down
2 changes: 2 additions & 0 deletions src/openforms/registrations/tests/test_registration_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from openforms.logging.models import TimelineLogProxy
from openforms.submissions.constants import PostSubmissionEvents, RegistrationStatuses
from openforms.submissions.tests.factories import SubmissionFactory
from openforms.utils.tests.logging import ensure_logger_level

from ..base import BasePlugin
from ..exceptions import RegistrationFailed
Expand Down Expand Up @@ -286,6 +287,7 @@ def register_submission(self, submission, options):

with self.subTest("on retry"):
with (
ensure_logger_level("DEBUG"),
self.assertRaises(RegistrationFailed),
self.assertLogs(level="DEBUG") as logs,
):
Expand Down
2 changes: 2 additions & 0 deletions src/openforms/submissions/tests/test_post_submission_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from openforms.forms.tests.factories import FormDefinitionFactory
from openforms.payments.constants import PaymentStatus
from openforms.payments.tests.factories import SubmissionPaymentFactory
from openforms.utils.tests.logging import ensure_logger_level

from ..constants import PostSubmissionEvents
from ..models import SubmissionReport
Expand Down Expand Up @@ -1058,6 +1059,7 @@ def test_payment_required_and_not_should_wait_for_registration(self):
"openforms.registrations.tasks.GlobalConfiguration.get_solo",
return_value=GlobalConfiguration(wait_for_payment_to_register=True),
),
ensure_logger_level("DEBUG"),
LogCapture() as logs,
):
on_post_submission_event(submission.id, PostSubmissionEvents.on_completion)
Expand Down
22 changes: 22 additions & 0 deletions src/openforms/utils/tests/logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from contextlib import contextmanager
from typing import Iterator

from django.test.utils import TestContextDecorator

Expand All @@ -9,3 +11,23 @@ def enable(self):

def disable(self):
logging.disable(logging.NOTSET)


@contextmanager
def ensure_logger_level(level: str = "INFO", name="openforms") -> Iterator[None]:
"""
Set the (minimum) level for a given logger.
Base or individual settings may tweak the log level to reduce the overload for the
developer, but this affects tests that verify that log records are "written" by
application code for a certain log level.
This context manager allows you to pin the log level for a given text, irrespective
of individual preferences/developer settings.
"""
logger = logging.getLogger(name)
original_level = logger.getEffectiveLevel()

logger.setLevel(logging._nameToLevel[level])
yield
logger.setLevel(original_level)

0 comments on commit 4595a48

Please sign in to comment.