From e499d8932ffb0571ae7d9b02ffade64255abf12d Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Mon, 31 Aug 2015 01:10:04 +0200 Subject: [PATCH 1/4] 100% misc.autoupdate coverage --- qutebrowser/misc/autoupdate.py | 7 +- tests/unit/misc/test_autoupdate.py | 105 +++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 tests/unit/misc/test_autoupdate.py diff --git a/qutebrowser/misc/autoupdate.py b/qutebrowser/misc/autoupdate.py index 6ea150300..2126a1296 100644 --- a/qutebrowser/misc/autoupdate.py +++ b/qutebrowser/misc/autoupdate.py @@ -49,9 +49,12 @@ class PyPIVersionClient(QObject): success = pyqtSignal(str) error = pyqtSignal(str) - def __init__(self, parent=None): + def __init__(self, parent=None, client=None): super().__init__(parent) - self._client = httpclient.HTTPClient(self) + if client is None: + self._client = httpclient.HTTPClient(self) + else: + self._client = client self._client.error.connect(self.error) self._client.success.connect(self.on_client_success) diff --git a/tests/unit/misc/test_autoupdate.py b/tests/unit/misc/test_autoupdate.py new file mode 100644 index 000000000..d83c79cc6 --- /dev/null +++ b/tests/unit/misc/test_autoupdate.py @@ -0,0 +1,105 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Alexander Cogneau (acogneau) : +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Tests for qutebrowser.misc.autoupdate""" + +import pytest +from PyQt5.QtTest import QSignalSpy + +from qutebrowser.misc import autoupdate, httpclient + + +class HTTPGetStub(httpclient.HTTPClient): + """A stub class for HTTPClient. + + Attributes: + _success: Wether get() will emit a success signal. + """ + + def __init__(self, success=True, json=None): + self._success = success + if json: + self._json = json + else: + self._json = '{"info": {"version": "test"}}' + + super(HTTPGetStub, self).__init__() + + def get(self, url): + if self._success is True: + self.success.emit(self._json) + else: + self.error.emit("error") + + +def test_constructor(): + client = autoupdate.PyPIVersionClient() + assert isinstance(client._client, httpclient.HTTPClient) + + +def test_get_version_success(qtbot): + """Test get_version() when success is emitted.""" + HTTPStub = HTTPGetStub(success=True) + client = autoupdate.PyPIVersionClient(client=HTTPStub) + + # Use a spy to inspect the signal + success_spy = QSignalSpy(client.success) + error_spy = QSignalSpy(client.error) + + with qtbot.waitSignal(client.success, timeout=2000, raising=False): + client.get_version('test') + + assert len(success_spy) == 1 + assert len(error_spy) == 0 + + +def test_get_version_error(qtbot): + """Test get_version() when error is emitted.""" + HTTPStub = HTTPGetStub(success=False) + client = autoupdate.PyPIVersionClient(client=HTTPStub) + + # Use a spy to inspect the signal + success_spy = QSignalSpy(client.success) + error_spy = QSignalSpy(client.error) + + with qtbot.waitSignal(client.error, timeout=2000, raising=False): + client.get_version('test') + + assert len(success_spy) == 0 + assert len(error_spy) == 1 + + +def test_invalid_json(): + """Test on_client_success() with invalid JSON.""" + json = '{"invalid": { "json"}' + HTTPStub = HTTPGetStub(json=json) + client = autoupdate.PyPIVersionClient(client=HTTPStub) + error_spy = QSignalSpy(client.error) + client.get_version('test') + assert len(error_spy) == 1 + + +def test_invalid_keys(): + """Test on_client_success() with valid JSON and wrong keys.""" + json = '{"wrong": "keys"}' + HTTPStub = HTTPGetStub(json=json) + client = autoupdate.PyPIVersionClient(client=HTTPStub) + error_spy = QSignalSpy(client.error) + client.get_version('test') + assert len(error_spy) == 1 From ee77951e66b9200a914297342fce8f7f98177003 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Mon, 31 Aug 2015 01:25:42 +0200 Subject: [PATCH 2/4] PEP8 fixes --- scripts/dev/check_coverage.py | 1 + tests/unit/misc/test_autoupdate.py | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index 8fffb7ce6..64d6938db 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -48,6 +48,7 @@ PERFECT_FILES = [ 'qutebrowser/keyinput/basekeyparser.py', + 'qutebrowser/misc/autoupdate.py', 'qutebrowser/misc/readline.py', 'qutebrowser/misc/split.py', 'qutebrowser/misc/msgbox.py', diff --git a/tests/unit/misc/test_autoupdate.py b/tests/unit/misc/test_autoupdate.py index d83c79cc6..bc29e18fe 100644 --- a/tests/unit/misc/test_autoupdate.py +++ b/tests/unit/misc/test_autoupdate.py @@ -19,7 +19,6 @@ """Tests for qutebrowser.misc.autoupdate""" -import pytest from PyQt5.QtTest import QSignalSpy from qutebrowser.misc import autoupdate, httpclient @@ -49,14 +48,14 @@ class HTTPGetStub(httpclient.HTTPClient): def test_constructor(): - client = autoupdate.PyPIVersionClient() - assert isinstance(client._client, httpclient.HTTPClient) + client = autoupdate.PyPIVersionClient() + assert isinstance(client._client, httpclient.HTTPClient) def test_get_version_success(qtbot): """Test get_version() when success is emitted.""" - HTTPStub = HTTPGetStub(success=True) - client = autoupdate.PyPIVersionClient(client=HTTPStub) + http_stub = HTTPGetStub(success=True) + client = autoupdate.PyPIVersionClient(client=http_stub) # Use a spy to inspect the signal success_spy = QSignalSpy(client.success) @@ -71,8 +70,8 @@ def test_get_version_success(qtbot): def test_get_version_error(qtbot): """Test get_version() when error is emitted.""" - HTTPStub = HTTPGetStub(success=False) - client = autoupdate.PyPIVersionClient(client=HTTPStub) + http_stub = HTTPGetStub(success=False) + client = autoupdate.PyPIVersionClient(client=http_stub) # Use a spy to inspect the signal success_spy = QSignalSpy(client.success) @@ -88,8 +87,8 @@ def test_get_version_error(qtbot): def test_invalid_json(): """Test on_client_success() with invalid JSON.""" json = '{"invalid": { "json"}' - HTTPStub = HTTPGetStub(json=json) - client = autoupdate.PyPIVersionClient(client=HTTPStub) + http_stub = HTTPGetStub(json=json) + client = autoupdate.PyPIVersionClient(client=http_stub) error_spy = QSignalSpy(client.error) client.get_version('test') assert len(error_spy) == 1 @@ -98,8 +97,8 @@ def test_invalid_json(): def test_invalid_keys(): """Test on_client_success() with valid JSON and wrong keys.""" json = '{"wrong": "keys"}' - HTTPStub = HTTPGetStub(json=json) - client = autoupdate.PyPIVersionClient(client=HTTPStub) + http_stub = HTTPGetStub(json=json) + client = autoupdate.PyPIVersionClient(client=http_stub) error_spy = QSignalSpy(client.error) client.get_version('test') assert len(error_spy) == 1 From 8ab85d624698a7149db3825f1512527bae7bbf58 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Mon, 31 Aug 2015 09:30:16 +0200 Subject: [PATCH 3/4] small edits to autoupdate test --- tests/unit/misc/test_autoupdate.py | 38 +++++++++++++----------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/tests/unit/misc/test_autoupdate.py b/tests/unit/misc/test_autoupdate.py index bc29e18fe..cda2d6a5a 100644 --- a/tests/unit/misc/test_autoupdate.py +++ b/tests/unit/misc/test_autoupdate.py @@ -19,29 +19,31 @@ """Tests for qutebrowser.misc.autoupdate""" +import pytest from PyQt5.QtTest import QSignalSpy from qutebrowser.misc import autoupdate, httpclient +INVALID_JSON = ['{"invalid": { "json"}', '{"wrong": "keys"}'] + class HTTPGetStub(httpclient.HTTPClient): """A stub class for HTTPClient. - Attributes: + Attributes: _success: Wether get() will emit a success signal. """ def __init__(self, success=True, json=None): + super().__init__() self._success = success if json: self._json = json else: self._json = '{"info": {"version": "test"}}' - super(HTTPGetStub, self).__init__() - def get(self, url): - if self._success is True: + if self._success: self.success.emit(self._json) else: self.error.emit("error") @@ -58,13 +60,11 @@ def test_get_version_success(qtbot): client = autoupdate.PyPIVersionClient(client=http_stub) # Use a spy to inspect the signal - success_spy = QSignalSpy(client.success) 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') - assert len(success_spy) == 1 assert len(error_spy) == 0 @@ -75,30 +75,24 @@ def test_get_version_error(qtbot): # Use a spy to inspect the signal 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') 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.""" - json = '{"invalid": { "json"}' http_stub = HTTPGetStub(json=json) client = autoupdate.PyPIVersionClient(client=http_stub) - error_spy = QSignalSpy(client.error) 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(): - """Test on_client_success() with valid JSON and wrong keys.""" - json = '{"wrong": "keys"}' - http_stub = HTTPGetStub(json=json) - client = autoupdate.PyPIVersionClient(client=http_stub) - error_spy = QSignalSpy(client.error) - client.get_version('test') - assert len(error_spy) == 1 + with qtbot.waitSignal(client.error, raising=True): + client.get_version('test') + + assert len(success_spy) == 0 From 2d12c26e8c6d2c5d6c217ca52238b8837c5e3fc9 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Tue, 1 Sep 2015 01:06:11 +0200 Subject: [PATCH 4/4] Add extra url assertion for autoupdate --- tests/unit/misc/test_autoupdate.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/misc/test_autoupdate.py b/tests/unit/misc/test_autoupdate.py index cda2d6a5a..f03db7bec 100644 --- a/tests/unit/misc/test_autoupdate.py +++ b/tests/unit/misc/test_autoupdate.py @@ -21,6 +21,7 @@ import pytest from PyQt5.QtTest import QSignalSpy +from PyQt5.QtCore import QUrl from qutebrowser.misc import autoupdate, httpclient @@ -31,6 +32,7 @@ class HTTPGetStub(httpclient.HTTPClient): """A stub class for HTTPClient. Attributes: + url: the last url used by get() _success: Wether get() will emit a success signal. """ @@ -43,6 +45,7 @@ class HTTPGetStub(httpclient.HTTPClient): self._json = '{"info": {"version": "test"}}' def get(self, url): + self.url = url if self._success: self.success.emit(self._json) else: @@ -67,6 +70,8 @@ def test_get_version_success(qtbot): assert len(error_spy) == 0 + assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json') + def test_get_version_error(qtbot): """Test get_version() when error is emitted."""