diff options
| author | John L. Villalovos <john.l.villalovos@intel.com> | 2015-08-21 18:14:59 -0700 |
|---|---|---|
| committer | John L. Villalovos <john.l.villalovos@intel.com> | 2015-09-11 13:44:09 -0700 |
| commit | 1285baee1c91eda75984099b6f1fd8244aaa0258 (patch) | |
| tree | 89fd83cd961ba755406667159b192a02ff5b22e0 /ironic_python_agent/encoding.py | |
| parent | f683a783af2898c4753ad2fc2dce29a88a225976 (diff) | |
| download | ironic-python-agent-1285baee1c91eda75984099b6f1fd8244aaa0258.tar.gz | |
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
Diffstat (limited to 'ironic_python_agent/encoding.py')
| -rw-r--r-- | ironic_python_agent/encoding.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/ironic_python_agent/encoding.py b/ironic_python_agent/encoding.py index 9022afd5..715d1341 100644 --- a/ironic_python_agent/encoding.py +++ b/ironic_python_agent/encoding.py @@ -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): |
