diff options
author | Chris Withers <chris@withers.org> | 2020-01-29 16:24:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-29 16:24:54 (GMT) |
commit | db5e86adbce12350c26e7ffc2c6673369971a2dc (patch) | |
tree | 4d0ea04ac823485a1366958f36136fdd0a809726 /Lib/unittest/mock.py | |
parent | a327677905956ae0b239ff430a1346dfe265709e (diff) | |
download | cpython-db5e86adbce12350c26e7ffc2c6673369971a2dc.zip cpython-db5e86adbce12350c26e7ffc2c6673369971a2dc.tar.gz cpython-db5e86adbce12350c26e7ffc2c6673369971a2dc.tar.bz2 |
Get mock coverage back to 100% (GH-18228)
* use the `: pass` and `: yield` patterns for code that isn't expected to ever be executed.
* The _Call items passed to _AnyComparer are only ever of length two, so assert instead of if/else
* fix typo
* Fix bug, where stop-without-start patching dict blows up with `TypeError: 'NoneType' object is not iterable`, highlighted by lack of coverage of an except branch.
* The fix for bpo-37972 means _Call.count and _Call.index are no longer needed.
* add coverage for calling next() on a mock_open with readline.return_value set.
* __aiter__ is defined on the Mock so the one on _AsyncIterator is never called.
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r-- | Lib/unittest/mock.py | 17 |
1 files changed, 4 insertions, 13 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 37ea5c7..9e69298 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1042,8 +1042,7 @@ class _AnyComparer(list): the left.""" def __contains__(self, item): for _call in self: - if len(item) != len(_call): - continue + assert len(item) == len(_call) if all([ expected == actual for expected, actual in zip(item, _call) @@ -1856,7 +1855,8 @@ class _patch_dict(object): def __exit__(self, *args): """Unpatch the dict.""" - self._unpatch_dict() + if self._original is not None: + self._unpatch_dict() return False @@ -2168,7 +2168,7 @@ class AsyncMockMixin(Base): self.__dict__['__code__'] = code_mock async def _execute_mock_call(self, /, *args, **kwargs): - # This is nearly just like super(), except for sepcial handling + # This is nearly just like super(), except for special handling # of coroutines _call = self.call_args @@ -2541,12 +2541,6 @@ class _Call(tuple): return tuple.__getattribute__(self, attr) - def count(self, /, *args, **kwargs): - return self.__getattr__('count')(*args, **kwargs) - - def index(self, /, *args, **kwargs): - return self.__getattr__('index')(*args, **kwargs) - def _get_call_arguments(self): if len(self) == 2: args, kwargs = self @@ -2917,9 +2911,6 @@ class _AsyncIterator: code_mock.co_flags = inspect.CO_ITERABLE_COROUTINE self.__dict__['__code__'] = code_mock - def __aiter__(self): - return self - async def __anext__(self): try: return next(self.iterator) |