Skip to content

Commit baf5a77

Browse files
committed
fix(analytics): exclude UUID from Event equality checks to allow for easier testing
1 parent fd49aba commit baf5a77

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/sentry/analytics/event.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def wrapper(cls: type[Event]) -> type[Event]:
6464
# set the Event subclass `type` attribute, if it is set to anything
6565
if isinstance(event_name_or_class, str):
6666
cls.type = event_name_or_class
67-
return cast(type[Event], dataclass(kw_only=True)(cls))
67+
return cast(type[Event], dataclass(kw_only=True, eq=False)(cls))
6868

6969
# for using without parenthesis, wrap the passed class
7070
if isinstance(event_name_or_class, type) and issubclass(event_name_or_class, Event):
@@ -75,7 +75,7 @@ def wrapper(cls: type[Event]) -> type[Event]:
7575

7676

7777
# unfortunately we cannot directly use `eventclass` here, as it is making a typecheck to Event
78-
@dataclass(kw_only=True)
78+
@dataclass(kw_only=True, eq=False)
7979
class Event:
8080
"""
8181
Base class for custom analytics Events.
@@ -123,6 +123,18 @@ def from_instance(cls, instance: Any, **kwargs: Any) -> Self:
123123
}
124124
)
125125

126+
def __eq__(self, other: Any) -> bool:
127+
if not isinstance(other, Event):
128+
return False
129+
130+
self_values = self.serialize()
131+
other_values = other.serialize()
132+
133+
self_values.pop("uuid")
134+
other_values.pop("uuid")
135+
136+
return self_values == other_values
137+
126138

127139
def serialize_event(event: Event) -> dict[str, Any]:
128140
# TODO: this is the "old-style" attributes based serializer. Once all events are migrated to the new style,

tests/sentry/analytics/test_event.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ def test_simple(self, mock_uuid1):
5353
"uuid": b"AAEC",
5454
}
5555

56+
def test_equality_no_uuid_mock(self):
57+
a = ExampleEvent(
58+
id="1", # type: ignore[arg-type]
59+
map={"key": "value"},
60+
optional=False,
61+
)
62+
a.datetime_ = datetime(2001, 4, 18, tzinfo=timezone.utc)
63+
b = ExampleEvent(
64+
id="1", # type: ignore[arg-type]
65+
map={"key": "value"},
66+
optional=False,
67+
)
68+
b.datetime_ = datetime(2001, 4, 18, tzinfo=timezone.utc)
69+
assert a == b
70+
5671
@patch("sentry.analytics.event.uuid1")
5772
def test_simple_old_style(self, mock_uuid1):
5873
mock_uuid1.return_value = self.get_mock_uuid()

0 commit comments

Comments
 (0)