small edits to autoupdate test

This commit is contained in:
Alexander Cogneau 2015-08-31 09:30:16 +02:00
parent ee77951e66
commit 8ab85d6246

View File

@ -19,29 +19,31 @@
"""Tests for qutebrowser.misc.autoupdate""" """Tests for qutebrowser.misc.autoupdate"""
import pytest
from PyQt5.QtTest import QSignalSpy from PyQt5.QtTest import QSignalSpy
from qutebrowser.misc import autoupdate, httpclient from qutebrowser.misc import autoupdate, httpclient
INVALID_JSON = ['{"invalid": { "json"}', '{"wrong": "keys"}']
class HTTPGetStub(httpclient.HTTPClient): class HTTPGetStub(httpclient.HTTPClient):
"""A stub class for HTTPClient. """A stub class for HTTPClient.
Attributes: Attributes:
_success: Wether get() will emit a success signal. _success: Wether get() will emit a success signal.
""" """
def __init__(self, success=True, json=None): def __init__(self, success=True, json=None):
super().__init__()
self._success = success self._success = success
if json: if json:
self._json = json self._json = json
else: else:
self._json = '{"info": {"version": "test"}}' self._json = '{"info": {"version": "test"}}'
super(HTTPGetStub, self).__init__()
def get(self, url): def get(self, url):
if self._success is True: if self._success:
self.success.emit(self._json) self.success.emit(self._json)
else: else:
self.error.emit("error") self.error.emit("error")
@ -58,13 +60,11 @@ def test_get_version_success(qtbot):
client = autoupdate.PyPIVersionClient(client=http_stub) client = autoupdate.PyPIVersionClient(client=http_stub)
# Use a spy to inspect the signal # Use a spy to inspect the signal
success_spy = QSignalSpy(client.success)
error_spy = QSignalSpy(client.error) error_spy = QSignalSpy(client.error)
with qtbot.waitSignal(client.success, timeout=2000, raising=False): with qtbot.waitSignal(client.success, raising=True):
client.get_version('test') client.get_version('test')
assert len(success_spy) == 1
assert len(error_spy) == 0 assert len(error_spy) == 0
@ -75,30 +75,24 @@ def test_get_version_error(qtbot):
# Use a spy to inspect the signal # Use a spy to inspect the signal
success_spy = QSignalSpy(client.success) success_spy = QSignalSpy(client.success)
error_spy = QSignalSpy(client.error)
with qtbot.waitSignal(client.error, timeout=2000, raising=False): with qtbot.waitSignal(client.error, raising=True):
client.get_version('test') client.get_version('test')
assert len(success_spy) == 0 assert len(success_spy) == 0
assert len(error_spy) == 1
def test_invalid_json(): @pytest.mark.parametrize('json', INVALID_JSON)
def test_invalid_json(qtbot, json):
"""Test on_client_success() with invalid JSON.""" """Test on_client_success() with invalid JSON."""
json = '{"invalid": { "json"}'
http_stub = HTTPGetStub(json=json) http_stub = HTTPGetStub(json=json)
client = autoupdate.PyPIVersionClient(client=http_stub) client = autoupdate.PyPIVersionClient(client=http_stub)
error_spy = QSignalSpy(client.error)
client.get_version('test') client.get_version('test')
assert len(error_spy) == 1
# Use a spy to inspect the signal
success_spy = QSignalSpy(client.success)
def test_invalid_keys(): with qtbot.waitSignal(client.error, raising=True):
"""Test on_client_success() with valid JSON and wrong keys.""" client.get_version('test')
json = '{"wrong": "keys"}'
http_stub = HTTPGetStub(json=json) assert len(success_spy) == 0
client = autoupdate.PyPIVersionClient(client=http_stub)
error_spy = QSignalSpy(client.error)
client.get_version('test')
assert len(error_spy) == 1