]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Implementing string representation for model classes
authorAvishay Balderman <avishayb@radware.com>
Tue, 25 Dec 2012 09:18:21 +0000 (11:18 +0200)
committerAvishay Balderman <avishayb@radware.com>
Wed, 26 Dec 2012 12:14:33 +0000 (14:14 +0200)
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

quantum/db/model_base.py
quantum/tests/unit/test_db_plugin.py

index e3ffa682c7f42b1d14096c1961bdf2fd85a8d16f..db3b36a3983db6646b02e318e33407ee7f55fde7 100644 (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):
 
index 65e0823c69b89624cc2c6efe6a1ec067ab79cb3d..266cb412034f71b81e7edaac95e6d3051651a780 100644 (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)