From 189982718a827956eaf35675f81a0884e8d02e82 Mon Sep 17 00:00:00 2001 From: Uma Annamalai Date: Wed, 25 Sep 2024 13:20:18 -0700 Subject: [PATCH] Linting in tests. --- tests/logger_loguru/test_local_decorating.py | 15 +++++--- tests/logger_loguru/test_log_forwarding.py | 30 ++++++++++++---- tests/logger_loguru/test_metrics.py | 7 ++-- tests/logger_loguru/test_settings.py | 36 +++++++++++++------- 4 files changed, 61 insertions(+), 27 deletions(-) diff --git a/tests/logger_loguru/test_local_decorating.py b/tests/logger_loguru/test_local_decorating.py index 5d1273a92a..846ab93831 100644 --- a/tests/logger_loguru/test_local_decorating.py +++ b/tests/logger_loguru/test_local_decorating.py @@ -20,7 +20,9 @@ from newrelic.api.transaction import current_transaction from testing_support.fixtures import reset_core_stats_engine from testing_support.validators.validate_log_event_count import validate_log_event_count -from testing_support.validators.validate_log_event_count_outside_transaction import validate_log_event_count_outside_transaction +from testing_support.validators.validate_log_event_count_outside_transaction import ( + validate_log_event_count_outside_transaction, +) def set_trace_ids(): @@ -31,6 +33,7 @@ def set_trace_ids(): if trace: trace.guid = "abcdefgh" + def exercise_logging(logger): set_trace_ids() @@ -42,7 +45,9 @@ def get_metadata_string(log_message, is_txn): assert host entity_guid = application_settings().entity_guid if is_txn: - metadata_string = f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28logger_loguru%29|" + metadata_string = ( + f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28logger_loguru%29|" + ) else: metadata_string = f"NR-LINKING|{entity_guid}|{host}|||Python%20Agent%20Test%20%28logger_loguru%29|" formatted_string = f"{log_message} {metadata_string}" @@ -55,7 +60,7 @@ def test_local_log_decoration_inside_transaction(logger): @background_task() def test(): exercise_logging(logger) - assert logger.caplog.records[0] == get_metadata_string('C', True) + assert logger.caplog.records[0] == get_metadata_string("C", True) test() @@ -65,7 +70,7 @@ def test_local_log_decoration_outside_transaction(logger): @validate_log_event_count_outside_transaction(1) def test(): exercise_logging(logger) - assert logger.caplog.records[0] == get_metadata_string('C', False) + assert logger.caplog.records[0] == get_metadata_string("C", False) test() @@ -80,6 +85,6 @@ def patch(record): def test(): patch_logger = logger.patch(patch) exercise_logging(patch_logger) - assert logger.caplog.records[0] == get_metadata_string('C-PATCH', False) + assert logger.caplog.records[0] == get_metadata_string("C-PATCH", False) test() diff --git a/tests/logger_loguru/test_log_forwarding.py b/tests/logger_loguru/test_log_forwarding.py index aa0414da25..2df50db2ad 100644 --- a/tests/logger_loguru/test_log_forwarding.py +++ b/tests/logger_loguru/test_log_forwarding.py @@ -20,7 +20,9 @@ from newrelic.api.transaction import current_transaction from testing_support.fixtures import reset_core_stats_engine from testing_support.validators.validate_log_event_count import validate_log_event_count -from testing_support.validators.validate_log_event_count_outside_transaction import validate_log_event_count_outside_transaction +from testing_support.validators.validate_log_event_count_outside_transaction import ( + validate_log_event_count_outside_transaction, +) from testing_support.validators.validate_log_events import validate_log_events from testing_support.validators.validate_log_events_outside_transaction import validate_log_events_outside_transaction @@ -33,23 +35,33 @@ def set_trace_ids(): if trace: trace.guid = "abcdefgh" + def exercise_logging(logger): set_trace_ids() logger.warning("C") logger.error("D") logger.critical("E") - + assert len(logger.caplog.records) == 3 -_common_attributes_service_linking = {"timestamp": None, "hostname": None, "entity.name": "Python Agent Test (logger_loguru)", "entity.guid": None} -_common_attributes_trace_linking = {"span.id": "abcdefgh", "trace.id": "abcdefgh12345678", **_common_attributes_service_linking} +_common_attributes_service_linking = { + "timestamp": None, + "hostname": None, + "entity.name": "Python Agent Test (logger_loguru)", + "entity.guid": None, +} +_common_attributes_trace_linking = { + "span.id": "abcdefgh", + "trace.id": "abcdefgh12345678", + **_common_attributes_service_linking, +} _test_logging_inside_transaction_events = [ {"message": "C", "level": "WARNING", **_common_attributes_trace_linking}, {"message": "D", "level": "ERROR", **_common_attributes_trace_linking}, - {"message": "E", "level": "CRITICAL", **_common_attributes_trace_linking}, + {"message": "E", "level": "CRITICAL", **_common_attributes_trace_linking}, ] @@ -67,9 +79,10 @@ def test(): _test_logging_outside_transaction_events = [ {"message": "C", "level": "WARNING", **_common_attributes_service_linking}, {"message": "D", "level": "ERROR", **_common_attributes_service_linking}, - {"message": "E", "level": "CRITICAL", **_common_attributes_service_linking}, + {"message": "E", "level": "CRITICAL", **_common_attributes_service_linking}, ] + @reset_core_stats_engine() def test_logging_outside_transaction(logger): @validate_log_events_outside_transaction(_test_logging_outside_transaction_events) @@ -96,6 +109,7 @@ def test(): _test_patcher_application_captured_event = {"message": "C-PATCH", "level": "WARNING"} _test_patcher_application_captured_event.update(_common_attributes_trace_linking) + @reset_core_stats_engine() def test_patcher_application_captured(logger): def patch(record): @@ -112,9 +126,11 @@ def test(): test() + _test_logger_catch_event = {"level": "ERROR"} # Message varies wildly, can't be included in test _test_logger_catch_event.update(_common_attributes_trace_linking) + @reset_core_stats_engine() def test_logger_catch(logger): @validate_log_events([_test_logger_catch_event]) @@ -132,5 +148,5 @@ def throw(): throw() except ValueError: pass - + test() diff --git a/tests/logger_loguru/test_metrics.py b/tests/logger_loguru/test_metrics.py index 9c02d405ec..b72a7d2f0d 100644 --- a/tests/logger_loguru/test_metrics.py +++ b/tests/logger_loguru/test_metrics.py @@ -14,7 +14,9 @@ from newrelic.api.background_task import background_task from testing_support.fixtures import reset_core_stats_engine -from testing_support.validators.validate_custom_metrics_outside_transaction import validate_custom_metrics_outside_transaction +from testing_support.validators.validate_custom_metrics_outside_transaction import ( + validate_custom_metrics_outside_transaction, +) from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics @@ -22,7 +24,7 @@ def exercise_logging(logger): logger.warning("C") logger.error("D") logger.critical("E") - + assert len(logger.caplog.records) == 3 @@ -33,6 +35,7 @@ def exercise_logging(logger): ("Logging/lines/CRITICAL", 1), ] + @reset_core_stats_engine() def test_logging_metrics_inside_transaction(logger): @validate_transaction_metrics( diff --git a/tests/logger_loguru/test_settings.py b/tests/logger_loguru/test_settings.py index 76bf5a1d0c..9d10056301 100644 --- a/tests/logger_loguru/test_settings.py +++ b/tests/logger_loguru/test_settings.py @@ -22,12 +22,15 @@ from testing_support.fixtures import override_application_settings from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics + def get_metadata_string(log_message, is_txn): host = platform.uname().node assert host entity_guid = application_settings().entity_guid if is_txn: - metadata_string = f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28internal_logging%29|" + metadata_string = ( + f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28internal_logging%29|" + ) else: metadata_string = f"NR-LINKING|{entity_guid}|{host}|||Python%20Agent%20Test%20%28internal_logging%29|" formatted_string = f"{log_message} {metadata_string}" @@ -49,10 +52,12 @@ def basic_logging(logger): @pytest.mark.parametrize("feature_setting,subfeature_setting,expected", _settings_matrix) @reset_core_stats_engine() def test_log_forwarding_settings(logger, feature_setting, subfeature_setting, expected): - @override_application_settings({ - "application_logging.enabled": feature_setting, - "application_logging.forwarding.enabled": subfeature_setting, - }) + @override_application_settings( + { + "application_logging.enabled": feature_setting, + "application_logging.forwarding.enabled": subfeature_setting, + } + ) @validate_log_event_count(1 if expected else 0) @background_task() def test(): @@ -65,10 +70,12 @@ def test(): @pytest.mark.parametrize("feature_setting,subfeature_setting,expected", _settings_matrix) @reset_core_stats_engine() def test_local_decorating_settings(logger, feature_setting, subfeature_setting, expected): - @override_application_settings({ - "application_logging.enabled": feature_setting, - "application_logging.local_decorating.enabled": subfeature_setting, - }) + @override_application_settings( + { + "application_logging.enabled": feature_setting, + "application_logging.local_decorating.enabled": subfeature_setting, + } + ) @background_task() def test(): basic_logging(logger) @@ -86,10 +93,13 @@ def test(): @reset_core_stats_engine() def test_log_metrics_settings(logger, feature_setting, subfeature_setting, expected): metric_count = 1 if expected else None - @override_application_settings({ - "application_logging.enabled": feature_setting, - "application_logging.metrics.enabled": subfeature_setting, - }) + + @override_application_settings( + { + "application_logging.enabled": feature_setting, + "application_logging.metrics.enabled": subfeature_setting, + } + ) @validate_transaction_metrics( "test_settings:test_log_metrics_settings..test", custom_metrics=[