use the right assertion

makes the error clearer on failure

Change-Id: I0c4d15b159111b821975f001f3e35ed4b1193ff4
This commit is contained in:
Eyal 2019-02-18 12:15:12 +02:00
parent c10cb80b6e
commit 7bed18c5b5
7 changed files with 37 additions and 38 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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(

View File

@ -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 = []

View File

@ -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)