From 431dbe89247e1fdfedebe9f952f231cf310f5ab1 Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 30 Aug 2024 15:07:31 +0200 Subject: [PATCH] [testing] improved dictionary comparison. Failed tests have completely different picks (not only snrdb) --- .../test_autopickstation.py | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_autopickstation/test_autopickstation.py b/tests/test_autopickstation/test_autopickstation.py index a05e1131..f861c3f3 100644 --- a/tests/test_autopickstation/test_autopickstation.py +++ b/tests/test_autopickstation/test_autopickstation.py @@ -315,10 +315,10 @@ class TestAutopickStation(unittest.TestCase): compare_dicts(result, expected) -def compare_dicts(result, expected): +def run_dict_comparison(result, expected): for key, expected_value in expected.items(): if isinstance(expected_value, dict): - compare_dicts(result[key], expected[key]) + run_dict_comparison(result[key], expected[key]) else: res = result[key] if isinstance(res, UTCDateTime) and isinstance(expected_value, UTCDateTime): @@ -327,5 +327,21 @@ def compare_dicts(result, expected): assert expected_value == pytest.approx(res), f'{key}: {expected_value} != {res}' +def compare_dicts(result, expected): + try: + run_dict_comparison(result, expected) + except AssertionError: + raise AssertionError(f'Dictionaries not equal.' + f'\n<>: \n\n{pretty_print_dict(expected)}' + f'\n<>: \n{pretty_print_dict(result)}') + + +def pretty_print_dict(dct): + retstr = '' + for key, value in sorted(dct.items(), key=lambda x: x[0]): + retstr += f"{key} : {value}\n" + + return retstr + if __name__ == '__main__': unittest.main()