From d729a6153364fbedb09f5d433ecec4c0ae603914 Mon Sep 17 00:00:00 2001 From: gecong1973 Date: Wed, 28 Sep 2016 16:42:18 +0800 Subject: [PATCH] Change assertTrue(isinstance()) by optimal assert Some of tests use different method of assertTrue(isinstance(A, B)) or assertEqual(type(A), B). The correct way is to use assertIsInstance(A, B) provided by testtools Change-Id: Id6f69e42bb933d37fab0ad0380fe238f81910070 --- toscaparser/tests/test_functions.py | 24 ++++++++++---------- toscaparser/tests/test_scalarunit.py | 6 ++--- toscaparser/tests/test_toscatpl.py | 4 ++-- toscaparser/tests/test_toscatplvalidation.py | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/toscaparser/tests/test_functions.py b/toscaparser/tests/test_functions.py index 7f3782c2..d4a79927 100644 --- a/toscaparser/tests/test_functions.py +++ b/toscaparser/tests/test_functions.py @@ -57,7 +57,7 @@ class IntrinsicFunctionsTest(TestCase): wordpress = self._get_node('wordpress') operation = self._get_operation(wordpress.interfaces, 'configure') wp_db_password = operation.inputs['wp_db_password'] - self.assertTrue(isinstance(wp_db_password, functions.GetProperty)) + self.assertIsInstance(wp_db_password, functions.GetProperty) result = wp_db_password.result() self.assertEqual('wp_pass', result) @@ -65,7 +65,7 @@ class IntrinsicFunctionsTest(TestCase): wordpress = self._get_node('wordpress') operation = self._get_operation(wordpress.interfaces, 'configure') wp_db_user = operation.inputs['wp_db_user'] - self.assertTrue(isinstance(wp_db_user, functions.GetProperty)) + self.assertIsInstance(wp_db_user, functions.GetProperty) result = wp_db_user.result() self.assertEqual('my_db_user', result) @@ -84,7 +84,7 @@ class IntrinsicFunctionsTest(TestCase): props = mysql_dbms.get_properties() for key in props.keys(): prop = props[key] - self.assertTrue(isinstance(prop.value, functions.GetInput)) + self.assertIsInstance(prop.value, functions.GetInput) expected_inputs.remove(prop.value.input_name) self.assertListEqual(expected_inputs, []) @@ -130,11 +130,11 @@ class IntrinsicFunctionsTest(TestCase): })) operation = self._get_operation(mysql_database.interfaces, 'configure') db_port = operation.inputs['db_port'] - self.assertTrue(isinstance(db_port, functions.GetProperty)) + self.assertIsInstance(db_port, functions.GetProperty) result = db_port.result() self.assertEqual(3306, result) test = operation.inputs['test'] - self.assertTrue(isinstance(test, functions.GetProperty)) + self.assertIsInstance(test, functions.GetProperty) result = test.result() self.assertEqual(1, result) @@ -148,10 +148,10 @@ class IntrinsicFunctionsTest(TestCase): 'db_root_pwd': '1234'})) operation = self._get_operation(webserver.interfaces, 'configure') wp_endpoint_prot = operation.inputs['wp_endpoint_protocol'] - self.assertTrue(isinstance(wp_endpoint_prot, functions.GetProperty)) + self.assertIsInstance(wp_endpoint_prot, functions.GetProperty) self.assertEqual('tcp', wp_endpoint_prot.result()) wp_list_prop = operation.inputs['wp_list_prop'] - self.assertTrue(isinstance(wp_list_prop, functions.GetProperty)) + self.assertIsInstance(wp_list_prop, functions.GetProperty) self.assertEqual(3, wp_list_prop.result()) def test_get_property_with_capabilties_inheritance(self): @@ -164,7 +164,7 @@ class IntrinsicFunctionsTest(TestCase): 'db_root_pwd': '1234'})) operation = self._get_operation(some_node.interfaces, 'configure') some_input = operation.inputs['some_input'] - self.assertTrue(isinstance(some_input, functions.GetProperty)) + self.assertIsInstance(some_input, functions.GetProperty) self.assertEqual('someval', some_input.result()) def test_get_property_source_target_keywords(self): @@ -182,10 +182,10 @@ class IntrinsicFunctionsTest(TestCase): operation = self._get_operation(rel_template.interfaces, 'pre_configure_source') target_test = operation.inputs['target_test'] - self.assertTrue(isinstance(target_test, functions.GetProperty)) + self.assertIsInstance(target_test, functions.GetProperty) self.assertEqual(1, target_test.result()) source_port = operation.inputs['source_port'] - self.assertTrue(isinstance(source_port, functions.GetProperty)) + self.assertIsInstance(source_port, functions.GetProperty) self.assertEqual(3306, source_port.result()) @@ -306,9 +306,9 @@ class GetAttributeTest(TestCase): operation = self._get_operation(rel_template.interfaces, 'pre_configure_source') target_test = operation.inputs['target_test'] - self.assertTrue(isinstance(target_test, functions.GetAttribute)) + self.assertIsInstance(target_test, functions.GetAttribute) source_port = operation.inputs['source_port'] - self.assertTrue(isinstance(source_port, functions.GetAttribute)) + self.assertIsInstance(source_port, functions.GetAttribute) def test_get_attribute_with_nested_params(self): self._load_template( diff --git a/toscaparser/tests/test_scalarunit.py b/toscaparser/tests/test_scalarunit.py index fcd1c834..09a24b62 100644 --- a/toscaparser/tests/test_scalarunit.py +++ b/toscaparser/tests/test_scalarunit.py @@ -243,7 +243,7 @@ class GetNumFromScalarUnitSizeNegative(TestCase): (ScalarUnit_Size(self.InputMemSize). get_num_from_scalar_unit(self.UserInputUnit)) except Exception as error: - self.assertTrue(isinstance(error, ValueError)) + self.assertIsInstance(error, ValueError) self.assertEqual(_('The unit "qB" is not valid. Valid units are ' '"[\'B\', \'GB\', \'GiB\', \'KiB\', \'MB\', ' '\'MiB\', \'TB\', \'TiB\', \'kB\']".'), @@ -260,7 +260,7 @@ class GetNumFromScalarUnitFrequencyNegative(TestCase): (ScalarUnit_Frequency(self.InputFrequency). get_num_from_scalar_unit(self.UserInputUnit)) except Exception as error: - self.assertTrue(isinstance(error, ValueError)) + self.assertIsInstance(error, ValueError) self.assertEqual(_('The unit "Jz" is not valid. Valid units are ' '"[\'GHz\', \'Hz\', \'MHz\', \'kHz\']".'), error.__str__()) @@ -276,7 +276,7 @@ class GetNumFromScalarUnitTimeNegative(TestCase): (ScalarUnit_Time(self.InputTime). get_num_from_scalar_unit(self.UserInputUnit)) except Exception as error: - self.assertTrue(isinstance(error, ValueError)) + self.assertIsInstance(error, ValueError) self.assertEqual(_('"Jz" is not a valid scalar-unit.'), error.__str__()) diff --git a/toscaparser/tests/test_toscatpl.py b/toscaparser/tests/test_toscatpl.py index 6bf5660f..b294529c 100644 --- a/toscaparser/tests/test_toscatpl.py +++ b/toscaparser/tests/test_toscatpl.py @@ -174,14 +174,14 @@ class ToscaTemplateTest(TestCase): self.assertEqual(3, len(interface.inputs)) TestCase.skip(self, 'bug #1440247') wp_db_port = interface.inputs['wp_db_port'] - self.assertTrue(isinstance(wp_db_port, GetProperty)) + self.assertIsInstance(wp_db_port, GetProperty) self.assertEqual('get_property', wp_db_port.name) self.assertEqual(['SELF', 'database_endpoint', 'port'], wp_db_port.args) result = wp_db_port.result() - self.assertTrue(isinstance(result, GetInput)) + self.assertIsInstance(result, GetInput) else: raise AssertionError( 'Unexpected interface: {0}'.format(interface.name)) diff --git a/toscaparser/tests/test_toscatplvalidation.py b/toscaparser/tests/test_toscatplvalidation.py index dc9b7d51..f9922ab7 100644 --- a/toscaparser/tests/test_toscatplvalidation.py +++ b/toscaparser/tests/test_toscatplvalidation.py @@ -381,7 +381,7 @@ heat-translator/master/translator/tests/data/custom_types/wordpress.yaml try: output.validate() except Exception as err: - self.assertTrue(isinstance(err, exception.UnknownFieldError)) + self.assertIsInstance(err, exception.UnknownFieldError) self.assertEqual(_('Output "server_address" contains unknown ' 'field "descriptions". Refer to the definition ' 'to verify valid values.'),