SQL code review changes.
- use mocker.Mock instead of mock.Mock to avoid an extra import - attach model to validator sooner so it can validate changes in the model during the test
This commit is contained in:
parent
f9f8900fe9
commit
cf4ac1a5b7
@ -484,20 +484,25 @@ def init_sql(data_tmpdir):
|
||||
sql.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def validate_model(qtmodeltester):
|
||||
"""Provides a function to validate a completion category."""
|
||||
def validate(cat, expected):
|
||||
"""Check that a category contains the items in the given order.
|
||||
class ModelValidator:
|
||||
|
||||
Args:
|
||||
cat: The category to inspect.
|
||||
expected: A list of tuples containing the expected items.
|
||||
"""
|
||||
qtmodeltester.data_display_may_return_none = True
|
||||
qtmodeltester.check(cat)
|
||||
assert cat.rowCount() == len(expected)
|
||||
"""Validates completion models."""
|
||||
|
||||
def __init__(self, modeltester):
|
||||
modeltester.data_display_may_return_none = True
|
||||
self._modeltester = modeltester
|
||||
|
||||
def set_model(self, model):
|
||||
self._model = model
|
||||
self._modeltester.check(model)
|
||||
|
||||
def validate(self, expected):
|
||||
assert self._model.rowCount() == len(expected)
|
||||
for row, items in enumerate(expected):
|
||||
for col, item in enumerate(items):
|
||||
assert cat.data(cat.index(row, col)) == item
|
||||
return validate
|
||||
assert self._model.data(self._model.index(row, col)) == item
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def model_validator(qtmodeltester):
|
||||
return ModelValidator(qtmodeltester)
|
||||
|
@ -21,7 +21,6 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
import unittest.mock
|
||||
|
||||
import pytest
|
||||
from PyQt5.QtCore import QUrl
|
||||
@ -115,7 +114,7 @@ def test_clear(qtbot, tmpdir, hist, mocker):
|
||||
hist.add_url(QUrl('http://www.qutebrowser.org/'))
|
||||
|
||||
m = mocker.patch('qutebrowser.browser.history.message.confirm_async',
|
||||
new=unittest.mock.Mock, spec=[])
|
||||
new=mocker.Mock, spec=[])
|
||||
hist.clear()
|
||||
assert m.called
|
||||
|
||||
|
@ -45,26 +45,29 @@ from qutebrowser.commands import cmdexc
|
||||
[('foo', 'bar'), ('bar', 'foo'), ('bar', 'bar')],
|
||||
[('foo', 'bar'), ('bar', 'foo')]),
|
||||
])
|
||||
def test_set_pattern(pattern, before, after, validate_model):
|
||||
def test_set_pattern(pattern, before, after, model_validator):
|
||||
"""Validate the filtering and sorting results of set_pattern."""
|
||||
cat = listcategory.ListCategory('Foo', before)
|
||||
model_validator.set_model(cat)
|
||||
cat.set_pattern(pattern)
|
||||
validate_model(cat, after)
|
||||
model_validator.validate(after)
|
||||
|
||||
|
||||
def test_delete_cur_item(validate_model):
|
||||
def test_delete_cur_item(model_validator):
|
||||
func = mock.Mock(spec=[])
|
||||
cat = listcategory.ListCategory('Foo', [('a', 'b'), ('c', 'd')],
|
||||
delete_func=func)
|
||||
model_validator.set_model(cat)
|
||||
idx = cat.index(0, 0)
|
||||
cat.delete_cur_item(idx)
|
||||
func.assert_called_once_with(['a', 'b'])
|
||||
validate_model(cat, [('c', 'd')])
|
||||
model_validator.validate([('c', 'd')])
|
||||
|
||||
|
||||
def test_delete_cur_item_no_func(validate_model):
|
||||
def test_delete_cur_item_no_func(model_validator):
|
||||
cat = listcategory.ListCategory('Foo', [('a', 'b'), ('c', 'd')])
|
||||
model_validator.set_model(cat)
|
||||
idx = cat.index(0, 0)
|
||||
with pytest.raises(cmdexc.CommandError, match="Cannot delete this item."):
|
||||
cat.delete_cur_item(idx)
|
||||
validate_model(cat, [('a', 'b'), ('c', 'd')])
|
||||
model_validator.validate([('a', 'b'), ('c', 'd')])
|
||||
|
@ -60,14 +60,15 @@ pytestmark = pytest.mark.usefixtures('init_sql')
|
||||
[('B', 'C', 2), ('A', 'F', 0), ('C', 'A', 1)],
|
||||
[('B', 'C', 2), ('C', 'A', 1), ('A', 'F', 0)]),
|
||||
])
|
||||
def test_sorting(sort_by, sort_order, data, expected, validate_model):
|
||||
def test_sorting(sort_by, sort_order, data, expected, model_validator):
|
||||
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
|
||||
for row in data:
|
||||
table.insert({'a': row[0], 'b': row[1], 'c': row[2]})
|
||||
cat = sqlcategory.SqlCategory('Foo', filter_fields=['a'], sort_by=sort_by,
|
||||
sort_order=sort_order)
|
||||
model_validator.set_model(cat)
|
||||
cat.set_pattern('')
|
||||
validate_model(cat, expected)
|
||||
model_validator.validate(expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pattern, filter_cols, before, after', [
|
||||
@ -115,23 +116,25 @@ def test_sorting(sort_by, sort_order, data, expected, validate_model):
|
||||
[("can't touch this", '', ''), ('a', '', '')],
|
||||
[("can't touch this", '', '')]),
|
||||
])
|
||||
def test_set_pattern(pattern, filter_cols, before, after, validate_model):
|
||||
def test_set_pattern(pattern, filter_cols, before, after, model_validator):
|
||||
"""Validate the filtering and sorting results of set_pattern."""
|
||||
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
|
||||
for row in before:
|
||||
table.insert({'a': row[0], 'b': row[1], 'c': row[2]})
|
||||
filter_fields = [['a', 'b', 'c'][i] for i in filter_cols]
|
||||
cat = sqlcategory.SqlCategory('Foo', filter_fields=filter_fields)
|
||||
model_validator.set_model(cat)
|
||||
cat.set_pattern(pattern)
|
||||
validate_model(cat, after)
|
||||
model_validator.validate(after)
|
||||
|
||||
|
||||
def test_select(validate_model):
|
||||
def test_select(model_validator):
|
||||
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
|
||||
table.insert({'a': 'foo', 'b': 'bar', 'c': 'baz'})
|
||||
cat = sqlcategory.SqlCategory('Foo', filter_fields=['a'], select='b, c, a')
|
||||
model_validator.set_model(cat)
|
||||
cat.set_pattern('')
|
||||
validate_model(cat, [('bar', 'baz', 'foo')])
|
||||
model_validator.validate([('bar', 'baz', 'foo')])
|
||||
|
||||
|
||||
def test_delete_cur_item():
|
||||
|
Loading…
Reference in New Issue
Block a user