From bf02cf3eefa2d176d146c8d4f5feda3e07cabe8a Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Fri, 30 Aug 2013 20:50:26 +0900 Subject: [PATCH] Utilize assertIsInstance Using assertTrue and the 'isinstance' function to test if an object is in an instance of some class is too python2.4. Our unit testing framework supports assertIsInstance which was created for these types of tests. Let's use assertIsInstance for these tests. Change-Id: I2c931a52ae20f072f53210bf3dedb47127b413c0 --- cinder/tests/image/test_glance.py | 6 +- cinder/tests/scheduler/test_scheduler.py | 2 +- cinder/tests/test_migrations.py | 276 +++++++++++------------ cinder/tests/test_quota.py | 4 +- 4 files changed, 144 insertions(+), 144 deletions(-) diff --git a/cinder/tests/image/test_glance.py b/cinder/tests/image/test_glance.py index 0f564fbe8..0e7576d75 100644 --- a/cinder/tests/image/test_glance.py +++ b/cinder/tests/image/test_glance.py @@ -622,7 +622,7 @@ class TestGlanceImageServiceClient(test.TestCase): self.stubs.Set(glance.glanceclient, 'Client', MyGlanceStubClient) client = glance._create_glance_client(self.context, 'fake_host:9292', False) - self.assertTrue(isinstance(client, MyGlanceStubClient)) + self.assertIsInstance(client, MyGlanceStubClient) def test_create_glance_client_auth_strategy_is_not_keystone(self): self.flags(auth_strategy='noauth') @@ -638,7 +638,7 @@ class TestGlanceImageServiceClient(test.TestCase): self.stubs.Set(glance.glanceclient, 'Client', MyGlanceStubClient) client = glance._create_glance_client(self.context, 'fake_host:9292', False) - self.assertTrue(isinstance(client, MyGlanceStubClient)) + self.assertIsInstance(client, MyGlanceStubClient) def test_create_glance_client_glance_request_default_timeout(self): self.flags(auth_strategy='keystone') @@ -654,7 +654,7 @@ class TestGlanceImageServiceClient(test.TestCase): self.stubs.Set(glance.glanceclient, 'Client', MyGlanceStubClient) client = glance._create_glance_client(self.context, 'fake_host:9292', False) - self.assertTrue(isinstance(client, MyGlanceStubClient)) + self.assertIsInstance(client, MyGlanceStubClient) def tearDown(self): self.stubs.UnsetAll() diff --git a/cinder/tests/scheduler/test_scheduler.py b/cinder/tests/scheduler/test_scheduler.py index ce78f50e6..db783f377 100644 --- a/cinder/tests/scheduler/test_scheduler.py +++ b/cinder/tests/scheduler/test_scheduler.py @@ -51,7 +51,7 @@ class SchedulerManagerTestCase(test.TestCase): def test_1_correct_init(self): # Correct scheduler driver manager = self.manager - self.assertTrue(isinstance(manager.driver, self.driver_cls)) + self.assertIsInstance(manager.driver, self.driver_cls) def test_update_service_capabilities(self): service_name = 'fake_service' diff --git a/cinder/tests/test_migrations.py b/cinder/tests/test_migrations.py index 3d0f85f98..3228b4325 100644 --- a/cinder/tests/test_migrations.py +++ b/cinder/tests/test_migrations.py @@ -478,8 +478,8 @@ class TestMigrations(test.TestCase): volumes = sqlalchemy.Table('volumes', metadata, autoload=True) - self.assertTrue(isinstance(volumes.c.source_volid.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(volumes.c.source_volid.type, + sqlalchemy.types.VARCHAR) def _metadatas(self, upgrade_to, downgrade_to=None): for (key, engine) in self.engines.items(): @@ -507,8 +507,8 @@ class TestMigrations(test.TestCase): def test_upgrade_006_adds_provider_location(self): for metadata in self.metadatas_upgraded_to(6): snapshots = sqlalchemy.Table('snapshots', metadata, autoload=True) - self.assertTrue(isinstance(snapshots.c.provider_location.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(snapshots.c.provider_location.type, + sqlalchemy.types.VARCHAR) def test_downgrade_006_removes_provider_location(self): for metadata in self.metadatas_downgraded_from(6): @@ -549,44 +549,44 @@ class TestMigrations(test.TestCase): metadata, autoload=True) - self.assertTrue(isinstance(backups.c.created_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(backups.c.updated_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(backups.c.deleted_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(backups.c.deleted.type, - sqlalchemy.types.BOOLEAN)) - self.assertTrue(isinstance(backups.c.id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.volume_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.user_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.project_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.host.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.availability_zone.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.display_name.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.display_description.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.container.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.status.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.fail_reason.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.service_metadata.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.service.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(backups.c.size.type, - sqlalchemy.types.INTEGER)) - self.assertTrue(isinstance(backups.c.object_count.type, - sqlalchemy.types.INTEGER)) + self.assertIsInstance(backups.c.created_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(backups.c.updated_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(backups.c.deleted_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(backups.c.deleted.type, + sqlalchemy.types.BOOLEAN) + self.assertIsInstance(backups.c.id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.volume_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.user_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.project_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.host.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.availability_zone.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.display_name.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.display_description.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.container.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.status.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.fail_reason.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.service_metadata.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.service.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(backups.c.size.type, + sqlalchemy.types.INTEGER) + self.assertIsInstance(backups.c.object_count.type, + sqlalchemy.types.INTEGER) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 7) @@ -611,24 +611,24 @@ class TestMigrations(test.TestCase): metadata, autoload=True) - self.assertTrue(isinstance(snapshot_metadata.c.created_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(snapshot_metadata.c.updated_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(snapshot_metadata.c.deleted_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(snapshot_metadata.c.deleted.type, - sqlalchemy.types.BOOLEAN)) - self.assertTrue(isinstance(snapshot_metadata.c.deleted.type, - sqlalchemy.types.BOOLEAN)) - self.assertTrue(isinstance(snapshot_metadata.c.id.type, - sqlalchemy.types.INTEGER)) - self.assertTrue(isinstance(snapshot_metadata.c.snapshot_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(snapshot_metadata.c.key.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(snapshot_metadata.c.value.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(snapshot_metadata.c.created_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(snapshot_metadata.c.updated_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(snapshot_metadata.c.deleted_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(snapshot_metadata.c.deleted.type, + sqlalchemy.types.BOOLEAN) + self.assertIsInstance(snapshot_metadata.c.deleted.type, + sqlalchemy.types.BOOLEAN) + self.assertIsInstance(snapshot_metadata.c.id.type, + sqlalchemy.types.INTEGER) + self.assertIsInstance(snapshot_metadata.c.snapshot_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(snapshot_metadata.c.key.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(snapshot_metadata.c.value.type, + sqlalchemy.types.VARCHAR) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 8) @@ -652,26 +652,26 @@ class TestMigrations(test.TestCase): metadata, autoload=True) - self.assertTrue(isinstance(transfers.c.created_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(transfers.c.updated_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(transfers.c.deleted_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(transfers.c.deleted.type, - sqlalchemy.types.BOOLEAN)) - self.assertTrue(isinstance(transfers.c.id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(transfers.c.volume_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(transfers.c.display_name.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(transfers.c.salt.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(transfers.c.crypt_hash.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(transfers.c.expires_at.type, - sqlalchemy.types.DATETIME)) + self.assertIsInstance(transfers.c.created_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(transfers.c.updated_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(transfers.c.deleted_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(transfers.c.deleted.type, + sqlalchemy.types.BOOLEAN) + self.assertIsInstance(transfers.c.id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(transfers.c.volume_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(transfers.c.display_name.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(transfers.c.salt.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(transfers.c.crypt_hash.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(transfers.c.expires_at.type, + sqlalchemy.types.DATETIME) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 9) @@ -706,8 +706,8 @@ class TestMigrations(test.TestCase): for column in volumes_v10.c: self.assertTrue(volumes.c.__contains__(column.name)) - self.assertTrue(isinstance(volumes.c.bootable.type, - sqlalchemy.types.BOOLEAN)) + self.assertIsInstance(volumes.c.bootable.type, + sqlalchemy.types.BOOLEAN) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 10) metadata = sqlalchemy.schema.MetaData() @@ -736,8 +736,8 @@ class TestMigrations(test.TestCase): volumes = sqlalchemy.Table('volumes', metadata, autoload=True) - self.assertTrue(isinstance(volumes.c.attached_host.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(volumes.c.attached_host.type, + sqlalchemy.types.VARCHAR) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 11) metadata = sqlalchemy.schema.MetaData() @@ -762,8 +762,8 @@ class TestMigrations(test.TestCase): volumes = sqlalchemy.Table('volumes', metadata, autoload=True) - self.assertTrue(isinstance(volumes.c.provider_geometry.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(volumes.c.provider_geometry.type, + sqlalchemy.types.VARCHAR) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 12) metadata = sqlalchemy.schema.MetaData() @@ -788,8 +788,8 @@ class TestMigrations(test.TestCase): volumes = sqlalchemy.Table('volumes', metadata, autoload=True) - self.assertTrue(isinstance(volumes.c._name_id.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(volumes.c._name_id.type, + sqlalchemy.types.VARCHAR) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 13) metadata = sqlalchemy.schema.MetaData() @@ -859,29 +859,29 @@ class TestMigrations(test.TestCase): # encryption key UUID volumes = sqlalchemy.Table('volumes', metadata, autoload=True) self.assertIn('encryption_key_id', volumes.c) - self.assertTrue(isinstance(volumes.c.encryption_key_id.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(volumes.c.encryption_key_id.type, + sqlalchemy.types.VARCHAR) snapshots = sqlalchemy.Table('snapshots', metadata, autoload=True) self.assertIn('encryption_key_id', snapshots.c) - self.assertTrue(isinstance(snapshots.c.encryption_key_id.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(snapshots.c.encryption_key_id.type, + sqlalchemy.types.VARCHAR) self.assertIn('volume_type_id', snapshots.c) - self.assertTrue(isinstance(snapshots.c.volume_type_id.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(snapshots.c.volume_type_id.type, + sqlalchemy.types.VARCHAR) # encryption types table encryption = sqlalchemy.Table('encryption', metadata, autoload=True) - self.assertTrue(isinstance(encryption.c.volume_type_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(encryption.c.cipher.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(encryption.c.key_size.type, - sqlalchemy.types.INTEGER)) - self.assertTrue(isinstance(encryption.c.provider.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(encryption.c.volume_type_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(encryption.c.cipher.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(encryption.c.key_size.type, + sqlalchemy.types.INTEGER) + self.assertIsInstance(encryption.c.provider.type, + sqlalchemy.types.VARCHAR) # downgrade schema migration_api.downgrade(engine, TestMigrations.REPOSITORY, 16) @@ -913,22 +913,22 @@ class TestMigrations(test.TestCase): qos_specs = sqlalchemy.Table('quality_of_service_specs', metadata, autoload=True) - self.assertTrue(isinstance(qos_specs.c.created_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(qos_specs.c.updated_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(qos_specs.c.deleted_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(qos_specs.c.deleted.type, - sqlalchemy.types.BOOLEAN)) - self.assertTrue(isinstance(qos_specs.c.id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(qos_specs.c.specs_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(qos_specs.c.key.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(qos_specs.c.value.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(qos_specs.c.created_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(qos_specs.c.updated_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(qos_specs.c.deleted_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(qos_specs.c.deleted.type, + sqlalchemy.types.BOOLEAN) + self.assertIsInstance(qos_specs.c.id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(qos_specs.c.specs_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(qos_specs.c.key.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(qos_specs.c.value.type, + sqlalchemy.types.VARCHAR) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 17) @@ -949,8 +949,8 @@ class TestMigrations(test.TestCase): volumes = sqlalchemy.Table('volumes', metadata, autoload=True) - self.assertTrue(isinstance(volumes.c.migration_status.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(volumes.c.migration_status.type, + sqlalchemy.types.VARCHAR) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 18) metadata = sqlalchemy.schema.MetaData() @@ -979,24 +979,24 @@ class TestMigrations(test.TestCase): metadata, autoload=True) - self.assertTrue(isinstance(volume_admin_metadata.c.created_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(volume_admin_metadata.c.updated_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(volume_admin_metadata.c.deleted_at.type, - sqlalchemy.types.DATETIME)) - self.assertTrue(isinstance(volume_admin_metadata.c.deleted.type, - sqlalchemy.types.BOOLEAN)) - self.assertTrue(isinstance(volume_admin_metadata.c.deleted.type, - sqlalchemy.types.BOOLEAN)) - self.assertTrue(isinstance(volume_admin_metadata.c.id.type, - sqlalchemy.types.INTEGER)) - self.assertTrue(isinstance(volume_admin_metadata.c.volume_id.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(volume_admin_metadata.c.key.type, - sqlalchemy.types.VARCHAR)) - self.assertTrue(isinstance(volume_admin_metadata.c.value.type, - sqlalchemy.types.VARCHAR)) + self.assertIsInstance(volume_admin_metadata.c.created_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(volume_admin_metadata.c.updated_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(volume_admin_metadata.c.deleted_at.type, + sqlalchemy.types.DATETIME) + self.assertIsInstance(volume_admin_metadata.c.deleted.type, + sqlalchemy.types.BOOLEAN) + self.assertIsInstance(volume_admin_metadata.c.deleted.type, + sqlalchemy.types.BOOLEAN) + self.assertIsInstance(volume_admin_metadata.c.id.type, + sqlalchemy.types.INTEGER) + self.assertIsInstance(volume_admin_metadata.c.volume_id.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(volume_admin_metadata.c.key.type, + sqlalchemy.types.VARCHAR) + self.assertIsInstance(volume_admin_metadata.c.value.type, + sqlalchemy.types.VARCHAR) migration_api.downgrade(engine, TestMigrations.REPOSITORY, 19) diff --git a/cinder/tests/test_quota.py b/cinder/tests/test_quota.py index 3c2c1fb87..99b2ed2c9 100644 --- a/cinder/tests/test_quota.py +++ b/cinder/tests/test_quota.py @@ -380,14 +380,14 @@ class QuotaEngineTestCase(test.TestCase): quota_obj = quota.QuotaEngine() self.assertEqual(quota_obj.resources, {}) - self.assertTrue(isinstance(quota_obj._driver, quota.DbQuotaDriver)) + self.assertIsInstance(quota_obj._driver, quota.DbQuotaDriver) def test_init_override_string(self): quota_obj = quota.QuotaEngine( quota_driver_class='cinder.tests.test_quota.FakeDriver') self.assertEqual(quota_obj.resources, {}) - self.assertTrue(isinstance(quota_obj._driver, FakeDriver)) + self.assertIsInstance(quota_obj._driver, FakeDriver) def test_init_override_obj(self): quota_obj = quota.QuotaEngine(quota_driver_class=FakeDriver) -- 2.45.2