Skip to content

Commit 9441a8d

Browse files
committed
Expand validator for log events
1 parent 364fd7b commit 9441a8d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tests/testing_support/validators/validate_log_events.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
function_wrapper)
2121
from testing_support.fixtures import catch_background_exceptions
2222

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

2728
@function_wrapper
@@ -57,14 +58,14 @@ def _validate_log_events(wrapped, instance, args, kwargs):
5758
matching_log_events = 0
5859
mismatches = []
5960
for captured in logs:
60-
if _check_log_attributes(expected, forgone_attrs, captured, mismatches):
61+
if _check_log_attributes(expected, required_attrs, forgone_attrs, captured, mismatches):
6162
matching_log_events += 1
6263
assert matching_log_events == 1, _log_details(matching_log_events, logs, mismatches)
6364

6465
return val
6566

6667

67-
def _check_log_attributes(expected, forgone_attrs, captured, mismatches):
68+
def _check_log_attributes(expected, required_attrs, forgone_attrs, captured, mismatches):
6869
for key, value in six.iteritems(expected):
6970
if hasattr(captured, key):
7071
captured_value = getattr(captured, key, None)
@@ -79,6 +80,11 @@ def _check_log_attributes(expected, forgone_attrs, captured, mismatches):
7980
mismatches.append("key: %s, value:<%s><%s>" % (key, value, captured_value))
8081
return False
8182

83+
for key in required_attrs:
84+
if not hasattr(captured, key) and key not in captured.attributes:
85+
mismatches.append("required_key: %s" % key)
86+
return False
87+
8288
for key in forgone_attrs:
8389
if hasattr(captured, key):
8490
mismatches.append("forgone_key: %s, value:<%s>" % (key, getattr(captured, key, None)))

0 commit comments

Comments
 (0)