From 7bed18c5b52afcf6e496ab53e8c57abe5245da9f Mon Sep 17 00:00:00 2001 From: Eyal Date: Mon, 18 Feb 2019 12:15:12 +0200 Subject: [PATCH] use the right assertion makes the error clearer on failure Change-Id: I0c4d15b159111b821975f001f3e35ed4b1193ff4 --- vitrage/tests/base.py | 8 ++-- vitrage/tests/functional/api/test_versions.py | 2 +- .../tests/functional/api/v1/test_keycloak.py | 2 +- .../tests/functional/api/v1/test_noauth.py | 20 +++++----- .../listener_service/test_listener_service.py | 2 +- .../test_jaccard_correlation.py | 39 +++++++++---------- .../unit/snmp_parsing/test_snmp_parsing.py | 2 +- 7 files changed, 37 insertions(+), 38 deletions(-) diff --git a/vitrage/tests/base.py b/vitrage/tests/base.py index 637b6564f..11e374db4 100644 --- a/vitrage/tests/base.py +++ b/vitrage/tests/base.py @@ -28,14 +28,14 @@ IsEmpty = lambda: HasLength(0) class BaseTest(base.BaseTestCase): """Test case base class for all unit tests.""" - def assert_list_equal(self, l1, l2): + def assert_list_equal(self, l1, l2, message=None): if tuple(sys.version_info)[0:2] < (2, 7): # for python 2.6 compatibility - self.assertEqual(l1, l2) + self.assertEqual(l1, l2, message) else: - super(BaseTest, self).assertListEqual(l1, l2) + super(BaseTest, self).assertListEqual(l1, l2, message) - def assert_dict_equal(self, d1, d2, message): + def assert_dict_equal(self, d1, d2, message=None): if tuple(sys.version_info)[0:2] < (2, 7): # for python 2.6 compatibility self.assertEqual(d1, d2) diff --git a/vitrage/tests/functional/api/test_versions.py b/vitrage/tests/functional/api/test_versions.py index 44549e157..33a6e5e0d 100644 --- a/vitrage/tests/functional/api/test_versions.py +++ b/vitrage/tests/functional/api/test_versions.py @@ -33,4 +33,4 @@ class TestVersions(api.FunctionalTest): def test_versions(self): data = self.get_json('/') - self.assertEqual(VERSIONS_RESPONSE, data) + self.assert_dict_equal(VERSIONS_RESPONSE, data) diff --git a/vitrage/tests/functional/api/v1/test_keycloak.py b/vitrage/tests/functional/api/v1/test_keycloak.py index 18bdc25e2..852a7a207 100644 --- a/vitrage/tests/functional/api/v1/test_keycloak.py +++ b/vitrage/tests/functional/api/v1/test_keycloak.py @@ -97,7 +97,7 @@ class KeycloakTest(FunctionalTest): resp = self.post_json('/topology/', expect_errors=True) self.assertEqual('401 Unauthorized', resp.status) - self.assertEqual(NO_TOKEN_ERROR_MSG, resp.json) + self.assert_dict_equal(NO_TOKEN_ERROR_MSG, resp.json) @mock.patch('jwt.decode', return_value=TOKEN) @requests_mock.Mocker() diff --git a/vitrage/tests/functional/api/v1/test_noauth.py b/vitrage/tests/functional/api/v1/test_noauth.py index ae9a83ee1..69f5a2248 100755 --- a/vitrage/tests/functional/api/v1/test_noauth.py +++ b/vitrage/tests/functional/api/v1/test_noauth.py @@ -63,7 +63,7 @@ class NoAuthTest(FunctionalTest): self.assertEqual(1, request.client.call.call_count) self.assertEqual('200 OK', resp.status) - self.assertEqual({}, resp.json) + self.assert_is_empty(resp.json) def test_noauth_mode_list_alarms(self): with mock.patch('pecan.request') as request: @@ -72,7 +72,7 @@ class NoAuthTest(FunctionalTest): data = self.get_json('/alarm/', params=params) self.assertEqual(1, request.client.call.call_count) - self.assertEqual([], data) + self.assert_is_empty(data) def test_noauth_mode_show_alarm(self): @@ -81,7 +81,7 @@ class NoAuthTest(FunctionalTest): data = self.get_json('/alarm/1234') self.assertEqual(1, request.client.call.call_count) - self.assertEqual({}, data) + self.assert_is_empty(data) def test_noauth_mode_show_alarm_count(self): with mock.patch('pecan.request') as request: @@ -91,7 +91,7 @@ class NoAuthTest(FunctionalTest): self.assertEqual(1, request.client.call.call_count) self.assertEqual('200 OK', resp.status) - self.assertEqual({}, resp.json) + self.assert_is_empty(resp.json) def test_noauth_mode_list_resources(self): @@ -101,7 +101,7 @@ class NoAuthTest(FunctionalTest): data = self.get_json('/resources/', params=params) self.assertEqual(1, request.client.call.call_count) - self.assertEqual([], data) + self.assert_is_empty(data) def test_noauth_mode_show_resource(self): @@ -110,7 +110,7 @@ class NoAuthTest(FunctionalTest): data = self.get_json('/resources/1234') self.assertEqual(1, request.client.call.call_count) - self.assertEqual({}, data) + self.assert_is_empty(data) def test_noauth_mode_list_templates(self): @@ -119,7 +119,7 @@ class NoAuthTest(FunctionalTest): data = self.get_json('/template/') self.assertEqual(1, request.storage.templates.query.call_count) - self.assertEqual([], data) + self.assert_is_empty(data) def test_noauth_mode_show_template(self): @@ -129,7 +129,7 @@ class NoAuthTest(FunctionalTest): data = self.get_json('/template/1234') self.assertEqual(1, request.storage.templates.query.call_count) - self.assertEqual({}, data) + self.assert_is_empty(data) def test_noauth_mode_validate_template(self): @@ -140,7 +140,7 @@ class NoAuthTest(FunctionalTest): self.assertEqual(1, request.client.call.call_count) self.assertEqual('200 OK', resp.status) - self.assertEqual({}, resp.json) + self.assert_is_empty(resp.json) def test_noauth_mode_get_rca(self): @@ -150,4 +150,4 @@ class NoAuthTest(FunctionalTest): data = self.get_json('/rca/1234/', params=params) self.assertEqual(1, request.client.call.call_count) - self.assertEqual({}, data) + self.assert_is_empty(data) diff --git a/vitrage/tests/functional/datasources/listener_service/test_listener_service.py b/vitrage/tests/functional/datasources/listener_service/test_listener_service.py index 899b59cf1..71830f403 100644 --- a/vitrage/tests/functional/datasources/listener_service/test_listener_service.py +++ b/vitrage/tests/functional/datasources/listener_service/test_listener_service.py @@ -47,7 +47,7 @@ class TestListenerService(base.BaseTest): self.actual_events = [] def _assert_events(self): - self.assertEqual(self.excepted_events, self.actual_events) + self.assert_list_equal(self.excepted_events, self.actual_events) def _generate_events(self, update_events): gen_list = mock_driver.simple_aodh_alarm_notification_generators( diff --git a/vitrage/tests/unit/machine_learning/jaccard_correlation/test_jaccard_correlation.py b/vitrage/tests/unit/machine_learning/jaccard_correlation/test_jaccard_correlation.py index 5ccc7b443..a715aa434 100644 --- a/vitrage/tests/unit/machine_learning/jaccard_correlation/test_jaccard_correlation.py +++ b/vitrage/tests/unit/machine_learning/jaccard_correlation/test_jaccard_correlation.py @@ -222,14 +222,13 @@ class JaccardCorrelationTest(base.BaseTest): self.data_manager.append_active(alarm_id, timestamp) # assert all alarm ids are right - for i in range(len(self.alarm_ids)): - self.assertEqual(self.alarm_ids[i], real_alarm_ids[i], '') + self.assert_list_equal(self.alarm_ids, real_alarm_ids) - self.assertEqual(expected_active_start_dict, - self.data_manager.active_start_times) + self.assert_dict_equal(expected_active_start_dict, + self.data_manager.active_start_times) - self.assertEqual({}, self.data_manager.alarms_activity) - self.assertEqual({}, self.data_manager.alarms_intersects) + self.assert_is_empty(self.data_manager.alarms_activity) + self.assert_is_empty(self.data_manager.alarms_intersects) def _test_flush_accumulations(self): @@ -238,8 +237,8 @@ class JaccardCorrelationTest(base.BaseTest): time.sleep(2) self.data_manager.flush_accumulations() - self.assertEqual(prev_active_start_dict, - self.data_manager.active_start_times) + self.assert_dict_equal(prev_active_start_dict, + self.data_manager.active_start_times) expected_activity_dict_len = len(ACTIVE_ALARMS) self.assertThat(self.data_manager.alarms_activity, @@ -266,7 +265,7 @@ class JaccardCorrelationTest(base.BaseTest): alarm.get(VProps.VITRAGE_RESOURCE_TYPE), alarm_name) - self.assertEqual(expected_alarm_id, alarm_id, '') + self.assertEqual(expected_alarm_id, alarm_id) self.inactivate_timestamps[alarm_id] = timestamp deleted_alarm_ids.append(alarm_id) @@ -274,12 +273,12 @@ class JaccardCorrelationTest(base.BaseTest): self.data_manager.append_inactive(alarm_id, timestamp) # assert all deleted alarms has same alarm ids as activated alarms - self.assertEqual(self.alarm_ids, deleted_alarm_ids) + self.assert_list_equal(self.alarm_ids, deleted_alarm_ids) # all alarm are inactive at this moment expected_active_start_dict = {} - self.assertEqual(expected_active_start_dict, - self.data_manager.active_start_times) + self.assert_dict_equal(expected_active_start_dict, + self.data_manager.active_start_times) expected_activity_dict = {} @@ -288,8 +287,8 @@ class JaccardCorrelationTest(base.BaseTest): self.inactivate_timestamps[alarm_id]\ - self.activate_timestamps[alarm_id] - self.assertEqual(expected_activity_dict, - self.data_manager.alarms_activity) + self.assert_dict_equal(expected_activity_dict, + self.data_manager.alarms_activity) expected_intersections_dict = {} @@ -304,8 +303,8 @@ class JaccardCorrelationTest(base.BaseTest): self.inactivate_timestamps[alarm_id]\ - self.activate_timestamps[alarm_id] - self.assertEqual(expected_intersections_dict, - self.data_manager.alarms_intersects) + self.assert_dict_equal(expected_intersections_dict, + self.data_manager.alarms_intersects) def _test_correlation_collection(self): self._test_correlation_list() @@ -337,7 +336,7 @@ class JaccardCorrelationTest(base.BaseTest): correlation) cnt += 1 - self.assertEqual(alarms_pairs, self.collection.correlation_list) + self.assert_list_equal(alarms_pairs, self.collection.correlation_list) def _test_correlations_aggregation(self): @@ -354,9 +353,9 @@ class JaccardCorrelationTest(base.BaseTest): if correlation_level[0] == CPriorities.LOW: cnt_low_correlations = len(correlation_level[1]) - self.assertEqual(cnt_high_correlations, 2, '') - self.assertEqual(cnt_med_correlations, 2, '') - self.assertEqual(cnt_low_correlations, 2, '') + self.assertEqual(cnt_high_correlations, 2) + self.assertEqual(cnt_med_correlations, 2) + self.assertEqual(cnt_low_correlations, 2) def _test_correlation_manager(self): report = [] diff --git a/vitrage/tests/unit/snmp_parsing/test_snmp_parsing.py b/vitrage/tests/unit/snmp_parsing/test_snmp_parsing.py index 06f2ec46a..abffac1ea 100644 --- a/vitrage/tests/unit/snmp_parsing/test_snmp_parsing.py +++ b/vitrage/tests/unit/snmp_parsing/test_snmp_parsing.py @@ -131,7 +131,7 @@ class TestSnmpParsing(base.BaseTest): def test_convert_binds_to_dict(self): parsing_service = SnmpParsingService(1, self.conf) dict_converted = parsing_service._convert_binds_to_dict(BINDS_REPORTED) - self.assertEqual(dict_converted, DICT_EXPECTED) + self.assert_dict_equal(dict_converted, DICT_EXPECTED) def test_get_event_type(self): parsing_service = SnmpParsingService(1, self.conf)