Skip to content

Commit 5725ccd

Browse files
committed
Simplify tests to address pylint issue and reformat with ruff.
1 parent d40ab67 commit 5725ccd

File tree

1 file changed

+56
-101
lines changed

1 file changed

+56
-101
lines changed

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_exporter_mixin.py

Lines changed: 56 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -537,111 +537,66 @@ def test_permanent_failure(self):
537537
"Failed to export traces to localhost:4317, error code: StatusCode.ALREADY_EXISTS",
538538
)
539539

540-
@patch.dict("os.environ", {}, clear=True)
541540
@patch("logging.Logger.debug")
542-
def test_does_not_record_partial_success_if_log_level_unset(
541+
def test_records_partial_success_if_log_level_enabled(
543542
self, mock_logger_debug
544543
):
545-
exporter = OTLPSpanExporterForTesting(insecure=True)
546-
# pylint: disable=protected-access
547-
exporter._client = Mock()
548-
exporter._client.Export.return_value = ExportTraceServiceResponse(
549-
partial_success=ExportTracePartialSuccess(
550-
rejected_spans=1,
551-
error_message="Span dropped",
552-
)
553-
)
554-
exporter.export([self.span])
555-
mock_logger_debug.assert_not_called()
556-
557-
@patch.dict("os.environ", {OTEL_LOG_LEVEL: "off"})
558-
@patch("logging.Logger.debug")
559-
def test_does_not_record_partial_success_if_log_level_off(
560-
self, mock_logger_debug
561-
):
562-
exporter = OTLPSpanExporterForTesting(insecure=True)
563-
# pylint: disable=protected-access
564-
exporter._client = Mock()
565-
exporter._client.Export.return_value = ExportTraceServiceResponse(
566-
partial_success=ExportTracePartialSuccess(
567-
rejected_spans=1,
568-
error_message="Span dropped",
569-
)
570-
)
571-
exporter.export([self.span])
572-
mock_logger_debug.assert_not_called()
573-
574-
@patch.dict("os.environ", {OTEL_LOG_LEVEL: "error"})
575-
@patch("logging.Logger.debug")
576-
def test_does_not_record_partial_success_if_log_level_error(
577-
self, mock_logger_debug
578-
):
579-
exporter = OTLPSpanExporterForTesting(insecure=True)
580-
# pylint: disable=protected-access
581-
exporter._client = Mock()
582-
exporter._client.Export.return_value = ExportTraceServiceResponse(
583-
partial_success=ExportTracePartialSuccess(
584-
rejected_spans=1,
585-
error_message="Span dropped",
586-
)
587-
)
588-
exporter.export([self.span])
589-
mock_logger_debug.assert_not_called()
590-
591-
@patch.dict("os.environ", {OTEL_LOG_LEVEL: "verbose"})
592-
@patch("logging.Logger.debug")
593-
def test_records_partial_success_if_log_level_verbose(
594-
self, mock_logger_debug
595-
):
596-
exporter = OTLPSpanExporterForTesting(insecure=True)
597-
# pylint: disable=protected-access
598-
exporter._client = Mock()
599-
partial_success = ExportTracePartialSuccess(
600-
rejected_spans=1,
601-
error_message="Span dropped",
602-
)
603-
exporter._client.Export.return_value = ExportTraceServiceResponse(
604-
partial_success=partial_success
605-
)
606-
exporter.export([self.span])
607-
mock_logger_debug.assert_called_once_with(
608-
"Partial success:\n%s", partial_success
609-
)
610-
611-
@patch.dict("os.environ", {OTEL_LOG_LEVEL: "debug"})
612-
@patch("logging.Logger.debug")
613-
def test_records_partial_success_if_log_level_debug(
614-
self, mock_logger_debug
615-
):
616-
exporter = OTLPSpanExporterForTesting(insecure=True)
617-
# pylint: disable=protected-access
618-
exporter._client = Mock()
619-
partial_success = ExportTracePartialSuccess(
620-
rejected_spans=1,
621-
error_message="Span dropped",
622-
)
623-
exporter._client.Export.return_value = ExportTraceServiceResponse(
624-
partial_success=partial_success
625-
)
626-
exporter.export([self.span])
627-
mock_logger_debug.assert_called_once_with(
628-
"Partial success:\n%s", partial_success
629-
)
544+
test_cases = ["verbose", "debug"]
545+
546+
for log_level_value in test_cases:
547+
with self.subTest(name=f"log_level_{log_level_value}"):
548+
with patch.dict(
549+
"os.environ",
550+
{OTEL_LOG_LEVEL: log_level_value},
551+
clear=True,
552+
):
553+
exporter = OTLPSpanExporterForTesting(insecure=True)
554+
# pylint: disable=protected-access
555+
exporter._client = Mock()
556+
partial_success = ExportTracePartialSuccess(
557+
rejected_spans=1,
558+
error_message="Span dropped",
559+
)
560+
exporter._client.Export.return_value = (
561+
ExportTraceServiceResponse(
562+
partial_success=partial_success
563+
)
564+
)
565+
exporter.export([self.span])
566+
567+
mock_logger_debug.assert_called_once_with(
568+
"Partial success:\n%s", partial_success
569+
)
570+
mock_logger_debug.reset_mock()
630571

631-
@patch.dict("os.environ", {OTEL_LOG_LEVEL: "info"})
632572
@patch("logging.Logger.debug")
633-
def test_does_not_record_partial_success_if_log_level_info(
573+
def test_does_not_record_partial_success_if_log_level_disabled(
634574
self, mock_logger_debug
635575
):
636-
exporter = OTLPSpanExporterForTesting(insecure=True)
637-
# pylint: disable=protected-access
638-
exporter._client = Mock()
639-
partial_success = ExportTracePartialSuccess(
640-
rejected_spans=1,
641-
error_message="Span dropped",
642-
)
643-
exporter._client.Export.return_value = ExportTraceServiceResponse(
644-
partial_success=partial_success
645-
)
646-
exporter.export([self.span])
647-
mock_logger_debug.assert_not_called()
576+
test_cases = [None, "off", "error", "info"]
577+
578+
for log_level_value in test_cases:
579+
with self.subTest(name=f"log_level_{log_level_value or 'unset'}"):
580+
with patch.dict(
581+
"os.environ",
582+
{OTEL_LOG_LEVEL: log_level_value}
583+
if log_level_value is not None
584+
else {},
585+
clear=True,
586+
):
587+
exporter = OTLPSpanExporterForTesting(insecure=True)
588+
# pylint: disable=protected-access
589+
exporter._client = Mock()
590+
partial_success = ExportTracePartialSuccess(
591+
rejected_spans=1,
592+
error_message="Span dropped",
593+
)
594+
exporter._client.Export.return_value = (
595+
ExportTraceServiceResponse(
596+
partial_success=partial_success
597+
)
598+
)
599+
exporter.export([self.span])
600+
601+
mock_logger_debug.assert_not_called()
602+
mock_logger_debug.reset_mock()

0 commit comments

Comments
 (0)