Skip to content

Commit 7208ec0

Browse files
Refactor common testing helpers into a separate module
1 parent 5541dda commit 7208ec0

File tree

6 files changed

+23
-93
lines changed

6 files changed

+23
-93
lines changed

tests/asyncio/helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
from unittest import mock
3+
4+
5+
def AsyncMock(*args, **kwargs):
6+
"""Return a mock asynchronous function."""
7+
m = mock.MagicMock(*args, **kwargs)
8+
9+
async def mock_coro(*args, **kwargs):
10+
return m(*args, **kwargs)
11+
12+
mock_coro.mock = m
13+
return mock_coro
14+
15+
16+
def _run(coro):
17+
"""Run the given coroutine."""
18+
return asyncio.get_event_loop().run_until_complete(coro)

tests/asyncio/test_asyncio_client.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,7 @@
1111
from engineio import exceptions as engineio_exceptions
1212
from socketio import exceptions
1313
from socketio import packet
14-
15-
16-
def AsyncMock(*args, **kwargs):
17-
"""Return a mock asynchronous function."""
18-
m = mock.MagicMock(*args, **kwargs)
19-
20-
async def mock_coro(*args, **kwargs):
21-
return m(*args, **kwargs)
22-
23-
mock_coro.mock = m
24-
return mock_coro
25-
26-
27-
@contextmanager
28-
def mock_wait_for():
29-
async def fake_wait_for(coro, timeout):
30-
await coro
31-
await fake_wait_for._mock(timeout)
32-
33-
original_wait_for = asyncio.wait_for
34-
asyncio.wait_for = fake_wait_for
35-
fake_wait_for._mock = AsyncMock()
36-
yield
37-
asyncio.wait_for = original_wait_for
38-
39-
40-
def _run(coro):
41-
"""Run the given coroutine."""
42-
return asyncio.get_event_loop().run_until_complete(coro)
14+
from .helpers import AsyncMock, _run
4315

4416

4517
@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')

tests/asyncio/test_asyncio_manager.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@
55

66
from socketio import asyncio_manager
77
from socketio import packet
8-
9-
10-
def AsyncMock(*args, **kwargs):
11-
"""Return a mock asynchronous function."""
12-
m = mock.MagicMock(*args, **kwargs)
13-
14-
async def mock_coro(*args, **kwargs):
15-
return m(*args, **kwargs)
16-
17-
mock_coro.mock = m
18-
return mock_coro
19-
20-
21-
def _run(coro):
22-
"""Run the given coroutine."""
23-
return asyncio.get_event_loop().run_until_complete(coro)
8+
from .helpers import AsyncMock, _run
249

2510

2611
@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')

tests/asyncio/test_asyncio_namespace.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,7 @@
44
from unittest import mock
55

66
from socketio import asyncio_namespace
7-
8-
9-
def AsyncMock(*args, **kwargs):
10-
"""Return a mock asynchronous function."""
11-
m = mock.MagicMock(*args, **kwargs)
12-
13-
async def mock_coro(*args, **kwargs):
14-
return m(*args, **kwargs)
15-
16-
mock_coro.mock = m
17-
return mock_coro
18-
19-
20-
def _run(coro):
21-
"""Run the given coroutine."""
22-
return asyncio.get_event_loop().run_until_complete(coro)
7+
from .helpers import AsyncMock, _run
238

249

2510
@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')

tests/asyncio/test_asyncio_pubsub_manager.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,7 @@
99
from socketio import asyncio_manager
1010
from socketio import asyncio_pubsub_manager
1111
from socketio import packet
12-
13-
14-
def AsyncMock(*args, **kwargs):
15-
"""Return a mock asynchronous function."""
16-
m = mock.MagicMock(*args, **kwargs)
17-
18-
async def mock_coro(*args, **kwargs):
19-
return m(*args, **kwargs)
20-
21-
mock_coro.mock = m
22-
return mock_coro
23-
24-
25-
def _run(coro):
26-
"""Run the given coroutine."""
27-
return asyncio.get_event_loop().run_until_complete(coro)
12+
from .helpers import AsyncMock, _run
2813

2914

3015
@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')

tests/asyncio/test_asyncio_server.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,7 @@
1313
from socketio import exceptions
1414
from socketio import namespace
1515
from socketio import packet
16-
17-
18-
def AsyncMock(*args, **kwargs):
19-
"""Return a mock asynchronous function."""
20-
m = mock.MagicMock(*args, **kwargs)
21-
22-
async def mock_coro(*args, **kwargs):
23-
return m(*args, **kwargs)
24-
25-
mock_coro.mock = m
26-
return mock_coro
27-
28-
29-
def _run(coro):
30-
"""Run the given coroutine."""
31-
return asyncio.get_event_loop().run_until_complete(coro)
16+
from .helpers import AsyncMock, _run
3217

3318

3419
@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')

0 commit comments

Comments
 (0)