From: Ihar Hrachyshka Date: Wed, 29 Jul 2015 15:47:21 +0000 (+0200) Subject: objects: consolidate single transaction checks into test_base X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=1f2c05a0b13d429ba6822b2e6acb5b56e10cb0ed;p=openstack-build%2Fneutron-build.git objects: consolidate single transaction checks into test_base Cover all base methods with it. Change-Id: I0a6d401f6c1d35cbed397eed79a9aa7db07d179b Partially-Implements: blueprint quantum-qos-api --- diff --git a/neutron/objects/qos/policy.py b/neutron/objects/qos/policy.py index b9c16c386..f35c8684c 100644 --- a/neutron/objects/qos/policy.py +++ b/neutron/objects/qos/policy.py @@ -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 diff --git a/neutron/tests/unit/objects/qos/test_policy.py b/neutron/tests/unit/objects/qos/test_policy.py index 4b12d80d2..5c2abd14c 100644 --- a/neutron/tests/unit/objects/qos/test_policy.py +++ b/neutron/tests/unit/objects/qos/test_policy.py @@ -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) diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 812939956..7f8be5b89 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -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)