20
20
function_wrapper )
21
21
from testing_support .fixtures import catch_background_exceptions
22
22
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 []
25
26
forgone_attrs = forgone_attrs or []
26
27
27
28
@function_wrapper
@@ -57,14 +58,14 @@ def _validate_log_events(wrapped, instance, args, kwargs):
57
58
matching_log_events = 0
58
59
mismatches = []
59
60
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 ):
61
62
matching_log_events += 1
62
63
assert matching_log_events == 1 , _log_details (matching_log_events , logs , mismatches )
63
64
64
65
return val
65
66
66
67
67
- def _check_log_attributes (expected , forgone_attrs , captured , mismatches ):
68
+ def _check_log_attributes (expected , required_attrs , forgone_attrs , captured , mismatches ):
68
69
for key , value in six .iteritems (expected ):
69
70
if hasattr (captured , key ):
70
71
captured_value = getattr (captured , key , None )
@@ -79,6 +80,11 @@ def _check_log_attributes(expected, forgone_attrs, captured, mismatches):
79
80
mismatches .append ("key: %s, value:<%s><%s>" % (key , value , captured_value ))
80
81
return False
81
82
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
+
82
88
for key in forgone_attrs :
83
89
if hasattr (captured , key ):
84
90
mismatches .append ("forgone_key: %s, value:<%s>" % (key , getattr (captured , key , None )))
0 commit comments