Fix histcategory query reuse logic.

I mistakenly checked the length of wheres instead of words. This fixes
that check, renames 'wheres' to 'where_clause' to be clear
that it is a string and not an array, and adds a test.
This commit is contained in:
Ryan Roden-Corrent 2017-12-13 08:39:34 -05:00
parent 2e36e5151e
commit 6420037dd9
2 changed files with 35 additions and 3 deletions

View File

@ -75,7 +75,7 @@ class HistoryCategory(QSqlQueryModel):
# build a where clause to match all of the words in any order
# given the search term "a b", the WHERE clause would be:
# ((url || title) LIKE '%a%') AND ((url || title) LIKE '%b%')
wheres = ' AND '.join(
where_clause = ' AND '.join(
"(url || title) LIKE :{} escape '\\'".format(i)
for i in range(len(words)))
@ -84,7 +84,7 @@ class HistoryCategory(QSqlQueryModel):
timefmt = ("strftime('{}', last_atime, 'unixepoch', 'localtime')"
.format(timestamp_format.replace("'", "`")))
if not self._query or len(wheres) != len(self._query.boundValues()):
if not self._query or len(words) != len(self._query.boundValues()):
# if the number of words changed, we need to generate a new query
# otherwise, we can reuse the prepared query for performance
self._query = sql.Query(' '.join([
@ -92,7 +92,7 @@ class HistoryCategory(QSqlQueryModel):
"FROM CompletionHistory",
# the incoming pattern will have literal % and _ escaped
# we need to tell sql to treat '\' as an escape character
'WHERE ({})'.format(wheres),
'WHERE ({})'.format(where_clause),
self._atime_expr(),
"ORDER BY last_atime DESC",
]), forward_only=False)

View File

@ -93,6 +93,38 @@ def test_set_pattern(pattern, before, after, model_validator, hist):
model_validator.validate(after)
def test_set_pattern_repeated(model_validator, hist):
"""Validate multiple subsequent calls to set_pattern."""
hist.insert({'url': 'example.com/foo', 'title': 'title1', 'last_atime': 1})
hist.insert({'url': 'example.com/bar', 'title': 'title2', 'last_atime': 1})
hist.insert({'url': 'example.com/baz', 'title': 'title3', 'last_atime': 1})
cat = histcategory.HistoryCategory()
model_validator.set_model(cat)
cat.set_pattern('b')
model_validator.validate([
('example.com/bar', 'title2'),
('example.com/baz', 'title3'),
])
cat.set_pattern('ba')
model_validator.validate([
('example.com/bar', 'title2'),
('example.com/baz', 'title3'),
])
cat.set_pattern('ba ')
model_validator.validate([
('example.com/bar', 'title2'),
('example.com/baz', 'title3'),
])
cat.set_pattern('ba z')
model_validator.validate([
('example.com/baz', 'title3'),
])
@pytest.mark.parametrize('max_items, before, after', [
(-1, [
('a', 'a', '2017-04-16'),