From 3786502ed57920d6b78283bf16150f1711721d38 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Thu, 3 Jan 2019 18:29:17 +0000 Subject: [PATCH] unit test reorganization --- setup.py | 3 +- tests/asyncio/__init__.py | 0 tests/{ => asyncio}/index.html | 0 tests/{ => asyncio}/test_async_aiohttp.py | 0 tests/{ => asyncio}/test_async_asgi.py | 4 +- tests/{ => asyncio}/test_async_tornado.py | 0 tests/{ => asyncio}/test_asyncio_client.py | 46 ++++++------------- tests/{ => asyncio}/test_asyncio_server.py | 53 ++++++++-------------- tests/{ => asyncio}/test_asyncio_socket.py | 13 ++---- tests/common/__init__.py | 0 tests/common/index.html | 1 + tests/{ => common}/test_async_eventlet.py | 0 tests/{ => common}/test_client.py | 0 tests/{ => common}/test_middleware.py | 0 tests/{ => common}/test_packet.py | 0 tests/{ => common}/test_payload.py | 0 tests/{ => common}/test_server.py | 0 tests/{ => common}/test_socket.py | 0 tox.ini | 3 +- 19 files changed, 41 insertions(+), 82 deletions(-) create mode 100644 tests/asyncio/__init__.py rename tests/{ => asyncio}/index.html (100%) rename tests/{ => asyncio}/test_async_aiohttp.py (100%) rename tests/{ => asyncio}/test_async_asgi.py (99%) rename tests/{ => asyncio}/test_async_tornado.py (100%) rename tests/{ => asyncio}/test_asyncio_client.py (98%) rename tests/{ => asyncio}/test_asyncio_server.py (96%) rename tests/{ => asyncio}/test_asyncio_socket.py (98%) create mode 100644 tests/common/__init__.py create mode 100644 tests/common/index.html rename tests/{ => common}/test_async_eventlet.py (100%) rename tests/{ => common}/test_client.py (100%) rename tests/{ => common}/test_middleware.py (100%) rename tests/{ => common}/test_packet.py (100%) rename tests/{ => common}/test_payload.py (100%) rename tests/{ => common}/test_server.py (100%) rename tests/{ => common}/test_socket.py (100%) diff --git a/setup.py b/setup.py index 2424618b..2e9d57fe 100755 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ """ import re from setuptools import setup, find_packages +import six with open('engineio/__init__.py', 'r') as f: @@ -45,7 +46,7 @@ 'mock', 'eventlet', ], - test_suite='tests', + test_suite='tests' if six.PY3 else 'tests.common', classifiers=[ 'Environment :: Web Environment', 'Intended Audience :: Developers', diff --git a/tests/asyncio/__init__.py b/tests/asyncio/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/index.html b/tests/asyncio/index.html similarity index 100% rename from tests/index.html rename to tests/asyncio/index.html diff --git a/tests/test_async_aiohttp.py b/tests/asyncio/test_async_aiohttp.py similarity index 100% rename from tests/test_async_aiohttp.py rename to tests/asyncio/test_async_aiohttp.py diff --git a/tests/test_async_asgi.py b/tests/asyncio/test_async_asgi.py similarity index 99% rename from tests/test_async_asgi.py rename to tests/asyncio/test_async_asgi.py index 0eccc8de..035bf65f 100644 --- a/tests/test_async_asgi.py +++ b/tests/asyncio/test_async_asgi.py @@ -10,7 +10,6 @@ if sys.version_info >= (3, 5): import asyncio - from asyncio import coroutine from engineio.async_drivers import asgi as async_asgi @@ -18,8 +17,7 @@ def AsyncMock(*args, **kwargs): """Return a mock asynchronous function.""" m = mock.MagicMock(*args, **kwargs) - @coroutine - def mock_coro(*args, **kwargs): + async def mock_coro(*args, **kwargs): return m(*args, **kwargs) mock_coro.mock = m diff --git a/tests/test_async_tornado.py b/tests/asyncio/test_async_tornado.py similarity index 100% rename from tests/test_async_tornado.py rename to tests/asyncio/test_async_tornado.py diff --git a/tests/test_asyncio_client.py b/tests/asyncio/test_asyncio_client.py similarity index 98% rename from tests/test_asyncio_client.py rename to tests/asyncio/test_asyncio_client.py index ce41da44..5ddcdc84 100644 --- a/tests/test_asyncio_client.py +++ b/tests/asyncio/test_asyncio_client.py @@ -1,3 +1,4 @@ +import asyncio import sys import time import unittest @@ -17,26 +18,18 @@ class _dummy(): websockets.exceptions = _dummy() websockets.exceptions.InvalidURI = _dummy() +from engineio import asyncio_client from engineio import client from engineio import exceptions from engineio import packet from engineio import payload -if sys.version_info >= (3, 5): - import asyncio - from asyncio import coroutine - from engineio import asyncio_client -else: - # mock coroutine so that Python 2 doesn't complain - def coroutine(f): - return f def AsyncMock(*args, **kwargs): """Return a mock asynchronous function.""" m = mock.MagicMock(*args, **kwargs) - @coroutine - def mock_coro(*args, **kwargs): + async def mock_coro(*args, **kwargs): return m(*args, **kwargs) mock_coro.mock = m @@ -130,8 +123,7 @@ def test_wait(self): c = asyncio_client.AsyncClient() done = [] - @coroutine - def fake_read_look_task(): + async def fake_read_look_task(): done.append(True) c.read_loop_task = fake_read_look_task() @@ -147,8 +139,7 @@ def test_send(self): c = asyncio_client.AsyncClient() saved_packets = [] - @coroutine - def fake_send_packet(pkt): + async def fake_send_packet(pkt): saved_packets.append(pkt) c._send_packet = fake_send_packet @@ -241,8 +232,7 @@ def test_disconnect_websocket_abort(self): def test_background_tasks(self): r = [] - @coroutine - def foo(arg): + async def foo(arg): r.append(arg) c = asyncio_client.AsyncClient() @@ -603,8 +593,7 @@ def foo_handler(arg): def test_trigger_event_coroutine(self): result = [] - @coroutine - def foo_handler(arg): + async def foo_handler(arg): result.append('ok') result.append(arg) @@ -627,12 +616,10 @@ def foo_handler(arg): self.assertIsNone(_run(c._trigger_event('message', 'bar'))) def test_trigger_event_coroutine_error(self): - @coroutine - def connect_handler(arg): + async def connect_handler(arg): return 1 / 0 - @coroutine - def foo_handler(arg): + async def foo_handler(arg): return 1 / 0 c = asyncio_client.AsyncClient() @@ -657,8 +644,7 @@ def foo_handler(arg): def test_trigger_event_coroutine_async(self): result = [] - @coroutine - def foo_handler(arg): + async def foo_handler(arg): result.append('ok') result.append(arg) @@ -686,8 +672,7 @@ def foo_handler(arg): def test_trigger_event_coroutine_async_error(self): result = [] - @coroutine - def foo_handler(arg): + async def foo_handler(arg): result.append(arg) return 1 / 0 @@ -721,8 +706,7 @@ def test_ping_loop_disconnect(self): ('disconnecting', True) ] - @coroutine - def fake_wait(): + async def fake_wait(): c.state, c.pong_received = states.pop(0) c.ping_loop_event.wait = fake_wait @@ -742,8 +726,7 @@ def test_ping_loop_missing_pong(self): ('connected', False) ] - @coroutine - def fake_wait(): + async def fake_wait(): c.state, c.pong_received = states.pop(0) c.ping_loop_event.wait = fake_wait @@ -765,8 +748,7 @@ def test_ping_loop_missing_pong_websocket(self): ('connected', False) ] - @coroutine - def fake_wait(): + async def fake_wait(): c.state, c.pong_received = states.pop(0) c.ping_loop_event.wait = fake_wait diff --git a/tests/test_asyncio_server.py b/tests/asyncio/test_asyncio_server.py similarity index 96% rename from tests/test_asyncio_server.py rename to tests/asyncio/test_asyncio_server.py index 95a88203..a3097a89 100644 --- a/tests/test_asyncio_server.py +++ b/tests/asyncio/test_asyncio_server.py @@ -1,3 +1,4 @@ +import asyncio import gzip import json import logging @@ -11,26 +12,18 @@ else: import mock +from engineio import asyncio_server +from engineio.async_drivers import aiohttp as async_aiohttp from engineio import exceptions from engineio import packet from engineio import payload -if sys.version_info >= (3, 5): - import asyncio - from asyncio import coroutine - from engineio import asyncio_server - from engineio.async_drivers import aiohttp as async_aiohttp -else: - # mock coroutine so that Python 2 doesn't complain - def coroutine(f): - return f def AsyncMock(*args, **kwargs): """Return a mock asynchronous function.""" m = mock.MagicMock(*args, **kwargs) - @coroutine - def mock_coro(*args, **kwargs): + async def mock_coro(*args, **kwargs): return m(*args, **kwargs) mock_coro.mock = m @@ -141,7 +134,7 @@ def test_attach(self, import_module): def test_session(self): s = asyncio_server.AsyncServer() - s.sockets['foo'] = mock_socket = self._get_mock_socket() + s.sockets['foo'] = self._get_mock_socket() async def _func(): async with s.session('foo') as session: @@ -366,8 +359,7 @@ def test_connect_transport_websocket_closed(self, import_module, # this mock handler just closes the socket, as it would happen on a # real websocket exchange - @coroutine - def mock_handle(environ): + async def mock_handle(environ): s.sockets['123'].closed = True AsyncSocket().handle_get_request = mock_handle @@ -553,12 +545,11 @@ def test_get_request_closes_socket(self, import_module): s = asyncio_server.AsyncServer() s.sockets['foo'] = mock_socket = self._get_mock_socket() - @coroutine - def mock_get_request(*args, **kwargs): + async def mock_get_request(*args, **kwargs): mock_socket.closed = True return 'resp' - mock_socket.handle_get_request.mock.return_value = mock_get_request() + mock_socket.handle_get_request = mock_get_request r = _run(s.handle_request('request')) self.assertEqual(r, 'resp') self.assertNotIn('foo', s.sockets) @@ -571,11 +562,10 @@ def test_get_request_error(self, import_module): s = asyncio_server.AsyncServer() s.sockets['foo'] = mock_socket = self._get_mock_socket() - @coroutine - def mock_get_request(*args, **kwargs): + async def mock_get_request(*args, **kwargs): raise exceptions.QueueEmpty() - mock_socket.handle_get_request.mock.return_value = mock_get_request() + mock_socket.handle_get_request = mock_get_request _run(s.handle_request('request')) self.assertEqual(a._async['make_response'].call_args[0][0], '400 BAD REQUEST') @@ -599,11 +589,10 @@ def test_post_request_error(self, import_module): s = asyncio_server.AsyncServer() s.sockets['foo'] = mock_socket = self._get_mock_socket() - @coroutine - def mock_post_request(*args, **kwargs): + async def mock_post_request(*args, **kwargs): raise exceptions.ContentTooLongError() - mock_socket.handle_post_request.mock.return_value = mock_post_request() + mock_socket.handle_post_request = mock_post_request _run(s.handle_request('request')) self.assertEqual(a._async['make_response'].call_args[0][0], '400 BAD REQUEST') @@ -773,8 +762,7 @@ def loads(*args, **kwargs): def test_background_tasks(self): r = [] - @coroutine - def foo(arg): + async def foo(arg): r.append(arg) s = asyncio_server.AsyncServer() @@ -802,8 +790,7 @@ def foo_handler(arg): def test_trigger_event_coroutine(self): result = [] - @coroutine - def foo_handler(arg): + async def foo_handler(arg): result.append('ok') result.append(arg) @@ -826,12 +813,10 @@ def foo_handler(arg): self.assertIsNone(_run(s._trigger_event('message', 'bar'))) def test_trigger_event_coroutine_error(self): - @coroutine - def connect_handler(arg): + async def connect_handler(arg): return 1 / 0 - @coroutine - def foo_handler(arg): + async def foo_handler(arg): return 1 / 0 s = asyncio_server.AsyncServer() @@ -856,8 +841,7 @@ def foo_handler(arg): def test_trigger_event_coroutine_async(self): result = [] - @coroutine - def foo_handler(arg): + async def foo_handler(arg): result.append('ok') result.append(arg) @@ -885,8 +869,7 @@ def foo_handler(arg): def test_trigger_event_coroutine_async_error(self): result = [] - @coroutine - def foo_handler(arg): + async def foo_handler(arg): result.append(arg) return 1 / 0 diff --git a/tests/test_asyncio_socket.py b/tests/asyncio/test_asyncio_socket.py similarity index 98% rename from tests/test_asyncio_socket.py rename to tests/asyncio/test_asyncio_socket.py index f8701c2e..c67746bb 100644 --- a/tests/test_asyncio_socket.py +++ b/tests/asyncio/test_asyncio_socket.py @@ -1,3 +1,4 @@ +import asyncio import sys import time import unittest @@ -8,25 +9,17 @@ else: import mock +from engineio import asyncio_socket from engineio import exceptions from engineio import packet from engineio import payload -if sys.version_info >= (3, 5): - import asyncio - from asyncio import coroutine - from engineio import asyncio_socket -else: - # mock coroutine so that Python 2 doesn't complain - def coroutine(f): - return f def AsyncMock(*args, **kwargs): """Return a mock asynchronous function.""" m = mock.MagicMock(*args, **kwargs) - @coroutine - def mock_coro(*args, **kwargs): + async def mock_coro(*args, **kwargs): return m(*args, **kwargs) mock_coro.mock = m diff --git a/tests/common/__init__.py b/tests/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/common/index.html b/tests/common/index.html new file mode 100644 index 00000000..18ecdcb7 --- /dev/null +++ b/tests/common/index.html @@ -0,0 +1 @@ + diff --git a/tests/test_async_eventlet.py b/tests/common/test_async_eventlet.py similarity index 100% rename from tests/test_async_eventlet.py rename to tests/common/test_async_eventlet.py diff --git a/tests/test_client.py b/tests/common/test_client.py similarity index 100% rename from tests/test_client.py rename to tests/common/test_client.py diff --git a/tests/test_middleware.py b/tests/common/test_middleware.py similarity index 100% rename from tests/test_middleware.py rename to tests/common/test_middleware.py diff --git a/tests/test_packet.py b/tests/common/test_packet.py similarity index 100% rename from tests/test_packet.py rename to tests/common/test_packet.py diff --git a/tests/test_payload.py b/tests/common/test_payload.py similarity index 100% rename from tests/test_payload.py rename to tests/common/test_payload.py diff --git a/tests/test_server.py b/tests/common/test_server.py similarity index 100% rename from tests/test_server.py rename to tests/common/test_server.py diff --git a/tests/test_socket.py b/tests/common/test_socket.py similarity index 100% rename from tests/test_socket.py rename to tests/common/test_socket.py diff --git a/tox.ini b/tox.ini index eec4c8df..e66e27f9 100644 --- a/tox.ini +++ b/tox.ini @@ -17,7 +17,7 @@ deps= websocket-client websockets basepython = - flake8: python3.6 + flake8: python3.5 py27: python2.7 py35: python3.5 py36: python3.6 @@ -47,6 +47,7 @@ deps= [testenv:flake8] deps= + six flake8 commands= flake8 --exclude=".*" --ignore=E402,E722 engineio tests