Skip to content

Commit

Permalink
Add test coverage for logging outside flow run (PrefectHQ#8394)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Adkins <michael@prefect.io>
  • Loading branch information
2 people authored and Åsmund Østvold committed May 11, 2023
1 parent 14eb5a7 commit 00c3789
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,19 @@ def test_does_not_send_logs_outside_of_run_context_with_default_setting(
output = capsys.readouterr()
assert output.err == ""

def test_does_not_raise_when_logger_outside_of_run_context_with_default_setting(
self,
logger,
):
with pytest.warns(
UserWarning,
match=(
"Logger 'tests.test_logging' attempted to send logs to the API without"
" a flow run id."
),
):
logger.info("test")

def test_does_not_send_logs_outside_of_run_context_with_error_setting(
self, logger, mock_log_worker, capsys
):
Expand All @@ -526,6 +539,22 @@ def test_does_not_send_logs_outside_of_run_context_with_error_setting(
output = capsys.readouterr()
assert output.err == ""

def test_does_not_warn_when_logger_outside_of_run_context_with_error_setting(
self,
logger,
):
with temporary_settings(
updates={PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW: "error"},
):
with pytest.raises(
MissingContextError,
match=(
"Logger 'tests.test_logging' attempted to send logs to the API"
" without a flow run id."
),
):
logger.info("test")

def test_does_not_send_logs_outside_of_run_context_with_ignore_setting(
self, logger, mock_log_worker, capsys
):
Expand All @@ -540,6 +569,15 @@ def test_does_not_send_logs_outside_of_run_context_with_ignore_setting(
output = capsys.readouterr()
assert output.err == ""

def test_does_not_raise_or_warn_when_logger_outside_of_run_context_with_ignore_setting(
self,
logger,
):
with temporary_settings(
updates={PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW: "ignore"},
):
logger.info("test")

def test_does_not_send_logs_outside_of_run_context_with_warn_setting(
self, logger, mock_log_worker, capsys
):
Expand All @@ -558,6 +596,24 @@ def test_does_not_send_logs_outside_of_run_context_with_warn_setting(
output = capsys.readouterr()
assert output.err == ""

def test_does_not_raise_when_logger_outside_of_run_context_with_warn_setting(
self, logger
):
with temporary_settings(
updates={PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW: "warn"},
):
# NOTE: We use `raises` instead of `warns` because pytest will otherwise
# capture the warning call and skip checing that we use it correctly
# See https://github.com/pytest-dev/pytest/issues/9288
with pytest.raises(
UserWarning,
match=(
"Logger 'tests.test_logging' attempted to send logs to the API"
" without a flow run id."
),
):
logger.info("test")

def test_missing_context_warning_refers_to_caller_lineno(
self, logger, mock_log_worker
):
Expand Down

0 comments on commit 00c3789

Please sign in to comment.