Skip to content

Commit

Permalink
Expand validator for log events
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed Dec 15, 2023
1 parent 364fd7b commit 9441a8d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions tests/testing_support/validators/validate_log_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
function_wrapper)
from testing_support.fixtures import catch_background_exceptions

def validate_log_events(events=None, forgone_attrs=None):
events = events or [{}] # Empty event allows assertions based on only forgone attrs to still run and validate
def validate_log_events(events=None, required_attrs=None, forgone_attrs=None):
events = events or [{}] # Empty event allows assertions based on only required or forgone attrs to still run and validate
required_attrs = required_attrs or []
forgone_attrs = forgone_attrs or []

@function_wrapper
Expand Down Expand Up @@ -57,14 +58,14 @@ def _validate_log_events(wrapped, instance, args, kwargs):
matching_log_events = 0
mismatches = []
for captured in logs:
if _check_log_attributes(expected, forgone_attrs, captured, mismatches):
if _check_log_attributes(expected, required_attrs, forgone_attrs, captured, mismatches):
matching_log_events += 1
assert matching_log_events == 1, _log_details(matching_log_events, logs, mismatches)

return val


def _check_log_attributes(expected, forgone_attrs, captured, mismatches):
def _check_log_attributes(expected, required_attrs, forgone_attrs, captured, mismatches):
for key, value in six.iteritems(expected):
if hasattr(captured, key):
captured_value = getattr(captured, key, None)
Expand All @@ -79,6 +80,11 @@ def _check_log_attributes(expected, forgone_attrs, captured, mismatches):
mismatches.append("key: %s, value:<%s><%s>" % (key, value, captured_value))
return False

for key in required_attrs:
if not hasattr(captured, key) and key not in captured.attributes:
mismatches.append("required_key: %s" % key)
return False

for key in forgone_attrs:
if hasattr(captured, key):
mismatches.append("forgone_key: %s, value:<%s>" % (key, getattr(captured, key, None)))
Expand Down

0 comments on commit 9441a8d

Please sign in to comment.