Fix test_migrations on Python 3

migrate.versioning.api.VerNum() conversion to integer doesn't work in
all cases. Cast explicitly to int in WalkVersionsMixin.walk_versions()
to fix this issue.

Modify unit tests to use VerNum objects instead of using directly
integers. The bug was not seen in tests because tests used int numbers.

Example of error:

    Traceback (most recent call last):
      ...
      File "oslo_db/sqlalchemy/test_migrations.py", line 189, in walk_versions
        versions = range(self.INIT_VERSION + 1, self.REPOSITORY.latest + 1)
    TypeError: 'VerNum' object cannot be interpreted as an integer

This bug impact for example glance.tests.unit.test_migrations of the
Glance project.

Change-Id: I043a66268b957b97f2e8f652c0ee8aec3a00e8dd
This commit is contained in:
Victor Stinner 2015-07-28 12:44:53 +02:00
parent 984d41cd7c
commit 897498eee2
2 changed files with 16 additions and 10 deletions

View File

@ -185,7 +185,8 @@ class WalkVersionsMixin(object):
self.REPOSITORY)) self.REPOSITORY))
LOG.debug('latest version is %s', self.REPOSITORY.latest) LOG.debug('latest version is %s', self.REPOSITORY.latest)
versions = range(self.INIT_VERSION + 1, self.REPOSITORY.latest + 1) versions = range(int(self.INIT_VERSION) + 1,
int(self.REPOSITORY.latest) + 1)
for version in versions: for version in versions:
# upgrade -> downgrade -> upgrade # upgrade -> downgrade -> upgrade

View File

@ -15,6 +15,7 @@
# under the License. # under the License.
import fixtures import fixtures
from migrate.versioning import api as versioning_api
import mock import mock
from oslotest import base as test from oslotest import base as test
import six import six
@ -30,7 +31,7 @@ class TestWalkVersions(test.BaseTestCase, migrate.WalkVersionsMixin):
migration_api = mock.MagicMock() migration_api = mock.MagicMock()
REPOSITORY = mock.MagicMock() REPOSITORY = mock.MagicMock()
engine = mock.MagicMock() engine = mock.MagicMock()
INIT_VERSION = 4 INIT_VERSION = versioning_api.VerNum(4)
@property @property
def migrate_engine(self): def migrate_engine(self):
@ -96,7 +97,7 @@ class TestWalkVersions(test.BaseTestCase, migrate.WalkVersionsMixin):
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up')
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down')
def test_walk_versions_all_default(self, migrate_up, migrate_down): def test_walk_versions_all_default(self, migrate_up, migrate_down):
self.REPOSITORY.latest = 20 self.REPOSITORY.latest = versioning_api.VerNum(20)
self.migration_api.db_version.return_value = self.INIT_VERSION self.migration_api.db_version.return_value = self.INIT_VERSION
self.walk_versions() self.walk_versions()
@ -106,7 +107,8 @@ class TestWalkVersions(test.BaseTestCase, migrate.WalkVersionsMixin):
self.migration_api.db_version.assert_called_with( self.migration_api.db_version.assert_called_with(
self.engine, self.REPOSITORY) self.engine, self.REPOSITORY)
versions = range(self.INIT_VERSION + 1, self.REPOSITORY.latest + 1) versions = range(int(self.INIT_VERSION) + 1,
int(self.REPOSITORY.latest) + 1)
upgraded = [mock.call(v, with_data=True) upgraded = [mock.call(v, with_data=True)
for v in versions] for v in versions]
self.assertEqual(self.migrate_up.call_args_list, upgraded) self.assertEqual(self.migrate_up.call_args_list, upgraded)
@ -117,12 +119,13 @@ class TestWalkVersions(test.BaseTestCase, migrate.WalkVersionsMixin):
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up')
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down')
def test_walk_versions_all_true(self, migrate_up, migrate_down): def test_walk_versions_all_true(self, migrate_up, migrate_down):
self.REPOSITORY.latest = 20 self.REPOSITORY.latest = versioning_api.VerNum(20)
self.migration_api.db_version.return_value = self.INIT_VERSION self.migration_api.db_version.return_value = self.INIT_VERSION
self.walk_versions(snake_walk=True, downgrade=True) self.walk_versions(snake_walk=True, downgrade=True)
versions = range(self.INIT_VERSION + 1, self.REPOSITORY.latest + 1) versions = range(int(self.INIT_VERSION) + 1,
int(self.REPOSITORY.latest) + 1)
upgraded = [] upgraded = []
for v in versions: for v in versions:
upgraded.append(mock.call(v, with_data=True)) upgraded.append(mock.call(v, with_data=True))
@ -141,12 +144,13 @@ class TestWalkVersions(test.BaseTestCase, migrate.WalkVersionsMixin):
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up')
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down')
def test_walk_versions_true_false(self, migrate_up, migrate_down): def test_walk_versions_true_false(self, migrate_up, migrate_down):
self.REPOSITORY.latest = 20 self.REPOSITORY.latest = versioning_api.VerNum(20)
self.migration_api.db_version.return_value = self.INIT_VERSION self.migration_api.db_version.return_value = self.INIT_VERSION
self.walk_versions(snake_walk=True, downgrade=False) self.walk_versions(snake_walk=True, downgrade=False)
versions = range(self.INIT_VERSION + 1, self.REPOSITORY.latest + 1) versions = range(int(self.INIT_VERSION) + 1,
int(self.REPOSITORY.latest) + 1)
upgraded = [] upgraded = []
for v in versions: for v in versions:
@ -160,12 +164,13 @@ class TestWalkVersions(test.BaseTestCase, migrate.WalkVersionsMixin):
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_up')
@mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down') @mock.patch.object(migrate.WalkVersionsMixin, 'migrate_down')
def test_walk_versions_all_false(self, migrate_up, migrate_down): def test_walk_versions_all_false(self, migrate_up, migrate_down):
self.REPOSITORY.latest = 20 self.REPOSITORY.latest = versioning_api.VerNum(20)
self.migration_api.db_version.return_value = self.INIT_VERSION self.migration_api.db_version.return_value = self.INIT_VERSION
self.walk_versions(snake_walk=False, downgrade=False) self.walk_versions(snake_walk=False, downgrade=False)
versions = range(self.INIT_VERSION + 1, self.REPOSITORY.latest + 1) versions = range(int(self.INIT_VERSION) + 1,
int(self.REPOSITORY.latest) + 1)
upgraded = [mock.call(v, with_data=True) for v in versions] upgraded = [mock.call(v, with_data=True) for v in versions]
self.assertEqual(upgraded, self.migrate_up.call_args_list) self.assertEqual(upgraded, self.migrate_up.call_args_list)