Assert the mock has been awaited with the specified calls. The :attr:`await_args_list` list is checked for the awaits. If `any_order` is False (the default) then the awaits must be sequential. There can be extra calls before or after the specified awaits.
(self, calls, any_order=False)
| 2418 | ) from cause |
| 2419 | |
| 2420 | def assert_has_awaits(self, calls, any_order=False): |
| 2421 | """ |
| 2422 | Assert the mock has been awaited with the specified calls. |
| 2423 | The :attr:`await_args_list` list is checked for the awaits. |
| 2424 | |
| 2425 | If `any_order` is False (the default) then the awaits must be |
| 2426 | sequential. There can be extra calls before or after the |
| 2427 | specified awaits. |
| 2428 | |
| 2429 | If `any_order` is True then the awaits can be in any order, but |
| 2430 | they must all appear in :attr:`await_args_list`. |
| 2431 | """ |
| 2432 | expected = [self._call_matcher(c) for c in calls] |
| 2433 | cause = next((e for e in expected if isinstance(e, Exception)), None) |
| 2434 | all_awaits = _CallList(self._call_matcher(c) for c in self.await_args_list) |
| 2435 | if not any_order: |
| 2436 | if expected not in all_awaits: |
| 2437 | if cause is None: |
| 2438 | problem = 'Awaits not found.' |
| 2439 | else: |
| 2440 | problem = ('Error processing expected awaits.\n' |
| 2441 | 'Errors: {}').format( |
| 2442 | [e if isinstance(e, Exception) else None |
| 2443 | for e in expected]) |
| 2444 | raise AssertionError( |
| 2445 | f'{problem}\n' |
| 2446 | f'Expected: {_CallList(calls)}\n' |
| 2447 | f'Actual: {self.await_args_list}' |
| 2448 | ) from cause |
| 2449 | return |
| 2450 | |
| 2451 | all_awaits = list(all_awaits) |
| 2452 | |
| 2453 | not_found = [] |
| 2454 | for kall in expected: |
| 2455 | try: |
| 2456 | all_awaits.remove(kall) |
| 2457 | except ValueError: |
| 2458 | not_found.append(kall) |
| 2459 | if not_found: |
| 2460 | raise AssertionError( |
| 2461 | '%r not all found in await list' % (tuple(not_found),) |
| 2462 | ) from cause |
| 2463 | |
| 2464 | def assert_not_awaited(self): |
| 2465 | """ |