diff --git a/qutebrowser/completion/models/histcategory.py b/qutebrowser/completion/models/histcategory.py index 7db6ca902..57a2aa936 100644 --- a/qutebrowser/completion/models/histcategory.py +++ b/qutebrowser/completion/models/histcategory.py @@ -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) diff --git a/tests/unit/completion/test_histcategory.py b/tests/unit/completion/test_histcategory.py index 8458c3311..e42f9b91f 100644 --- a/tests/unit/completion/test_histcategory.py +++ b/tests/unit/completion/test_histcategory.py @@ -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'),