tests: Switch to qtbot.assertNotEmitted.

This commit is contained in:
Florian Bruhin 2016-01-08 09:57:33 +01:00
parent f004e45566
commit 9ed79ad57d
3 changed files with 29 additions and 45 deletions

View File

@ -22,7 +22,6 @@
from unittest import mock
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtTest import QSignalSpy
from PyQt5.QtCore import QUrl
import pytest
@ -90,15 +89,15 @@ def test_set_cookies_accept(config_stub, qtbot, monkeypatch):
assert saved_cookie.name(), saved_cookie.value() == expected
def test_set_cookies_never_accept(config_stub):
def test_set_cookies_never_accept(qtbot, config_stub):
"""Test setCookiesFromUrl when cookies are not accepted."""
config_stub.data = CONFIG_NEVER_COOKIES
ram_jar = cookies.RAMCookieJar()
changed_signal_spy = QSignalSpy(ram_jar.changed)
url = QUrl('http://example.com/')
assert not ram_jar.setCookiesFromUrl(url, 'test')
assert not changed_signal_spy
with qtbot.assertNotEmitted(ram_jar.changed):
assert not ram_jar.setCookiesFromUrl(url, 'test')
assert not ram_jar.cookiesForUrl(url)

View File

@ -20,7 +20,6 @@
"""Tests for qutebrowser.misc.autoupdate"""
import pytest
from PyQt5.QtTest import QSignalSpy
from PyQt5.QtCore import QUrl
from qutebrowser.misc import autoupdate, httpclient
@ -63,13 +62,9 @@ def test_get_version_success(qtbot):
http_stub = HTTPGetStub(success=True)
client = autoupdate.PyPIVersionClient(client=http_stub)
# Use a spy to inspect the signal
error_spy = QSignalSpy(client.error)
with qtbot.waitSignal(client.success):
client.get_version('test')
assert len(error_spy) == 0
with qtbot.assertNotEmitted(client.error):
with qtbot.waitSignal(client.success):
client.get_version('test')
assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json')
@ -79,13 +74,9 @@ def test_get_version_error(qtbot):
http_stub = HTTPGetStub(success=False)
client = autoupdate.PyPIVersionClient(client=http_stub)
# Use a spy to inspect the signal
success_spy = QSignalSpy(client.success)
with qtbot.waitSignal(client.error):
client.get_version('test')
assert len(success_spy) == 0
with qtbot.assertNotEmitted(client.success):
with qtbot.waitSignal(client.error):
client.get_version('test')
@pytest.mark.parametrize('json', INVALID_JSON)
@ -95,10 +86,6 @@ def test_invalid_json(qtbot, json):
client = autoupdate.PyPIVersionClient(client=http_stub)
client.get_version('test')
# Use a spy to inspect the signal
success_spy = QSignalSpy(client.success)
with qtbot.waitSignal(client.error):
client.get_version('test')
assert len(success_spy) == 0
with qtbot.assertNotEmitted(client.success):
with qtbot.waitSignal(client.error):
client.get_version('test')

View File

@ -494,22 +494,19 @@ NEW_VERSION = str(ipc.PROTOCOL_VERSION + 1).encode('utf-8')
(b'{"args": [], "target_arg": null}\n', 'invalid version'),
])
def test_invalid_data(qtbot, ipc_server, connected_socket, caplog, data, msg):
got_args_spy = QSignalSpy(ipc_server.got_args)
signals = [ipc_server.got_invalid_data, connected_socket.disconnected]
with caplog.at_level(logging.ERROR):
with qtbot.waitSignals(signals):
connected_socket.write(data)
with qtbot.assertNotEmitted(ipc_server.got_args):
with qtbot.waitSignals(signals):
connected_socket.write(data)
messages = [r.message for r in caplog.records]
assert messages[-1] == 'Ignoring invalid IPC data.'
assert messages[-2].startswith(msg)
assert not got_args_spy
def test_multiline(qtbot, ipc_server, connected_socket):
spy = QSignalSpy(ipc_server.got_args)
error_spy = QSignalSpy(ipc_server.got_invalid_data)
data = ('{{"args": ["one"], "target_arg": "tab",'
' "protocol_version": {version}}}\n'
@ -517,10 +514,10 @@ def test_multiline(qtbot, ipc_server, connected_socket):
' "protocol_version": {version}}}\n'.format(
version=ipc.PROTOCOL_VERSION))
with qtbot.waitSignals([ipc_server.got_args, ipc_server.got_args]):
connected_socket.write(data.encode('utf-8'))
with qtbot.assertNotEmitted(ipc_server.got_invalid_data):
with qtbot.waitSignals([ipc_server.got_args, ipc_server.got_args]):
connected_socket.write(data.encode('utf-8'))
assert not error_spy
assert len(spy) == 2
assert spy[0] == [['one'], 'tab', '']
assert spy[1] == [['two'], '', '']
@ -539,18 +536,19 @@ class TestSendToRunningInstance:
def test_normal(self, qtbot, tmpdir, ipc_server, mocker, has_cwd):
ipc_server.listen()
raw_spy = QSignalSpy(ipc_server.got_raw)
error_spy = QSignalSpy(ipc_server.got_invalid_data)
with qtbot.waitSignal(ipc_server.got_args, timeout=5000) as blocker:
with tmpdir.as_cwd():
if not has_cwd:
m = mocker.patch('qutebrowser.misc.ipc.os')
m.getcwd.side_effect = OSError
sent = ipc.send_to_running_instance('qute-test', ['foo'], None)
with qtbot.assertNotEmitted(ipc_server.got_invalid_data):
with qtbot.waitSignal(ipc_server.got_args,
timeout=5000) as blocker:
with tmpdir.as_cwd():
if not has_cwd:
m = mocker.patch('qutebrowser.misc.ipc.os')
m.getcwd.side_effect = OSError
sent = ipc.send_to_running_instance('qute-test', ['foo'],
None)
assert sent
assert sent
assert not error_spy
expected_cwd = str(tmpdir) if has_cwd else ''
assert blocker.args == [['foo'], '', expected_cwd]