From ca8d935cf4ca581fdc16a705e0c4ea4e7d65aaaf Mon Sep 17 00:00:00 2001 From: George Edward Bulmer Date: Tue, 13 Feb 2018 18:38:27 +0000 Subject: [PATCH] Update tests as per code review --- tests/helpers/stubs.py | 32 ++++++++++++++ tests/unit/misc/test_pastebin.py | 26 +----------- tests/unit/utils/test_version.py | 73 ++++++++++++++------------------ 3 files changed, 66 insertions(+), 65 deletions(-) diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 64bc793cb..ede322b74 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -563,3 +563,35 @@ class ApplicationStub(QObject): """Stub to insert as the app object in objreg.""" new_window = pyqtSignal(mainwindow.MainWindow) + + +class HTTPPostStub(QObject): + + """A stub class for HTTPClient. + + Attributes: + url: the last url send by post() + data: the last data send by post() + """ + + success = pyqtSignal(str) + error = pyqtSignal(str) + + def __init__(self, parent=None): + super().__init__(parent) + self.url = None + self.data = None + + def post(self, url, data=None): + self.url = url + self.data = data + + +@pytest.fixture +def pbclient(stubs): + http_stub = stubs.HTTPPostStub() + client = pastebin.PastebinClient(http_stub) + return client + + + diff --git a/tests/unit/misc/test_pastebin.py b/tests/unit/misc/test_pastebin.py index b352f52c8..9546bcf36 100644 --- a/tests/unit/misc/test_pastebin.py +++ b/tests/unit/misc/test_pastebin.py @@ -23,31 +23,9 @@ from PyQt5.QtCore import pyqtSignal, QUrl, QObject from qutebrowser.misc import httpclient, pastebin -class HTTPPostStub(QObject): - - """A stub class for HTTPClient. - - Attributes: - url: the last url send by post() - data: the last data send by post() - """ - - success = pyqtSignal(str) - error = pyqtSignal(str) - - def __init__(self, parent=None): - super().__init__(parent) - self.url = None - self.data = None - - def post(self, url, data=None): - self.url = url - self.data = data - - @pytest.fixture -def pbclient(): - http_stub = HTTPPostStub() +def pbclient(stubs): + http_stub = stubs.HTTPPostStub() client = pastebin.PastebinClient(http_stub) return client diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py index 96d0a5f4b..f4f7270ba 100644 --- a/tests/unit/utils/test_version.py +++ b/tests/unit/utils/test_version.py @@ -955,62 +955,53 @@ def test_opengl_vendor(): return version.opengl_vendor() -class HTTPPostStub(QObject): - - """A stub class for HTTPClient. - - Attributes: - url: the last url send by post() - data: the last data send by post() - """ - - success = pyqtSignal(str) - error = pyqtSignal(str) - - def __init__(self, parent=None): - super().__init__(parent) - self.url = None - self.data = None - - def post(self, url, data=None): - self.url = url - self.data = data - - @pytest.fixture -def pbclient(): - http_stub = HTTPPostStub() +def pbclient(stubs): + http_stub = stubs.HTTPPostStub() client = pastebin.PastebinClient(http_stub) return client -def test_pastebin_version(pbclient, monkeypatch): - """Test version.pastebin_version() twice.""" - patches = { - '_path_info': lambda: {'PATH DESC': 'PATH NAME'}, - '_uptime': lambda: datetime.timedelta(hours=1, minutes=23, seconds=45), - } - - for name, val in patches.items(): - monkeypatch.setattr('qutebrowser.utils.version.' + name, val) +def test_pastebin_version(pbclient, message_mock, monkeypatch, qtbot): + """Test version.pastebin_version() sets the url.""" + monkeypatch.setattr('qutebrowser.utils.version.version', + lambda: "dummy") + monkeypatch.setattr('qutebrowser.utils.utils.log_clipboard', True) version.pastebin_version(pbclient) pbclient.success.emit("test") + + msg = message_mock.getmsg(usertypes.MessageLevel.info) + assert msg.text == "Version url test yanked to clipboard." assert version.pastebin_url == "test" + version.pastebin_url = None + + +def test_pastebin_version_twice(pbclient, monkeypatch): + """Test whether calling pastebin_version twice sends no data.""" + monkeypatch.setattr('qutebrowser.utils.version.version', + lambda: "dummy") + version.pastebin_version(pbclient) - assert version.pastebin_url == "test" + pbclient.success.emit("test") + + pbclient.url = None + pbclient.data = None + version.pastebin_url = "test2" + + version.pastebin_version(pbclient) + assert pbclient.url is None + assert pbclient.data is None + assert version.pastebin_url == "test2" + + version.pastebin_url = None def test_pastebin_version_error(pbclient, caplog, monkeypatch): """Test version.pastebin_version() with errors.""" - patches = { - '_path_info': lambda: {'PATH DESC': 'PATH NAME'}, - '_uptime': lambda: datetime.timedelta(hours=1, minutes=23, seconds=45), - } - - for name, val in patches.items(): - monkeypatch.setattr('qutebrowser.utils.version.' + name, val) + monkeypatch.setattr('qutebrowser.utils.version.version', + lambda: "dummy") version.pastebin_url = None with caplog.at_level(logging.ERROR):