Add some more sql.Query tests

This commit is contained in:
Florian Bruhin 2018-09-01 11:43:58 +02:00
parent 0e284944e7
commit fa1fe63a93

View File

@ -252,12 +252,24 @@ class TestSqlQuery:
match='Cannot iterate inactive query'): match='Cannot iterate inactive query'):
next(iter(q)) next(iter(q))
def test_iter_empty(self):
q = sql.Query('SELECT 0 AS col WHERE 0')
q.run()
with pytest.raises(StopIteration):
next(iter(q))
def test_iter(self): def test_iter(self):
q = sql.Query('SELECT 0 AS col') q = sql.Query('SELECT 0 AS col')
q.run() q.run()
result = next(iter(q)) result = next(iter(q))
assert result.col == 0 assert result.col == 0
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
def test_run_binding(self): def test_run_binding(self):
q = sql.Query('SELECT :answer') q = sql.Query('SELECT :answer')
q.run(answer=42) q.run(answer=42)
@ -268,9 +280,24 @@ class TestSqlQuery:
with pytest.raises(sql.SqlError, match='Missing bound values!'): with pytest.raises(sql.SqlError, match='Missing bound values!'):
q.run() q.run()
def test_run_batch(self):
q = sql.Query('SELECT :answer')
q.run_batch(values={'answer': [42]})
assert q.value() == 42
def test_value_missing(self): def test_value_missing(self):
q = sql.Query('SELECT 0 WHERE 0') q = sql.Query('SELECT 0 WHERE 0')
q.run() q.run()
with pytest.raises(sql.SqlError, with pytest.raises(sql.SqlError,
match='No result for single-result query'): match='No result for single-result query'):
q.value() q.value()
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}