tests: Add connection and message tests for client.

This commit is contained in:
Shiz 2014-03-15 00:26:55 +01:00
parent c3535e3493
commit c938873ac4
1 changed files with 62 additions and 18 deletions

View File

@ -1,35 +1,79 @@
import time
import pydle
import pytest
from pytest import raises, mark
from .fixtures import with_client
from .mocks import Mock
pydle.client.PING_TIMEOUT = 10
@with_client()
def test_client_same_nick(server, client):
assert client.is_same_nick('WiZ', 'WiZ')
assert not client.is_same_nick('WiZ', 'jilles')
assert not client.is_same_nick('WiZ', 'wiz')
## Initialization.
@with_client(invalid_kwarg=False)
def test_client_superfluous_arguments(server, client):
assert client.logger.warning.called
## Connection.
@with_client()
def test_client_same_channel(server, client):
assert client.is_same_channel('#lobby', '#lobby')
assert not client.is_same_channel('#lobby', '#support')
assert not client.is_same_channel('#lobby', 'jilles')
def test_client_reconnect(server, client):
client.disconnect(expected=True)
assert not client.connected
client.connect(reconnect=True)
assert client.connected
@mark.slow
@with_client()
def test_client_reconnect_delay(server, client):
client._reconnect_delay = Mock(return_value=1)
client.disconnect(expected=False)
assert not client.connected
time.sleep(1.1)
assert client.connected
@with_client()
def test_client_disconnect_on_connect(server, client):
client.disconnect = Mock()
client.connect('mock://local', 1337)
assert client.connected
assert client.disconnect.called
@with_client(connected=False)
def test_client_connect_invalid_params(server, client):
with raises(ValueError):
client.connect()
with raises(ValueError):
client.connect(port=1337)
@mark.slow
@with_client()
def test_client_timeout(server, client):
client.on_data_error = Mock()
time.sleep(pydle.client.PING_TIMEOUT + 1)
assert client.on_data_error.called
## Messages.
@with_client()
def test_client_message(server, client):
client.on_raw_install = Mock()
server.send('INSTALL', 'gentoo')
assert client.on_raw_install.called
message = client.on_raw_install.call_args[0][0]
assert isinstance(message, pydle.protocol.Message)
assert message.command == 'INSTALL'
assert message.params == ('gentoo',)
@with_client()
def test_client_unknown(server, client):
client.on_unknown = Mock()
server.send('INSTALL', 'gentoo')
assert client.on_unknown.called
@pytest.mark.slow
@with_client()
def test_client_timeout(server, client):
client.on_data_error = Mock()
time.sleep(pydle.client.PING_TIMEOUT + 1)
assert client.on_data_error.called