Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for logging outside flow run #8394

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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