Skip to content

Commit

Permalink
unit test reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 3, 2019
1 parent 9f21867 commit b0a8b1f
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 82 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import re
from setuptools import setup
import six

with open('socketio/__init__.py', 'r') as f:
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
Expand Down Expand Up @@ -42,7 +43,7 @@
tests_require=[
'mock',
],
test_suite='tests',
test_suite='tests' if six.PY3 else 'tests.common',
classifiers=[
'Environment :: Web Environment',
'Intended Audience :: Developers',
Expand Down
Empty file added tests/asyncio/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import sys
import unittest

Expand All @@ -7,26 +8,18 @@
else:
import mock

from socketio import asyncio_client
from socketio import asyncio_namespace
from engineio import exceptions as engineio_exceptions
from socketio import exceptions
from socketio import packet
if six.PY3:
import asyncio
from asyncio import coroutine
from socketio import asyncio_client
from socketio import asyncio_namespace
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
Expand Down Expand Up @@ -119,8 +112,7 @@ def test_wait_reconnect_failed(self):
c.sleep = AsyncMock()
states = ['disconnected']

@coroutine
def fake_wait():
async def fake_wait():
c.eio.state = states.pop(0)

c._reconnect_task = fake_wait()
Expand All @@ -134,8 +126,7 @@ def test_wait_reconnect_successful(self):
c.sleep = AsyncMock()
states = ['connected', 'disconnected']

@coroutine
def fake_wait():
async def fake_wait():
c.eio.state = states.pop(0)
c._reconnect_task = fake_wait()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import sys
import unittest

Expand All @@ -7,22 +8,14 @@
else:
import mock

if sys.version_info >= (3, 5):
import asyncio
from asyncio import coroutine
from socketio import asyncio_manager
else:
# mock coroutine so that Python 2 doesn't complain
def coroutine(f):
return f
from socketio import asyncio_manager


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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import sys
import unittest

Expand All @@ -7,22 +8,14 @@
else:
import mock

if sys.version_info >= (3, 5):
import asyncio
from asyncio import coroutine
from socketio import asyncio_namespace
else:
# mock coroutine so that Python 2 doesn't complain
def coroutine(f):
return f
from socketio import asyncio_namespace


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
Expand All @@ -40,8 +33,7 @@ def test_connect_event(self):
result = {}

class MyNamespace(asyncio_namespace.AsyncNamespace):
@coroutine
def on_connect(self, sid, environ):
async def on_connect(self, sid, environ):
result['result'] = (sid, environ)

ns = MyNamespace('/foo')
Expand All @@ -53,8 +45,7 @@ def test_disconnect_event(self):
result = {}

class MyNamespace(asyncio_namespace.AsyncNamespace):
@coroutine
def on_disconnect(self, sid):
async def on_disconnect(self, sid):
result['result'] = sid

ns = MyNamespace('/foo')
Expand All @@ -78,8 +69,7 @@ def test_async_event(self):
result = {}

class MyNamespace(asyncio_namespace.AsyncNamespace):
@coroutine
def on_custom_message(self, sid, data):
async def on_custom_message(self, sid, data):
result['result'] = (sid, data)

ns = MyNamespace('/foo')
Expand All @@ -91,8 +81,7 @@ def test_event_not_found(self):
result = {}

class MyNamespace(asyncio_namespace.AsyncNamespace):
@coroutine
def on_custom_message(self, sid, data):
async def on_custom_message(self, sid, data):
result['result'] = (sid, data)

ns = MyNamespace('/foo')
Expand Down Expand Up @@ -217,8 +206,7 @@ def test_async_event_client(self):
result = {}

class MyNamespace(asyncio_namespace.AsyncClientNamespace):
@coroutine
def on_custom_message(self, sid, data):
async def on_custom_message(self, sid, data):
result['result'] = (sid, data)

ns = MyNamespace('/foo')
Expand All @@ -230,8 +218,7 @@ def test_event_not_found_client(self):
result = {}

class MyNamespace(asyncio_namespace.AsyncClientNamespace):
@coroutine
def on_custom_message(self, sid, data):
async def on_custom_message(self, sid, data):
result['result'] = (sid, data)

ns = MyNamespace('/foo')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import functools
import sys
import unittest
Expand All @@ -8,23 +9,15 @@
else:
import mock

if sys.version_info >= (3, 5):
import asyncio
from asyncio import coroutine
from socketio import asyncio_manager
from socketio import asyncio_pubsub_manager
else:
# mock coroutine so that Python 2 doesn't complain
def coroutine(f):
return f
from socketio import asyncio_manager
from socketio import asyncio_pubsub_manager


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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys
import unittest

if sys.version_info >= (3, 5):
from socketio import asyncio_redis_manager
from socketio import asyncio_redis_manager


@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import json
import logging
import sys
Expand All @@ -9,25 +10,17 @@
else:
import mock

from socketio import asyncio_server
from socketio import asyncio_namespace
from socketio import packet
from socketio import namespace
if sys.version_info >= (3, 5):
import asyncio
from asyncio import coroutine
from socketio import asyncio_server
from socketio import asyncio_namespace
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
Expand Down Expand Up @@ -476,12 +469,15 @@ async def _test():
self.assertEqual(session, {'foo': 'bar'})
session['foo'] = 'baz'
session['bar'] = 'foo'
self.assertEqual(await s.get_session('123'), {'foo': 'baz', 'bar': 'foo'})
self.assertEqual(fake_session, {'/': {'foo': 'baz', 'bar': 'foo'}})
self.assertEqual(await s.get_session('123'),
{'foo': 'baz', 'bar': 'foo'})
self.assertEqual(fake_session,
{'/': {'foo': 'baz', 'bar': 'foo'}})
async with s.session('123', namespace='/ns') as session:
self.assertEqual(session, {})
session['a'] = 'b'
self.assertEqual(await s.get_session('123', namespace='/ns'), {'a': 'b'})
self.assertEqual(await s.get_session('123', namespace='/ns'),
{'a': 'b'})
self.assertEqual(fake_session, {'/': {'foo': 'baz', 'bar': 'foo'},
'/ns': {'a': 'b'}})
_run(_test())
Expand Down Expand Up @@ -528,19 +524,16 @@ class MyNamespace(asyncio_namespace.AsyncNamespace):
def on_connect(self, sid, environ):
result['result'] = (sid, environ)

@coroutine
def on_disconnect(self, sid):
async def on_disconnect(self, sid):
result['result'] = ('disconnect', sid)

@coroutine
def on_foo(self, sid, data):
async def on_foo(self, sid, data):
result['result'] = (sid, data)

def on_bar(self, sid):
result['result'] = 'bar'

@coroutine
def on_baz(self, sid, data1, data2):
async def on_baz(self, sid, data1, data2):
result['result'] = (data1, data2)

s = asyncio_server.AsyncServer()
Expand Down
Empty file added tests/common/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ deps=
coverage
mock
basepython =
flake8: python3.6
flake8: python3.5
py27: python2.7
py35: python3.5
py36: python3.6
Expand All @@ -23,6 +23,7 @@ basepython =

[testenv:flake8]
deps=
six
flake8
commands=
flake8 --exclude=".*" --ignore=E402,E722 socketio tests
Expand Down

0 comments on commit b0a8b1f

Please sign in to comment.