Provide usable __repr__ for serializable objects
These objects are frequently logged or compared in unit tests. It's very helpful to be able to inspect their content. Change-Id: Ib725dcd5f54f4492205f95974d887b8b42c74039
This commit is contained in:
@@ -24,6 +24,11 @@ class Serializable(object):
|
||||
"""Turn this object into a dict."""
|
||||
return dict((f, getattr(self, f)) for f in self.serializable_fields)
|
||||
|
||||
def __repr__(self):
|
||||
fields = " ".join(f"{f}=" + repr(getattr(self, f))
|
||||
for f in self.serializable_fields)
|
||||
return f"<{self.__class__.__name__} {fields}>"
|
||||
|
||||
|
||||
class SerializableComparable(Serializable):
|
||||
"""A Serializable class which supports some comparison operators
|
||||
|
@@ -43,6 +43,8 @@ class TestSerializable(base.IronicAgentTest):
|
||||
expected = {'jack': 'hello', 'jill': 'world'}
|
||||
obj = SerializableTesting('hello', 'world')
|
||||
self.assertEqual(expected, obj.serialize())
|
||||
self.assertEqual(
|
||||
"<SerializableTesting jack='hello' jill='world'>", repr(obj))
|
||||
|
||||
|
||||
class TestSerializableComparable(base.IronicAgentTest):
|
||||
|
Reference in New Issue
Block a user