qutebrowser/tests/unit/misc/test_autoupdate.py

92 lines
2.8 KiB
Python
Raw Normal View History

2015-08-31 01:10:04 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2016-01-04 07:12:39 +01:00
# Copyright 2015-2016 Alexander Cogneau (acogneau) <alexander.cogneau@gmail.com>:
2015-08-31 01:10:04 +02:00
#
# 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 <http://www.gnu.org/licenses/>.
"""Tests for qutebrowser.misc.autoupdate"""
2015-08-31 09:30:16 +02:00
import pytest
2015-09-01 01:06:11 +02:00
from PyQt5.QtCore import QUrl
2015-08-31 01:10:04 +02:00
from qutebrowser.misc import autoupdate, httpclient
2015-08-31 09:30:16 +02:00
INVALID_JSON = ['{"invalid": { "json"}', '{"wrong": "keys"}']
2015-08-31 01:10:04 +02:00
class HTTPGetStub(httpclient.HTTPClient):
"""A stub class for HTTPClient.
2015-08-31 09:30:16 +02:00
Attributes:
2015-09-01 01:06:11 +02:00
url: the last url used by get()
2015-10-04 15:41:42 +02:00
_success: Whether get() will emit a success signal.
2015-08-31 01:10:04 +02:00
"""
def __init__(self, success=True, json=None):
2015-08-31 09:30:16 +02:00
super().__init__()
self.url = None
2015-08-31 01:10:04 +02:00
self._success = success
if json:
self._json = json
else:
self._json = '{"info": {"version": "test"}}'
def get(self, url):
2015-09-01 01:06:11 +02:00
self.url = url
2015-08-31 09:30:16 +02:00
if self._success:
2015-08-31 01:10:04 +02:00
self.success.emit(self._json)
else:
self.error.emit("error")
def test_constructor(qapp):
2015-08-31 01:25:42 +02:00
client = autoupdate.PyPIVersionClient()
assert isinstance(client._client, httpclient.HTTPClient)
2015-08-31 01:10:04 +02:00
def test_get_version_success(qtbot):
"""Test get_version() when success is emitted."""
2015-08-31 01:25:42 +02:00
http_stub = HTTPGetStub(success=True)
client = autoupdate.PyPIVersionClient(client=http_stub)
2015-08-31 01:10:04 +02:00
with qtbot.assertNotEmitted(client.error):
with qtbot.waitSignal(client.success):
client.get_version('test')
2015-08-31 01:10:04 +02:00
2015-09-01 01:06:11 +02:00
assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json')
2015-08-31 01:10:04 +02:00
def test_get_version_error(qtbot):
"""Test get_version() when error is emitted."""
2015-08-31 01:25:42 +02:00
http_stub = HTTPGetStub(success=False)
client = autoupdate.PyPIVersionClient(client=http_stub)
2015-08-31 01:10:04 +02:00
with qtbot.assertNotEmitted(client.success):
with qtbot.waitSignal(client.error):
client.get_version('test')
2015-08-31 01:10:04 +02:00
2015-08-31 09:30:16 +02:00
@pytest.mark.parametrize('json', INVALID_JSON)
def test_invalid_json(qtbot, json):
2015-08-31 01:10:04 +02:00
"""Test on_client_success() with invalid JSON."""
2015-08-31 01:25:42 +02:00
http_stub = HTTPGetStub(json=json)
client = autoupdate.PyPIVersionClient(client=http_stub)
2015-08-31 01:10:04 +02:00
client.get_version('test')
with qtbot.assertNotEmitted(client.success):
with qtbot.waitSignal(client.error):
client.get_version('test')