Skip to content

Commit

Permalink
Linting in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
umaannamalai committed Sep 25, 2024
1 parent c3184e0 commit 1899827
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
15 changes: 10 additions & 5 deletions tests/logger_loguru/test_local_decorating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -31,6 +33,7 @@ def set_trace_ids():
if trace:
trace.guid = "abcdefgh"


def exercise_logging(logger):
set_trace_ids()

Expand All @@ -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}"
Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()
30 changes: 23 additions & 7 deletions tests/logger_loguru/test_log_forwarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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},
]


Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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])
Expand All @@ -132,5 +148,5 @@ def throw():
throw()
except ValueError:
pass

test()
7 changes: 5 additions & 2 deletions tests/logger_loguru/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@

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


def exercise_logging(logger):
logger.warning("C")
logger.error("D")
logger.critical("E")

assert len(logger.caplog.records) == 3


Expand All @@ -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(
Expand Down
36 changes: 23 additions & 13 deletions tests/logger_loguru/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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():
Expand All @@ -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)
Expand All @@ -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.<locals>.test",
custom_metrics=[
Expand Down

0 comments on commit 1899827

Please sign in to comment.