|
22 | 22 | from fingerprint_pro_server_api_sdk.api.fingerprint_api import FingerprintApi # noqa: E501 |
23 | 23 | from fingerprint_pro_server_api_sdk.rest import KnownApiException, ApiException |
24 | 24 | from urllib.parse import urlencode |
| 25 | +from collections import Counter |
25 | 26 |
|
26 | 27 | API_KEY = 'private_key' |
27 | 28 |
|
@@ -88,22 +89,40 @@ def expect_request(self, *args, **kwargs): |
88 | 89 | def get_mock_from_path(path): |
89 | 90 | return path.split('/')[-1] |
90 | 91 |
|
| 92 | + @staticmethod |
| 93 | + def _flatten(fields): |
| 94 | + flatten = [] |
| 95 | + for k, obj in fields: |
| 96 | + if isinstance(obj, list): |
| 97 | + flatten.extend((k, v) for v in obj) |
| 98 | + else: |
| 99 | + flatten.append((k, obj)) |
| 100 | + return flatten |
| 101 | + |
91 | 102 | def request(self, *args, **kwargs): |
92 | 103 | self._tc.assertTrue(len(self._reqs) > 0) |
93 | | - r = self._reqs.pop(0) |
| 104 | + (request_method, request_url), request_config = self._reqs.pop(0) |
94 | 105 | status = 200 |
95 | | - if r[1].get('status') is not None: |
96 | | - status = r[1].get('status') |
97 | | - r[1].pop('status') |
| 106 | + if request_config.get('status') is not None: |
| 107 | + status = request_config.get('status') |
| 108 | + request_config.pop('status') |
98 | 109 |
|
99 | | - if r[1].get('method') != 'GET': |
100 | | - request_path = r[0][1].split('?')[0] |
| 110 | + if request_config.get('method') != 'GET': |
| 111 | + request_path = request_url.split('?')[0] |
101 | 112 | else: |
102 | | - request_path = r[0][1] |
| 113 | + request_path = request_url |
103 | 114 |
|
104 | 115 | self._tc.maxDiff = None |
105 | | - self._tc.assertEqual(r[0], args) |
106 | | - self._tc.assertCountEqual(r[1], kwargs) |
| 116 | + self._tc.assertEqual(request_method, args[0]) |
| 117 | + self._tc.assertEqual(request_url, args[1]) |
| 118 | + |
| 119 | + self._tc.assertEqual(set(request_config.keys()), set(kwargs.keys())) |
| 120 | + for k in request_config.keys() - { 'fields' }: |
| 121 | + self._tc.assertEqual(request_config[k], kwargs[k], msg=f"Mismatch on request key: '{k}'") |
| 122 | + |
| 123 | + expected_fields = MockPoolManager._flatten(request_config.get('fields')) |
| 124 | + actual_fields = MockPoolManager._flatten(kwargs.get('fields')) |
| 125 | + self._tc.assertEqual(Counter(expected_fields), Counter(actual_fields), msg="fields on request do not match") |
107 | 126 |
|
108 | 127 | # TODO Add support for more complex paths? |
109 | 128 | mock_file_by_first_argument = MockPoolManager.get_mock_from_path(request_path) |
|
0 commit comments