|
1 | | -from typing import List, Dict |
| 1 | +from typing import List, Dict, Any |
2 | 2 |
|
3 | 3 |
|
4 | 4 | class HandledError(AssertionError): |
@@ -59,61 +59,77 @@ def assert_expected_value(expected, received, where: str = "response"): |
59 | 59 | raise UnexpectedValueError(f"Expected {where}='{expected}', not '{received}'") |
60 | 60 |
|
61 | 61 |
|
62 | | -# It only works if there is no duplicated data inside lists. lst1=expected, lst2=received |
63 | | -def assert_equal_unique_lists(lst1: List, lst2: List, name_list1: str = "list1", name_list2: str = "list2", strict=True, **kwargs): |
64 | | - assert_expected_type(lst1, (list, tuple, set), where=name_list1) |
65 | | - assert_expected_type(lst2, (list, tuple, set), where=name_list2) |
66 | | - if strict: |
67 | | - assert_same_len(lst1, lst2, where=f"{name_list1} vs {name_list2}") |
68 | | - |
69 | | - for x in lst1: |
70 | | - if isinstance(x, (list, tuple, set, dict)): |
71 | | - for z in lst2: # iterate every element z from list2 and compare if equal to element x from list1 |
72 | | - if isinstance(z, type(x)): |
73 | | - try: |
74 | | - if isinstance(x, dict): |
75 | | - assert_data(x, z, where=name_list1) |
76 | | - else: |
77 | | - assert_equal_unique_lists(x, z, name_list1=name_list1, name_list2=name_list2) |
78 | | - except: |
79 | | - pass # so this z is not equal, continue to next z |
80 | | - else: |
81 | | - break # found z == x, continue with next x on lst1 |
82 | | - else: |
83 | | - raise AssertionError(f"{type(x)} from {name_list1} not found in {name_list2}. {x}") |
84 | | - else: |
85 | | - if x not in lst2: |
86 | | - raise UnexpectedValueError(f"Value '{x}' from {name_list1} not in {name_list2}") |
87 | | - |
88 | | - |
89 | | -# It only works if there is no duplicated data inside lists |
90 | | -def assert_data(expected_values: Dict, response: Dict, where="", strict: bool = False, **kwargs): |
91 | | - if isinstance(expected_values, dict): |
92 | | - if strict and len(expected_values) != len(response): |
93 | | - raise ExpectedFieldMissingError(f"Not same amount of keys: {expected_values.keys()=} not {response.keys()=}") |
94 | | - |
95 | | - for field, value in expected_values.items(): |
96 | | - if field not in response: |
97 | | - raise ExpectedFieldMissingError(f"{field=} not in {where}") |
98 | | - if isinstance(value, (list, tuple, set)): |
99 | | - assert_expected_type(response[field], type(value), where=f"{where}.{field}") |
100 | | - assert_equal_unique_lists(value, response[field], f"{where}/{field}[expected]", f"{where}/{field}[response]") |
101 | | - elif isinstance(value, dict): |
102 | | - assert_expected_type(response[field], dict, where=f"{where}.{field}") |
103 | | - assert_data(value, response[field], where=f"{where}/{field}") |
104 | | - else: |
105 | | - assert_expected_value(value, response[field], where=f"{where}.{field}") |
106 | | - elif isinstance(expected_values, list): |
107 | | - assert_equal_unique_lists(expected_values, response, name_list1="expected", name_list2="received", strict=strict) |
108 | | - elif isinstance(expected_values, str): |
109 | | - assert expected_values == response, f"{where} Expected value does not meet received." |
110 | | - else: |
111 | | - if strict: |
112 | | - assert expected_values == response, f"{where} Expected value does not meet received." |
113 | | - else: |
114 | | - assert expected_values in response, f"{where} Expected value not part of received." |
115 | | - |
116 | | - |
117 | 62 | def assert_status_code(expected_status_code, received_status_code): |
118 | 63 | if expected_status_code != received_status_code: |
119 | 64 | raise UnexpectedValueError(f"Unexpected status_code {received_status_code}, expected {expected_status_code}") |
| 65 | + |
| 66 | + |
| 67 | +def assert_equal_complex_object( |
| 68 | + expected: Any, |
| 69 | + received: Any, |
| 70 | + expected_name: str = "expected", |
| 71 | + received_name: str = "received", |
| 72 | +) -> None: |
| 73 | + if isinstance(expected, dict) and isinstance(received, dict): |
| 74 | + assert_equal_dicts( |
| 75 | + expected, received, expected_name=expected_name, received_name=received_name |
| 76 | + ) |
| 77 | + elif isinstance(expected, (list, tuple, set)) and isinstance( |
| 78 | + received, (list, tuple, set) |
| 79 | + ): |
| 80 | + assert_equal_lists( |
| 81 | + expected, received, expected_name=expected_name, received_name=received_name |
| 82 | + ) |
| 83 | + else: |
| 84 | + assert ( |
| 85 | + expected == received |
| 86 | + ), f"{expected_name} = '{expected}' not equal to {received_name} = '{received}'" |
| 87 | + |
| 88 | + |
| 89 | +def assert_equal_dicts( |
| 90 | + expected: Any, |
| 91 | + received: Any, |
| 92 | + expected_name: str = "expected", |
| 93 | + received_name: str = "received", |
| 94 | +) -> None: |
| 95 | + for key in expected: |
| 96 | + assert key in received, f"{expected_name}.{key} is not in dict {received_name}" |
| 97 | + assert_equal_complex_object( |
| 98 | + expected[key], |
| 99 | + received[key], |
| 100 | + expected_name=f"{expected_name}.{key}", |
| 101 | + received_name=f"{received_name}.{key}", |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def assert_equal_lists( |
| 106 | + expected: Any, |
| 107 | + received: Any, |
| 108 | + expected_name: str = "expected", |
| 109 | + received_name: str = "received", |
| 110 | + same_len: bool = True, |
| 111 | +) -> None: |
| 112 | + if same_len: |
| 113 | + assert ( |
| 114 | + len(expected) == len(received) |
| 115 | + ), f"{expected_name} has {len(expected)} elements != {received_name} has {len(received)} elements" |
| 116 | + |
| 117 | + list2 = list(received) |
| 118 | + for i1, v1 in enumerate(expected): |
| 119 | + for i2, v2 in enumerate(list2): |
| 120 | + try: |
| 121 | + assert_equal_complex_object( |
| 122 | + v1, |
| 123 | + v2, |
| 124 | + expected_name=f"{expected_name}[{i1}]", |
| 125 | + received_name=f"{received_name}[{i2}]", |
| 126 | + ) |
| 127 | + except AssertionError: |
| 128 | + pass |
| 129 | + else: |
| 130 | + list2.remove(v2) |
| 131 | + break |
| 132 | + else: |
| 133 | + raise AssertionError( |
| 134 | + f"{expected_name}[{i1}] '{v1}' is not inside {received_name}" |
| 135 | + ) |
0 commit comments