From c1492c9a57fca46339e99ad64b5564c803eb8836 Mon Sep 17 00:00:00 2001 From: Avishay Balderman Date: Tue, 25 Dec 2012 11:18:21 +0200 Subject: [PATCH] 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 --- quantum/db/model_base.py | 8 ++++++++ quantum/tests/unit/test_db_plugin.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/quantum/db/model_base.py b/quantum/db/model_base.py index e3ffa682c7..db3b36a398 100644 --- a/quantum/db/model_base.py +++ b/quantum/db/model_base.py @@ -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): diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 65e0823c69..266cb41203 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -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 = "") + final_exp = exp_start_with + exp_middle + exp_end_with + self.assertEqual(actual_repr_output, final_exp)