Skip to content

Commit

Permalink
3.4 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
kwarunek committed Jun 11, 2019
1 parent e232235 commit dad58e0
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 75 deletions.
10 changes: 7 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This is a helper library to ease of your pain (and boilerplate), when writing a
* synchronous code (same as the :code:`unittest.TestCase`)
* asynchronous code, it supports syntax with :code:`async`/:code:`await` (Python 3.5+) and :code:`asyncio.coroutine`/:code:`yield from` (Python 3.4)


Installation
============

Expand All @@ -27,7 +27,7 @@ Use pip:

pip install aiounittest


Usage
=====

Expand Down Expand Up @@ -59,7 +59,11 @@ It's as simple as use of :code:`unittest.TestCase`. Full docs at http://aiounitt
def test_something(self):
self.assertTrue(true)
Library exposes some other helpers like async_test_, AsyncMockIterator_, futurized_ to mock coroutines.
Library provide some additional tooling:

* async_test_,
* AsyncMockIterator_ to mock object for `async for`,
* futurized_ to mock coroutines.

.. _futurized: http://aiounittest.readthedocs.io/en/latest/futurized.html
.. _async_test: http://aiounittest.readthedocs.io/en/latest/async_test.html
Expand Down
2 changes: 1 addition & 1 deletion aiounittest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .case import AsyncTestCase
from .helpers import futurized, run_sync, async_test, AsyncMockIterator
from .helpers import futurized, run_sync, async_test
66 changes: 0 additions & 66 deletions aiounittest/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,69 +149,3 @@ def wrapper(*args, **kwargs):


async_test = run_sync


class AsyncMockIterator:
''' Allows to asynchronous for loops.
.. code-block:: python
from aiounittest import AsyncTestCase, AsyncMockIterator
from unittest.mock import Mock
async def fetch_some_text(source):
res = ''
async for txt in source.paginate():
res += txt
return res
class MyAsyncMockIteratorTest(AsyncTestCase):
async def test_add(self):
source = Mock()
mock_iter = AsyncMockIterator([
'asdf', 'qwer', 'zxcv'
])
source.paginate.return_value = mock_iter
res = await fetch_some_text(source)
self.assertEqual(res, 'asdfqwerzxcv')
mock_iter.assertFullyConsumed()
mock_iter.assertIterCount(3)
'''
def __init__(self, seq):
self.iter = iter(seq)
self.__consumed = False
self.__iter_count = 0

def __aiter__(self, *args, **kwargs):
return self

async def __anext__(self, *args, **kwargs):
try:
val = next(self.iter)
self.__iter_count += 1
return val
except StopIteration:
self.__consumed = True
raise StopAsyncIteration

def assertFullyConsumed(self):
''' Does `async for` reached the end of the giden sequence
'''
assert self.__consumed, 'Iterator wasnt fully consumed'

def assertIterCount(self, expected):
''' Checks whenever number of a mock iteration matches expected.
:param expected int: Expected number of iterations
'''
assert expected == self.__iter_count, '%d iterations instead of %d' % (self.__iter_count, expected)
2 changes: 1 addition & 1 deletion docs/asyncmockiterator.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AsyncMockIterator
=================

.. autoclass:: aiounittest.AsyncMockIterator
.. autoclass:: aiounittest.mock.AsyncMockIterator
:members: assertFullyConsumed, assertIterCount
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
setup(
name='aiounittest',
packages=['aiounittest'],
version='1.2.0',
version='1.2.1',
author='Krzysztof Warunek',
author_email='krzysztof@warunek.net',
description='Test asyncio code more easily.',
Expand Down
6 changes: 3 additions & 3 deletions tests/test_asyncmockiterator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from aiounittest import futurized, AsyncTestCase, AsyncMockIterator
from unittest.mock import Mock, patch
from aiounittest import AsyncTestCase
from aiounittest import AsyncMockIterator
from unittest.mock import Mock


async def fetch_some_text(source):
Expand All @@ -22,4 +23,3 @@ async def test_add(self):
self.assertEqual(res, 'asdfqwerzxcv')
mock_iter.assertFullyConsumed()
mock_iter.assertIterCount(3)

0 comments on commit dad58e0

Please sign in to comment.