From 6826a979101fa8e292c37875a2225ff26165b6ad Mon Sep 17 00:00:00 2001 From: avk Date: Mon, 22 Feb 2016 15:10:39 +0100 Subject: [PATCH 1/3] Tests for browser.network.pastebin --- tests/unit/browser/network/test_pastebin.py | 60 +++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/unit/browser/network/test_pastebin.py diff --git a/tests/unit/browser/network/test_pastebin.py b/tests/unit/browser/network/test_pastebin.py new file mode 100644 index 000000000..e0999bc60 --- /dev/null +++ b/tests/unit/browser/network/test_pastebin.py @@ -0,0 +1,60 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2016 Anna Kobak (avk) : +# +# 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.browser.network""" + +import pytest +from PyQt5.QtCore import QUrl + +from qutebrowser.browser.network import pastebin +from qutebrowser.misc import httpclient + +DATA = [{"name" : "XYZ", "title" : "hello world", "text" : "xyz. 123 \n 172ANB", "reply" : "abc" }] + +class HTTPPostStub(httpclient.HTTPClient): + + """A stub class for HTTPClient. + + Attributes: + url: the last url send by post() + data: the last data send by post() + """ + + def __init__(self): + super().__init__() + + def post(self, url, data): + self.url = url + self.data = data + + + +def test_constructor(qapp): + client = pastebin.PastebinClient() + assert isinstance(client._client, httpclient.HTTPClient) + +@pytest.mark.parametrize('data', DATA) +def test_paste(data): + client = pastebin.PastebinClient() + http_stub = HTTPPostStub() + client._client = http_stub + client.paste(data["name"], data["title"], data["text"], data["reply"]) + assert http_stub.data == data + assert http_stub.url == QUrl('http://paste.the-compiler.org/api/create') + From 90c80782258c89a4c2612f73985fba15078803ad Mon Sep 17 00:00:00 2001 From: avk Date: Mon, 22 Feb 2016 16:09:57 +0100 Subject: [PATCH 2/3] More tests for browser.network.pastebin --- tests/unit/browser/network/test_pastebin.py | 41 +++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/tests/unit/browser/network/test_pastebin.py b/tests/unit/browser/network/test_pastebin.py index e0999bc60..1e237d58c 100644 --- a/tests/unit/browser/network/test_pastebin.py +++ b/tests/unit/browser/network/test_pastebin.py @@ -1,6 +1,6 @@ # vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: -# Copyright 2016 Anna Kobak (avk) : +# Copyright 2016 Anna Kobak (avk) : # # This file is part of qutebrowser. # @@ -25,7 +25,15 @@ from PyQt5.QtCore import QUrl from qutebrowser.browser.network import pastebin from qutebrowser.misc import httpclient -DATA = [{"name" : "XYZ", "title" : "hello world", "text" : "xyz. 123 \n 172ANB", "reply" : "abc" }] +DATA = [{"name" : "XYZ", "title" : "hello world", "text" : "xyz. 123 \n 172ANB", "reply" : "abc" }, + {"name" : "the name", "title" : "the title", "text" : "some Text", "reply" : "some parent"}] + +DATA_NOPARENT = [{"name" : "XYZ", "title" : "hello world", "text" : "xyz. 123 \n 172ANB"}, + {"name" : "the name", "title" : "the title", "text" : "some Text"}] + +HTTP_VALID = ["http://paste.the-compiler.org/view/ges83nt3", "http://paste.the-compiler.org/view/3gjnwg4"] + +HTTP_INVALID = ["http invalid", "http:/invalid.org"] class HTTPPostStub(httpclient.HTTPClient): @@ -50,7 +58,7 @@ def test_constructor(qapp): assert isinstance(client._client, httpclient.HTTPClient) @pytest.mark.parametrize('data', DATA) -def test_paste(data): +def test_paste_with_parent(data): client = pastebin.PastebinClient() http_stub = HTTPPostStub() client._client = http_stub @@ -58,3 +66,30 @@ def test_paste(data): assert http_stub.data == data assert http_stub.url == QUrl('http://paste.the-compiler.org/api/create') +@pytest.mark.parametrize('data', DATA_NOPARENT) +def test_paste_without_parent(data): + client = pastebin.PastebinClient() + http_stub = HTTPPostStub() + client._client = http_stub + client.paste(data["name"], data["title"], data["text"]) + assert http_stub.data == data + assert http_stub.url == QUrl('http://paste.the-compiler.org/api/create') + +@pytest.mark.parametrize('http', HTTP_VALID) +def test_on_client_success(http, qtbot): + client = pastebin.PastebinClient() + http_stub = HTTPPostStub() + client._client = http_stub + with qtbot.assertNotEmitted(client.error): + with qtbot.waitSignal(client.success): + client.on_client_success(http) + +@pytest.mark.parametrize('http', HTTP_INVALID) +def test_on_client_success_invalid_http(http, qtbot): + client = pastebin.PastebinClient() + http_stub = HTTPPostStub() + client._client = http_stub + with qtbot.assertNotEmitted(client.success): + with qtbot.waitSignal(client.error): + client.on_client_success(http) + From c9bb6d01119c53f250459a96505759e5ed5f56e3 Mon Sep 17 00:00:00 2001 From: avk Date: Mon, 22 Feb 2016 20:46:22 +0100 Subject: [PATCH 3/3] tests for browser.network.pastebin - code refactor --- scripts/dev/check_coverage.py | 2 + tests/unit/browser/network/test_pastebin.py | 114 ++++++++++++-------- 2 files changed, 71 insertions(+), 45 deletions(-) diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index ff5bc01f5..939fd5e71 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -65,6 +65,8 @@ PERFECT_FILES = [ 'qutebrowser/browser/network/filescheme.py'), ('tests/unit/browser/network/test_networkreply.py', 'qutebrowser/browser/network/networkreply.py'), + ('tests/unit/browser/network/test_pastebin.py', + 'qutebrowser/browser/network/pastebin.py'), ('tests/unit/browser/test_signalfilter.py', 'qutebrowser/browser/signalfilter.py'), diff --git a/tests/unit/browser/network/test_pastebin.py b/tests/unit/browser/network/test_pastebin.py index 1e237d58c..d72c9f6b8 100644 --- a/tests/unit/browser/network/test_pastebin.py +++ b/tests/unit/browser/network/test_pastebin.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU General Public License # along with qutebrowser. If not, see . -"""Tests for qutebrowser.browser.network""" +"""Tests for qutebrowser.browser.network.""" import pytest from PyQt5.QtCore import QUrl @@ -25,15 +25,6 @@ from PyQt5.QtCore import QUrl from qutebrowser.browser.network import pastebin from qutebrowser.misc import httpclient -DATA = [{"name" : "XYZ", "title" : "hello world", "text" : "xyz. 123 \n 172ANB", "reply" : "abc" }, - {"name" : "the name", "title" : "the title", "text" : "some Text", "reply" : "some parent"}] - -DATA_NOPARENT = [{"name" : "XYZ", "title" : "hello world", "text" : "xyz. 123 \n 172ANB"}, - {"name" : "the name", "title" : "the title", "text" : "some Text"}] - -HTTP_VALID = ["http://paste.the-compiler.org/view/ges83nt3", "http://paste.the-compiler.org/view/3gjnwg4"] - -HTTP_INVALID = ["http invalid", "http:/invalid.org"] class HTTPPostStub(httpclient.HTTPClient): @@ -46,50 +37,83 @@ class HTTPPostStub(httpclient.HTTPClient): def __init__(self): super().__init__() - - def post(self, url, data): + self.url = None + self.data = None + + def post(self, url, data=None): self.url = url self.data = data +@pytest.fixture +def pbclient(): + client = pastebin.PastebinClient() + http_stub = HTTPPostStub() + client._client = http_stub + return client + def test_constructor(qapp): - client = pastebin.PastebinClient() - assert isinstance(client._client, httpclient.HTTPClient) + pbclient = pastebin.PastebinClient() + assert isinstance(pbclient._client, httpclient.HTTPClient) -@pytest.mark.parametrize('data', DATA) -def test_paste_with_parent(data): - client = pastebin.PastebinClient() - http_stub = HTTPPostStub() - client._client = http_stub - client.paste(data["name"], data["title"], data["text"], data["reply"]) - assert http_stub.data == data - assert http_stub.url == QUrl('http://paste.the-compiler.org/api/create') - -@pytest.mark.parametrize('data', DATA_NOPARENT) -def test_paste_without_parent(data): - client = pastebin.PastebinClient() - http_stub = HTTPPostStub() - client._client = http_stub - client.paste(data["name"], data["title"], data["text"]) + +@pytest.mark.parametrize('data', [ + { + "name": "XYZ", + "title": "hello world", + "text": "xyz. 123 \n 172ANB", + "reply": "abc" + }, + { + "name": "the name", + "title": "the title", + "text": "some Text", + "reply": "some parent" + } +]) +def test_paste_with_parent(data, pbclient): + http_stub = pbclient._client + pbclient.paste(data["name"], data["title"], data["text"], data["reply"]) assert http_stub.data == data assert http_stub.url == QUrl('http://paste.the-compiler.org/api/create') -@pytest.mark.parametrize('http', HTTP_VALID) -def test_on_client_success(http, qtbot): - client = pastebin.PastebinClient() - http_stub = HTTPPostStub() - client._client = http_stub - with qtbot.assertNotEmitted(client.error): - with qtbot.waitSignal(client.success): - client.on_client_success(http) -@pytest.mark.parametrize('http', HTTP_INVALID) -def test_on_client_success_invalid_http(http, qtbot): - client = pastebin.PastebinClient() - http_stub = HTTPPostStub() - client._client = http_stub - with qtbot.assertNotEmitted(client.success): - with qtbot.waitSignal(client.error): - client.on_client_success(http) +@pytest.mark.parametrize('data', [ + { + "name": "XYZ", + "title": "hello world", + "text": "xyz. 123 \n 172ANB" + }, + { + "name": "the name", + "title": "the title", + "text": "some Text" + } +]) +def test_paste_without_parent(data, pbclient): + http_stub = pbclient._client + pbclient.paste(data["name"], data["title"], data["text"]) + assert pbclient._client.data == data + assert http_stub.url == QUrl('http://paste.the-compiler.org/api/create') + +@pytest.mark.parametrize('http', [ + "http://paste.the-compiler.org/view/ges83nt3", + "http://paste.the-compiler.org/view/3gjnwg4" +]) +def test_on_client_success(http, pbclient, qtbot): + with qtbot.assertNotEmitted(pbclient.error): + with qtbot.waitSignal(pbclient.success): + pbclient.on_client_success(http) + + +@pytest.mark.parametrize('http', [ + "http invalid", + "http:/invalid.org" + "http//invalid.com" +]) +def test_on_client_success_invalid_http(http, pbclient, qtbot): + with qtbot.assertNotEmitted(pbclient.success): + with qtbot.waitSignal(pbclient.error): + pbclient.on_client_success(http)