Get test_models mostly working again.

- Adjust _check_completions to work for CompletionModel and SqlCompletionModel
- Move sql initialization into a reusable fixture
- Remove the bookmark/quickmark/history stubs, as they're now handled by sql
- Disable quickmark/bookmark model tests until their completion is ported to
  sql.
- Disable urlmodel tests for features that have to be implemented in SQL:
    - LIMIT (for history-max-items)
    - Configurable column order (for quickmarks)
    - Configurable formatting (for timestamp-format
This commit is contained in:
Ryan Roden-Corrent 2017-02-12 08:39:24 -05:00
parent d4f2a70f83
commit a774647c26
5 changed files with 76 additions and 92 deletions

View File

@ -41,7 +41,7 @@ import helpers.stubs as stubsmod
from qutebrowser.config import config
from qutebrowser.utils import objreg, standarddir
from qutebrowser.browser.webkit import cookies
from qutebrowser.misc import savemanager
from qutebrowser.misc import savemanager, sql
from qutebrowser.keyinput import modeman
from PyQt5.QtCore import PYQT_VERSION, pyqtSignal, QEvent, QSize, Qt, QObject
@ -239,33 +239,6 @@ def host_blocker_stub(stubs):
objreg.delete('host-blocker')
@pytest.fixture
def quickmark_manager_stub(stubs):
"""Fixture which provides a fake quickmark manager object."""
stub = stubs.QuickmarkManagerStub()
objreg.register('quickmark-manager', stub)
yield stub
objreg.delete('quickmark-manager')
@pytest.fixture
def bookmark_manager_stub(stubs):
"""Fixture which provides a fake bookmark manager object."""
stub = stubs.BookmarkManagerStub()
objreg.register('bookmark-manager', stub)
yield stub
objreg.delete('bookmark-manager')
@pytest.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
def session_manager_stub(stubs):
"""Fixture which provides a fake web-history object."""
@ -482,3 +455,11 @@ def short_tmpdir():
"""A short temporary directory for a XDG_RUNTIME_DIR."""
with tempfile.TemporaryDirectory() as tdir:
yield py.path.local(tdir) # pylint: disable=no-member
@pytest.fixture()
def init_sql():
"""Initialize the SQL module, and shut it down after the test."""
sql.init()
yield
sql.close()

View File

@ -32,12 +32,9 @@ from qutebrowser.misc import sql
@pytest.fixture(autouse=True)
def prerequisites(config_stub, fake_save_manager):
def prerequisites(config_stub, fake_save_manager, init_sql):
"""Make sure everything is ready to initialize a WebHistory."""
config_stub.data = {'general': {'private-browsing': False}}
sql.init()
yield
sql.close()
@pytest.fixture()

View File

@ -30,6 +30,7 @@ from qutebrowser.completion.models import (miscmodels, urlmodel, configmodel,
sortfilter)
from qutebrowser.browser import history
from qutebrowser.config import sections, value
from qutebrowser.misc import sql
def _check_completions(model, expected):
@ -45,17 +46,16 @@ def _check_completions(model, expected):
"""
assert model.rowCount() == len(expected)
for i in range(0, model.rowCount()):
actual_cat = model.item(i)
catname = actual_cat.text()
catidx = model.index(i, 0)
catname = model.data(catidx)
assert catname in expected
expected_cat = expected[catname]
assert actual_cat.rowCount() == len(expected_cat)
for j in range(0, actual_cat.rowCount()):
name = actual_cat.child(j, 0)
desc = actual_cat.child(j, 1)
misc = actual_cat.child(j, 2)
actual_item = (name.text(), desc.text(), misc.text())
assert actual_item in expected_cat
assert model.rowCount(catidx) == len(expected_cat)
for j in range(model.rowCount(catidx)):
name = model.data(model.index(j, 0, parent=catidx))
desc = model.data(model.index(j, 1, parent=catidx))
misc = model.data(model.index(j, 2, parent=catidx))
assert (name, desc, misc) in expected_cat
# sanity-check the column_widths
assert len(model.column_widths) == 3
assert sum(model.column_widths) == 100
@ -133,42 +133,34 @@ def _mock_view_index(model, category_idx, child_idx, qtbot):
@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
def quickmarks(init_sql):
"""Pre-populate the quickmark database."""
table = sql.SqlTable('Quickmarks', ['name', 'url'], primary_key='name')
table.insert('aw', 'https://wiki.archlinux.org')
table.insert('ddg', 'https://duckduckgo.com')
table.insert('wiki', 'https://wikipedia.org')
@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
def bookmarks(init_sql):
"""Pre-populate the bookmark database."""
table = sql.SqlTable('Bookmarks', ['url', 'title'], primary_key='url')
table.insert('https://github.com', 'GitHub')
table.insert('https://python.org', 'Welcome to Python.org')
table.insert('http://qutebrowser.org', 'qutebrowser | qutebrowser')
@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', history.Entry(
datetime(2015, 9, 5).timestamp(),
QUrl('http://qutebrowser.org'), 'qutebrowser | qutebrowser')),
('https://python.org', history.Entry(
datetime(2016, 3, 8).timestamp(),
QUrl('https://python.org'), 'Welcome to Python.org')),
('https://github.com', history.Entry(
datetime(2016, 5, 1).timestamp(),
QUrl('https://github.com'), 'GitHub')),
])
return web_history_stub
def web_history(stubs, init_sql):
"""Pre-populate the web-history database."""
table = sql.SqlTable("History", ['url', 'title', 'atime', 'redirect'],
primary_key='url')
table.insert('http://qutebrowser.org', 'qutebrowser',
datetime(2015, 9, 5).timestamp(), False)
table.insert('https://python.org', 'Welcome to Python.org',
datetime(2016, 3, 8).timestamp(), False)
table.insert('https://github.com', 'https://github.com',
datetime(2016, 5, 1).timestamp(), False)
def test_command_completion(qtmodeltester, monkeypatch, stubs, config_stub,
@ -237,6 +229,7 @@ def test_help_completion(qtmodeltester, monkeypatch, stubs, key_config_stub):
})
@pytest.mark.skip
def test_quickmark_completion(qtmodeltester, quickmarks):
"""Test the results of quickmark completion."""
model = miscmodels.quickmark()
@ -252,6 +245,7 @@ def test_quickmark_completion(qtmodeltester, quickmarks):
})
@pytest.mark.skip
def test_bookmark_completion(qtmodeltester, bookmarks):
"""Test the results of bookmark completion."""
model = miscmodels.bookmark()
@ -273,33 +267,52 @@ def test_url_completion(qtmodeltester, config_stub, web_history, quickmarks,
Verify that:
- quickmarks, bookmarks, and urls are included
- no more than 'web-history-max-items' history entries are included
- no more than 'web-history-max-items' items are included (TODO)
- the most recent entries are included
"""
config_stub.data['completion'] = {'timestamp-format': '%Y-%m-%d',
'web-history-max-items': 2}
# TODO: time formatting and item limiting
#config_stub.data['completion'] = {'timestamp-format': '%Y-%m-%d',
# 'web-history-max-items': 2}
model = urlmodel.url()
qtmodeltester.data_display_may_return_none = True
qtmodeltester.check(model)
_check_completions(model, {
# TODO: rearrange columns so address comes first
#"Quickmarks": [
# ('https://wiki.archlinux.org', 'aw', None),
# ('https://duckduckgo.com', 'ddg', None),
# ('https://wikipedia.org', 'wiki', None),
#],
"Quickmarks": [
('https://wiki.archlinux.org', 'aw', ''),
('https://duckduckgo.com', 'ddg', ''),
('https://wikipedia.org', 'wiki', ''),
('aw', 'https://wiki.archlinux.org', None),
('ddg', 'https://duckduckgo.com', None),
('wiki', 'https://wikipedia.org', None),
],
"Bookmarks": [
('https://github.com', 'GitHub', ''),
('https://python.org', 'Welcome to Python.org', ''),
('http://qutebrowser.org', 'qutebrowser | qutebrowser', ''),
('https://github.com', 'GitHub', None),
('https://python.org', 'Welcome to Python.org', None),
('http://qutebrowser.org', 'qutebrowser | qutebrowser', None),
],
# TODO: time formatting and item limiting
#"History": [
# ('https://python.org', 'Welcome to Python.org', '2016-03-08'),
# ('https://github.com', 'GitHub', '2016-05-01'),
# ('http://qutebrowser.org', 'qutebrowser | qutebrowser',
# '2015-09-05', False)
#],
"History": [
('https://python.org', 'Welcome to Python.org', '2016-03-08'),
('https://github.com', 'GitHub', '2016-05-01'),
('http://qutebrowser.org', 'qutebrowser',
datetime(2015, 9, 5).timestamp()),
('https://python.org', 'Welcome to Python.org',
datetime(2016, 3, 8).timestamp()),
('https://github.com', 'https://github.com',
datetime(2016, 5, 1).timestamp()),
],
})
@pytest.mark.skip
def test_url_completion_delete_bookmark(qtmodeltester, config_stub,
web_history, quickmarks, bookmarks,
qtbot):
@ -318,6 +331,7 @@ def test_url_completion_delete_bookmark(qtmodeltester, config_stub,
assert 'http://qutebrowser.org' in bookmarks.marks
@pytest.mark.skip
def test_url_completion_delete_quickmark(qtmodeltester, config_stub,
web_history, quickmarks, bookmarks,
qtbot):

View File

@ -26,11 +26,7 @@ from qutebrowser.misc import sql
from qutebrowser.completion.models import sqlmodel
@pytest.fixture(autouse=True)
def init():
sql.init()
yield
sql.close()
pytestmark = pytest.mark.usefixtures('init_sql')
def _check_model(model, expected):

View File

@ -23,11 +23,7 @@ import pytest
from qutebrowser.misc import sql
@pytest.fixture(autouse=True)
def init():
sql.init()
yield
sql.close()
pytestmark = pytest.mark.usefixtures('init_sql')
def test_init():