2016-12-23 18:47:36 +01:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2017-02-21 14:27:52 +01:00
|
|
|
# Copyright 2016-2017 Ryan Roden-Corrent (rcorre) <ryan@rcorre.net>
|
2016-12-23 18:47:36 +01:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Test the SQL API."""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from qutebrowser.misc import sql
|
|
|
|
|
|
|
|
|
2017-02-12 14:39:24 +01:00
|
|
|
pytestmark = pytest.mark.usefixtures('init_sql')
|
2016-12-23 18:47:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_init():
|
2017-04-04 14:27:42 +02:00
|
|
|
sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2017-03-26 22:52:10 +02:00
|
|
|
# should not error if table already exists
|
2017-04-04 14:27:42 +02:00
|
|
|
sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2016-12-23 18:47:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_insert(qtbot):
|
2017-04-04 14:27:42 +02:00
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2016-12-23 18:47:36 +01:00
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['one', 1, False])
|
2016-12-23 18:47:36 +01:00
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['wan', 1, False])
|
2016-12-23 18:47:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_iter():
|
2017-04-04 14:27:42 +02:00
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['one', 1, False])
|
|
|
|
table.insert(['nine', 9, False])
|
|
|
|
table.insert(['thirteen', 13, True])
|
2016-12-23 18:47:36 +01:00
|
|
|
assert list(table) == [('one', 1, False),
|
|
|
|
('nine', 9, False),
|
|
|
|
('thirteen', 13, True)]
|
|
|
|
|
|
|
|
|
2017-03-26 22:52:10 +02:00
|
|
|
@pytest.mark.parametrize('rows, sort_by, sort_order, limit, result', [
|
|
|
|
([[2, 5], [1, 6], [3, 4]], 'a', 'asc', 5, [(1, 6), (2, 5), (3, 4)]),
|
|
|
|
([[2, 5], [1, 6], [3, 4]], 'a', 'desc', 3, [(3, 4), (2, 5), (1, 6)]),
|
|
|
|
([[2, 5], [1, 6], [3, 4]], 'b', 'desc', 2, [(1, 6), (2, 5)])
|
|
|
|
])
|
|
|
|
def test_select(rows, sort_by, sort_order, limit, result):
|
2017-04-04 14:27:42 +02:00
|
|
|
table = sql.SqlTable('Foo', ['a', 'b'])
|
2017-03-26 22:52:10 +02:00
|
|
|
for row in rows:
|
|
|
|
table.insert(row)
|
|
|
|
assert list(table.select(sort_by, sort_order, limit)) == result
|
|
|
|
|
|
|
|
|
2016-12-23 18:47:36 +01:00
|
|
|
def test_delete(qtbot):
|
2017-04-04 14:27:42 +02:00
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['one', 1, False])
|
|
|
|
table.insert(['nine', 9, False])
|
|
|
|
table.insert(['thirteen', 13, True])
|
2016-12-23 18:47:36 +01:00
|
|
|
with pytest.raises(KeyError):
|
2017-04-04 14:27:42 +02:00
|
|
|
table.delete('nope', 'name')
|
2016-12-23 18:47:36 +01:00
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-04-04 14:27:42 +02:00
|
|
|
table.delete('thirteen', 'name')
|
2016-12-23 18:47:36 +01:00
|
|
|
assert list(table) == [('one', 1, False), ('nine', 9, False)]
|
|
|
|
with qtbot.waitSignal(table.changed):
|
|
|
|
table.delete(False, field='lucky')
|
2017-02-13 00:14:46 +01:00
|
|
|
assert not list(table)
|
2016-12-23 18:47:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_len():
|
2017-04-04 14:27:42 +02:00
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2016-12-23 18:47:36 +01:00
|
|
|
assert len(table) == 0
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['one', 1, False])
|
2016-12-23 18:47:36 +01:00
|
|
|
assert len(table) == 1
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['nine', 9, False])
|
2016-12-23 18:47:36 +01:00
|
|
|
assert len(table) == 2
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['thirteen', 13, True])
|
2016-12-23 18:47:36 +01:00
|
|
|
assert len(table) == 3
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_all(qtbot):
|
2017-04-04 14:27:42 +02:00
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2017-02-14 14:59:01 +01:00
|
|
|
table.insert(['one', 1, False])
|
|
|
|
table.insert(['nine', 9, False])
|
|
|
|
table.insert(['thirteen', 13, True])
|
2016-12-23 18:47:36 +01:00
|
|
|
with qtbot.waitSignal(table.changed):
|
|
|
|
table.delete_all()
|
|
|
|
assert list(table) == []
|
2017-02-20 14:33:38 +01:00
|
|
|
|
2017-02-20 18:15:06 +01:00
|
|
|
|
2017-02-20 14:33:38 +01:00
|
|
|
def test_version():
|
|
|
|
assert isinstance(sql.version(), str)
|