Implementing string representation for model classes

We want to have meaningfull representation. This is
useful when we debug and want to see the actual
attributes of the object. The current __repr__
of those classes is the default python implementation
 and does not provide
information about the object attributes.
Solve Bug #1084231

Change-Id: I1ea5d741d2fd2da13712e0d51d2c73dfba4991cf
Solve the conflict below:
Conflicts:
	quantum/tests/unit/test_db_plugin.py
This commit is contained in:
Avishay Balderman 2012-12-25 11:18:21 +02:00
parent 461417b640
commit 5d41c5c817
2 changed files with 24 additions and 0 deletions

View File

@ -51,6 +51,14 @@ class QuantumBase(object):
local.update(joined)
return local.iteritems()
def __repr__(self):
"""sqlalchemy based automatic __repr__ method"""
items = ['%s=%r' % (col.name, getattr(self, col.name))
for col in self.__table__.columns]
return "<%s.%s[object at %x] {%s}>" % (self.__class__.__module__,
self.__class__.__name__,
id(self), ', '.join(items))
class QuantumBaseV2(QuantumBase):

View File

@ -2782,3 +2782,19 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
req = self.new_delete_request('subnets', subnet['subnet']['id'])
res = req.get_response(self.api)
self.assertEqual(res.status_int, 204)
class DbModelTestCase(unittest2.TestCase):
""" DB model tests """
def test_repr(self):
""" testing the string representation of 'model' classes """
network = models_v2.Network(name="net_net", status="OK",
admin_state_up=True)
actual_repr_output = repr(network)
exp_start_with = "<quantum.db.models_v2.Network"
exp_middle = "[object at %x]" % id(network)
exp_end_with = (" {tenant_id=None, id=None, "
"name='net_net', status='OK', "
"admin_state_up=True, shared=None}>")
final_exp = exp_start_with + exp_middle + exp_end_with
self.assertEqual(actual_repr_output, final_exp)