SQL code review fixes.

- Fix comment and empty line check in _parse_entry
- connect layoutAboutToBeChanged signal
- assert sort_order is None if sort_by is None
- modify sql init failure message to ask about Qt sqlite support.
This commit is contained in:
Ryan Roden-Corrent 2017-07-07 21:16:50 -04:00
parent cf4ac1a5b7
commit 182d067ff8
5 changed files with 12 additions and 5 deletions

View File

@ -59,6 +59,7 @@ class CompletionModel(QAbstractItemModel):
def add_category(self, cat):
"""Add a completion category to the model."""
self._categories.append(cat)
cat.layoutAboutToBeChanged.connect(self.layoutAboutToBeChanged)
cat.layoutChanged.connect(self.layoutChanged)
def data(self, index, role=Qt.DisplayRole):

View File

@ -59,6 +59,8 @@ class SqlCategory(QSqlQueryModel):
if sort_by:
assert sort_order in ['asc', 'desc'], sort_order
querystr += ' order by {} {}'.format(sort_by, sort_order)
else:
assert sort_order is None, sort_order
self._query = sql.Query(querystr, forward_only=False)

View File

@ -38,7 +38,8 @@ def init(db_path):
"""Initialize the SQL database connection."""
database = QSqlDatabase.addDatabase('QSQLITE')
if not database.isValid():
raise SqlException('Failed to add database. Is sqlite installed?')
raise SqlException('Failed to add database. '
'Are sqlite and Qt sqlite support installed?')
database.setDatabaseName(db_path)
if not database.open():
raise SqlException("Failed to open sqlite database at {}: {}"

View File

@ -36,7 +36,7 @@ def test_first_last_item(counts):
"""Test that first() and last() index to the first and last items."""
model = completionmodel.CompletionModel()
for c in counts:
cat = mock.Mock(spec=['layoutChanged'])
cat = mock.Mock(spec=['layoutChanged', 'layoutAboutToBeChanged'])
cat.rowCount = mock.Mock(return_value=c, spec=[])
model.add_category(cat)
data = [i for i, rowCount in enumerate(counts) if rowCount > 0]
@ -60,7 +60,8 @@ def test_first_last_item(counts):
def test_count(counts):
model = completionmodel.CompletionModel()
for c in counts:
cat = mock.Mock(spec=['rowCount', 'layoutChanged'])
cat = mock.Mock(spec=['rowCount', 'layoutChanged',
'layoutAboutToBeChanged'])
cat.rowCount = mock.Mock(return_value=c, spec=[])
model.add_category(cat)
assert model.count() == sum(counts)
@ -70,7 +71,9 @@ def test_count(counts):
def test_set_pattern(pat):
"""Validate the filtering and sorting results of set_pattern."""
model = completionmodel.CompletionModel()
cats = [mock.Mock(spec=['set_pattern', 'layoutChanged']) for _ in range(3)]
cats = [mock.Mock(spec=['set_pattern', 'layoutChanged',
'layoutAboutToBeChanged'])
for _ in range(3)]
for c in cats:
c.set_pattern = mock.Mock(spec=[])
model.add_category(c)

View File

@ -32,7 +32,7 @@ pytestmark = pytest.mark.usefixtures('init_sql')
@pytest.mark.parametrize('sort_by, sort_order, data, expected', [
(None, 'asc',
(None, None,
[('B', 'C', 'D'), ('A', 'F', 'C'), ('C', 'A', 'G')],
[('B', 'C', 'D'), ('A', 'F', 'C'), ('C', 'A', 'G')]),