Separate sqlcategory title from table name.

Also fix a number of sql/completion tests that were failing.
This commit is contained in:
Ryan Roden-Corrent 2017-06-09 08:06:12 -04:00
parent 61a1709141
commit 679e001a48
4 changed files with 25 additions and 25 deletions

View File

@ -31,12 +31,14 @@ class SqlCategory(QSqlQueryModel):
"""Wraps a SqlQuery for use as a completion category."""
def __init__(self, name, *, filter_fields, sort_by=None, sort_order=None,
select='*', where=None, group_by=None, parent=None):
def __init__(self, name, *, title=None, filter_fields, sort_by=None,
sort_order=None, select='*', where=None, group_by=None,
parent=None):
"""Create a new completion category backed by a sql table.
Args:
name: Name of category, and the table in the database.
name: Name of the table in the database.
title: Title of category, defaults to table name.
filter_fields: Names of fields to apply filter pattern to.
select: A custom result column expression for the select statement.
where: An optional clause to filter out some rows.
@ -44,7 +46,7 @@ class SqlCategory(QSqlQueryModel):
sort_order: Either 'asc' or 'desc', if sort_by is non-None
"""
super().__init__(parent=parent)
self.name = name
self.name = title or name
querystr = 'select {} from {} where ('.format(select, name)
# the incoming pattern will have literal % and _ escaped with '\'

View File

@ -78,7 +78,8 @@ def url():
timefmt = config.get('completion', 'timestamp-format').replace("'", "`")
select_time = "strftime('{}', last_atime, 'unixepoch')".format(timefmt)
hist_cat = sqlcategory.SqlCategory(
'CompletionHistory', sort_order='desc', sort_by='last_atime',
'CompletionHistory', title='History',
sort_order='desc', sort_by='last_atime',
filter_fields=['url', 'title'],
select='url, title, {}'.format(select_time))
model.add_category(hist_cat)

View File

@ -161,16 +161,15 @@ def web_history_stub(stubs, init_sql):
@pytest.fixture
def web_history(web_history_stub, init_sql):
"""Pre-populate the web-history database."""
web_history_stub.insert(['http://qutebrowser.org', 'qutebrowser',
datetime(2015, 9, 5).timestamp()])
web_history_stub.insert(['https://python.org', 'Welcome to Python.org',
datetime(2016, 2, 8).timestamp()])
web_history_stub.insert(['https://python.org', 'Welcome to Python.org',
datetime(2016, 3, 8).timestamp()])
web_history_stub.insert(['https://python.org', 'Welcome to Python.org',
datetime(2014, 3, 8).timestamp()])
web_history_stub.insert(['https://github.com', 'https://github.com',
datetime(2016, 5, 1).timestamp()])
web_history_stub.insert(url='http://qutebrowser.org',
title='qutebrowser',
last_atime=datetime(2015, 9, 5).timestamp())
web_history_stub.insert(url='https://python.org',
title='Welcome to Python.org',
last_atime=datetime(2016, 3, 8).timestamp())
web_history_stub.insert(url='https://github.com',
title='https://github.com',
last_atime=datetime(2016, 5, 1).timestamp())
return web_history_stub
@ -304,9 +303,7 @@ def test_url_completion(qtmodeltester, config_stub, web_history, quickmarks,
"History": [
('https://github.com', 'https://github.com', '2016-05-01'),
('https://python.org', 'Welcome to Python.org', '2016-03-08'),
('https://python.org', 'Welcome to Python.org', '2016-02-08'),
('http://qutebrowser.org', 'qutebrowser', '2015-09-05'),
('https://python.org', 'Welcome to Python.org', '2014-03-08'),
],
})
@ -334,7 +331,7 @@ def test_url_completion_pattern(config_stub, web_history_stub,
url, title, pattern, rowcount):
"""Test that url completion filters by url and title."""
config_stub.data['completion'] = {'timestamp-format': '%Y-%m-%d'}
web_history_stub.insert([url, title, 0])
web_history_stub.insert(url=url, title=title, last_atime=0)
model = urlmodel.url()
model.set_pattern(pattern)
# 2, 0 is History

View File

@ -126,7 +126,7 @@ def test_set_pattern(pattern, filter_cols, before, after):
def test_select():
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
table.insert({'a': 'foo', 'b': 'bar', 'c': 'baz'})
table.insert(a='foo', b='bar', c='baz')
cat = sqlcategory.SqlCategory('Foo', filter_fields=['a'], select='b, c, a')
cat.set_pattern('')
utils.validate_model(cat, [('bar', 'baz', 'foo')])
@ -134,8 +134,8 @@ def test_select():
def test_where():
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
table.insert({'a': 'foo', 'b': 'bar', 'c': False})
table.insert({'a': 'baz', 'b': 'biz', 'c': True})
table.insert(a='foo', b='bar', c=False)
table.insert(a='baz', b='biz', c=True)
cat = sqlcategory.SqlCategory('Foo', filter_fields=['a'], where='not c')
cat.set_pattern('')
utils.validate_model(cat, [('foo', 'bar', False)])
@ -143,10 +143,10 @@ def test_where():
def test_group():
table = sql.SqlTable('Foo', ['a', 'b'])
table.insert({'a': 'foo', 'b': 1})
table.insert({'a': 'bar', 'b': 3})
table.insert({'a': 'foo', 'b': 2})
table.insert({'a': 'bar', 'b': 0})
table.insert(a='foo', b=1)
table.insert(a='bar', b=3)
table.insert(a='foo', b=2)
table.insert(a='bar', b=0)
cat = sqlcategory.SqlCategory('Foo', filter_fields=['a'],
select='a, max(b)', group_by='a')
cat.set_pattern('')