diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2014-09-09 12:40:38 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2014-09-26 21:04:37 +0000 |
commit | 7f9f4f1b49ff426bcdd713a62c4174eb9996037b (patch) | |
tree | 3a95ff02529321d404d848ff721f86ff471ff564 /tests | |
parent | 4fa33509458e75675bd639d3c9fa9a0d46595e66 (diff) | |
download | oslo-db-7f9f4f1b49ff426bcdd713a62c4174eb9996037b.tar.gz |
Create a nested helper function that will work on py3.x
The contextlib.nested function doesn't exist on py3.x so
in order to retain similar functionality create an equivalent
that will perform good enough for oslo.db usage.
Change-Id: I326816c9301f1c625e1e2723d4d639fd81cc596c
Diffstat (limited to 'tests')
-rw-r--r-- | tests/sqlalchemy/test_exc_filters.py | 7 | ||||
-rw-r--r-- | tests/sqlalchemy/test_handle_error.py | 6 | ||||
-rw-r--r-- | tests/sqlalchemy/test_migration_common.py | 12 | ||||
-rw-r--r-- | tests/utils.py | 14 |
4 files changed, 27 insertions, 12 deletions
diff --git a/tests/sqlalchemy/test_exc_filters.py b/tests/sqlalchemy/test_exc_filters.py index d0ea38b..7b45758 100644 --- a/tests/sqlalchemy/test_exc_filters.py +++ b/tests/sqlalchemy/test_exc_filters.py @@ -25,6 +25,7 @@ from oslo.db import exception 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 (?) @@ -630,7 +631,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): |