From fa1fe63a93169169ee18a60bf59689d21f21e2c8 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 1 Sep 2018 11:43:58 +0200 Subject: [PATCH] Add some more sql.Query tests --- tests/unit/misc/test_sql.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/unit/misc/test_sql.py b/tests/unit/misc/test_sql.py index 8e660ecc7..3479259ee 100644 --- a/tests/unit/misc/test_sql.py +++ b/tests/unit/misc/test_sql.py @@ -252,12 +252,24 @@ class TestSqlQuery: match='Cannot iterate inactive query'): 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): q = sql.Query('SELECT 0 AS col') q.run() result = next(iter(q)) 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): q = sql.Query('SELECT :answer') q.run(answer=42) @@ -268,9 +280,24 @@ class TestSqlQuery: with pytest.raises(sql.SqlError, match='Missing bound values!'): 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): q = sql.Query('SELECT 0 WHERE 0') q.run() with pytest.raises(sql.SqlError, match='No result for single-result query'): 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}