Skip to content

Commit

Permalink
Exclude the "sent-at" header from equality checks
Browse files Browse the repository at this point in the history
In fedora-infra/bodhi#2858 it was noted that
tests involving equality of messages fails because of the sent-at
header. It doesn't make a lot of sense to include this in an equality
check for the same reason the id is not included in the check - it's
metadata and of little interest to developers (unless they are tracking
messages).

Signed-off-by: Jeremy Cline <jcline@redhat.com>
  • Loading branch information
jeremycline authored and mergify[bot] committed Dec 24, 2018
1 parent 5019eb0 commit c93c6c9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
22 changes: 19 additions & 3 deletions fedora_messaging/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,17 +473,33 @@ def __eq__(self, other):
"""
Two messages of the same class with the same topic, headers, and body are equal.
The "sent-at" header is excluded from the equality check as this is set
automatically and is dependent on when the object is created.
Args:
other (object): The object to check for equality.
Returns:
bool: True if the messages are equal.
"""
if not isinstance(other, self.__class__):
return False

headers = self._headers.copy()
other_headers = other._headers.copy()
try:
del headers["sent-at"]
except KeyError:
pass
try:
del other_headers["sent-at"]
except KeyError:
pass

return (
isinstance(other, self.__class__)
and self.topic == other.topic
self.topic == other.topic
and self._body == other._body
and self._headers == other._headers
and headers == other_headers
)

def validate(self):
Expand Down
8 changes: 8 additions & 0 deletions fedora_messaging/tests/unit/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ def test_equality(self):
message.Message(topic="test.topic", body={"my": "key"}),
)

def test_equality_different_sent_at(self):
"""Assert the "sent-at" key is not included in the equality check."""
m1 = message.Message(topic="test.topic", body={"my": "key"})
m2 = message.Message(topic="test.topic", body={"my": "key"})
m2._headers["sent-at"] = datetime.datetime(1970, 1, 2).isoformat()

self.assertEqual(m1, m2)

def test_repr(self):
"""Assert the message produces a valid representation of the message."""
msg = message.Message(topic="test.topic", body={"my": "key"})
Expand Down
2 changes: 2 additions & 0 deletions news/109.bug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the equality check on :class:`fedora_messaging.message.Message` objects to
exclude the 'sent-at' header.

0 comments on commit c93c6c9

Please sign in to comment.