Skip to content

Commit e5df1ff

Browse files
test(analytics): add additional assertion helper functions (#95421)
1 parent 20c6cc4 commit e5df1ff

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/sentry/testutils/helpers/analytics.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ def assert_event_equal(
1010
recorded_event: Event,
1111
check_uuid: bool = False,
1212
check_datetime: bool = False,
13+
exclude_fields: list[str] | None = None,
1314
):
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+
1420
assert expected_event.type == recorded_event.type
1521
for field in fields(expected_event):
1622
if field.name == "uuid_" and not check_uuid:
1723
continue
1824
if field.name == "datetime_" and not check_datetime:
1925
continue
26+
if exclude_fields and field.name in exclude_fields:
27+
continue
2028
assert getattr(expected_event, field.name) == getattr(recorded_event, field.name)
2129

2230

@@ -25,28 +33,42 @@ def assert_analytics_events_recorded(
2533
expected_events: list[Event],
2634
check_uuid: bool = False,
2735
check_datetime: bool = False,
36+
exclude_fields: list[str] | None = None,
2837
):
2938
recorded_events = [call.args[0] for call in mock_record.call_args_list]
3039
assert len(expected_events) == len(recorded_events)
3140
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]
3348

3449

3550
def assert_last_analytics_event(
3651
mock_record: MagicMock,
3752
expected_event: Event,
3853
check_uuid: bool = False,
3954
check_datetime: bool = False,
55+
exclude_fields: list[str] | None = None,
4056
):
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+
)
4364

4465

4566
@contextlib.contextmanager
4667
def assert_analytics_events(
4768
expected_events: list[Event],
4869
check_uuid: bool = False,
4970
check_datetime: bool = False,
71+
exclude_fields: list[str] | None = None,
5072
):
5173
"""
5274
Context manager that allows you to track analytics events recorded during the context.
@@ -58,4 +80,6 @@ def assert_analytics_events(
5880
"""
5981
with patch("sentry.analytics.record") as mock_record:
6082
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

Comments
 (0)