]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
objects: consolidate single transaction checks into test_base
authorIhar Hrachyshka <ihrachys@redhat.com>
Wed, 29 Jul 2015 15:47:21 +0000 (17:47 +0200)
committerIhar Hrachyshka <ihrachys@redhat.com>
Sat, 1 Aug 2015 20:16:57 +0000 (20:16 +0000)
Cover all base methods with it.

Change-Id: I0a6d401f6c1d35cbed397eed79a9aa7db07d179b
Partially-Implements: blueprint quantum-qos-api

neutron/objects/qos/policy.py
neutron/tests/unit/objects/qos/test_policy.py
neutron/tests/unit/objects/test_base.py

index b9c16c38688389032869cbf318bd5d1cffa22177..f35c8684c008a209f0695f6a7b9f908d81c92b67 100644 (file)
@@ -85,7 +85,6 @@ class QosPolicy(base.NeutronDbObject):
             policy_obj._load_rules()
             return policy_obj
 
-    # TODO(QoS): Test that all objects are fetched within one transaction
     @classmethod
     def get_objects(cls, context, **kwargs):
         # We want to get the policy regardless of its tenant id. We'll make
index 4b12d80d2c30ccb3eb223be3cc8719b59b8a57a7..5c2abd14cac04b7242b7122e6da938a0d3cd6d6e 100644 (file)
@@ -224,14 +224,6 @@ class QosPolicyDbObjectTestCase(test_base.BaseDbObjectTestCase,
         policy_obj = policy.QosPolicy.get_by_id(self.context, policy_obj.id)
         self.assertEqual([rule_obj], policy_obj.rules)
 
-    def test_create_is_in_single_transaction(self):
-        obj = self._test_class(self.context, **self.db_obj)
-        with mock.patch('sqlalchemy.engine.'
-                        'Connection._commit_impl') as mock_commit,\
-                mock.patch.object(obj._context.session, 'add'):
-            obj.create()
-        self.assertEqual(1, mock_commit.call_count)
-
     def test_get_by_id_fetches_rules_non_lazily(self):
         policy_obj, rule_obj = self._create_test_policy_with_rule()
         policy_obj = policy.QosPolicy.get_by_id(self.context, policy_obj.id)
index 812939956c831bef8a49097e688a09ba57535988..7f8be5b89b84c484ec957dc426cf1a2ea25aa582 100644 (file)
@@ -24,6 +24,9 @@ from neutron.objects import base
 from neutron.tests import base as test_base
 
 
+SQLALCHEMY_COMMIT = 'sqlalchemy.engine.Connection._commit_impl'
+
+
 class FakeModel(object):
     def __init__(self, *args, **kwargs):
         pass
@@ -241,3 +244,41 @@ class BaseDbObjectTestCase(_BaseObjectTestCase):
     def test_delete_non_existent_object_raises_not_found(self):
         obj = self._test_class(self.context, **self.db_obj)
         self.assertRaises(n_exc.ObjectNotFound, obj.delete)
+
+    @mock.patch(SQLALCHEMY_COMMIT)
+    def test_create_single_transaction(self, mock_commit):
+        obj = self._test_class(self.context, **self.db_obj)
+        obj.create()
+        self.assertEqual(1, mock_commit.call_count)
+
+    def test_update_single_transaction(self):
+        obj = self._test_class(self.context, **self.db_obj)
+        obj.create()
+
+        for key, val in self.get_updatable_fields(self.db_obj).items():
+            setattr(obj, key, val)
+
+        with mock.patch(SQLALCHEMY_COMMIT) as mock_commit:
+            obj.update()
+        self.assertEqual(1, mock_commit.call_count)
+
+    def test_delete_single_transaction(self):
+        obj = self._test_class(self.context, **self.db_obj)
+        obj.create()
+
+        with mock.patch(SQLALCHEMY_COMMIT) as mock_commit:
+            obj.delete()
+        self.assertEqual(1, mock_commit.call_count)
+
+    @mock.patch(SQLALCHEMY_COMMIT)
+    def test_get_objects_single_transaction(self, mock_commit):
+        self._test_class.get_objects(self.context)
+        self.assertEqual(1, mock_commit.call_count)
+
+    @mock.patch(SQLALCHEMY_COMMIT)
+    def test_get_by_id_single_transaction(self, mock_commit):
+        obj = self._test_class(self.context, **self.db_obj)
+        obj.create()
+
+        obj = self._test_class.get_by_id(self.context, obj.id)
+        self.assertEqual(2, mock_commit.call_count)