From 8f0e66c1cd1cd63dcef703576cc9cb9c99104df7 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sun, 24 Nov 2024 20:24:13 +0000 Subject: [PATCH] Adopted unittest.mock.AsyncMock in async unit tests --- tests/async/helpers.py | 12 - tests/async/test_admin.py | 3 +- tests/async/test_client.py | 455 ++++++++++++++--------------- tests/async/test_manager.py | 110 +++---- tests/async/test_namespace.py | 78 ++--- tests/async/test_pubsub_manager.py | 172 ++++++----- tests/async/test_server.py | 240 ++++++++------- tests/async/test_simple_client.py | 40 +-- 8 files changed, 543 insertions(+), 567 deletions(-) diff --git a/tests/async/helpers.py b/tests/async/helpers.py index 09e323c7..c9b708c9 100644 --- a/tests/async/helpers.py +++ b/tests/async/helpers.py @@ -1,16 +1,4 @@ import asyncio -from unittest import mock - - -def AsyncMock(*args, **kwargs): - """Return a mock asynchronous function.""" - m = mock.MagicMock(*args, **kwargs) - - async def mock_coro(*args, **kwargs): - return m(*args, **kwargs) - - mock_coro.mock = m - return mock_coro def _run(coro): diff --git a/tests/async/test_admin.py b/tests/async/test_admin.py index 4f283538..a1cf97c4 100644 --- a/tests/async/test_admin.py +++ b/tests/async/test_admin.py @@ -10,7 +10,6 @@ import socketio from socketio.exceptions import ConnectionError from tests.asyncio_web_server import SocketIOWebServer -from .helpers import AsyncMock def with_instrumented_server(auth=False, **ikwargs): @@ -211,7 +210,7 @@ def test_admin_connect_with_others(self): sid1 = client1.sid saved_check_for_upgrade = self.isvr._check_for_upgrade - self.isvr._check_for_upgrade = AsyncMock() + self.isvr._check_for_upgrade = mock.AsyncMock() client2.connect('http://localhost:8900', namespace='/foo', transports=['polling']) sid2 = client2.sid diff --git a/tests/async/test_client.py b/tests/async/test_client.py index 38c690d3..289abd94 100644 --- a/tests/async/test_client.py +++ b/tests/async/test_client.py @@ -8,7 +8,7 @@ from engineio import exceptions as engineio_exceptions from socketio import exceptions from socketio import packet -from .helpers import AsyncMock, _run +from .helpers import _run class TestAsyncClient: @@ -18,7 +18,7 @@ def test_is_asyncio_based(self): def test_connect(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() _run( c.connect( 'url', @@ -36,7 +36,7 @@ def test_connect(self): assert c.connection_transports == 'transports' assert c.connection_namespaces == ['/foo', '/', '/bar'] assert c.socketio_path == 'path' - c.eio.connect.mock.assert_called_once_with( + c.eio.connect.assert_awaited_once_with( 'url', headers='headers', transports='transports', @@ -48,7 +48,7 @@ async def headers(): return 'headers' c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() _run( c.connect( lambda: 'url', @@ -60,7 +60,7 @@ async def headers(): wait=False, ) ) - c.eio.connect.mock.assert_called_once_with( + c.eio.connect.assert_awaited_once_with( 'url', headers='headers', transports='transports', @@ -69,7 +69,7 @@ async def headers(): def test_connect_one_namespace(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() _run( c.connect( 'url', @@ -85,7 +85,7 @@ def test_connect_one_namespace(self): assert c.connection_transports == 'transports' assert c.connection_namespaces == ['/foo'] assert c.socketio_path == 'path' - c.eio.connect.mock.assert_called_once_with( + c.eio.connect.assert_awaited_once_with( 'url', headers='headers', transports='transports', @@ -94,7 +94,7 @@ def test_connect_one_namespace(self): def test_connect_default_namespaces(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() c.on('foo', mock.MagicMock(), namespace='/foo') c.on('bar', mock.MagicMock(), namespace='/') c.on('baz', mock.MagicMock(), namespace='*') @@ -113,7 +113,7 @@ def test_connect_default_namespaces(self): assert c.connection_namespaces == ['/', '/foo'] or \ c.connection_namespaces == ['/foo', '/'] assert c.socketio_path == 'path' - c.eio.connect.mock.assert_called_once_with( + c.eio.connect.assert_awaited_once_with( 'url', headers='headers', transports='transports', @@ -122,7 +122,7 @@ def test_connect_default_namespaces(self): def test_connect_no_namespaces(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() _run( c.connect( 'url', @@ -137,7 +137,7 @@ def test_connect_no_namespaces(self): assert c.connection_transports == 'transports' assert c.connection_namespaces == ['/'] assert c.socketio_path == 'path' - c.eio.connect.mock.assert_called_once_with( + c.eio.connect.assert_awaited_once_with( 'url', headers='headers', transports='transports', @@ -146,7 +146,7 @@ def test_connect_no_namespaces(self): def test_connect_error(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock( + c.eio.connect = mock.AsyncMock( side_effect=engineio_exceptions.ConnectionError('foo') ) c.on('foo', mock.MagicMock(), namespace='/foo') @@ -164,7 +164,7 @@ def test_connect_error(self): def test_connect_twice(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() _run( c.connect( 'url', @@ -181,7 +181,7 @@ def test_connect_twice(self): def test_connect_wait_single_namespace(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() c._connect_event = mock.MagicMock() async def mock_connect(): @@ -200,7 +200,7 @@ async def mock_connect(): def test_connect_wait_two_namespaces(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() + c.eio.connect = mock.AsyncMock() c._connect_event = mock.MagicMock() async def mock_connect(): @@ -226,8 +226,8 @@ async def mock_connect(): def test_connect_timeout(self): c = async_client.AsyncClient() - c.eio.connect = AsyncMock() - c.disconnect = AsyncMock() + c.eio.connect = mock.AsyncMock() + c.disconnect = mock.AsyncMock() with pytest.raises(exceptions.ConnectionError): _run( c.connect( @@ -236,21 +236,21 @@ def test_connect_timeout(self): wait_timeout=0.01, ) ) - c.disconnect.mock.assert_called_once_with() + c.disconnect.assert_awaited_once_with() def test_wait_no_reconnect(self): c = async_client.AsyncClient() - c.eio.wait = AsyncMock() - c.sleep = AsyncMock() + c.eio.wait = mock.AsyncMock() + c.sleep = mock.AsyncMock() c._reconnect_task = None _run(c.wait()) - c.eio.wait.mock.assert_called_once_with() - c.sleep.mock.assert_called_once_with(1) + c.eio.wait.assert_awaited_once_with() + c.sleep.assert_awaited_once_with(1) def test_wait_reconnect_failed(self): c = async_client.AsyncClient() - c.eio.wait = AsyncMock() - c.sleep = AsyncMock() + c.eio.wait = mock.AsyncMock() + c.sleep = mock.AsyncMock() states = ['disconnected'] async def fake_wait(): @@ -258,13 +258,13 @@ async def fake_wait(): c._reconnect_task = fake_wait() _run(c.wait()) - c.eio.wait.mock.assert_called_once_with() - c.sleep.mock.assert_called_once_with(1) + c.eio.wait.assert_awaited_once_with() + c.sleep.assert_awaited_once_with(1) def test_wait_reconnect_successful(self): c = async_client.AsyncClient() - c.eio.wait = AsyncMock() - c.sleep = AsyncMock() + c.eio.wait = mock.AsyncMock() + c.sleep = mock.AsyncMock() states = ['connected', 'disconnected'] async def fake_wait(): @@ -273,26 +273,26 @@ async def fake_wait(): c._reconnect_task = fake_wait() _run(c.wait()) - assert c.eio.wait.mock.call_count == 2 - assert c.sleep.mock.call_count == 2 + assert c.eio.wait.await_count == 2 + assert c.sleep.await_count == 2 def test_emit_no_arguments(self): c = async_client.AsyncClient() c.namespaces = {'/': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() _run(c.emit('foo')) expected_packet = packet.Packet( packet.EVENT, namespace='/', data=['foo'], id=None) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_emit_one_argument(self): c = async_client.AsyncClient() c.namespaces = {'/': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() _run(c.emit('foo', 'bar')) expected_packet = packet.Packet( packet.EVENT, @@ -300,16 +300,16 @@ def test_emit_one_argument(self): data=['foo', 'bar'], id=None, ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_emit_one_argument_list(self): c = async_client.AsyncClient() c.namespaces = {'/': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() _run(c.emit('foo', ['bar', 'baz'])) expected_packet = packet.Packet( packet.EVENT, @@ -317,16 +317,16 @@ def test_emit_one_argument_list(self): data=['foo', ['bar', 'baz']], id=None, ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_emit_two_arguments(self): c = async_client.AsyncClient() c.namespaces = {'/': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() _run(c.emit('foo', ('bar', 'baz'))) expected_packet = packet.Packet( packet.EVENT, @@ -334,22 +334,22 @@ def test_emit_two_arguments(self): data=['foo', 'bar', 'baz'], id=None, ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_emit_namespace(self): c = async_client.AsyncClient() c.namespaces = {'/foo': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() _run(c.emit('foo', namespace='/foo')) expected_packet = packet.Packet( packet.EVENT, namespace='/foo', data=['foo'], id=None) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) @@ -361,15 +361,15 @@ def test_emit_unknown_namespace(self): def test_emit_with_callback(self): c = async_client.AsyncClient() - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() c._generate_ack_id = mock.MagicMock(return_value=123) c.namespaces = {'/': '1'} _run(c.emit('foo', callback='cb')) expected_packet = packet.Packet( packet.EVENT, namespace='/', data=['foo'], id=123) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) c._generate_ack_id.assert_called_once_with('/', 'cb') @@ -377,14 +377,14 @@ def test_emit_with_callback(self): def test_emit_namespace_with_callback(self): c = async_client.AsyncClient() c.namespaces = {'/foo': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() c._generate_ack_id = mock.MagicMock(return_value=123) _run(c.emit('foo', namespace='/foo', callback='cb')) expected_packet = packet.Packet( packet.EVENT, namespace='/foo', data=['foo'], id=123) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) c._generate_ack_id.assert_called_once_with('/foo', 'cb') @@ -392,7 +392,7 @@ def test_emit_namespace_with_callback(self): def test_emit_binary(self): c = async_client.AsyncClient() c.namespaces = {'/': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() _run(c.emit('foo', b'bar')) expected_packet = packet.Packet( packet.EVENT, @@ -400,16 +400,16 @@ def test_emit_binary(self): data=['foo', b'bar'], id=None, ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_emit_not_binary(self): c = async_client.AsyncClient() c.namespaces = {'/': '1'} - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() _run(c.emit('foo', 'bar')) expected_packet = packet.Packet( packet.EVENT, @@ -417,25 +417,25 @@ def test_emit_not_binary(self): data=['foo', 'bar'], id=None, ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_send(self): c = async_client.AsyncClient() - c.emit = AsyncMock() + c.emit = mock.AsyncMock() _run(c.send('data', 'namespace', 'callback')) - c.emit.mock.assert_called_once_with( + c.emit.assert_awaited_once_with( 'message', data='data', namespace='namespace', callback='callback' ) def test_send_with_defaults(self): c = async_client.AsyncClient() - c.emit = AsyncMock() + c.emit = mock.AsyncMock() _run(c.send('data')) - c.emit.mock.assert_called_once_with( + c.emit.assert_awaited_once_with( 'message', data='data', namespace=None, callback=None ) @@ -446,16 +446,16 @@ def test_call(self): async def fake_event_wait(): c._generate_ack_id.call_args_list[0][0][1]('foo', 321) - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() c._generate_ack_id = mock.MagicMock(return_value=123) c.eio = mock.MagicMock() c.eio.create_event.return_value.wait = fake_event_wait assert _run(c.call('foo')) == ('foo', 321) expected_packet = packet.Packet( packet.EVENT, namespace='/', data=['foo'], id=123) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) @@ -466,7 +466,7 @@ def test_call_with_timeout(self): async def fake_event_wait(): await asyncio.sleep(1) - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() c._generate_ack_id = mock.MagicMock(return_value=123) c.eio = mock.MagicMock() c.eio.create_event.return_value.wait = fake_event_wait @@ -474,9 +474,9 @@ async def fake_event_wait(): _run(c.call('foo', timeout=0.01)) expected_packet = packet.Packet( packet.EVENT, namespace='/', data=['foo'], id=123) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) @@ -484,41 +484,41 @@ def test_disconnect(self): c = async_client.AsyncClient() c.connected = True c.namespaces = {'/': '1'} - c._trigger_event = AsyncMock() - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock() + c._send_packet = mock.AsyncMock() c.eio = mock.MagicMock() - c.eio.disconnect = AsyncMock() + c.eio.disconnect = mock.AsyncMock() c.eio.state = 'connected' _run(c.disconnect()) assert c.connected - assert c._trigger_event.mock.call_count == 0 - assert c._send_packet.mock.call_count == 1 + assert c._trigger_event.await_count == 0 + assert c._send_packet.await_count == 1 expected_packet = packet.Packet(packet.DISCONNECT, namespace='/') assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) - c.eio.disconnect.mock.assert_called_once_with(abort=True) + c.eio.disconnect.assert_awaited_once_with(abort=True) def test_disconnect_namespaces(self): c = async_client.AsyncClient() c.namespaces = {'/foo': '1', '/bar': '2'} - c._trigger_event = AsyncMock() - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock() + c._send_packet = mock.AsyncMock() c.eio = mock.MagicMock() - c.eio.disconnect = AsyncMock() + c.eio.disconnect = mock.AsyncMock() c.eio.state = 'connected' _run(c.disconnect()) - assert c._trigger_event.mock.call_count == 0 - assert c._send_packet.mock.call_count == 2 + assert c._trigger_event.await_count == 0 + assert c._send_packet.await_count == 2 expected_packet = packet.Packet(packet.DISCONNECT, namespace='/foo') assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) expected_packet = packet.Packet(packet.DISCONNECT, namespace='/bar') assert ( - c._send_packet.mock.call_args_list[1][0][0].encode() + c._send_packet.await_args_list[1][0][0].encode() == expected_packet.encode() ) @@ -532,103 +532,103 @@ def test_start_background_task(self): def test_sleep(self): c = async_client.AsyncClient() - c.eio.sleep = AsyncMock() + c.eio.sleep = mock.AsyncMock() _run(c.sleep(1.23)) - c.eio.sleep.mock.assert_called_once_with(1.23) + c.eio.sleep.assert_awaited_once_with(1.23) def test_send_packet(self): c = async_client.AsyncClient() - c.eio.send = AsyncMock() + c.eio.send = mock.AsyncMock() _run(c._send_packet(packet.Packet(packet.EVENT, 'foo'))) - c.eio.send.mock.assert_called_once_with('2"foo"') + c.eio.send.assert_awaited_once_with('2"foo"') def test_send_packet_binary(self): c = async_client.AsyncClient() - c.eio.send = AsyncMock() + c.eio.send = mock.AsyncMock() _run(c._send_packet(packet.Packet(packet.EVENT, b'foo'))) - assert c.eio.send.mock.call_args_list == [ + assert c.eio.send.await_args_list == [ mock.call('51-{"_placeholder":true,"num":0}'), mock.call(b'foo'), - ] or c.eio.send.mock.call_args_list == [ + ] or c.eio.send.await_args_list == [ mock.call('51-{"num":0,"_placeholder":true}'), mock.call(b'foo'), ] def test_send_packet_default_binary(self): c = async_client.AsyncClient() - c.eio.send = AsyncMock() + c.eio.send = mock.AsyncMock() _run(c._send_packet(packet.Packet(packet.EVENT, 'foo'))) - c.eio.send.mock.assert_called_once_with('2"foo"') + c.eio.send.assert_awaited_once_with('2"foo"') def test_handle_connect(self): c = async_client.AsyncClient() c._connect_event = mock.MagicMock() - c._trigger_event = AsyncMock() - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock() + c._send_packet = mock.AsyncMock() _run(c._handle_connect('/', {'sid': '123'})) c._connect_event.set.assert_called_once_with() - c._trigger_event.mock.assert_called_once_with('connect', namespace='/') - c._send_packet.mock.assert_not_called() + c._trigger_event.assert_awaited_once_with('connect', namespace='/') + c._send_packet.assert_not_awaited() def test_handle_connect_with_namespaces(self): c = async_client.AsyncClient() c.namespaces = {'/foo': '1', '/bar': '2'} c._connect_event = mock.MagicMock() - c._trigger_event = AsyncMock() - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock() + c._send_packet = mock.AsyncMock() _run(c._handle_connect('/', {'sid': '3'})) c._connect_event.set.assert_called_once_with() - c._trigger_event.mock.assert_called_once_with('connect', namespace='/') + c._trigger_event.assert_awaited_once_with('connect', namespace='/') assert c.namespaces == {'/': '3', '/foo': '1', '/bar': '2'} def test_handle_connect_namespace(self): c = async_client.AsyncClient() c.namespaces = {'/foo': '1'} c._connect_event = mock.MagicMock() - c._trigger_event = AsyncMock() - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock() + c._send_packet = mock.AsyncMock() _run(c._handle_connect('/foo', {'sid': '123'})) _run(c._handle_connect('/bar', {'sid': '2'})) - assert c._trigger_event.mock.call_count == 1 + assert c._trigger_event.await_count == 1 c._connect_event.set.assert_called_once_with() - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'connect', namespace='/bar') assert c.namespaces == {'/foo': '1', '/bar': '2'} def test_handle_disconnect(self): c = async_client.AsyncClient() c.connected = True - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() _run(c._handle_disconnect('/')) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( 'disconnect', namespace='/' ) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( '__disconnect_final', namespace='/' ) assert not c.connected _run(c._handle_disconnect('/')) - assert c._trigger_event.mock.call_count == 2 + assert c._trigger_event.await_count == 2 def test_handle_disconnect_namespace(self): c = async_client.AsyncClient() c.connected = True c.namespaces = {'/foo': '1', '/bar': '2'} - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() _run(c._handle_disconnect('/foo')) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( 'disconnect', namespace='/foo' ) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( '__disconnect_final', namespace='/foo' ) assert c.namespaces == {'/bar': '2'} assert c.connected _run(c._handle_disconnect('/bar')) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( 'disconnect', namespace='/bar' ) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( '__disconnect_final', namespace='/bar' ) assert c.namespaces == {} @@ -638,12 +638,12 @@ def test_handle_disconnect_unknown_namespace(self): c = async_client.AsyncClient() c.connected = True c.namespaces = {'/foo': '1', '/bar': '2'} - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() _run(c._handle_disconnect('/baz')) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( 'disconnect', namespace='/baz' ) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( '__disconnect_final', namespace='/baz' ) assert c.namespaces == {'/foo': '1', '/bar': '2'} @@ -653,83 +653,82 @@ def test_handle_disconnect_default_namespaces(self): c = async_client.AsyncClient() c.connected = True c.namespaces = {'/foo': '1', '/bar': '2'} - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() _run(c._handle_disconnect('/')) - c._trigger_event.mock.assert_any_call('disconnect', namespace='/') - c._trigger_event.mock.assert_any_call('__disconnect_final', - namespace='/') + c._trigger_event.assert_any_await('disconnect', namespace='/') + c._trigger_event.assert_any_await('__disconnect_final', namespace='/') assert c.namespaces == {'/foo': '1', '/bar': '2'} assert c.connected def test_handle_event(self): c = async_client.AsyncClient() - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() _run(c._handle_event('/', None, ['foo', ('bar', 'baz')])) - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'foo', '/', ('bar', 'baz') ) def test_handle_event_with_id_no_arguments(self): c = async_client.AsyncClient() - c._trigger_event = AsyncMock(return_value=None) - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock(return_value=None) + c._send_packet = mock.AsyncMock() _run(c._handle_event('/', 123, ['foo', ('bar', 'baz')])) - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'foo', '/', ('bar', 'baz') ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 expected_packet = packet.Packet( packet.ACK, namespace='/', id=123, data=[]) assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_handle_event_with_id_one_argument(self): c = async_client.AsyncClient() - c._trigger_event = AsyncMock(return_value='ret') - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock(return_value='ret') + c._send_packet = mock.AsyncMock() _run(c._handle_event('/', 123, ['foo', ('bar', 'baz')])) - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'foo', '/', ('bar', 'baz') ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 expected_packet = packet.Packet( packet.ACK, namespace='/', id=123, data=['ret']) assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_handle_event_with_id_one_list_argument(self): c = async_client.AsyncClient() - c._trigger_event = AsyncMock(return_value=['a', 'b']) - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock(return_value=['a', 'b']) + c._send_packet = mock.AsyncMock() _run(c._handle_event('/', 123, ['foo', ('bar', 'baz')])) - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'foo', '/', ('bar', 'baz') ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 expected_packet = packet.Packet( packet.ACK, namespace='/', id=123, data=[['a', 'b']]) assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) def test_handle_event_with_id_two_arguments(self): c = async_client.AsyncClient() - c._trigger_event = AsyncMock(return_value=('a', 'b')) - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock(return_value=('a', 'b')) + c._send_packet = mock.AsyncMock() _run(c._handle_event('/', 123, ['foo', ('bar', 'baz')])) - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'foo', '/', ('bar', 'baz') ) - assert c._send_packet.mock.call_count == 1 + assert c._send_packet.await_count == 1 expected_packet = packet.Packet( packet.ACK, namespace='/', id=123, data=['a', 'b']) assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) @@ -743,10 +742,10 @@ def test_handle_ack(self): def test_handle_ack_async(self): c = async_client.AsyncClient() - mock_cb = AsyncMock() + mock_cb = mock.AsyncMock() c.callbacks['/foo'] = {123: mock_cb} _run(c._handle_ack('/foo', 123, ['bar', 'baz'])) - mock_cb.mock.assert_called_once_with('bar', 'baz') + mock_cb.assert_awaited_once_with('bar', 'baz') assert 123 not in c.callbacks['/foo'] def test_handle_ack_not_found(self): @@ -761,13 +760,13 @@ def test_handle_error(self): c = async_client.AsyncClient() c.connected = True c._connect_event = mock.MagicMock() - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() c.namespaces = {'/foo': '1', '/bar': '2'} _run(c._handle_error('/', 'error')) assert c.namespaces == {} assert not c.connected c._connect_event.set.assert_called_once_with() - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'connect_error', '/', 'error' ) @@ -775,25 +774,25 @@ def test_handle_error_with_no_arguments(self): c = async_client.AsyncClient() c.connected = True c._connect_event = mock.MagicMock() - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() c.namespaces = {'/foo': '1', '/bar': '2'} _run(c._handle_error('/', None)) assert c.namespaces == {} assert not c.connected c._connect_event.set.assert_called_once_with() - c._trigger_event.mock.assert_called_once_with('connect_error', '/') + c._trigger_event.assert_awaited_once_with('connect_error', '/') def test_handle_error_namespace(self): c = async_client.AsyncClient() c.connected = True c.namespaces = {'/foo': '1', '/bar': '2'} c._connect_event = mock.MagicMock() - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() _run(c._handle_error('/bar', ['error', 'message'])) assert c.namespaces == {'/foo': '1'} assert c.connected c._connect_event.set.assert_called_once_with() - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'connect_error', '/bar', 'error', 'message' ) @@ -802,12 +801,12 @@ def test_handle_error_namespace_with_no_arguments(self): c.connected = True c.namespaces = {'/foo': '1', '/bar': '2'} c._connect_event = mock.MagicMock() - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() _run(c._handle_error('/bar', None)) assert c.namespaces == {'/foo': '1'} assert c.connected c._connect_event.set.assert_called_once_with() - c._trigger_event.mock.assert_called_once_with('connect_error', '/bar') + c._trigger_event.assert_awaited_once_with('connect_error', '/bar') def test_handle_error_unknown_namespace(self): c = async_client.AsyncClient() @@ -833,14 +832,14 @@ def test_trigger_event(self): def test_trigger_event_namespace(self): c = async_client.AsyncClient() - handler = AsyncMock() - catchall_handler = AsyncMock() + handler = mock.AsyncMock() + catchall_handler = mock.AsyncMock() c.on('foo', handler, namespace='/bar') c.on('*', catchall_handler, namespace='/bar') _run(c._trigger_event('foo', '/bar', 1, '2')) _run(c._trigger_event('bar', '/bar', 1, '2', 3)) - handler.mock.assert_called_once_with(1, '2') - catchall_handler.mock.assert_called_once_with('bar', 1, '2', 3) + handler.assert_awaited_once_with(1, '2') + catchall_handler.assert_awaited_once_with('bar', 1, '2', 3) def test_trigger_event_class_namespace(self): c = async_client.AsyncClient() @@ -902,19 +901,19 @@ def on_foo(self, a, b): @mock.patch( 'asyncio.wait_for', - new_callable=AsyncMock, + new_callable=mock.AsyncMock, side_effect=asyncio.TimeoutError, ) @mock.patch('socketio.client.random.random', side_effect=[1, 0, 0.5]) def test_handle_reconnect(self, random, wait_for): c = async_client.AsyncClient() c._reconnect_task = 'foo' - c.connect = AsyncMock( + c.connect = mock.AsyncMock( side_effect=[ValueError, exceptions.ConnectionError, None] ) _run(c._handle_reconnect()) - assert wait_for.mock.call_count == 3 - assert [x[0][1] for x in asyncio.wait_for.mock.call_args_list] == [ + assert wait_for.await_count == 3 + assert [x[0][1] for x in asyncio.wait_for.await_args_list] == [ 1.5, 1.5, 4.0, @@ -923,19 +922,19 @@ def test_handle_reconnect(self, random, wait_for): @mock.patch( 'asyncio.wait_for', - new_callable=AsyncMock, + new_callable=mock.AsyncMock, side_effect=asyncio.TimeoutError, ) @mock.patch('socketio.client.random.random', side_effect=[1, 0, 0.5]) def test_handle_reconnect_max_delay(self, random, wait_for): c = async_client.AsyncClient(reconnection_delay_max=3) c._reconnect_task = 'foo' - c.connect = AsyncMock( + c.connect = mock.AsyncMock( side_effect=[ValueError, exceptions.ConnectionError, None] ) _run(c._handle_reconnect()) - assert wait_for.mock.call_count == 3 - assert [x[0][1] for x in asyncio.wait_for.mock.call_args_list] == [ + assert wait_for.await_count == 3 + assert [x[0][1] for x in asyncio.wait_for.await_args_list] == [ 1.5, 1.5, 3.0, @@ -944,7 +943,7 @@ def test_handle_reconnect_max_delay(self, random, wait_for): @mock.patch( 'asyncio.wait_for', - new_callable=AsyncMock, + new_callable=mock.AsyncMock, side_effect=asyncio.TimeoutError, ) @mock.patch('socketio.client.random.random', side_effect=[1, 0, 0.5]) @@ -952,23 +951,23 @@ def test_handle_reconnect_max_attempts(self, random, wait_for): c = async_client.AsyncClient(reconnection_attempts=2, logger=True) c.connection_namespaces = ['/'] c._reconnect_task = 'foo' - c._trigger_event = AsyncMock() - c.connect = AsyncMock( + c._trigger_event = mock.AsyncMock() + c.connect = mock.AsyncMock( side_effect=[ValueError, exceptions.ConnectionError, None] ) _run(c._handle_reconnect()) - assert wait_for.mock.call_count == 2 - assert [x[0][1] for x in asyncio.wait_for.mock.call_args_list] == [ + assert wait_for.await_count == 2 + assert [x[0][1] for x in asyncio.wait_for.await_args_list] == [ 1.5, 1.5, ] assert c._reconnect_task == 'foo' - c._trigger_event.mock.assert_called_once_with('__disconnect_final', - namespace='/') + c._trigger_event.assert_awaited_once_with('__disconnect_final', + namespace='/') @mock.patch( 'asyncio.wait_for', - new_callable=AsyncMock, + new_callable=mock.AsyncMock, side_effect=[asyncio.TimeoutError, None], ) @mock.patch('socketio.client.random.random', side_effect=[1, 0, 0.5]) @@ -976,59 +975,59 @@ def test_handle_reconnect_aborted(self, random, wait_for): c = async_client.AsyncClient(logger=True) c.connection_namespaces = ['/'] c._reconnect_task = 'foo' - c._trigger_event = AsyncMock() - c.connect = AsyncMock( + c._trigger_event = mock.AsyncMock() + c.connect = mock.AsyncMock( side_effect=[ValueError, exceptions.ConnectionError, None] ) _run(c._handle_reconnect()) - assert wait_for.mock.call_count == 2 - assert [x[0][1] for x in asyncio.wait_for.mock.call_args_list] == [ + assert wait_for.await_count == 2 + assert [x[0][1] for x in asyncio.wait_for.await_args_list] == [ 1.5, 1.5, ] assert c._reconnect_task == 'foo' - c._trigger_event.mock.assert_called_once_with('__disconnect_final', - namespace='/') + c._trigger_event.assert_awaited_once_with('__disconnect_final', + namespace='/') def test_shutdown_disconnect(self): c = async_client.AsyncClient() c.connected = True c.namespaces = {'/': '1'} - c._trigger_event = AsyncMock() - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock() + c._send_packet = mock.AsyncMock() c.eio = mock.MagicMock() - c.eio.disconnect = AsyncMock() + c.eio.disconnect = mock.AsyncMock() c.eio.state = 'connected' _run(c.shutdown()) - assert c._trigger_event.mock.call_count == 0 - assert c._send_packet.mock.call_count == 1 + assert c._trigger_event.await_count == 0 + assert c._send_packet.await_count == 1 expected_packet = packet.Packet(packet.DISCONNECT, namespace='/') assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) - c.eio.disconnect.mock.assert_called_once_with(abort=True) + c.eio.disconnect.assert_awaited_once_with(abort=True) def test_shutdown_disconnect_namespaces(self): c = async_client.AsyncClient() c.connected = True c.namespaces = {'/foo': '1', '/bar': '2'} - c._trigger_event = AsyncMock() - c._send_packet = AsyncMock() + c._trigger_event = mock.AsyncMock() + c._send_packet = mock.AsyncMock() c.eio = mock.MagicMock() - c.eio.disconnect = AsyncMock() + c.eio.disconnect = mock.AsyncMock() c.eio.state = 'connected' _run(c.shutdown()) - assert c._trigger_event.mock.call_count == 0 - assert c._send_packet.mock.call_count == 2 + assert c._trigger_event.await_count == 0 + assert c._send_packet.await_count == 2 expected_packet = packet.Packet(packet.DISCONNECT, namespace='/foo') assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) expected_packet = packet.Packet(packet.DISCONNECT, namespace='/bar') assert ( - c._send_packet.mock.call_args_list[1][0][0].encode() + c._send_packet.await_args_list[1][0][0].encode() == expected_packet.encode() ) @@ -1036,9 +1035,9 @@ def test_shutdown_disconnect_namespaces(self): def test_shutdown_reconnect(self, random): c = async_client.AsyncClient() c.connection_namespaces = ['/'] - c._reconnect_task = AsyncMock()() - c._trigger_event = AsyncMock() - c.connect = AsyncMock(side_effect=exceptions.ConnectionError) + c._reconnect_task = mock.AsyncMock()() + c._trigger_event = mock.AsyncMock() + c.connect = mock.AsyncMock(side_effect=exceptions.ConnectionError) async def r(): task = c.start_background_task(c._handle_reconnect) @@ -1047,29 +1046,29 @@ async def r(): await task _run(r()) - c._trigger_event.mock.assert_called_once_with('__disconnect_final', - namespace='/') + c._trigger_event.assert_awaited_once_with('__disconnect_final', + namespace='/') def test_handle_eio_connect(self): c = async_client.AsyncClient() c.connection_namespaces = ['/', '/foo'] c.connection_auth = 'auth' - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() c.eio.sid = 'foo' assert c.sid is None _run(c._handle_eio_connect()) assert c.sid == 'foo' - assert c._send_packet.mock.call_count == 2 + assert c._send_packet.await_count == 2 expected_packet = packet.Packet( packet.CONNECT, data='auth', namespace='/') assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) expected_packet = packet.Packet( packet.CONNECT, data='auth', namespace='/foo') assert ( - c._send_packet.mock.call_args_list[1][0][0].encode() + c._send_packet.await_args_list[1][0][0].encode() == expected_packet.encode() ) @@ -1077,59 +1076,59 @@ def test_handle_eio_connect_function(self): c = async_client.AsyncClient() c.connection_namespaces = ['/', '/foo'] c.connection_auth = lambda: 'auth' - c._send_packet = AsyncMock() + c._send_packet = mock.AsyncMock() c.eio.sid = 'foo' assert c.sid is None _run(c._handle_eio_connect()) assert c.sid == 'foo' - assert c._send_packet.mock.call_count == 2 + assert c._send_packet.await_count == 2 expected_packet = packet.Packet( packet.CONNECT, data='auth', namespace='/') assert ( - c._send_packet.mock.call_args_list[0][0][0].encode() + c._send_packet.await_args_list[0][0][0].encode() == expected_packet.encode() ) expected_packet = packet.Packet( packet.CONNECT, data='auth', namespace='/foo') assert ( - c._send_packet.mock.call_args_list[1][0][0].encode() + c._send_packet.await_args_list[1][0][0].encode() == expected_packet.encode() ) def test_handle_eio_message(self): c = async_client.AsyncClient() - c._handle_connect = AsyncMock() - c._handle_disconnect = AsyncMock() - c._handle_event = AsyncMock() - c._handle_ack = AsyncMock() - c._handle_error = AsyncMock() + c._handle_connect = mock.AsyncMock() + c._handle_disconnect = mock.AsyncMock() + c._handle_event = mock.AsyncMock() + c._handle_ack = mock.AsyncMock() + c._handle_error = mock.AsyncMock() _run(c._handle_eio_message('0{"sid":"123"}')) - c._handle_connect.mock.assert_called_with(None, {'sid': '123'}) + c._handle_connect.assert_awaited_with(None, {'sid': '123'}) _run(c._handle_eio_message('0/foo,{"sid":"123"}')) - c._handle_connect.mock.assert_called_with('/foo', {'sid': '123'}) + c._handle_connect.assert_awaited_with('/foo', {'sid': '123'}) _run(c._handle_eio_message('1')) - c._handle_disconnect.mock.assert_called_with(None) + c._handle_disconnect.assert_awaited_with(None) _run(c._handle_eio_message('1/foo')) - c._handle_disconnect.mock.assert_called_with('/foo') + c._handle_disconnect.assert_awaited_with('/foo') _run(c._handle_eio_message('2["foo"]')) - c._handle_event.mock.assert_called_with(None, None, ['foo']) + c._handle_event.assert_awaited_with(None, None, ['foo']) _run(c._handle_eio_message('3/foo,["bar"]')) - c._handle_ack.mock.assert_called_with('/foo', None, ['bar']) + c._handle_ack.assert_awaited_with('/foo', None, ['bar']) _run(c._handle_eio_message('4')) - c._handle_error.mock.assert_called_with(None, None) + c._handle_error.assert_awaited_with(None, None) _run(c._handle_eio_message('4"foo"')) - c._handle_error.mock.assert_called_with(None, 'foo') + c._handle_error.assert_awaited_with(None, 'foo') _run(c._handle_eio_message('4["foo"]')) - c._handle_error.mock.assert_called_with(None, ['foo']) + c._handle_error.assert_awaited_with(None, ['foo']) _run(c._handle_eio_message('4/foo')) - c._handle_error.mock.assert_called_with('/foo', None) + c._handle_error.assert_awaited_with('/foo', None) _run(c._handle_eio_message('4/foo,["foo","bar"]')) - c._handle_error.mock.assert_called_with('/foo', ['foo', 'bar']) + c._handle_error.assert_awaited_with('/foo', ['foo', 'bar']) _run(c._handle_eio_message('51-{"_placeholder":true,"num":0}')) assert c._binary_packet.packet_type == packet.BINARY_EVENT _run(c._handle_eio_message(b'foo')) - c._handle_event.mock.assert_called_with(None, None, b'foo') + c._handle_event.assert_awaited_with(None, None, b'foo') _run( c._handle_eio_message( '62-/foo,{"1":{"_placeholder":true,"num":1},' @@ -1139,7 +1138,7 @@ def test_handle_eio_message(self): assert c._binary_packet.packet_type == packet.BINARY_ACK _run(c._handle_eio_message(b'bar')) _run(c._handle_eio_message(b'foo')) - c._handle_ack.mock.assert_called_with( + c._handle_ack.assert_awaited_with( '/foo', None, {'1': b'foo', '2': b'bar'} ) with pytest.raises(ValueError): @@ -1149,12 +1148,12 @@ def test_eio_disconnect(self): c = async_client.AsyncClient() c.namespaces = {'/': '1'} c.connected = True - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() c.start_background_task = mock.MagicMock() c.sid = 'foo' c.eio.state = 'connected' _run(c._handle_eio_disconnect()) - c._trigger_event.mock.assert_called_once_with( + c._trigger_event.assert_awaited_once_with( 'disconnect', namespace='/' ) assert c.sid is None @@ -1164,12 +1163,12 @@ def test_eio_disconnect_namespaces(self): c = async_client.AsyncClient(reconnection=False) c.namespaces = {'/foo': '1', '/bar': '2'} c.connected = True - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() c.sid = 'foo' c.eio.state = 'connected' _run(c._handle_eio_disconnect()) - c._trigger_event.mock.assert_any_call('disconnect', namespace='/foo') - c._trigger_event.mock.assert_any_call('disconnect', namespace='/bar') + c._trigger_event.assert_any_await('disconnect', namespace='/foo') + c._trigger_event.assert_any_await('disconnect', namespace='/bar') assert c.sid is None assert not c.connected @@ -1191,15 +1190,15 @@ def test_eio_disconnect_no_reconnect(self): c = async_client.AsyncClient(reconnection=False) c.namespaces = {'/': '1'} c.connected = True - c._trigger_event = AsyncMock() + c._trigger_event = mock.AsyncMock() c.start_background_task = mock.MagicMock() c.sid = 'foo' c.eio.state = 'connected' _run(c._handle_eio_disconnect()) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( 'disconnect', namespace='/' ) - c._trigger_event.mock.assert_any_call( + c._trigger_event.assert_any_await( '__disconnect_final', namespace='/' ) assert c.sid is None diff --git a/tests/async/test_manager.py b/tests/async/test_manager.py index 50d1dd9d..5d60e039 100644 --- a/tests/async/test_manager.py +++ b/tests/async/test_manager.py @@ -2,7 +2,7 @@ from socketio import async_manager from socketio import packet -from .helpers import AsyncMock, _run +from .helpers import _run class TestAsyncManager: @@ -15,8 +15,8 @@ def generate_id(): return str(id) mock_server = mock.MagicMock() - mock_server._send_packet = AsyncMock() - mock_server._send_eio_packet = AsyncMock() + mock_server._send_packet = mock.AsyncMock() + mock_server._send_eio_packet = mock.AsyncMock() mock_server.eio.generate_id = generate_id mock_server.packet_class = packet.Packet self.bm = async_manager.AsyncManager() @@ -128,14 +128,14 @@ def test_trigger_sync_callback(self): def test_trigger_async_callback(self): sid1 = _run(self.bm.connect('123', '/')) sid2 = _run(self.bm.connect('123', '/foo')) - cb = AsyncMock() + cb = mock.AsyncMock() id1 = self.bm._generate_ack_id(sid1, cb) id2 = self.bm._generate_ack_id(sid2, cb) _run(self.bm.trigger_callback(sid1, id1, ['foo'])) _run(self.bm.trigger_callback(sid2, id2, ['bar', 'baz'])) - assert cb.mock.call_count == 2 - cb.mock.assert_any_call('foo') - cb.mock.assert_any_call('bar', 'baz') + assert cb.await_count == 2 + cb.assert_any_await('foo') + cb.assert_any_await('bar', 'baz') def test_invalid_callback(self): sid = _run(self.bm.connect('123', '/')) @@ -145,7 +145,7 @@ def test_invalid_callback(self): # these should not raise an exception _run(self.bm.trigger_callback('xxx', id, ['foo'])) _run(self.bm.trigger_callback(sid, id + 1, ['foo'])) - assert cb.mock.call_count == 0 + assert cb.call_count == 0 def test_get_namespaces(self): assert list(self.bm.get_namespaces()) == [] @@ -207,10 +207,10 @@ def test_emit_to_sid(self): 'my event', {'foo': 'bar'}, namespace='/foo', to=sid ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 1 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 1 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' def test_emit_to_room(self): @@ -224,13 +224,13 @@ def test_emit_to_room(self): 'my event', {'foo': 'bar'}, namespace='/foo', room='bar' ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 2 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 2 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ + assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \ == '456' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] + assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \ == pkt assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' @@ -246,17 +246,17 @@ def test_emit_to_rooms(self): self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', room=['bar', 'baz']) ) - assert self.bm.server._send_eio_packet.mock.call_count == 3 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 3 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ + assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \ == '456' - assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][0] \ + assert self.bm.server._send_eio_packet.await_args_list[2][0][0] \ == '789' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] + assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \ == pkt - assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][1] \ + assert self.bm.server._send_eio_packet.await_args_list[2][0][1] \ == pkt assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' @@ -268,17 +268,17 @@ def test_emit_to_all(self): _run(self.bm.connect('789', '/foo')) _run(self.bm.connect('abc', '/bar')) _run(self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo')) - assert self.bm.server._send_eio_packet.mock.call_count == 3 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 3 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ + assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \ == '456' - assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][0] \ + assert self.bm.server._send_eio_packet.await_args_list[2][0][0] \ == '789' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] + assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \ == pkt - assert self.bm.server._send_eio_packet.mock.call_args_list[2][0][1] \ + assert self.bm.server._send_eio_packet.await_args_list[2][0][1] \ == pkt assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' @@ -294,13 +294,13 @@ def test_emit_to_all_skip_one(self): 'my event', {'foo': 'bar'}, namespace='/foo', skip_sid=sid2 ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 2 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 2 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ + assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \ == '789' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] \ + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] + assert self.bm.server._send_eio_packet.await_args_list[1][0][1] \ == pkt assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' @@ -319,10 +319,10 @@ def test_emit_to_all_skip_two(self): skip_sid=[sid1, sid3], ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 1 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 1 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '456' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] assert pkt.encode() == '42/foo,["my event",{"foo":"bar"}]' def test_emit_with_callback(self): @@ -335,10 +335,10 @@ def test_emit_with_callback(self): ) ) self.bm._generate_ack_id.assert_called_once_with(sid, 'cb') - assert self.bm.server._send_packet.mock.call_count == 1 - assert self.bm.server._send_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_packet.await_count == 1 + assert self.bm.server._send_packet.await_args_list[0][0][0] \ == '123' - pkt = self.bm.server._send_packet.mock.call_args_list[0][0][1] + pkt = self.bm.server._send_packet.await_args_list[0][0][1] assert pkt.encode() == '2/foo,11["my event",{"foo":"bar"}]' def test_emit_to_invalid_room(self): @@ -356,10 +356,10 @@ def test_emit_with_tuple(self): 'my event', ('foo', 'bar'), namespace='/foo', room=sid ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 1 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 1 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] assert pkt.encode() == '42/foo,["my event","foo","bar"]' def test_emit_with_list(self): @@ -369,10 +369,10 @@ def test_emit_with_list(self): 'my event', ['foo', 'bar'], namespace='/foo', room=sid ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 1 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 1 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] assert pkt.encode() == '42/foo,["my event",["foo","bar"]]' def test_emit_with_none(self): @@ -382,10 +382,10 @@ def test_emit_with_none(self): 'my event', None, namespace='/foo', room=sid ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 1 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 1 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] assert pkt.encode() == '42/foo,["my event"]' def test_emit_binary(self): @@ -395,12 +395,12 @@ def test_emit_binary(self): u'my event', b'my binary data', namespace='/', room=sid ) ) - assert self.bm.server._send_eio_packet.mock.call_count == 2 - assert self.bm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + assert self.bm.server._send_eio_packet.await_count == 2 + assert self.bm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[0][0][1] + pkt = self.bm.server._send_eio_packet.await_args_list[0][0][1] assert pkt.encode() == '451-["my event",{"_placeholder":true,"num":0}]' - assert self.bm.server._send_eio_packet.mock.call_args_list[1][0][0] \ + assert self.bm.server._send_eio_packet.await_args_list[1][0][0] \ == '123' - pkt = self.bm.server._send_eio_packet.mock.call_args_list[1][0][1] + pkt = self.bm.server._send_eio_packet.await_args_list[1][0][1] assert pkt.encode() == b'my binary data' diff --git a/tests/async/test_namespace.py b/tests/async/test_namespace.py index 873f4791..62560159 100644 --- a/tests/async/test_namespace.py +++ b/tests/async/test_namespace.py @@ -1,7 +1,7 @@ from unittest import mock from socketio import async_namespace -from .helpers import AsyncMock, _run +from .helpers import _run class TestAsyncNamespace: @@ -70,14 +70,14 @@ async def on_custom_message(self, sid, data): def test_emit(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.emit = AsyncMock() + mock_server.emit = mock.AsyncMock() ns._set_server(mock_server) _run( ns.emit( 'ev', data='data', to='room', skip_sid='skip', callback='cb' ) ) - ns.server.emit.mock.assert_called_with( + ns.server.emit.assert_awaited_with( 'ev', data='data', to='room', @@ -98,7 +98,7 @@ def test_emit(self): ignore_queue=True, ) ) - ns.server.emit.mock.assert_called_with( + ns.server.emit.assert_awaited_with( 'ev', data='data', to=None, @@ -112,10 +112,10 @@ def test_emit(self): def test_send(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.send = AsyncMock() + mock_server.send = mock.AsyncMock() ns._set_server(mock_server) _run(ns.send(data='data', to='room', skip_sid='skip', callback='cb')) - ns.server.send.mock.assert_called_with( + ns.server.send.assert_awaited_with( 'data', to='room', room=None, @@ -134,7 +134,7 @@ def test_send(self): ignore_queue=True, ) ) - ns.server.send.mock.assert_called_with( + ns.server.send.assert_awaited_with( 'data', to=None, room='room', @@ -147,10 +147,10 @@ def test_send(self): def test_call(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.call = AsyncMock() + mock_server.call = mock.AsyncMock() ns._set_server(mock_server) _run(ns.call('ev', data='data', to='sid')) - ns.server.call.mock.assert_called_with( + ns.server.call.assert_awaited_with( 'ev', data='data', to='sid', @@ -161,7 +161,7 @@ def test_call(self): ) _run(ns.call('ev', data='data', sid='sid', namespace='/bar', timeout=45, ignore_queue=True)) - ns.server.call.mock.assert_called_with( + ns.server.call.assert_awaited_with( 'ev', data='data', to=None, @@ -174,40 +174,40 @@ def test_call(self): def test_enter_room(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.enter_room = AsyncMock() + mock_server.enter_room = mock.AsyncMock() ns._set_server(mock_server) _run(ns.enter_room('sid', 'room')) - ns.server.enter_room.mock.assert_called_with( + ns.server.enter_room.assert_awaited_with( 'sid', 'room', namespace='/foo' ) _run(ns.enter_room('sid', 'room', namespace='/bar')) - ns.server.enter_room.mock.assert_called_with( + ns.server.enter_room.assert_awaited_with( 'sid', 'room', namespace='/bar' ) def test_leave_room(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.leave_room = AsyncMock() + mock_server.leave_room = mock.AsyncMock() ns._set_server(mock_server) _run(ns.leave_room('sid', 'room')) - ns.server.leave_room.mock.assert_called_with( + ns.server.leave_room.assert_awaited_with( 'sid', 'room', namespace='/foo' ) _run(ns.leave_room('sid', 'room', namespace='/bar')) - ns.server.leave_room.mock.assert_called_with( + ns.server.leave_room.assert_awaited_with( 'sid', 'room', namespace='/bar' ) def test_close_room(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.close_room = AsyncMock() + mock_server.close_room = mock.AsyncMock() ns._set_server(mock_server) _run(ns.close_room('room')) - ns.server.close_room.mock.assert_called_with('room', namespace='/foo') + ns.server.close_room.assert_awaited_with('room', namespace='/foo') _run(ns.close_room('room', namespace='/bar')) - ns.server.close_room.mock.assert_called_with('room', namespace='/bar') + ns.server.close_room.assert_awaited_with('room', namespace='/bar') def test_rooms(self): ns = async_namespace.AsyncNamespace('/foo') @@ -220,19 +220,19 @@ def test_rooms(self): def test_session(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.get_session = AsyncMock() - mock_server.save_session = AsyncMock() + mock_server.get_session = mock.AsyncMock() + mock_server.save_session = mock.AsyncMock() ns._set_server(mock_server) _run(ns.get_session('sid')) - ns.server.get_session.mock.assert_called_with('sid', namespace='/foo') + ns.server.get_session.assert_awaited_with('sid', namespace='/foo') _run(ns.get_session('sid', namespace='/bar')) - ns.server.get_session.mock.assert_called_with('sid', namespace='/bar') + ns.server.get_session.assert_awaited_with('sid', namespace='/bar') _run(ns.save_session('sid', {'a': 'b'})) - ns.server.save_session.mock.assert_called_with( + ns.server.save_session.assert_awaited_with( 'sid', {'a': 'b'}, namespace='/foo' ) _run(ns.save_session('sid', {'a': 'b'}, namespace='/bar')) - ns.server.save_session.mock.assert_called_with( + ns.server.save_session.assert_awaited_with( 'sid', {'a': 'b'}, namespace='/bar' ) ns.session('sid') @@ -243,12 +243,12 @@ def test_session(self): def test_disconnect(self): ns = async_namespace.AsyncNamespace('/foo') mock_server = mock.MagicMock() - mock_server.disconnect = AsyncMock() + mock_server.disconnect = mock.AsyncMock() ns._set_server(mock_server) _run(ns.disconnect('sid')) - ns.server.disconnect.mock.assert_called_with('sid', namespace='/foo') + ns.server.disconnect.assert_awaited_with('sid', namespace='/foo') _run(ns.disconnect('sid', namespace='/bar')) - ns.server.disconnect.mock.assert_called_with('sid', namespace='/bar') + ns.server.disconnect.assert_awaited_with('sid', namespace='/bar') def test_sync_event_client(self): result = {} @@ -291,49 +291,49 @@ async def on_custom_message(self, sid, data): def test_emit_client(self): ns = async_namespace.AsyncClientNamespace('/foo') mock_client = mock.MagicMock() - mock_client.emit = AsyncMock() + mock_client.emit = mock.AsyncMock() ns._set_client(mock_client) _run(ns.emit('ev', data='data', callback='cb')) - ns.client.emit.mock.assert_called_with( + ns.client.emit.assert_awaited_with( 'ev', data='data', namespace='/foo', callback='cb' ) _run(ns.emit('ev', data='data', namespace='/bar', callback='cb')) - ns.client.emit.mock.assert_called_with( + ns.client.emit.assert_awaited_with( 'ev', data='data', namespace='/bar', callback='cb' ) def test_send_client(self): ns = async_namespace.AsyncClientNamespace('/foo') mock_client = mock.MagicMock() - mock_client.send = AsyncMock() + mock_client.send = mock.AsyncMock() ns._set_client(mock_client) _run(ns.send(data='data', callback='cb')) - ns.client.send.mock.assert_called_with( + ns.client.send.assert_awaited_with( 'data', namespace='/foo', callback='cb' ) _run(ns.send(data='data', namespace='/bar', callback='cb')) - ns.client.send.mock.assert_called_with( + ns.client.send.assert_awaited_with( 'data', namespace='/bar', callback='cb' ) def test_call_client(self): ns = async_namespace.AsyncClientNamespace('/foo') mock_client = mock.MagicMock() - mock_client.call = AsyncMock() + mock_client.call = mock.AsyncMock() ns._set_client(mock_client) _run(ns.call('ev', data='data')) - ns.client.call.mock.assert_called_with( + ns.client.call.assert_awaited_with( 'ev', data='data', namespace='/foo', timeout=None ) _run(ns.call('ev', data='data', namespace='/bar', timeout=45)) - ns.client.call.mock.assert_called_with( + ns.client.call.assert_awaited_with( 'ev', data='data', namespace='/bar', timeout=45 ) def test_disconnect_client(self): ns = async_namespace.AsyncClientNamespace('/foo') mock_client = mock.MagicMock() - mock_client.disconnect = AsyncMock() + mock_client.disconnect = mock.AsyncMock() ns._set_client(mock_client) _run(ns.disconnect()) - ns.client.disconnect.mock.assert_called_with() + ns.client.disconnect.assert_awaited_with() diff --git a/tests/async/test_pubsub_manager.py b/tests/async/test_pubsub_manager.py index 8a509d23..46928827 100644 --- a/tests/async/test_pubsub_manager.py +++ b/tests/async/test_pubsub_manager.py @@ -7,7 +7,7 @@ from socketio import async_manager from socketio import async_pubsub_manager from socketio import packet -from .helpers import AsyncMock, _run +from .helpers import _run class TestAsyncPubSubManager: @@ -22,11 +22,11 @@ def generate_id(): mock_server = mock.MagicMock() mock_server.eio.generate_id = generate_id mock_server.packet_class = packet.Packet - mock_server._send_packet = AsyncMock() - mock_server._send_eio_packet = AsyncMock() - mock_server.disconnect = AsyncMock() + mock_server._send_packet = mock.AsyncMock() + mock_server._send_eio_packet = mock.AsyncMock() + mock_server.disconnect = mock.AsyncMock() self.pm = async_pubsub_manager.AsyncPubSubManager() - self.pm._publish = AsyncMock() + self.pm._publish = mock.AsyncMock() self.pm.set_server(mock_server) self.pm.host_id = '123456' self.pm.initialize() @@ -53,7 +53,7 @@ def test_write_only_init(self): def test_emit(self): _run(self.pm.emit('foo', 'bar')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( { 'method': 'emit', 'event': 'foo', @@ -69,7 +69,7 @@ def test_emit(self): def test_emit_with_to(self): sid = 'room-mate' _run(self.pm.emit('foo', 'bar', to=sid)) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( { 'method': 'emit', 'event': 'foo', @@ -84,7 +84,7 @@ def test_emit_with_to(self): def test_emit_with_namespace(self): _run(self.pm.emit('foo', 'bar', namespace='/baz')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( { 'method': 'emit', 'event': 'foo', @@ -99,7 +99,7 @@ def test_emit_with_namespace(self): def test_emit_with_room(self): _run(self.pm.emit('foo', 'bar', room='baz')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( { 'method': 'emit', 'event': 'foo', @@ -114,7 +114,7 @@ def test_emit_with_room(self): def test_emit_with_skip_sid(self): _run(self.pm.emit('foo', 'bar', skip_sid='baz')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( { 'method': 'emit', 'event': 'foo', @@ -132,7 +132,7 @@ def test_emit_with_callback(self): self.pm, '_generate_ack_id', return_value='123' ): _run(self.pm.emit('foo', 'bar', room='baz', callback='cb')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( { 'method': 'emit', 'event': 'foo', @@ -164,25 +164,25 @@ def test_emit_with_ignore_queue(self): 'foo', 'bar', room=sid, namespace='/', ignore_queue=True ) ) - self.pm._publish.mock.assert_not_called() - assert self.pm.server._send_eio_packet.mock.call_count == 1 - assert self.pm.server._send_eio_packet.mock.call_args_list[0][0][0] \ + self.pm._publish.assert_not_awaited() + assert self.pm.server._send_eio_packet.await_count == 1 + assert self.pm.server._send_eio_packet.await_args_list[0][0][0] \ == '123' - pkt = self.pm.server._send_eio_packet.mock.call_args_list[0][0][1] + pkt = self.pm.server._send_eio_packet.await_args_list[0][0][1] assert pkt.encode() == '42["foo","bar"]' def test_can_disconnect(self): sid = _run(self.pm.connect('123', '/')) assert _run(self.pm.can_disconnect(sid, '/')) is True _run(self.pm.can_disconnect(sid, '/foo')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( {'method': 'disconnect', 'sid': sid, 'namespace': '/foo', 'host_id': '123456'} ) def test_disconnect(self): _run(self.pm.disconnect('foo', '/')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( {'method': 'disconnect', 'sid': 'foo', 'namespace': '/', 'host_id': '123456'} ) @@ -191,7 +191,7 @@ def test_disconnect_ignore_queue(self): sid = _run(self.pm.connect('123', '/')) self.pm.pre_disconnect(sid, '/') _run(self.pm.disconnect(sid, '/', ignore_queue=True)) - self.pm._publish.mock.assert_not_called() + self.pm._publish.assert_not_awaited() assert self.pm.is_connected(sid, '/') is False def test_enter_room(self): @@ -200,7 +200,7 @@ def test_enter_room(self): _run(self.pm.enter_room('456', '/', 'foo')) assert sid in self.pm.rooms['/']['foo'] assert self.pm.rooms['/']['foo'][sid] == '123' - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( {'method': 'enter_room', 'sid': '456', 'room': 'foo', 'namespace': '/', 'host_id': '123456'} ) @@ -210,32 +210,31 @@ def test_leave_room(self): _run(self.pm.leave_room(sid, '/', 'foo')) _run(self.pm.leave_room('456', '/', 'foo')) assert 'foo' not in self.pm.rooms['/'] - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( {'method': 'leave_room', 'sid': '456', 'room': 'foo', 'namespace': '/', 'host_id': '123456'} ) def test_close_room(self): _run(self.pm.close_room('foo')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( {'method': 'close_room', 'room': 'foo', 'namespace': '/', 'host_id': '123456'} ) def test_close_room_with_namespace(self): _run(self.pm.close_room('foo', '/bar')) - self.pm._publish.mock.assert_called_once_with( + self.pm._publish.assert_awaited_once_with( {'method': 'close_room', 'room': 'foo', 'namespace': '/bar', 'host_id': '123456'} ) def test_handle_emit(self): with mock.patch.object( - async_manager.AsyncManager, 'emit', new=AsyncMock() + async_manager.AsyncManager, 'emit' ) as super_emit: _run(self.pm._handle_emit({'event': 'foo', 'data': 'bar'})) - super_emit.mock.assert_called_once_with( - self.pm, + super_emit.assert_awaited_once_with( 'foo', 'bar', namespace=None, @@ -246,15 +245,14 @@ def test_handle_emit(self): def test_handle_emit_with_namespace(self): with mock.patch.object( - async_manager.AsyncManager, 'emit', new=AsyncMock() + async_manager.AsyncManager, 'emit' ) as super_emit: _run( self.pm._handle_emit( {'event': 'foo', 'data': 'bar', 'namespace': '/baz'} ) ) - super_emit.mock.assert_called_once_with( - self.pm, + super_emit.assert_awaited_once_with( 'foo', 'bar', namespace='/baz', @@ -265,15 +263,14 @@ def test_handle_emit_with_namespace(self): def test_handle_emit_with_room(self): with mock.patch.object( - async_manager.AsyncManager, 'emit', new=AsyncMock() + async_manager.AsyncManager, 'emit' ) as super_emit: _run( self.pm._handle_emit( {'event': 'foo', 'data': 'bar', 'room': 'baz'} ) ) - super_emit.mock.assert_called_once_with( - self.pm, + super_emit.assert_awaited_once_with( 'foo', 'bar', namespace=None, @@ -284,15 +281,14 @@ def test_handle_emit_with_room(self): def test_handle_emit_with_skip_sid(self): with mock.patch.object( - async_manager.AsyncManager, 'emit', new=AsyncMock() + async_manager.AsyncManager, 'emit' ) as super_emit: _run( self.pm._handle_emit( {'event': 'foo', 'data': 'bar', 'skip_sid': '123'} ) ) - super_emit.mock.assert_called_once_with( - self.pm, + super_emit.assert_awaited_once_with( 'foo', 'bar', namespace=None, @@ -303,7 +299,7 @@ def test_handle_emit_with_skip_sid(self): def test_handle_emit_with_remote_callback(self): with mock.patch.object( - async_manager.AsyncManager, 'emit', new=AsyncMock() + async_manager.AsyncManager, 'emit' ) as super_emit: _run( self.pm._handle_emit( @@ -316,16 +312,16 @@ def test_handle_emit_with_remote_callback(self): } ) ) - assert super_emit.mock.call_count == 1 - assert super_emit.mock.call_args[0] == (self.pm, 'foo', 'bar') - assert super_emit.mock.call_args[1]['namespace'] == '/baz' - assert super_emit.mock.call_args[1]['room'] is None - assert super_emit.mock.call_args[1]['skip_sid'] is None + assert super_emit.await_count == 1 + assert super_emit.await_args[0] == ('foo', 'bar') + assert super_emit.await_args[1]['namespace'] == '/baz' + assert super_emit.await_args[1]['room'] is None + assert super_emit.await_args[1]['skip_sid'] is None assert isinstance( - super_emit.mock.call_args[1]['callback'], functools.partial + super_emit.await_args[1]['callback'], functools.partial ) - _run(super_emit.mock.call_args[1]['callback']('one', 2, 'three')) - self.pm._publish.mock.assert_called_once_with( + _run(super_emit.await_args[1]['callback']('one', 2, 'three')) + self.pm._publish.assert_awaited_once_with( { 'method': 'callback', 'host_id': 'x', @@ -338,7 +334,7 @@ def test_handle_emit_with_remote_callback(self): def test_handle_emit_with_local_callback(self): with mock.patch.object( - async_manager.AsyncManager, 'emit', new=AsyncMock() + async_manager.AsyncManager, 'emit' ) as super_emit: _run( self.pm._handle_emit( @@ -351,21 +347,21 @@ def test_handle_emit_with_local_callback(self): } ) ) - assert super_emit.mock.call_count == 1 - assert super_emit.mock.call_args[0] == (self.pm, 'foo', 'bar') - assert super_emit.mock.call_args[1]['namespace'] == '/baz' - assert super_emit.mock.call_args[1]['room'] is None - assert super_emit.mock.call_args[1]['skip_sid'] is None + assert super_emit.await_count == 1 + assert super_emit.await_args[0] == ('foo', 'bar') + assert super_emit.await_args[1]['namespace'] == '/baz' + assert super_emit.await_args[1]['room'] is None + assert super_emit.await_args[1]['skip_sid'] is None assert isinstance( - super_emit.mock.call_args[1]['callback'], functools.partial + super_emit.await_args[1]['callback'], functools.partial ) - _run(super_emit.mock.call_args[1]['callback']('one', 2, 'three')) - self.pm._publish.mock.assert_not_called() + _run(super_emit.await_args[1]['callback']('one', 2, 'three')) + self.pm._publish.assert_not_awaited() def test_handle_callback(self): host_id = self.pm.host_id with mock.patch.object( - self.pm, 'trigger_callback', new=AsyncMock() + self.pm, 'trigger_callback' ) as trigger: _run( self.pm._handle_callback( @@ -379,11 +375,11 @@ def test_handle_callback(self): } ) ) - trigger.mock.assert_called_once_with('sid', 123, ('one', 2)) + trigger.assert_awaited_once_with('sid', 123, ('one', 2)) def test_handle_callback_bad_host_id(self): with mock.patch.object( - self.pm, 'trigger_callback', new=AsyncMock() + self.pm, 'trigger_callback' ) as trigger: _run( self.pm._handle_callback( @@ -397,12 +393,12 @@ def test_handle_callback_bad_host_id(self): } ) ) - assert trigger.mock.call_count == 0 + assert trigger.await_count == 0 def test_handle_callback_missing_args(self): host_id = self.pm.host_id with mock.patch.object( - self.pm, 'trigger_callback', new=AsyncMock() + self.pm, 'trigger_callback' ) as trigger: _run( self.pm._handle_callback( @@ -435,7 +431,7 @@ def test_handle_callback_missing_args(self): {'method': 'callback', 'host_id': host_id} ) ) - assert trigger.mock.call_count == 0 + assert trigger.await_count == 0 def test_handle_disconnect(self): _run( @@ -443,14 +439,14 @@ def test_handle_disconnect(self): {'method': 'disconnect', 'sid': '123', 'namespace': '/foo'} ) ) - self.pm.server.disconnect.mock.assert_called_once_with( + self.pm.server.disconnect.assert_awaited_once_with( sid='123', namespace='/foo', ignore_queue=True ) def test_handle_enter_room(self): sid = _run(self.pm.connect('123', '/')) with mock.patch.object( - async_manager.AsyncManager, 'enter_room', new=AsyncMock() + async_manager.AsyncManager, 'enter_room' ) as super_enter_room: _run( self.pm._handle_enter_room( @@ -464,14 +460,12 @@ def test_handle_enter_room(self): 'room': 'foo'} ) ) - super_enter_room.mock.assert_called_once_with( - self.pm, sid, '/', 'foo' - ) + super_enter_room.assert_awaited_once_with(sid, '/', 'foo') def test_handle_leave_room(self): sid = _run(self.pm.connect('123', '/')) with mock.patch.object( - async_manager.AsyncManager, 'leave_room', new=AsyncMock() + async_manager.AsyncManager, 'leave_room' ) as super_leave_room: _run( self.pm._handle_leave_room( @@ -485,26 +479,24 @@ def test_handle_leave_room(self): 'room': 'foo'} ) ) - super_leave_room.mock.assert_called_once_with( - self.pm, sid, '/', 'foo' - ) + super_leave_room.assert_awaited_once_with(sid, '/', 'foo') def test_handle_close_room(self): with mock.patch.object( - async_manager.AsyncManager, 'close_room', new=AsyncMock() + async_manager.AsyncManager, 'close_room' ) as super_close_room: _run( self.pm._handle_close_room( {'method': 'close_room', 'room': 'foo'} ) ) - super_close_room.mock.assert_called_once_with( - self.pm, room='foo', namespace=None + super_close_room.assert_awaited_once_with( + room='foo', namespace=None ) def test_handle_close_room_with_namespace(self): with mock.patch.object( - async_manager.AsyncManager, 'close_room', new=AsyncMock() + async_manager.AsyncManager, 'close_room' ) as super_close_room: _run( self.pm._handle_close_room( @@ -515,17 +507,17 @@ def test_handle_close_room_with_namespace(self): } ) ) - super_close_room.mock.assert_called_once_with( - self.pm, room='foo', namespace='/bar' + super_close_room.assert_awaited_once_with( + room='foo', namespace='/bar' ) def test_background_thread(self): - self.pm._handle_emit = AsyncMock() - self.pm._handle_callback = AsyncMock() - self.pm._handle_disconnect = AsyncMock() - self.pm._handle_enter_room = AsyncMock() - self.pm._handle_leave_room = AsyncMock() - self.pm._handle_close_room = AsyncMock() + self.pm._handle_emit = mock.AsyncMock() + self.pm._handle_callback = mock.AsyncMock() + self.pm._handle_disconnect = mock.AsyncMock() + self.pm._handle_enter_room = mock.AsyncMock() + self.pm._handle_leave_room = mock.AsyncMock() + self.pm._handle_close_room = mock.AsyncMock() host_id = self.pm.host_id async def messages(): @@ -558,34 +550,34 @@ async def messages(): self.pm._listen = messages _run(self.pm._thread()) - self.pm._handle_emit.mock.assert_called_once_with( + self.pm._handle_emit.assert_awaited_once_with( {'method': 'emit', 'value': 'foo', 'host_id': 'x'} ) - self.pm._handle_callback.mock.assert_any_call( + self.pm._handle_callback.assert_any_await( {'method': 'callback', 'value': 'bar', 'host_id': 'x'} ) - self.pm._handle_callback.mock.assert_any_call( + self.pm._handle_callback.assert_any_await( {'method': 'callback', 'value': 'bar', 'host_id': host_id} ) - self.pm._handle_disconnect.mock.assert_called_once_with( + self.pm._handle_disconnect.assert_awaited_once_with( {'method': 'disconnect', 'sid': '123', 'namespace': '/foo', 'host_id': 'x'} ) - self.pm._handle_enter_room.mock.assert_called_once_with( + self.pm._handle_enter_room.assert_awaited_once_with( {'method': 'enter_room', 'sid': '123', 'namespace': '/foo', 'room': 'room', 'host_id': 'x'} ) - self.pm._handle_leave_room.mock.assert_called_once_with( + self.pm._handle_leave_room.assert_awaited_once_with( {'method': 'leave_room', 'sid': '123', 'namespace': '/foo', 'room': 'room', 'host_id': 'x'} ) - self.pm._handle_close_room.mock.assert_called_once_with( + self.pm._handle_close_room.assert_awaited_once_with( {'method': 'close_room', 'value': 'baz', 'host_id': 'x'} ) def test_background_thread_exception(self): - self.pm._handle_emit = AsyncMock(side_effect=[ValueError(), - asyncio.CancelledError]) + self.pm._handle_emit = mock.AsyncMock(side_effect=[ + ValueError(), asyncio.CancelledError]) async def messages(): yield {'method': 'emit', 'value': 'foo', 'host_id': 'x'} @@ -594,9 +586,9 @@ async def messages(): self.pm._listen = messages _run(self.pm._thread()) - self.pm._handle_emit.mock.assert_any_call( + self.pm._handle_emit.assert_any_await( {'method': 'emit', 'value': 'foo', 'host_id': 'x'} ) - self.pm._handle_emit.mock.assert_called_with( + self.pm._handle_emit.assert_awaited_with( {'method': 'emit', 'value': 'bar', 'host_id': 'x'} ) diff --git a/tests/async/test_server.py b/tests/async/test_server.py index 8b2dfe28..704982bd 100644 --- a/tests/async/test_server.py +++ b/tests/async/test_server.py @@ -11,12 +11,12 @@ from socketio import exceptions from socketio import namespace from socketio import packet -from .helpers import AsyncMock, _run +from .helpers import _run @mock.patch('socketio.server.engineio.AsyncServer', **{ 'return_value.generate_id.side_effect': [str(i) for i in range(1, 10)], - 'return_value.send_packet': AsyncMock()}) + 'return_value.send_packet': mock.AsyncMock()}) class TestAsyncServer: def teardown_method(self): # restore JSON encoder, in case a test changed it @@ -24,16 +24,16 @@ def teardown_method(self): def _get_mock_manager(self): mgr = mock.MagicMock() - mgr.can_disconnect = AsyncMock() - mgr.emit = AsyncMock() - mgr.enter_room = AsyncMock() - mgr.leave_room = AsyncMock() - mgr.close_room = AsyncMock() - mgr.trigger_callback = AsyncMock() + mgr.can_disconnect = mock.AsyncMock() + mgr.emit = mock.AsyncMock() + mgr.enter_room = mock.AsyncMock() + mgr.leave_room = mock.AsyncMock() + mgr.close_room = mock.AsyncMock() + mgr.trigger_callback = mock.AsyncMock() return mgr def test_create(self, eio): - eio.return_value.handle_request = AsyncMock() + eio.return_value.handle_request = mock.AsyncMock() mgr = self._get_mock_manager() s = async_server.AsyncServer( client_manager=mgr, async_handlers=True, foo='bar' @@ -80,7 +80,7 @@ def test_emit(self, eio): callback='cb', ) ) - s.manager.emit.mock.assert_called_once_with( + s.manager.emit.assert_awaited_once_with( 'my event', {'foo': 'bar'}, '/foo', @@ -100,7 +100,7 @@ def test_emit(self, eio): ignore_queue=True, ) ) - s.manager.emit.mock.assert_called_with( + s.manager.emit.assert_awaited_with( 'my event', {'foo': 'bar'}, '/foo', @@ -122,7 +122,7 @@ def test_emit_default_namespace(self, eio): callback='cb', ) ) - s.manager.emit.mock.assert_called_once_with( + s.manager.emit.assert_awaited_once_with( 'my event', {'foo': 'bar'}, '/', @@ -141,7 +141,7 @@ def test_emit_default_namespace(self, eio): ignore_queue=True, ) ) - s.manager.emit.mock.assert_called_with( + s.manager.emit.assert_awaited_with( 'my event', {'foo': 'bar'}, '/', @@ -163,7 +163,7 @@ def test_send(self, eio): callback='cb', ) ) - s.manager.emit.mock.assert_called_once_with( + s.manager.emit.assert_awaited_once_with( 'message', 'foo', '/foo', @@ -182,7 +182,7 @@ def test_send(self, eio): ignore_queue=True, ) ) - s.manager.emit.mock.assert_called_with( + s.manager.emit.assert_awaited_with( 'message', 'foo', '/foo', @@ -197,7 +197,7 @@ def test_call(self, eio): s = async_server.AsyncServer(client_manager=mgr) async def fake_event_wait(): - s.manager.emit.mock.call_args_list[0][1]['callback']('foo', 321) + s.manager.emit.await_args_list[0][1]['callback']('foo', 321) return True s.eio.create_event.return_value.wait = fake_event_wait @@ -231,39 +231,37 @@ def test_enter_room(self, eio): mgr = self._get_mock_manager() s = async_server.AsyncServer(client_manager=mgr) _run(s.enter_room('123', 'room', namespace='/foo')) - s.manager.enter_room.mock.assert_called_once_with('123', '/foo', - 'room') + s.manager.enter_room.assert_awaited_once_with('123', '/foo', 'room') def test_enter_room_default_namespace(self, eio): mgr = self._get_mock_manager() s = async_server.AsyncServer(client_manager=mgr) _run(s.enter_room('123', 'room')) - s.manager.enter_room.mock.assert_called_once_with('123', '/', 'room') + s.manager.enter_room.assert_awaited_once_with('123', '/', 'room') def test_leave_room(self, eio): mgr = self._get_mock_manager() s = async_server.AsyncServer(client_manager=mgr) _run(s.leave_room('123', 'room', namespace='/foo')) - s.manager.leave_room.mock.assert_called_once_with('123', '/foo', - 'room') + s.manager.leave_room.assert_awaited_once_with('123', '/foo', 'room') def test_leave_room_default_namespace(self, eio): mgr = self._get_mock_manager() s = async_server.AsyncServer(client_manager=mgr) _run(s.leave_room('123', 'room')) - s.manager.leave_room.mock.assert_called_once_with('123', '/', 'room') + s.manager.leave_room.assert_awaited_once_with('123', '/', 'room') def test_close_room(self, eio): mgr = self._get_mock_manager() s = async_server.AsyncServer(client_manager=mgr) _run(s.close_room('room', namespace='/foo')) - s.manager.close_room.mock.assert_called_once_with('room', '/foo') + s.manager.close_room.assert_awaited_once_with('room', '/foo') def test_close_room_default_namespace(self, eio): mgr = self._get_mock_manager() s = async_server.AsyncServer(client_manager=mgr) _run(s.close_room('room')) - s.manager.close_room.mock.assert_called_once_with('room', '/') + s.manager.close_room.assert_awaited_once_with('room', '/') def test_rooms(self, eio): mgr = self._get_mock_manager() @@ -278,32 +276,32 @@ def test_rooms_default_namespace(self, eio): s.manager.get_rooms.assert_called_once_with('123', '/') def test_handle_request(self, eio): - eio.return_value.handle_request = AsyncMock() + eio.return_value.handle_request = mock.AsyncMock() s = async_server.AsyncServer() _run(s.handle_request('environ')) - s.eio.handle_request.mock.assert_called_once_with('environ') + s.eio.handle_request.assert_awaited_once_with('environ') def test_send_packet(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() _run(s._send_packet('123', packet.Packet( packet.EVENT, ['my event', 'my data'], namespace='/foo'))) - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '2/foo,["my event","my data"]' ) def test_send_eio_packet(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() _run(s._send_eio_packet('123', eio_packet.Packet( eio_packet.MESSAGE, 'hello'))) - assert s.eio.send_packet.mock.call_count == 1 - assert s.eio.send_packet.mock.call_args_list[0][0][0] == '123' - pkt = s.eio.send_packet.mock.call_args_list[0][0][1] + assert s.eio.send_packet.await_count == 1 + assert s.eio.send_packet.await_args_list[0][0][0] == '123' + pkt = s.eio.send_packet.await_args_list[0][0][1] assert pkt.encode() == '4hello' def test_transport(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.eio.transport = mock.MagicMock(return_value='polling') sid_foo = _run(s.manager.connect('123', '/foo')) @@ -311,7 +309,7 @@ def test_transport(self, eio): s.eio.transport.assert_called_once_with('123') def test_handle_connect(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.manager.initialize = mock.MagicMock() handler = mock.MagicMock() @@ -320,14 +318,14 @@ def test_handle_connect(self, eio): _run(s._handle_eio_message('123', '0')) assert s.manager.is_connected('1', '/') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') + s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}') assert s.manager.initialize.call_count == 1 _run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_message('456', '0')) assert s.manager.initialize.call_count == 1 def test_handle_connect_with_auth(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.manager.initialize = mock.MagicMock() handler = mock.MagicMock() @@ -336,14 +334,14 @@ def test_handle_connect_with_auth(self, eio): _run(s._handle_eio_message('123', '0{"token":"abc"}')) assert s.manager.is_connected('1', '/') handler.assert_called_once_with('1', 'environ', {'token': 'abc'}) - s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') + s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}') assert s.manager.initialize.call_count == 1 _run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_message('456', '0')) assert s.manager.initialize.call_count == 1 def test_handle_connect_with_auth_none(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.manager.initialize = mock.MagicMock() handler = mock.MagicMock(side_effect=[TypeError, None, None]) @@ -352,30 +350,30 @@ def test_handle_connect_with_auth_none(self, eio): _run(s._handle_eio_message('123', '0')) assert s.manager.is_connected('1', '/') handler.assert_called_with('1', 'environ', None) - s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') + s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}') assert s.manager.initialize.call_count == 1 _run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_message('456', '0')) assert s.manager.initialize.call_count == 1 def test_handle_connect_async(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.manager.initialize = mock.MagicMock() - handler = AsyncMock() + handler = mock.AsyncMock() s.on('connect', handler) _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) assert s.manager.is_connected('1', '/') - handler.mock.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') + handler.assert_awaited_once_with('1', 'environ') + s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}') assert s.manager.initialize.call_count == 1 _run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_message('456', '0')) assert s.manager.initialize.call_count == 1 def test_handle_connect_with_default_implied_namespaces(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) @@ -384,7 +382,7 @@ def test_handle_connect_with_default_implied_namespaces(self, eio): assert not s.manager.is_connected('2', '/foo') def test_handle_connect_with_implied_namespaces(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(namespaces=['/foo']) _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) @@ -393,7 +391,7 @@ def test_handle_connect_with_implied_namespaces(self, eio): assert s.manager.is_connected('1', '/foo') def test_handle_connect_with_all_implied_namespaces(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(namespaces='*') _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) @@ -402,7 +400,7 @@ def test_handle_connect_with_all_implied_namespaces(self, eio): assert s.manager.is_connected('2', '/foo') def test_handle_connect_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock() s.on('connect', handler, namespace='/foo') @@ -410,10 +408,10 @@ def test_handle_connect_namespace(self, eio): _run(s._handle_eio_message('123', '0/foo,')) assert s.manager.is_connected('1', '/foo') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with('123', '0/foo,{"sid":"1"}') + s.eio.send.assert_awaited_once_with('123', '0/foo,{"sid":"1"}') def test_handle_connect_always_connect(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(always_connect=True) s.manager.initialize = mock.MagicMock() handler = mock.MagicMock() @@ -422,14 +420,14 @@ def test_handle_connect_always_connect(self, eio): _run(s._handle_eio_message('123', '0')) assert s.manager.is_connected('1', '/') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with('123', '0{"sid":"1"}') + s.eio.send.assert_awaited_once_with('123', '0{"sid":"1"}') assert s.manager.initialize.call_count == 1 _run(s._handle_eio_connect('456', 'environ')) _run(s._handle_eio_message('456', '0')) assert s.manager.initialize.call_count == 1 def test_handle_connect_rejected(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock(return_value=False) s.on('connect', handler) @@ -437,12 +435,12 @@ def test_handle_connect_rejected(self, eio): _run(s._handle_eio_message('123', '0')) assert not s.manager.is_connected('1', '/foo') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '4{"message":"Connection rejected by server"}') assert s.environ == {'123': 'environ'} def test_handle_connect_namespace_rejected(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock(return_value=False) s.on('connect', handler, namespace='/foo') @@ -450,12 +448,12 @@ def test_handle_connect_namespace_rejected(self, eio): _run(s._handle_eio_message('123', '0/foo,')) assert not s.manager.is_connected('1', '/foo') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_any_call( + s.eio.send.assert_any_await( '123', '4/foo,{"message":"Connection rejected by server"}') assert s.environ == {'123': 'environ'} def test_handle_connect_rejected_always_connect(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(always_connect=True) handler = mock.MagicMock(return_value=False) s.on('connect', handler) @@ -463,13 +461,13 @@ def test_handle_connect_rejected_always_connect(self, eio): _run(s._handle_eio_message('123', '0')) assert not s.manager.is_connected('1', '/') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_any_call('123', '0{"sid":"1"}') - s.eio.send.mock.assert_any_call( + s.eio.send.assert_any_await('123', '0{"sid":"1"}') + s.eio.send.assert_any_await( '123', '1{"message":"Connection rejected by server"}') assert s.environ == {'123': 'environ'} def test_handle_connect_namespace_rejected_always_connect(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(always_connect=True) handler = mock.MagicMock(return_value=False) s.on('connect', handler, namespace='/foo') @@ -477,13 +475,13 @@ def test_handle_connect_namespace_rejected_always_connect(self, eio): _run(s._handle_eio_message('123', '0/foo,')) assert not s.manager.is_connected('1', '/foo') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_any_call('123', '0/foo,{"sid":"1"}') - s.eio.send.mock.assert_any_call( + s.eio.send.assert_any_await('123', '0/foo,{"sid":"1"}') + s.eio.send.assert_any_await( '123', '1/foo,{"message":"Connection rejected by server"}') assert s.environ == {'123': 'environ'} def test_handle_connect_rejected_with_exception(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock( side_effect=exceptions.ConnectionRefusedError('fail_reason') @@ -493,12 +491,12 @@ def test_handle_connect_rejected_with_exception(self, eio): _run(s._handle_eio_message('123', '0')) assert not s.manager.is_connected('1', '/') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '4{"message":"fail_reason"}') assert s.environ == {'123': 'environ'} def test_handle_connect_rejected_with_empty_exception(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock( side_effect=exceptions.ConnectionRefusedError() @@ -508,12 +506,12 @@ def test_handle_connect_rejected_with_empty_exception(self, eio): _run(s._handle_eio_message('123', '0')) assert not s.manager.is_connected('1', '/') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '4{"message":"Connection rejected by server"}') assert s.environ == {'123': 'environ'} def test_handle_connect_namespace_rejected_with_exception(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock( side_effect=exceptions.ConnectionRefusedError( @@ -524,12 +522,12 @@ def test_handle_connect_namespace_rejected_with_exception(self, eio): _run(s._handle_eio_message('123', '0/foo,')) assert not s.manager.is_connected('1', '/foo') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '4/foo,{"message":"fail_reason","data":[1,"2"]}') assert s.environ == {'123': 'environ'} def test_handle_connect_namespace_rejected_with_empty_exception(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock( side_effect=exceptions.ConnectionRefusedError() @@ -539,26 +537,26 @@ def test_handle_connect_namespace_rejected_with_empty_exception(self, eio): _run(s._handle_eio_message('123', '0/foo,')) assert not s.manager.is_connected('1', '/foo') handler.assert_called_once_with('1', 'environ') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '4/foo,{"message":"Connection rejected by server"}') assert s.environ == {'123': 'environ'} def test_handle_disconnect(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() - s.manager.disconnect = AsyncMock() + s.manager.disconnect = mock.AsyncMock() handler = mock.MagicMock() s.on('disconnect', handler) _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) _run(s._handle_eio_disconnect('123')) handler.assert_called_once_with('1') - s.manager.disconnect.mock.assert_called_once_with( + s.manager.disconnect.assert_awaited_once_with( '1', '/', ignore_queue=True) assert s.environ == {} def test_handle_disconnect_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock() s.on('disconnect', handler) @@ -572,7 +570,7 @@ def test_handle_disconnect_namespace(self, eio): assert s.environ == {} def test_handle_disconnect_only_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() handler = mock.MagicMock() s.on('disconnect', handler) @@ -591,21 +589,21 @@ def test_handle_disconnect_unknown_client(self, eio): _run(s._handle_eio_disconnect('123')) def test_handle_event(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/')) - handler = AsyncMock() - catchall_handler = AsyncMock() + handler = mock.AsyncMock() + catchall_handler = mock.AsyncMock() s.on('msg', handler) s.on('*', catchall_handler) _run(s._handle_eio_message('123', '2["msg","a","b"]')) _run(s._handle_eio_message('123', '2["my message","a","b","c"]')) - handler.mock.assert_called_once_with(sid, 'a', 'b') - catchall_handler.mock.assert_called_once_with( + handler.assert_awaited_once_with(sid, 'a', 'b') + catchall_handler.assert_awaited_once_with( 'my message', sid, 'a', 'b', 'c') def test_handle_event_with_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/foo')) handler = mock.MagicMock() @@ -619,7 +617,7 @@ def test_handle_event_with_namespace(self, eio): 'my message', sid, 'a', 'b', 'c') def test_handle_event_with_catchall_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid_foo = _run(s.manager.connect('123', '/foo')) sid_bar = _run(s.manager.connect('123', '/bar')) @@ -648,7 +646,7 @@ def test_handle_event_with_catchall_namespace(self, eio): 'my message', '/bar', sid_bar, 'a', 'b', 'c') def test_handle_event_with_disconnected_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) _run(s.manager.connect('123', '/foo')) handler = mock.MagicMock() @@ -657,7 +655,7 @@ def test_handle_event_with_disconnected_namespace(self, eio): handler.assert_not_called() def test_handle_event_binary(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/')) handler = mock.MagicMock() @@ -675,9 +673,9 @@ def test_handle_event_binary(self, eio): handler.assert_called_once_with(sid, 'a', b'bar', b'foo') def test_handle_event_binary_ack(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) - s.manager.trigger_callback = AsyncMock() + s.manager.trigger_callback = mock.AsyncMock() sid = _run(s.manager.connect('123', '/')) _run( s._handle_eio_message( @@ -686,67 +684,67 @@ def test_handle_event_binary_ack(self, eio): ) ) _run(s._handle_eio_message('123', b'foo')) - s.manager.trigger_callback.mock.assert_called_once_with( + s.manager.trigger_callback.assert_awaited_once_with( sid, 321, ['my message', 'a', b'foo'] ) def test_handle_event_with_ack(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/')) handler = mock.MagicMock(return_value='foo') s.on('my message', handler) _run(s._handle_eio_message('123', '21000["my message","foo"]')) handler.assert_called_once_with(sid, 'foo') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '31000["foo"]' ) def test_handle_unknown_event_with_ack(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) _run(s.manager.connect('123', '/')) handler = mock.MagicMock(return_value='foo') s.on('my message', handler) _run(s._handle_eio_message('123', '21000["another message","foo"]')) - s.eio.send.mock.assert_not_called() + s.eio.send.assert_not_awaited() def test_handle_event_with_ack_none(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/')) handler = mock.MagicMock(return_value=None) s.on('my message', handler) _run(s._handle_eio_message('123', '21000["my message","foo"]')) handler.assert_called_once_with(sid, 'foo') - s.eio.send.mock.assert_called_once_with('123', '31000[]') + s.eio.send.assert_awaited_once_with('123', '31000[]') def test_handle_event_with_ack_tuple(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/')) handler = mock.MagicMock(return_value=(1, '2', True)) s.on('my message', handler) _run(s._handle_eio_message('123', '21000["my message","a","b","c"]')) handler.assert_called_once_with(sid, 'a', 'b', 'c') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '31000[1,"2",true]' ) def test_handle_event_with_ack_list(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/')) handler = mock.MagicMock(return_value=[1, '2', True]) s.on('my message', handler) _run(s._handle_eio_message('123', '21000["my message","a","b","c"]')) handler.assert_called_once_with(sid, 'a', 'b', 'c') - s.eio.send.mock.assert_called_once_with( + s.eio.send.assert_awaited_once_with( '123', '31000[[1,"2",true]]' ) def test_handle_event_with_ack_binary(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer(async_handlers=False) sid = _run(s.manager.connect('123', '/')) handler = mock.MagicMock(return_value=b'foo') @@ -765,7 +763,7 @@ def test_handle_invalid_packet(self, eio): _run(s._handle_eio_message('123', '9')) def test_send_with_ack(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.handlers['/'] = {} _run(s._handle_eio_connect('123', 'environ')) @@ -781,7 +779,7 @@ def test_send_with_ack(self, eio): cb.assert_called_once_with('foo', 2) def test_send_with_ack_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.handlers['/foo'] = {} _run(s._handle_eio_connect('123', 'environ')) @@ -809,7 +807,7 @@ async def fake_save_session(eio_sid, session): assert eio_sid == '123' fake_session = session - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() s.handlers['/'] = {} s.handlers['/ns'] = {} @@ -841,63 +839,63 @@ async def _test(): _run(_test()) def test_disconnect(self, eio): - eio.return_value.send = AsyncMock() - eio.return_value.disconnect = AsyncMock() + eio.return_value.send = mock.AsyncMock() + eio.return_value.disconnect = mock.AsyncMock() s = async_server.AsyncServer() s.handlers['/'] = {} _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) _run(s.disconnect('1')) - s.eio.send.mock.assert_any_call('123', '1') + s.eio.send.assert_any_await('123', '1') assert not s.manager.is_connected('1', '/') def test_disconnect_ignore_queue(self, eio): - eio.return_value.send = AsyncMock() - eio.return_value.disconnect = AsyncMock() + eio.return_value.send = mock.AsyncMock() + eio.return_value.disconnect = mock.AsyncMock() s = async_server.AsyncServer() s.handlers['/'] = {} _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) _run(s.disconnect('1', ignore_queue=True)) - s.eio.send.mock.assert_any_call('123', '1') + s.eio.send.assert_any_await('123', '1') assert not s.manager.is_connected('1', '/') def test_disconnect_namespace(self, eio): - eio.return_value.send = AsyncMock() - eio.return_value.disconnect = AsyncMock() + eio.return_value.send = mock.AsyncMock() + eio.return_value.disconnect = mock.AsyncMock() s = async_server.AsyncServer() s.handlers['/foo'] = {} _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0/foo,')) _run(s.disconnect('1', namespace='/foo')) - s.eio.send.mock.assert_any_call('123', '1/foo,') + s.eio.send.assert_any_await('123', '1/foo,') assert not s.manager.is_connected('1', '/foo') def test_disconnect_twice(self, eio): - eio.return_value.send = AsyncMock() - eio.return_value.disconnect = AsyncMock() + eio.return_value.send = mock.AsyncMock() + eio.return_value.disconnect = mock.AsyncMock() s = async_server.AsyncServer() _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0')) _run(s.disconnect('1')) - calls = s.eio.send.mock.call_count + calls = s.eio.send.await_count assert not s.manager.is_connected('1', '/') _run(s.disconnect('1')) - assert calls == s.eio.send.mock.call_count + assert calls == s.eio.send.await_count def test_disconnect_twice_namespace(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0/foo,')) _run(s.disconnect('1', namespace='/foo')) - calls = s.eio.send.mock.call_count + calls = s.eio.send.await_count assert not s.manager.is_connected('1', '/foo') _run(s.disconnect('1', namespace='/foo')) - assert calls == s.eio.send.mock.call_count + assert calls == s.eio.send.await_count def test_namespace_handler(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() result = {} class MyNamespace(async_namespace.AsyncNamespace): @@ -931,7 +929,7 @@ async def on_baz(self, sid, data1, data2): assert result['result'] == ('disconnect', '1') def test_catchall_namespace_handler(self, eio): - eio.return_value.send = AsyncMock() + eio.return_value.send = mock.AsyncMock() result = {} class MyNamespace(async_namespace.AsyncNamespace): @@ -1047,9 +1045,9 @@ def test_async_handlers(self, eio): def test_shutdown(self, eio): s = async_server.AsyncServer() - s.eio.shutdown = AsyncMock() + s.eio.shutdown = mock.AsyncMock() _run(s.shutdown()) - s.eio.shutdown.mock.assert_called_once_with() + s.eio.shutdown.assert_awaited_once_with() def test_start_background_task(self, eio): s = async_server.AsyncServer() @@ -1059,7 +1057,7 @@ def test_start_background_task(self, eio): ) def test_sleep(self, eio): - eio.return_value.sleep = AsyncMock() + eio.return_value.sleep = mock.AsyncMock() s = async_server.AsyncServer() _run(s.sleep(1.23)) - s.eio.sleep.mock.assert_called_once_with(1.23) + s.eio.sleep.assert_awaited_once_with(1.23) diff --git a/tests/async/test_simple_client.py b/tests/async/test_simple_client.py index 6a2eb7ad..a8d08f41 100644 --- a/tests/async/test_simple_client.py +++ b/tests/async/test_simple_client.py @@ -4,7 +4,7 @@ from socketio import AsyncSimpleClient from socketio.exceptions import SocketIOError, TimeoutError, DisconnectedError -from .helpers import AsyncMock, _run +from .helpers import _run class TestAsyncAsyncSimpleClient: @@ -20,14 +20,14 @@ def test_connect(self): client = AsyncSimpleClient(123, a='b') with mock.patch('socketio.async_simple_client.AsyncClient') \ as mock_client: - mock_client.return_value.connect = AsyncMock() + mock_client.return_value.connect = mock.AsyncMock() _run(client.connect('url', headers='h', auth='a', transports='t', namespace='n', socketio_path='s', wait_timeout='w')) mock_client.assert_called_once_with(123, a='b') assert client.client == mock_client() - mock_client().connect.mock.assert_called_once_with( + mock_client().connect.assert_awaited_once_with( 'url', headers='h', auth='a', transports='t', namespaces=['n'], socketio_path='s', wait_timeout='w') mock_client().event.call_count == 3 @@ -40,14 +40,14 @@ async def _t(): async with AsyncSimpleClient(123, a='b') as client: with mock.patch('socketio.async_simple_client.AsyncClient') \ as mock_client: - mock_client.return_value.connect = AsyncMock() + mock_client.return_value.connect = mock.AsyncMock() await client.connect('url', headers='h', auth='a', transports='t', namespace='n', socketio_path='s', wait_timeout='w') mock_client.assert_called_once_with(123, a='b') assert client.client == mock_client() - mock_client().connect.mock.assert_called_once_with( + mock_client().connect.assert_awaited_once_with( 'url', headers='h', auth='a', transports='t', namespaces=['n'], socketio_path='s', wait_timeout='w') mock_client().event.call_count == 3 @@ -79,14 +79,14 @@ def test_properties(self): def test_emit(self): client = AsyncSimpleClient() client.client = mock.MagicMock() - client.client.emit = AsyncMock() + client.client.emit = mock.AsyncMock() client.namespace = '/ns' client.connected_event.set() client.connected = True _run(client.emit('foo', 'bar')) - client.client.emit.mock.assert_called_once_with('foo', 'bar', - namespace='/ns') + client.client.emit.assert_awaited_once_with('foo', 'bar', + namespace='/ns') def test_emit_disconnected(self): client = AsyncSimpleClient() @@ -100,23 +100,23 @@ def test_emit_retries(self): client.connected_event.set() client.connected = True client.client = mock.MagicMock() - client.client.emit = AsyncMock() - client.client.emit.mock.side_effect = [SocketIOError(), None] + client.client.emit = mock.AsyncMock() + client.client.emit.side_effect = [SocketIOError(), None] _run(client.emit('foo', 'bar')) - client.client.emit.mock.assert_called_with('foo', 'bar', namespace='/') + client.client.emit.assert_awaited_with('foo', 'bar', namespace='/') def test_call(self): client = AsyncSimpleClient() client.client = mock.MagicMock() - client.client.call = AsyncMock() - client.client.call.mock.return_value = 'result' + client.client.call = mock.AsyncMock() + client.client.call.return_value = 'result' client.namespace = '/ns' client.connected_event.set() client.connected = True assert _run(client.call('foo', 'bar')) == 'result' - client.client.call.mock.assert_called_once_with( + client.client.call.assert_awaited_once_with( 'foo', 'bar', namespace='/ns', timeout=60) def test_call_disconnected(self): @@ -131,12 +131,12 @@ def test_call_retries(self): client.connected_event.set() client.connected = True client.client = mock.MagicMock() - client.client.call = AsyncMock() - client.client.call.mock.side_effect = [SocketIOError(), 'result'] + client.client.call = mock.AsyncMock() + client.client.call.side_effect = [SocketIOError(), 'result'] assert _run(client.call('foo', 'bar')) == 'result' - client.client.call.mock.assert_called_with('foo', 'bar', namespace='/', - timeout=60) + client.client.call.assert_awaited_with('foo', 'bar', namespace='/', + timeout=60) def test_receive_with_input_buffer(self): client = AsyncSimpleClient() @@ -180,10 +180,10 @@ def test_receive_disconnected(self): def test_disconnect(self): client = AsyncSimpleClient() mc = mock.MagicMock() - mc.disconnect = AsyncMock() + mc.disconnect = mock.AsyncMock() client.client = mc client.connected = True _run(client.disconnect()) _run(client.disconnect()) - mc.disconnect.mock.assert_called_once_with() + mc.disconnect.assert_awaited_once_with() assert client.client is None