Merge "Create a nested helper function that will work on py3.x"
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user