Create a SerializableComparable class

Create a SerializableComparable class derived from the Serializable
class.

Added the following functions to the SerializableComparable class:
  '__eq__'
  '__ne__'

Disable the '__hash__' function in the SerializableComparable class as
some derived classes are mutable.

Use the SerializableComparable class in hardware.py and
extensions/base.py

This should make unit testing users of the class easier when doing a
self.assertEqual() or self.assertNotEqual()

Added some initial unit testing for encoding.py

Change-Id: If0f14b3bfe7f1391f65dd730a16a534afed0da82
This commit is contained in:
John L. Villalovos
2015-08-21 18:14:59 -07:00
parent f683a783af
commit 1285baee1c
4 changed files with 85 additions and 5 deletions
ironic_python_agent

@ -25,6 +25,24 @@ class Serializable(object):
return dict((f, getattr(self, f)) for f in self.serializable_fields)
class SerializableComparable(Serializable):
"""A Serializable class which supports some comparison operators
This class supports the '__eq__' and '__ne__' comparison operators, but
intentionally disables the '__hash__' operator as some child classes may be
mutable. The addition of these comparison operators is mainly used to
assist with unit testing.
"""
__hash__ = None
def __eq__(self, other):
return self.serialize() == other.serialize()
def __ne__(self, other):
return self.serialize() != other.serialize()
class RESTJSONEncoder(json.JSONEncoder):
"""A slightly customized JSON encoder."""
def encode(self, o):