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.
This commit is contained in:
Ryan Roden-Corrent 2016-06-15 23:41:14 -04:00
parent b037ec489f
commit baf8d00a20
3 changed files with 166 additions and 1 deletions

View File

@ -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."""

View File

@ -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."""

View File

@ -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.