summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-10-02 16:11:45 +0000
committerGerrit Code Review <review@openstack.org>2014-10-02 16:11:45 +0000
commita7b3522477356d5bdf25294f41f7b3090648d4d6 (patch)
treeffe5f3b20d283bc89e5bbc5163326485253bbd0c /tests
parent633eadae08021fa2cbc6cf7f223badb99a422abd (diff)
parent7f9f4f1b49ff426bcdd713a62c4174eb9996037b (diff)
downloadoslo-db-a7b3522477356d5bdf25294f41f7b3090648d4d6.tar.gz
Merge "Create a nested helper function that will work on py3.x"
Diffstat (limited to 'tests')
-rw-r--r--tests/sqlalchemy/test_exc_filters.py7
-rw-r--r--tests/sqlalchemy/test_handle_error.py6
-rw-r--r--tests/sqlalchemy/test_migration_common.py12
-rw-r--r--tests/utils.py14
4 files changed, 27 insertions, 12 deletions
diff --git a/tests/sqlalchemy/test_exc_filters.py b/tests/sqlalchemy/test_exc_filters.py
index 09b5931..68c583c 100644
--- a/tests/sqlalchemy/test_exc_filters.py
+++ b/tests/sqlalchemy/test_exc_filters.py
@@ -26,6 +26,7 @@ from oslo.db.sqlalchemy import compat
from oslo.db.sqlalchemy import exc_filters
from oslo.db.sqlalchemy import session
from oslo.db.sqlalchemy import test_base
+from tests import utils as test_utils
_TABLE_NAME = '__tmp__test__tmp__'
@@ -73,7 +74,7 @@ class TestsExceptionFilter(oslo_test_base.BaseTestCase):
@contextlib.contextmanager
def _dbapi_fixture(self, dialect_name):
engine = self.engine
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(engine.dialect.dbapi,
"Error",
self.Error),
@@ -94,7 +95,7 @@ class TestsExceptionFilter(oslo_test_base.BaseTestCase):
# statement
self.engine.connect().close()
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(engine.dialect, "do_execute", do_execute),
# replace the whole DBAPI rather than patching "Error"
# as some DBAPIs might not be patchable (?)
@@ -632,7 +633,7 @@ class TestDBDisconnected(TestsExceptionFilter):
raise exception
with self._dbapi_fixture(dialect_name):
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(engine.dialect,
"do_execute",
fake_do_execute),
diff --git a/tests/sqlalchemy/test_handle_error.py b/tests/sqlalchemy/test_handle_error.py
index 6ebcd28..14269a3 100644
--- a/tests/sqlalchemy/test_handle_error.py
+++ b/tests/sqlalchemy/test_handle_error.py
@@ -16,7 +16,6 @@ This event is added as of SQLAlchemy 0.9.7; oslo.db provides a compatibility
layer for prior SQLAlchemy versions.
"""
-import contextlib
import mock
from oslotest import base as test_base
@@ -29,6 +28,7 @@ from sqlalchemy.types import TypeDecorator
from oslo.db.sqlalchemy.compat import handle_error
from oslo.db.sqlalchemy.compat import utils
+from tests import utils as test_utils
class MyException(Exception):
@@ -79,7 +79,7 @@ class ExceptionReraiseTest(test_base.BaseTestCase):
def test_is_disconnect_not_interrupted(self):
self._fixture()
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(
self.engine.dialect.execution_ctx_cls,
"handle_dbapi_exception"
@@ -100,7 +100,7 @@ class ExceptionReraiseTest(test_base.BaseTestCase):
def test_no_is_disconnect_not_invalidated(self):
self._fixture()
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(
self.engine.dialect.execution_ctx_cls,
"handle_dbapi_exception"
diff --git a/tests/sqlalchemy/test_migration_common.py b/tests/sqlalchemy/test_migration_common.py
index 155c20c..86f5d3f 100644
--- a/tests/sqlalchemy/test_migration_common.py
+++ b/tests/sqlalchemy/test_migration_common.py
@@ -14,7 +14,6 @@
# under the License.
#
-import contextlib
import os
import tempfile
@@ -26,6 +25,7 @@ import sqlalchemy
from oslo.db import exception as db_exception
from oslo.db.sqlalchemy import migration
from oslo.db.sqlalchemy import test_base
+from tests import utils as test_utils
class TestMigrationCommon(test_base.DbTestCase):
@@ -73,7 +73,7 @@ class TestMigrationCommon(test_base.DbTestCase):
self.assertNotEqual(repo1, repo2)
def test_db_version_control(self):
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(migration, '_find_migrate_repo'),
mock.patch.object(versioning_api, 'version_control'),
) as (mock_find_repo, mock_version_control):
@@ -136,7 +136,7 @@ class TestMigrationCommon(test_base.DbTestCase):
def test_db_sync_upgrade(self):
init_ver = 55
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(migration, '_find_migrate_repo'),
mock.patch.object(versioning_api, 'upgrade')
) as (mock_find_repo, mock_upgrade):
@@ -151,7 +151,7 @@ class TestMigrationCommon(test_base.DbTestCase):
self.engine, self.return_value, self.test_version)
def test_db_sync_downgrade(self):
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(migration, '_find_migrate_repo'),
mock.patch.object(versioning_api, 'downgrade')
) as (mock_find_repo, mock_downgrade):
@@ -165,7 +165,7 @@ class TestMigrationCommon(test_base.DbTestCase):
self.engine, self.return_value, self.test_version)
def test_db_sync_sanity_called(self):
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(migration, '_find_migrate_repo'),
mock.patch.object(migration, '_db_schema_sanity_check'),
mock.patch.object(versioning_api, 'downgrade')
@@ -177,7 +177,7 @@ class TestMigrationCommon(test_base.DbTestCase):
mock_sanity.assert_called_once_with(self.engine)
def test_db_sync_sanity_skipped(self):
- with contextlib.nested(
+ with test_utils.nested(
mock.patch.object(migration, '_find_migrate_repo'),
mock.patch.object(migration, '_db_schema_sanity_check'),
mock.patch.object(versioning_api, 'downgrade')
diff --git a/tests/utils.py b/tests/utils.py
index c21a618..3f29d25 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -13,9 +13,23 @@
# License for the specific language governing permissions and limitations
# under the License.
+import contextlib
+
from oslo.config import cfg
from oslotest import base as test_base
from oslotest import moxstubout
+import six
+
+
+if six.PY3:
+ @contextlib.contextmanager
+ def nested(*contexts):
+ with contextlib.ExitStack() as stack:
+ for c in contexts:
+ stack.enter_context(c)
+ yield
+else:
+ nested = contextlib.nested
class BaseTestCase(test_base.BaseTestCase):