2016-12-23 18:47:36 +01:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2018-02-05 12:19:50 +01:00
|
|
|
# Copyright 2016-2018 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
|
|
|
|
|
2017-10-09 14:18:26 +02:00
|
|
|
from PyQt5.QtSql import QSqlError
|
|
|
|
|
2017-12-15 14:35:07 +01:00
|
|
|
from qutebrowser.misc import sql
|
|
|
|
|
2016-12-23 18:47:36 +01:00
|
|
|
|
2017-02-12 14:39:24 +01:00
|
|
|
pytestmark = pytest.mark.usefixtures('init_sql')
|
2016-12-23 18:47:36 +01:00
|
|
|
|
|
|
|
|
2018-09-01 21:14:13 +02:00
|
|
|
@pytest.mark.parametrize('klass', [sql.SqlEnvironmentError, sql.SqlBugError])
|
|
|
|
def test_sqlerror(klass):
|
2017-10-09 16:08:56 +02:00
|
|
|
text = "Hello World"
|
2018-09-01 21:14:13 +02:00
|
|
|
err = klass(text)
|
2017-10-09 16:08:56 +02:00
|
|
|
assert str(err) == text
|
|
|
|
assert err.text() == text
|
2017-10-09 14:18:26 +02:00
|
|
|
|
|
|
|
|
2018-09-01 21:19:20 +02:00
|
|
|
class TestSqlError:
|
2017-10-09 14:18:26 +02:00
|
|
|
|
2018-09-01 21:14:13 +02:00
|
|
|
@pytest.mark.parametrize('error_code, exception', [
|
2018-09-01 21:31:01 +02:00
|
|
|
(sql.SqliteErrorCode.BUSY, sql.SqlEnvironmentError),
|
|
|
|
(sql.SqliteErrorCode.CONSTRAINT, sql.SqlBugError),
|
2017-10-09 14:18:26 +02:00
|
|
|
])
|
2018-09-01 21:14:13 +02:00
|
|
|
def test_environmental(self, error_code, exception):
|
2017-10-09 14:18:26 +02:00
|
|
|
sql_err = QSqlError("driver text", "db text", QSqlError.UnknownError,
|
|
|
|
error_code)
|
2018-09-01 21:14:13 +02:00
|
|
|
with pytest.raises(exception):
|
|
|
|
sql.raise_sqlite_error("Message", sql_err)
|
2017-10-09 14:18:26 +02:00
|
|
|
|
2018-09-12 10:48:43 +02:00
|
|
|
def test_qtbug_70506(self):
|
|
|
|
"""Test Qt's wrong handling of errors while opening the database.
|
|
|
|
|
|
|
|
Due to https://bugreports.qt.io/browse/QTBUG-70506 we get an error with
|
|
|
|
"out of memory" as string and -1 as error code.
|
|
|
|
"""
|
|
|
|
sql_err = QSqlError("Error opening database",
|
2018-09-12 01:36:50 +02:00
|
|
|
"out of memory",
|
|
|
|
QSqlError.UnknownError,
|
|
|
|
sql.SqliteErrorCode.UNKNOWN)
|
|
|
|
with pytest.raises(sql.SqlEnvironmentError):
|
|
|
|
sql.raise_sqlite_error("Message", sql_err)
|
|
|
|
|
2017-10-09 14:18:26 +02:00
|
|
|
def test_logging(self, caplog):
|
|
|
|
sql_err = QSqlError("driver text", "db text", QSqlError.UnknownError,
|
|
|
|
'23')
|
2018-09-01 21:19:20 +02:00
|
|
|
with pytest.raises(sql.SqlBugError):
|
2018-09-01 21:14:13 +02:00
|
|
|
sql.raise_sqlite_error("Message", sql_err)
|
|
|
|
|
2017-10-09 14:18:26 +02:00
|
|
|
expected = ['SQL error:',
|
|
|
|
'type: UnknownError',
|
|
|
|
'database text: db text',
|
|
|
|
'driver text: driver text',
|
|
|
|
'error code: 23']
|
|
|
|
|
2018-10-24 10:49:26 +02:00
|
|
|
assert caplog.messages == expected
|
2017-10-09 14:18:26 +02:00
|
|
|
|
2018-09-01 21:14:13 +02:00
|
|
|
@pytest.mark.parametrize('klass',
|
2018-09-01 21:19:20 +02:00
|
|
|
[sql.SqlEnvironmentError, sql.SqlBugError])
|
2018-09-01 21:14:13 +02:00
|
|
|
def test_text(self, klass):
|
2017-10-09 16:08:56 +02:00
|
|
|
sql_err = QSqlError("driver text", "db text")
|
2018-09-01 21:14:13 +02:00
|
|
|
err = klass("Message", sql_err)
|
2017-10-09 16:08:56 +02:00
|
|
|
assert err.text() == "db text"
|
|
|
|
|
2017-10-09 14:18:26 +02:00
|
|
|
|
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-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 1, 'lucky': False})
|
2016-12-23 18:47:36 +01:00
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'wan', 'val': 1, 'lucky': False})
|
2017-06-09 03:43:00 +02:00
|
|
|
|
|
|
|
|
2017-06-11 19:57:38 +02:00
|
|
|
def test_insert_replace(qtbot):
|
2017-06-09 03:43:00 +02:00
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'],
|
|
|
|
constraints={'name': 'PRIMARY KEY'})
|
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 1, 'lucky': False}, replace=True)
|
2017-06-09 03:43:00 +02:00
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 11, 'lucky': True}, replace=True)
|
2017-06-09 03:43:00 +02:00
|
|
|
assert list(table) == [('one', 11, True)]
|
2016-12-23 18:47:36 +01:00
|
|
|
|
2018-09-01 21:19:20 +02:00
|
|
|
with pytest.raises(sql.SqlBugError):
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 11, 'lucky': True}, replace=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_insert_batch(qtbot):
|
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
|
|
|
|
|
|
|
with qtbot.waitSignal(table.changed):
|
|
|
|
table.insert_batch({'name': ['one', 'nine', 'thirteen'],
|
|
|
|
'val': [1, 9, 13],
|
|
|
|
'lucky': [False, False, True]})
|
|
|
|
|
|
|
|
assert list(table) == [('one', 1, False),
|
|
|
|
('nine', 9, False),
|
|
|
|
('thirteen', 13, True)]
|
|
|
|
|
|
|
|
|
|
|
|
def test_insert_batch_replace(qtbot):
|
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'],
|
|
|
|
constraints={'name': 'PRIMARY KEY'})
|
|
|
|
|
|
|
|
with qtbot.waitSignal(table.changed):
|
|
|
|
table.insert_batch({'name': ['one', 'nine', 'thirteen'],
|
|
|
|
'val': [1, 9, 13],
|
|
|
|
'lucky': [False, False, True]})
|
|
|
|
|
|
|
|
with qtbot.waitSignal(table.changed):
|
|
|
|
table.insert_batch({'name': ['one', 'nine'],
|
|
|
|
'val': [11, 19],
|
|
|
|
'lucky': [True, True]},
|
2017-06-14 13:15:42 +02:00
|
|
|
replace=True)
|
2017-06-11 19:57:38 +02:00
|
|
|
|
|
|
|
assert list(table) == [('thirteen', 13, True),
|
|
|
|
('one', 11, True),
|
|
|
|
('nine', 19, True)]
|
|
|
|
|
2018-09-01 21:19:20 +02:00
|
|
|
with pytest.raises(sql.SqlBugError):
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert_batch({'name': ['one', 'nine'],
|
|
|
|
'val': [11, 19],
|
|
|
|
'lucky': [True, True]})
|
|
|
|
|
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-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 1, 'lucky': False})
|
|
|
|
table.insert({'name': 'nine', 'val': 9, 'lucky': False})
|
|
|
|
table.insert({'name': 'thirteen', 'val': 13, 'lucky': 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', [
|
2017-06-09 03:43:00 +02:00
|
|
|
([{"a": 2, "b": 5}, {"a": 1, "b": 6}, {"a": 3, "b": 4}], 'a', 'asc', 5,
|
2017-12-15 19:08:15 +01:00
|
|
|
[(1, 6), (2, 5), (3, 4)]),
|
2017-06-09 03:43:00 +02:00
|
|
|
([{"a": 2, "b": 5}, {"a": 1, "b": 6}, {"a": 3, "b": 4}], 'a', 'desc', 3,
|
2017-12-15 19:08:15 +01:00
|
|
|
[(3, 4), (2, 5), (1, 6)]),
|
2017-06-09 03:43:00 +02:00
|
|
|
([{"a": 2, "b": 5}, {"a": 1, "b": 6}, {"a": 3, "b": 4}], 'b', 'desc', 2,
|
2017-12-15 19:08:15 +01:00
|
|
|
[(1, 6), (2, 5)]),
|
2017-06-09 03:43:00 +02:00
|
|
|
([{"a": 2, "b": 5}, {"a": 1, "b": 6}, {"a": 3, "b": 4}], 'a', 'asc', -1,
|
2017-12-15 19:08:15 +01:00
|
|
|
[(1, 6), (2, 5), (3, 4)]),
|
2017-03-26 22:52:10 +02:00
|
|
|
])
|
|
|
|
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:
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert(row)
|
2017-03-26 22:52:10 +02:00
|
|
|
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-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 1, 'lucky': False})
|
|
|
|
table.insert({'name': 'nine', 'val': 9, 'lucky': False})
|
|
|
|
table.insert({'name': 'thirteen', 'val': 13, 'lucky': True})
|
2016-12-23 18:47:36 +01:00
|
|
|
with pytest.raises(KeyError):
|
2017-06-30 02:43:42 +02:00
|
|
|
table.delete('name', 'nope')
|
2016-12-23 18:47:36 +01:00
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-06-30 02:43:42 +02:00
|
|
|
table.delete('name', 'thirteen')
|
2016-12-23 18:47:36 +01:00
|
|
|
assert list(table) == [('one', 1, False), ('nine', 9, False)]
|
|
|
|
with qtbot.waitSignal(table.changed):
|
2017-06-30 02:43:42 +02:00
|
|
|
table.delete('lucky', False)
|
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-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 1, 'lucky': False})
|
2016-12-23 18:47:36 +01:00
|
|
|
assert len(table) == 1
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'nine', 'val': 9, 'lucky': False})
|
2016-12-23 18:47:36 +01:00
|
|
|
assert len(table) == 2
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'thirteen', 'val': 13, 'lucky': True})
|
2016-12-23 18:47:36 +01:00
|
|
|
assert len(table) == 3
|
|
|
|
|
|
|
|
|
2017-04-08 23:18:20 +02:00
|
|
|
def test_contains():
|
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 1, 'lucky': False})
|
|
|
|
table.insert({'name': 'nine', 'val': 9, 'lucky': False})
|
|
|
|
table.insert({'name': 'thirteen', 'val': 13, 'lucky': True})
|
2017-06-07 03:48:58 +02:00
|
|
|
|
|
|
|
name_query = table.contains_query('name')
|
|
|
|
val_query = table.contains_query('val')
|
|
|
|
lucky_query = table.contains_query('lucky')
|
|
|
|
|
2017-06-09 03:43:00 +02:00
|
|
|
assert name_query.run(val='one').value()
|
|
|
|
assert name_query.run(val='thirteen').value()
|
|
|
|
assert val_query.run(val=9).value()
|
|
|
|
assert lucky_query.run(val=False).value()
|
|
|
|
assert lucky_query.run(val=True).value()
|
|
|
|
assert not name_query.run(val='oone').value()
|
|
|
|
assert not name_query.run(val=1).value()
|
|
|
|
assert not name_query.run(val='*').value()
|
|
|
|
assert not val_query.run(val=10).value()
|
2017-04-08 23:18:20 +02:00
|
|
|
|
|
|
|
|
2016-12-23 18:47:36 +01:00
|
|
|
def test_delete_all(qtbot):
|
2017-04-04 14:27:42 +02:00
|
|
|
table = sql.SqlTable('Foo', ['name', 'val', 'lucky'])
|
2017-06-11 19:57:38 +02:00
|
|
|
table.insert({'name': 'one', 'val': 1, 'lucky': False})
|
|
|
|
table.insert({'name': 'nine', 'val': 9, 'lucky': False})
|
|
|
|
table.insert({'name': 'thirteen', 'val': 13, 'lucky': 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)
|
2018-08-31 23:31:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestSqlQuery:
|
|
|
|
|
|
|
|
def test_prepare_error(self):
|
2018-09-01 21:19:20 +02:00
|
|
|
with pytest.raises(sql.SqlBugError) as excinfo:
|
2018-08-31 23:31:39 +02:00
|
|
|
sql.Query('invalid')
|
2018-09-01 21:14:13 +02:00
|
|
|
|
|
|
|
expected = ('Failed to prepare query "invalid": "near "invalid": '
|
|
|
|
'syntax error Unable to execute statement"')
|
|
|
|
assert str(excinfo.value) == expected
|
2018-08-31 23:31:39 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('forward_only', [True, False])
|
|
|
|
def test_forward_only(self, forward_only):
|
|
|
|
q = sql.Query('SELECT 0 WHERE 0', forward_only=forward_only)
|
2018-09-01 11:17:14 +02:00
|
|
|
assert q.query.isForwardOnly() == forward_only
|
2018-08-31 23:31:39 +02:00
|
|
|
|
|
|
|
def test_iter_inactive(self):
|
|
|
|
q = sql.Query('SELECT 0')
|
2018-09-01 21:14:13 +02:00
|
|
|
with pytest.raises(sql.SqlBugError,
|
2018-08-31 23:31:39 +02:00
|
|
|
match='Cannot iterate inactive query'):
|
|
|
|
next(iter(q))
|
|
|
|
|
2018-09-01 11:43:58 +02:00
|
|
|
def test_iter_empty(self):
|
|
|
|
q = sql.Query('SELECT 0 AS col WHERE 0')
|
|
|
|
q.run()
|
|
|
|
with pytest.raises(StopIteration):
|
|
|
|
next(iter(q))
|
|
|
|
|
2018-08-31 23:31:39 +02:00
|
|
|
def test_iter(self):
|
|
|
|
q = sql.Query('SELECT 0 AS col')
|
|
|
|
q.run()
|
|
|
|
result = next(iter(q))
|
|
|
|
assert result.col == 0
|
|
|
|
|
2018-09-01 11:43:58 +02:00
|
|
|
def test_iter_multiple(self):
|
|
|
|
q = sql.Query('VALUES (1), (2), (3);')
|
|
|
|
res = list(q.run())
|
|
|
|
assert len(res) == 3
|
|
|
|
assert res[0].column1 == 1
|
|
|
|
|
2018-08-31 23:31:39 +02:00
|
|
|
def test_run_binding(self):
|
|
|
|
q = sql.Query('SELECT :answer')
|
|
|
|
q.run(answer=42)
|
|
|
|
assert q.value() == 42
|
|
|
|
|
2018-08-31 23:35:00 +02:00
|
|
|
def test_run_missing_binding(self):
|
|
|
|
q = sql.Query('SELECT :answer')
|
2018-09-01 21:14:13 +02:00
|
|
|
with pytest.raises(sql.SqlBugError, match='Missing bound values!'):
|
2018-08-31 23:35:00 +02:00
|
|
|
q.run()
|
|
|
|
|
2018-09-01 11:43:58 +02:00
|
|
|
def test_run_batch(self):
|
|
|
|
q = sql.Query('SELECT :answer')
|
|
|
|
q.run_batch(values={'answer': [42]})
|
|
|
|
assert q.value() == 42
|
|
|
|
|
2018-09-01 11:44:46 +02:00
|
|
|
def test_run_batch_missing_binding(self):
|
|
|
|
q = sql.Query('SELECT :answer')
|
2018-09-01 21:14:13 +02:00
|
|
|
with pytest.raises(sql.SqlBugError, match='Missing bound values!'):
|
2018-09-01 11:44:46 +02:00
|
|
|
q.run_batch(values={})
|
|
|
|
|
2018-08-31 23:31:39 +02:00
|
|
|
def test_value_missing(self):
|
|
|
|
q = sql.Query('SELECT 0 WHERE 0')
|
|
|
|
q.run()
|
2018-09-01 21:14:13 +02:00
|
|
|
with pytest.raises(sql.SqlBugError,
|
2018-08-31 23:31:39 +02:00
|
|
|
match='No result for single-result query'):
|
|
|
|
q.value()
|
2018-09-01 11:43:58 +02:00
|
|
|
|
|
|
|
def test_num_rows_affected(self):
|
|
|
|
q = sql.Query('SELECT 0')
|
|
|
|
q.run()
|
|
|
|
assert q.rows_affected() == 0
|
|
|
|
|
|
|
|
def test_bound_values(self):
|
|
|
|
q = sql.Query('SELECT :answer')
|
|
|
|
q.run(answer=42)
|
|
|
|
assert q.bound_values() == {':answer': 42}
|