From 1285baee1c91eda75984099b6f1fd8244aaa0258 Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Fri, 21 Aug 2015 18:14:59 -0700 Subject: 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 --- ironic_python_agent/encoding.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'ironic_python_agent/encoding.py') 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): -- cgit v1.2.1