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()
|
sql.close()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
class ModelValidator:
|
||||||
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.
|
|
||||||
|
|
||||||
Args:
|
"""Validates completion models."""
|
||||||
cat: The category to inspect.
|
|
||||||
expected: A list of tuples containing the expected items.
|
def __init__(self, modeltester):
|
||||||
"""
|
modeltester.data_display_may_return_none = True
|
||||||
qtmodeltester.data_display_may_return_none = True
|
self._modeltester = modeltester
|
||||||
qtmodeltester.check(cat)
|
|
||||||
assert cat.rowCount() == len(expected)
|
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 row, items in enumerate(expected):
|
||||||
for col, item in enumerate(items):
|
for col, item in enumerate(items):
|
||||||
assert cat.data(cat.index(row, col)) == item
|
assert self._model.data(self._model.index(row, col)) == item
|
||||||
return validate
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def model_validator(qtmodeltester):
|
||||||
|
return ModelValidator(qtmodeltester)
|
||||||
|
@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import unittest.mock
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from PyQt5.QtCore import QUrl
|
from PyQt5.QtCore import QUrl
|
||||||
@ -115,7 +114,7 @@ def test_clear(qtbot, tmpdir, hist, mocker):
|
|||||||
hist.add_url(QUrl('http://www.qutebrowser.org/'))
|
hist.add_url(QUrl('http://www.qutebrowser.org/'))
|
||||||
|
|
||||||
m = mocker.patch('qutebrowser.browser.history.message.confirm_async',
|
m = mocker.patch('qutebrowser.browser.history.message.confirm_async',
|
||||||
new=unittest.mock.Mock, spec=[])
|
new=mocker.Mock, spec=[])
|
||||||
hist.clear()
|
hist.clear()
|
||||||
assert m.called
|
assert m.called
|
||||||
|
|
||||||
|
@ -45,26 +45,29 @@ from qutebrowser.commands import cmdexc
|
|||||||
[('foo', 'bar'), ('bar', 'foo'), ('bar', 'bar')],
|
[('foo', 'bar'), ('bar', 'foo'), ('bar', 'bar')],
|
||||||
[('foo', 'bar'), ('bar', 'foo')]),
|
[('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."""
|
"""Validate the filtering and sorting results of set_pattern."""
|
||||||
cat = listcategory.ListCategory('Foo', before)
|
cat = listcategory.ListCategory('Foo', before)
|
||||||
|
model_validator.set_model(cat)
|
||||||
cat.set_pattern(pattern)
|
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=[])
|
func = mock.Mock(spec=[])
|
||||||
cat = listcategory.ListCategory('Foo', [('a', 'b'), ('c', 'd')],
|
cat = listcategory.ListCategory('Foo', [('a', 'b'), ('c', 'd')],
|
||||||
delete_func=func)
|
delete_func=func)
|
||||||
|
model_validator.set_model(cat)
|
||||||
idx = cat.index(0, 0)
|
idx = cat.index(0, 0)
|
||||||
cat.delete_cur_item(idx)
|
cat.delete_cur_item(idx)
|
||||||
func.assert_called_once_with(['a', 'b'])
|
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')])
|
cat = listcategory.ListCategory('Foo', [('a', 'b'), ('c', 'd')])
|
||||||
|
model_validator.set_model(cat)
|
||||||
idx = cat.index(0, 0)
|
idx = cat.index(0, 0)
|
||||||
with pytest.raises(cmdexc.CommandError, match="Cannot delete this item."):
|
with pytest.raises(cmdexc.CommandError, match="Cannot delete this item."):
|
||||||
cat.delete_cur_item(idx)
|
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), ('A', 'F', 0), ('C', 'A', 1)],
|
||||||
[('B', 'C', 2), ('C', 'A', 1), ('A', 'F', 0)]),
|
[('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'])
|
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
|
||||||
for row in data:
|
for row in data:
|
||||||
table.insert({'a': row[0], 'b': row[1], 'c': row[2]})
|
table.insert({'a': row[0], 'b': row[1], 'c': row[2]})
|
||||||
cat = sqlcategory.SqlCategory('Foo', filter_fields=['a'], sort_by=sort_by,
|
cat = sqlcategory.SqlCategory('Foo', filter_fields=['a'], sort_by=sort_by,
|
||||||
sort_order=sort_order)
|
sort_order=sort_order)
|
||||||
|
model_validator.set_model(cat)
|
||||||
cat.set_pattern('')
|
cat.set_pattern('')
|
||||||
validate_model(cat, expected)
|
model_validator.validate(expected)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('pattern, filter_cols, before, after', [
|
@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", '', ''), ('a', '', '')],
|
||||||
[("can't touch this", '', '')]),
|
[("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."""
|
"""Validate the filtering and sorting results of set_pattern."""
|
||||||
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
|
table = sql.SqlTable('Foo', ['a', 'b', 'c'])
|
||||||
for row in before:
|
for row in before:
|
||||||
table.insert({'a': row[0], 'b': row[1], 'c': row[2]})
|
table.insert({'a': row[0], 'b': row[1], 'c': row[2]})
|
||||||
filter_fields = [['a', 'b', 'c'][i] for i in filter_cols]
|
filter_fields = [['a', 'b', 'c'][i] for i in filter_cols]
|
||||||
cat = sqlcategory.SqlCategory('Foo', filter_fields=filter_fields)
|
cat = sqlcategory.SqlCategory('Foo', filter_fields=filter_fields)
|
||||||
|
model_validator.set_model(cat)
|
||||||
cat.set_pattern(pattern)
|
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 = 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 = sqlcategory.SqlCategory('Foo', filter_fields=['a'], select='b, c, a')
|
||||||
|
model_validator.set_model(cat)
|
||||||
cat.set_pattern('')
|
cat.set_pattern('')
|
||||||
validate_model(cat, [('bar', 'baz', 'foo')])
|
model_validator.validate([('bar', 'baz', 'foo')])
|
||||||
|
|
||||||
|
|
||||||
def test_delete_cur_item():
|
def test_delete_cur_item():
|
||||||
|
Loading…
Reference in New Issue
Block a user