Skip to content

Commit

Permalink
Fix Issue 35 , comparing a Message to something else (such as None), …
Browse files Browse the repository at this point in the history
…raises an AttributeError

instead of returning false.  Thanks to [email protected] for reporting this.
  • Loading branch information
barryp committed Jul 18, 2011
1 parent dfdbe31 commit 4cdf18d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion amqplib/client_0_8/basic_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@ def __eq__(self, other):
which isn't compared.
"""
return super(Message, self).__eq__(other) and (self.body == other.body)
return super(Message, self).__eq__(other) \
and hasattr(other, 'body') and (self.body == other.body)
3 changes: 2 additions & 1 deletion amqplib/client_0_8/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ def __eq__(self, other):
content object.
"""
return (self.properties == other.properties)
return hasattr(other, 'properties') \
and (self.properties == other.properties)


def __getattr__(self, name):
Expand Down
18 changes: 18 additions & 0 deletions tests/client_0_8/test_basic_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ def check_proplist(self, msg):
self.assertEqual(msg, new_msg)


def test_eq(self):
msg = Message('hello', content_type='text/plain')
self.assertNotEqual(msg, None)

#
# Make sure that something that looks vaguely
# like a Message doesn't raise an Attribute
# error when compared to a Message, and instead
# returns False
#
class FakeMsg(object): pass

fake_msg = FakeMsg()
fake_msg.properties = {'content_type': 'text/plain'}

self.assertNotEqual(msg, fake_msg)


def test_pickle(self):
msg = Message(
'some body' * 200000,
Expand Down
15 changes: 14 additions & 1 deletion tests/client_0_8/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import settings

from amqplib.client_0_8.serialization import AMQPReader, AMQPWriter
from amqplib.client_0_8.serialization import AMQPReader, AMQPWriter, GenericContent

class TestSerialization(unittest.TestCase):
if sys.version_info[0] >= 3:
Expand Down Expand Up @@ -347,6 +347,19 @@ def test_table_multi(self):
r = AMQPReader(s)
self.assertEqual(r.read_table(), val)

#
# GenericContent
#
def test_generic_content_eq(self):
msg_1 = GenericContent(dummy='foo')
msg_2 = GenericContent(dummy='foo')
msg_3 = GenericContent(dummy='bar')

self.assertEqual(msg_1, msg_1)
self.assertEqual(msg_1, msg_2)
self.assertNotEqual(msg_1, msg_3)
self.assertNotEqual(msg_1, None)


def main():
suite = unittest.TestLoader().loadTestsFromTestCase(TestSerialization)
Expand Down

0 comments on commit 4cdf18d

Please sign in to comment.