Use @mock.patch.object instead of mock.MagicMock

Some unittests replace methods by mock objects, so they are leave mocked
when unittest finished. It can cause a lot of issues, because when we
try to get access to these attributes in the next test, we get mock
object instead of real attribute. Use @mock.patch.object decorator to
get attributes `unmocked` after test.

Change-Id: I23b3926a7671e0669315836da787ec88a812d67b
This commit is contained in:
Victor Sergeyev 2014-07-31 18:37:44 +03:00 committed by Oleksii Chuprykov
parent 941a81d88d
commit d8c657ab1c
1 changed files with 40 additions and 40 deletions

View File

@ -46,62 +46,62 @@ class TestManageBase(testtools.TestCase):
class TestLegacyManage(TestManageBase):
def test_legacy_db_version(self):
migration.db_version = mock.Mock()
@mock.patch.object(migration, 'db_version')
def test_legacy_db_version(self, db_version):
self._main_test_helper(['glance.cmd.manage', 'db_version'],
migration.db_version,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, 0)
def test_legacy_db_sync(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_sync(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db_sync'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, None,
sanity_check=True)
def test_legacy_db_upgrade(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_upgrade(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db_upgrade'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, None,
sanity_check=True)
def test_legacy_db_version_control(self):
migration.db_version_control = mock.Mock()
@mock.patch.object(migration, 'db_version_control')
def test_legacy_db_version_control(self, db_version_control):
self._main_test_helper(['glance.cmd.manage', 'db_version_control'],
migration.db_version_control,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, None)
def test_legacy_db_sync_version(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_sync_version(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db_sync', '20'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, '20',
sanity_check=True)
def test_legacy_db_upgrade_version(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_upgrade_version(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db_upgrade', '20'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, '20',
sanity_check=True)
def test_legacy_db_downgrade_version(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_downgrade_version(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db_downgrade', '20'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, '20',
sanity_check=True)
def test_legacy_db_sync_version_without_sanity_check(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_sync_version_without_sanity_check(self, db_sync):
manage.CONF.db_enforce_mysql_charset = False
self._main_test_helper(['glance.cmd.manage', 'db_sync', '20'],
migration.db_sync,
@ -109,8 +109,8 @@ class TestLegacyManage(TestManageBase):
db_migration.MIGRATE_REPO_PATH, '20',
sanity_check=False)
def test_legacy_db_upgrade_version_without_sanity_check(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_upgrade_version_without_sanity_check(self, db_sync):
manage.CONF.db_enforce_mysql_charset = False
self._main_test_helper(['glance.cmd.manage', 'db_upgrade', '40'],
migration.db_sync,
@ -118,8 +118,8 @@ class TestLegacyManage(TestManageBase):
db_migration.MIGRATE_REPO_PATH, '40',
sanity_check=False)
def test_legacy_db_downgrade_version_without_sanity_check(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_legacy_db_downgrade_version_without_sanity_check(self, db_sync):
manage.CONF.db_enforce_mysql_charset = False
self._main_test_helper(['glance.cmd.manage', 'db_downgrade', '20'],
migration.db_sync,
@ -130,62 +130,62 @@ class TestLegacyManage(TestManageBase):
class TestManage(TestManageBase):
def test_db_version(self):
migration.db_version = mock.Mock()
@mock.patch.object(migration, 'db_version')
def test_db_version(self, db_version):
self._main_test_helper(['glance.cmd.manage', 'db', 'version'],
migration.db_version,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, 0)
def test_db_sync(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_sync(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db', 'sync'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, None,
sanity_check=True)
def test_db_upgrade(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_upgrade(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db', 'upgrade'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, None,
sanity_check=True)
def test_db_version_control(self):
migration.db_version_control = mock.Mock()
@mock.patch.object(migration, 'db_version_control')
def test_db_version_control(self, db_version_control):
self._main_test_helper(['glance.cmd.manage', 'db', 'version_control'],
migration.db_version_control,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, None)
def test_db_sync_version(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_sync_version(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db', 'sync', '20'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, '20',
sanity_check=True)
def test_db_upgrade_version(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_upgrade_version(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db', 'upgrade', '20'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, '20',
sanity_check=True)
def test_db_downgrade_version(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_downgrade_version(self, db_sync):
self._main_test_helper(['glance.cmd.manage', 'db', 'downgrade', '20'],
migration.db_sync,
db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH, '20',
sanity_check=True)
def test_db_sync_version_without_sanity_check(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_sync_version_without_sanity_check(self, db_sync):
manage.CONF.db_enforce_mysql_charset = False
self._main_test_helper(['glance.cmd.manage', 'db', 'sync', '20'],
migration.db_sync,
@ -193,8 +193,8 @@ class TestManage(TestManageBase):
db_migration.MIGRATE_REPO_PATH, u'20',
sanity_check=False)
def test_db_upgrade_version_without_sanity_check(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_upgrade_version_without_sanity_check(self, db_sync):
manage.CONF.db_enforce_mysql_charset = False
self._main_test_helper(['glance.cmd.manage', 'db', 'upgrade', '40'],
migration.db_sync,
@ -202,8 +202,8 @@ class TestManage(TestManageBase):
db_migration.MIGRATE_REPO_PATH, '40',
sanity_check=False)
def test_db_downgrade_version_without_sanity_check(self):
migration.db_sync = mock.Mock()
@mock.patch.object(migration, 'db_sync')
def test_db_downgrade_version_without_sanity_check(self, db_sync):
manage.CONF.db_enforce_mysql_charset = False
self._main_test_helper(['glance.cmd.manage', 'db', 'downgrade', '20'],
migration.db_sync,