@@ -10,13 +10,21 @@ def assert_event_equal(
10
10
recorded_event : Event ,
11
11
check_uuid : bool = False ,
12
12
check_datetime : bool = False ,
13
+ exclude_fields : list [str ] | None = None ,
13
14
):
15
+ if type (expected_event ) is not type (recorded_event ):
16
+ raise AssertionError (
17
+ f"Expected event type { type (expected_event )} but got { type (recorded_event )} "
18
+ )
19
+
14
20
assert expected_event .type == recorded_event .type
15
21
for field in fields (expected_event ):
16
22
if field .name == "uuid_" and not check_uuid :
17
23
continue
18
24
if field .name == "datetime_" and not check_datetime :
19
25
continue
26
+ if exclude_fields and field .name in exclude_fields :
27
+ continue
20
28
assert getattr (expected_event , field .name ) == getattr (recorded_event , field .name )
21
29
22
30
@@ -25,28 +33,42 @@ def assert_analytics_events_recorded(
25
33
expected_events : list [Event ],
26
34
check_uuid : bool = False ,
27
35
check_datetime : bool = False ,
36
+ exclude_fields : list [str ] | None = None ,
28
37
):
29
38
recorded_events = [call .args [0 ] for call in mock_record .call_args_list ]
30
39
assert len (expected_events ) == len (recorded_events )
31
40
for expected_event , recorded_event in zip (expected_events , recorded_events ):
32
- assert_event_equal (expected_event , recorded_event , check_uuid , check_datetime )
41
+ assert_event_equal (
42
+ expected_event , recorded_event , check_uuid , check_datetime , exclude_fields
43
+ )
44
+
45
+
46
+ def get_last_analytics_event (mock_record : MagicMock ) -> Event :
47
+ return mock_record .call_args_list [- 1 ].args [0 ]
33
48
34
49
35
50
def assert_last_analytics_event (
36
51
mock_record : MagicMock ,
37
52
expected_event : Event ,
38
53
check_uuid : bool = False ,
39
54
check_datetime : bool = False ,
55
+ exclude_fields : list [str ] | None = None ,
40
56
):
41
- recorded_event = mock_record .call_args_list [- 1 ].args [0 ]
42
- assert_event_equal (expected_event , recorded_event , check_uuid , check_datetime )
57
+ assert_event_equal (
58
+ expected_event ,
59
+ get_last_analytics_event (mock_record ),
60
+ check_uuid ,
61
+ check_datetime ,
62
+ exclude_fields ,
63
+ )
43
64
44
65
45
66
@contextlib .contextmanager
46
67
def assert_analytics_events (
47
68
expected_events : list [Event ],
48
69
check_uuid : bool = False ,
49
70
check_datetime : bool = False ,
71
+ exclude_fields : list [str ] | None = None ,
50
72
):
51
73
"""
52
74
Context manager that allows you to track analytics events recorded during the context.
@@ -58,4 +80,6 @@ def assert_analytics_events(
58
80
"""
59
81
with patch ("sentry.analytics.record" ) as mock_record :
60
82
yield
61
- assert_analytics_events_recorded (mock_record , expected_events , check_uuid , check_datetime )
83
+ assert_analytics_events_recorded (
84
+ mock_record , expected_events , check_uuid , check_datetime , exclude_fields
85
+ )
0 commit comments