Coverage for django_napse/utils/dict_comparison.py: 8%
61 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
1"""Module that contains a function to compare two dictionaries."""
2from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
5def compare_responses(response1, response2, **kwargs):
6 """Compare two responses.
8 Kwargs:
9 ------
10 authorize_abstract_comparison (bool): Comparizon between an empty dict and a no empty dict is True. Same for list (default: False).
11 """
12 response1 = convert_response(response1)
13 response2 = convert_response(response2)
15 if response1 is None and response2 is None:
16 return True
17 if isinstance(response1, str) and isinstance(response2, str):
18 return True
19 if isinstance(response1, (int, float)) and isinstance(response2, (int, float)):
20 return True
21 if isinstance(response1, bool) and isinstance(response2, bool):
22 return True
23 if isinstance(response1, dict) and isinstance(response2, dict):
24 return compare_dicts(response1, response2, **kwargs)
25 if isinstance(response1, list) and isinstance(response2, list):
26 return compare_list(response1, response2, **kwargs)
27 if type(response1) != type(response2):
28 error_msg: str = f"{type(response1)} != {type(response2)}"
29 raise TypeError(error_msg)
30 return True
33def convert_response(response: ReturnList):
34 if isinstance(response, ReturnList):
35 return list(response)
36 if isinstance(response, ReturnDict):
37 return dict(response)
38 return response
41def compare_dicts(dict1, dict2, **kwargs):
42 if not isinstance(dict1, dict) or not isinstance(dict2, dict):
43 error_message = "One of the arguments is not a dictionary."
44 raise TypeError(error_message)
46 # Only dict type comparaison
47 if kwargs.get("authorize_abstract_comparison", False) and (dict1 == {} or dict2 == {}):
48 return True
50 if set(dict1.keys()) != set(dict2.keys()):
51 error_msg: str = f"Dictionaries do not contain the same keys ({len(dict1.keys())} != {len(dict2.keys())})\n"
52 error_msg += f"{dict1.keys()} != {dict2.keys()}"
53 raise ValueError(error_msg)
55 for key in dict1:
56 val1 = dict1[key]
57 val2 = dict2[key]
58 try:
59 compare_responses(val1, val2, **kwargs)
60 except TypeError as error:
61 error_msg = f"{error} on {key} key"
62 raise TypeError(error_msg) from error
63 return True
66def compare_list(list1, list2, **kwargs):
67 if not isinstance(list1, list) or not isinstance(list2, list):
68 error_message = "One of the arguments is not a list."
69 raise TypeError(error_message)
71 # Only list type comparaison
72 if kwargs.get("authorize_abstract_comparison", False) and (list1 == [] or list2 == []):
73 return True
75 if len(list1) != len(list2):
76 error_msg: str = f"List haven't the same lenght ({len(list1)} != {len(list1)})"
77 raise ValueError(error_msg)
79 for i in range(len(list1)):
80 try:
81 compare_responses(list1[i], list2[i], **kwargs)
82 except TypeError as error:
83 error_msg = f"{error} on {i} index"
84 raise TypeError(error_msg) from error
85 return True