From baf8d00a20563f02aa1dd6d8078b02ab2723c291 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Wed, 15 Jun 2016 23:41:14 -0400 Subject: [PATCH] Unit test url/quickmark/bookmark completion. This also adds a few more stubs/fakes to mock out the quickmark-manager, bookmark-manager, and web-history objects. --- tests/helpers/fixtures.py | 27 ++++++++ tests/helpers/stubs.py | 42 ++++++++++++ tests/unit/completion/test_models.py | 98 +++++++++++++++++++++++++++- 3 files changed, 166 insertions(+), 1 deletion(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 5de771514..3c7c905a0 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -198,6 +198,33 @@ def host_blocker_stub(stubs): objreg.delete('host-blocker') +@pytest.yield_fixture +def quickmark_manager_stub(stubs): + """Fixture which provides a fake quickmark manager object.""" + stub = stubs.UrlMarkManagerStub() + objreg.register('quickmark-manager', stub) + yield stub + objreg.delete('quickmark-manager') + + +@pytest.yield_fixture +def bookmark_manager_stub(stubs): + """Fixture which provides a fake bookmark manager object.""" + stub = stubs.UrlMarkManagerStub() + objreg.register('bookmark-manager', stub) + yield stub + objreg.delete('bookmark-manager') + + +@pytest.yield_fixture +def web_history_stub(stubs): + """Fixture which provides a fake web-history object.""" + stub = stubs.WebHistoryStub() + objreg.register('web-history', stub) + yield stub + objreg.delete('web-history') + + @pytest.fixture(scope='session') def stubs(): """Provide access to stub objects useful for testing.""" diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 57d9a4da0..5bff97c09 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -21,6 +21,7 @@ """Fake objects/stubs.""" +import collections from unittest import mock from PyQt5.QtCore import pyqtSignal, QPoint, QProcess, QObject @@ -421,6 +422,47 @@ class KeyConfigStub: self.bindings[section] = bindings +class FakeHistoryEntry: + + """Mock for webkit.history.Entry.""" + + def __init__(self, atime, url, title, redirect=False): + self.atime = float(atime) + self.url = url + self.title = title + self.redirect = redirect + + +class UrlMarkManagerStub(QObject): + + """Stub for the quickmark-manager or bookmark-manager object.""" + + added = pyqtSignal(str, str) + removed = pyqtSignal(str) + + def __init__(self, parent=None): + super().__init__(parent) + self.marks = {} + + +class WebHistoryStub(QObject): + + """Stub for the web-history object.""" + + add_completion_item = pyqtSignal(FakeHistoryEntry) + cleared = pyqtSignal() + + def __init__(self, parent=None): + super().__init__(parent) + self.history_dict = collections.OrderedDict() + + def __iter__(self): + return iter(self.history_dict.values()) + + def __len__(self): + return len(self.history_dict) + + class HostBlockerStub: """Stub for the host-blocker object.""" diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index 2107d9128..45e17ab52 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -20,8 +20,51 @@ """Tests for completion models.""" import collections +from datetime import datetime -from qutebrowser.completion.models import miscmodels +import pytest +from PyQt5.QtCore import QUrl + +from qutebrowser.completion.models import miscmodels, urlmodel + + +@pytest.fixture +def quickmarks(quickmark_manager_stub): + """Pre-populate the quickmark-manager stub with some quickmarks.""" + quickmark_manager_stub.marks = collections.OrderedDict([ + ('aw', 'https://wiki.archlinux.org'), + ('ddg', 'https://duckduckgo.com'), + ('wiki', 'https://wikipedia.org'), + ]) + return quickmark_manager_stub + + +@pytest.fixture +def bookmarks(bookmark_manager_stub): + """Pre-populate the bookmark-manager stub with some quickmarks.""" + bookmark_manager_stub.marks = collections.OrderedDict([ + ('https://github.com', 'GitHub'), + ('https://python.org', 'Welcome to Python.org'), + ('http://qutebrowser.org', 'qutebrowser | qutebrowser'), + ]) + return bookmark_manager_stub + + +@pytest.fixture +def web_history(stubs, web_history_stub): + """Pre-populate the web-history stub with some history entries.""" + web_history_stub.history_dict = collections.OrderedDict([ + ('http://qutebrowser.org', stubs.FakeHistoryEntry( + datetime(2015, 9, 5).timestamp(), + QUrl('http://qutebrowser.org'), 'qutebrowser | qutebrowser')), + ('https://python.org', stubs.FakeHistoryEntry( + datetime(2016, 3, 8).timestamp(), + QUrl('https://python.org'), 'Welcome to Python.org')), + ('https://github.com', stubs.FakeHistoryEntry( + datetime(2016, 5, 1).timestamp(), + QUrl('https://github.com'), 'GitHub')), + ]) + return web_history_stub def test_command_completion(monkeypatch, stubs, config_stub, key_config_stub): @@ -80,6 +123,59 @@ def test_help_completion(monkeypatch, stubs): ] +def test_quickmark_completion(quickmarks): + """Test the results of quickmark completion.""" + actual = _get_completions(miscmodels.QuickmarkCompletionModel()) + assert actual == [ + ("Quickmarks", [ + ('aw', 'https://wiki.archlinux.org', ''), + ('ddg', 'https://duckduckgo.com', ''), + ('wiki', 'https://wikipedia.org', ''), + ]) + ] + + +def test_bookmark_completion(bookmarks): + """Test the results of bookmark completion.""" + actual = _get_completions(miscmodels.BookmarkCompletionModel()) + assert actual == [ + ("Bookmarks", [ + ('https://github.com', 'GitHub', ''), + ('https://python.org', 'Welcome to Python.org', ''), + ('http://qutebrowser.org', 'qutebrowser | qutebrowser', ''), + ]) + ] + + +def test_url_completion(config_stub, web_history, quickmarks, bookmarks): + """Test the results of url completion. + + Verify that: + - quickmarks, bookmarks, and urls are included + - no more than 'web-history-max-items' history entries are included + - the most recent entries are included + """ + config_stub.data['completion'] = {'timestamp-format': '%Y-%m-%d', + 'web-history-max-items': 2} + actual = _get_completions(urlmodel.UrlCompletionModel()) + assert actual == [ + ("Quickmarks", [ + ('https://wiki.archlinux.org', 'aw', ''), + ('https://duckduckgo.com', 'ddg', ''), + ('https://wikipedia.org', 'wiki', ''), + ]), + ("Bookmarks", [ + ('https://github.com', 'GitHub', ''), + ('https://python.org', 'Welcome to Python.org', ''), + ('http://qutebrowser.org', 'qutebrowser | qutebrowser', ''), + ]), + ("History", [ + ('https://python.org', 'Welcome to Python.org', '2016-03-08'), + ('https://github.com', 'GitHub', '2016-05-01'), + ]), + ] + + def _get_completions(model): """Collect all the completion entries of a model, organized by category.