Skip to content

Commit

Permalink
Merge pull request #1209 from ingwinlu/feature_comparable_spec_classes
Browse files Browse the repository at this point in the history
Implement comparable spec classes
  • Loading branch information
lukebakken committed May 2, 2019
2 parents 4114710 + 886dae7 commit eb8d113
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pika/amqp_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def __repr__(self):
return "<%s>" % self.NAME
return "<%s(%s)>" % (self.NAME, sorted(items))

def __eq__(self, other):
if other is not None:
return self.__dict__ == other.__dict__
else:
return False


class Class(AMQPObject):
"""Is extended by AMQP classes"""
Expand All @@ -40,8 +46,8 @@ def _set_content(self, properties, body):
:param bytes body: The message body
"""
self._properties = properties # pylint: disable=W0201
self._body = body # pylint: disable=W0201
self._properties = properties # pylint: disable=W0201
self._body = body # pylint: disable=W0201

def get_properties(self):
"""Return the properties if they are set.
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/amqp_object_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,27 @@ def test_repr_items(self):
setattr(obj, 'baz', 'qux')
self.assertEqual(repr(obj), "<AMQPObject(['baz=qux', 'foo=bar'])>")

def test_equality(self):
a = amqp_object.AMQPObject()
b = amqp_object.AMQPObject()
self.assertEqual(a, b)

setattr(a, "a_property", "test")
self.assertNotEqual(a, b)

setattr(b, "a_property", "test")
self.assertEqual(a, b)


class ClassTests(unittest.TestCase):
def test_base_name(self):
self.assertEqual(amqp_object.Class().NAME, 'Unextended Class')

def test_equality(self):
a = amqp_object.Class()
b = amqp_object.Class()
self.assertEqual(a, b)


class MethodTests(unittest.TestCase):
def test_base_name(self):
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/spec_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf8 -*-
"""
Tests for pika.spec
"""
import unittest

from pika import spec


class BasicPropertiesTests(unittest.TestCase):
def test_equality(self):
a = spec.BasicProperties(content_type='text/plain')
self.assertEqual(a, a)
self.assertNotEqual(a, None)

b = spec.BasicProperties()
self.assertNotEqual(a, b)
b.content_type = 'text/plain'
self.assertEqual(a, b)

a.correlation_id = 'abc123'
self.assertNotEqual(a, b)

b.correlation_id = 'abc123'
self.assertEqual(a, b)
6 changes: 6 additions & 0 deletions utils/codegen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""
codegen.py generates pika/spec.py
The required spec json file can be found at
https://github.com/rabbitmq/rabbitmq-codegen
.
After cloning it run the following to generate a spec.py file:
python2 ./codegen.py ../../rabbitmq-codegen
"""
from __future__ import nested_scopes

Expand Down

0 comments on commit eb8d113

Please sign in to comment.