From 33a459c2c309670b0fcb2fcac5d128e802be7a69 Mon Sep 17 00:00:00 2001 From: zhang-jinnan Date: Fri, 14 Feb 2014 13:14:24 +0800 Subject: [PATCH] Replace assertEqual(None, *) with assertIsNone in tests Replace assertEqual(None, *) with assertIsNone in tests to have more clear messages in case of failure. Change-Id: I0d38a82e78fbe94657ab9a71c08422007843d179 --- HACKING.rst | 2 ++ nova/hacking/checks.py | 17 +++++++++++++++++ .../compute/contrib/test_aggregates.py | 2 +- .../compute/plugins/v3/test_aggregates.py | 2 +- nova/tests/compute/test_compute_mgr.py | 2 +- nova/tests/compute/test_vmmode.py | 2 +- nova/tests/objects/test_dns_domain.py | 2 +- nova/tests/objects/test_fixed_ip.py | 4 ++-- nova/tests/test_hacking.py | 10 ++++++++++ nova/tests/virt/baremetal/test_driver.py | 2 +- nova/tests/virt/test_cpu.py | 2 +- nova/tests/virt/test_virt.py | 4 ++-- nova/tests/virt/xenapi/test_vm_utils.py | 2 +- 13 files changed, 41 insertions(+), 12 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 0d435d420f66..7399e5132431 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -28,6 +28,8 @@ Nova Specific Commandments assertIsInstance(A, B). - [N317] Change assertEqual(type(A), B) by optimal assert like assertIsInstance(A, B) +- [N318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert like + assertIsNone(A) Creating Unit Tests ------------------- diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index 1203e63390d8..9cfb1675f0b6 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -44,6 +44,10 @@ asse_trueinst_re = re.compile( asse_equal_type_re = re.compile( r"(.)*assertEqual\(type\((\w|\.|\'|\"|\[|\])+\), " "(\w|\.|\'|\"|\[|\])+\)") +asse_equal_end_with_none_re = re.compile( + r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)") +asse_equal_start_with_none_re = re.compile( + r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)") def import_no_db_in_virt(logical_line, filename): @@ -177,6 +181,18 @@ def assert_equal_type(logical_line): yield (0, "N317: assertEqual(type(A), B) sentences not allowed") +def assert_equal_none(logical_line): + """Check for assertEqual(A, None) or assertEqual(None, A) sentences + + N318 + """ + res = (asse_equal_start_with_none_re.match(logical_line) or + asse_equal_end_with_none_re.match(logical_line)) + if res: + yield (0, "N318: assertEqual(A, None) or assertEqual(None, A) " + "sentences not allowed") + + def factory(register): register(import_no_db_in_virt) register(no_db_session_in_public_api) @@ -188,3 +204,4 @@ def factory(register): register(no_author_tags) register(assert_true_instance) register(assert_equal_type) + register(assert_equal_none) diff --git a/nova/tests/api/openstack/compute/contrib/test_aggregates.py b/nova/tests/api/openstack/compute/contrib/test_aggregates.py index ff01da80bdef..71704d4467b0 100644 --- a/nova/tests/api/openstack/compute/contrib/test_aggregates.py +++ b/nova/tests/api/openstack/compute/contrib/test_aggregates.py @@ -130,7 +130,7 @@ class AggregateTestCase(test.NoDBTestCase): def stub_create_aggregate(context, name, availability_zone): self.assertEqual(context, self.context, "context") self.assertEqual("test", name, "name") - self.assertEqual(None, availability_zone, "availability_zone") + self.assertIsNone(availability_zone, "availability_zone") return AGGREGATE self.stubs.Set(self.controller.api, "create_aggregate", stub_create_aggregate) diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_aggregates.py b/nova/tests/api/openstack/compute/plugins/v3/test_aggregates.py index fe52d04fe0dc..2af74ce76c0a 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_aggregates.py +++ b/nova/tests/api/openstack/compute/plugins/v3/test_aggregates.py @@ -129,7 +129,7 @@ class AggregateTestCase(test.NoDBTestCase): def stub_create_aggregate(context, name, availability_zone): self.assertEqual(context, self.context, "context") self.assertEqual("test", name, "name") - self.assertEqual(None, availability_zone, "availability_zone") + self.assertIsNone(availability_zone, "availability_zone") return AGGREGATE self.stubs.Set(self.controller.api, "create_aggregate", stub_create_aggregate) diff --git a/nova/tests/compute/test_compute_mgr.py b/nova/tests/compute/test_compute_mgr.py index 27230202c3d6..86be098695dd 100644 --- a/nova/tests/compute/test_compute_mgr.py +++ b/nova/tests/compute/test_compute_mgr.py @@ -339,7 +339,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase): self.mox.ReplayAll() self.compute._init_instance(self.context, instance) - self.assertEqual(None, instance.task_state) + self.assertIsNone(instance.task_state) def test_init_instance_reverts_crashed_migration_from_active(self): self._test_init_instance_reverts_crashed_migrations( diff --git a/nova/tests/compute/test_vmmode.py b/nova/tests/compute/test_vmmode.py index 812e76e82350..09d8a5b9b766 100644 --- a/nova/tests/compute/test_vmmode.py +++ b/nova/tests/compute/test_vmmode.py @@ -60,7 +60,7 @@ class ComputeVMModeTest(test.NoDBTestCase): def test_name_none(self): mode = vm_mode.name(None) - self.assertEqual(None, mode) + self.assertIsNone(mode) def test_name_invalid(self): self.assertRaises(exception.Invalid, vm_mode.name, 'invalid') diff --git a/nova/tests/objects/test_dns_domain.py b/nova/tests/objects/test_dns_domain.py index 3791d172092d..141f6f6c05bf 100644 --- a/nova/tests/objects/test_dns_domain.py +++ b/nova/tests/objects/test_dns_domain.py @@ -67,7 +67,7 @@ class _TestDNSDomain(object): dns_domain.DNSDomain.delete_by_domain(self.context.elevated(), 'domain') dnsd = dns_domain.DNSDomain.get_by_domain(self.context, 'domain') - self.assertEqual(None, dnsd) + self.assertIsNone(dnsd) def test_get_all(self): with mock.patch.object(db, 'dnsdomain_get_all') as get: diff --git a/nova/tests/objects/test_fixed_ip.py b/nova/tests/objects/test_fixed_ip.py index e6863326c03d..235a16878abc 100644 --- a/nova/tests/objects/test_fixed_ip.py +++ b/nova/tests/objects/test_fixed_ip.py @@ -122,7 +122,7 @@ class _TestFixedIPObject(object): 'instance']) self._compare(fixedip, db_fixed) self.assertEqual(db_fixed['network']['uuid'], fixedip.network.uuid) - self.assertEqual(None, fixedip.instance) + self.assertIsNone(fixedip.instance) self.assertFalse(network_get.called) self.assertFalse(instance_get.called) @@ -209,7 +209,7 @@ class _TestFixedIPObject(object): fixedip.obj_reset_changes() fixedip.disassociate() disassociate.assert_called_once_with(self.context, '1.2.3.4') - self.assertEqual(None, fixedip.instance_uuid) + self.assertIsNone(fixedip.instance_uuid) @mock.patch('nova.db.fixed_ip_get_all') def test_get_all(self, get_all): diff --git a/nova/tests/test_hacking.py b/nova/tests/test_hacking.py index 33e4df9b9705..09b218eb1190 100644 --- a/nova/tests/test_hacking.py +++ b/nova/tests/test_hacking.py @@ -70,3 +70,13 @@ class HackingTestCase(test.NoDBTestCase): self.assertEqual( len(list(checks.assert_equal_type("self.assertTrue()"))), 0) + + def test_assert_equal_none(self): + self.assertEqual(len(list(checks.assert_equal_none( + "self.assertEqual(A, None)"))), 1) + + self.assertEqual(len(list(checks.assert_equal_none( + "self.assertEqual(None, A)"))), 1) + + self.assertEqual( + len(list(checks.assert_equal_none("self.assertIsNone()"))), 0) diff --git a/nova/tests/virt/baremetal/test_driver.py b/nova/tests/virt/baremetal/test_driver.py index fbb76979165a..af1eeb936671 100644 --- a/nova/tests/virt/baremetal/test_driver.py +++ b/nova/tests/virt/baremetal/test_driver.py @@ -194,7 +194,7 @@ class BareMetalDriverWithDBTestCase(bm_db_base.BMDBTestCase): self.assertEqual(row['instance_name'], node['instance']['hostname']) instance = main_db.instance_get_by_uuid(self.context, node['instance']['uuid']) - self.assertEqual(instance['default_ephemeral_device'], None) + self.assertIsNone(instance['default_ephemeral_device']) def _test_rebuild(self, ephemeral): node = self._create_node(ephemeral=ephemeral) diff --git a/nova/tests/virt/test_cpu.py b/nova/tests/virt/test_cpu.py index 2f41fbe28428..52d4d376259d 100644 --- a/nova/tests/virt/test_cpu.py +++ b/nova/tests/virt/test_cpu.py @@ -22,7 +22,7 @@ class CpuSetTestCase(test.NoDBTestCase): def test_get_cpuset_ids_none_returns_none(self): self.flags(vcpu_pin_set=None) cpuset_ids = cpu.get_cpuset_ids() - self.assertEqual(None, cpuset_ids) + self.assertIsNone(cpuset_ids) def test_get_cpuset_ids_valid_syntax_works(self): self.flags(vcpu_pin_set="1") diff --git a/nova/tests/virt/test_virt.py b/nova/tests/virt/test_virt.py index cd4708202a54..259db5eb9b68 100644 --- a/nova/tests/virt/test_virt.py +++ b/nova/tests/virt/test_virt.py @@ -161,7 +161,7 @@ class TestDiskImage(test.NoDBTestCase): self.assertEqual(diskimage._mounter, fakemount) self.assertEqual(dev, '/dev/fake') diskimage.umount() - self.assertEqual(diskimage._mounter, None) + self.assertIsNone(diskimage._mounter) @mock.patch('__builtin__.open') def test_teardown(self, mock_open): @@ -181,7 +181,7 @@ class TestDiskImage(test.NoDBTestCase): self.assertEqual(diskimage._mounter, fakemount) self.assertEqual(dev, '/dev/fake') diskimage.teardown() - self.assertEqual(diskimage._mounter, None) + self.assertIsNone(diskimage._mounter) class TestVirtDisk(test.NoDBTestCase): diff --git a/nova/tests/virt/xenapi/test_vm_utils.py b/nova/tests/virt/xenapi/test_vm_utils.py index 3a72bae09a06..0717c945998f 100644 --- a/nova/tests/virt/xenapi/test_vm_utils.py +++ b/nova/tests/virt/xenapi/test_vm_utils.py @@ -1917,7 +1917,7 @@ class ImportMigratedDisksTestCase(VMUtilsTestBase): session = mock.Mock() vdi_ref = 'vdi_ref' vdi_rec = {'sm_config': {}} - self.assertEqual(None, vm_utils._get_vhd_parent_uuid(session, + self.assertIsNone(vm_utils._get_vhd_parent_uuid(session, vdi_ref, vdi_rec)) self.assertFalse(session.call_xenapi.called)