Skip to content

Commit 7427104

Browse files
committed
Add more tests for cursors
1 parent 10bfaec commit 7427104

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/client/test_cursor.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,45 @@
3838
from crate.client.converter import DataType, DefaultTypeConverter
3939

4040

41+
def test_cursor_fetch(mocked_connection):
42+
"""Verify fetchone/fetchmany behaviour"""
43+
cursor = mocked_connection.cursor()
44+
response = {
45+
"col_types": [4, 5],
46+
"cols": ["name", "address"],
47+
"rows": [["foo", "10.10.10.1"], ["bar", "10.10.10.2"]],
48+
"rowcount": 2,
49+
"duration": 123,
50+
}
51+
with mock.patch.object(mocked_connection.client,
52+
'sql',
53+
return_value=response):
54+
cursor.execute("")
55+
assert cursor.fetchone() == ['foo', '10.10.10.1']
56+
assert cursor.fetchmany() == [["bar", "10.10.10.2"], ]
57+
58+
59+
def test_cursor_executemany(mocked_connection):
60+
"""
61+
Verify executemany.
62+
"""
63+
response = {
64+
"col_types": [],
65+
"cols": [],
66+
"duration": 123,
67+
"results": [{'rowcount': 1, 'rowcount:': 1}]
68+
}
69+
with mock.patch.object(mocked_connection.client,
70+
'sql',
71+
return_value=response):
72+
73+
cursor = mocked_connection.cursor()
74+
result = cursor.executemany("some sql", ())
75+
76+
assert isinstance(result, list)
77+
assert response['results'] == result
78+
79+
4180
def test_create_with_timezone_as_datetime_object(mocked_connection):
4281
"""
4382
The cursor can return timezone-aware `datetime` objects when requested.
@@ -141,6 +180,7 @@ def test_execute_with_args(mocked_connection):
141180
cursor.execute(statement, 1)
142181
mocked_connection.client.sql.assert_called_once_with(statement, 1, None)
143182

183+
144184
def test_execute_with_bulk_args(mocked_connection):
145185
"""
146186
Verify that `cursor.execute` is called with the right parameters
@@ -414,6 +454,11 @@ def test_cursor_close(mocked_connection):
414454

415455
assert cursor._closed is True
416456
assert not cursor._result
457+
assert cursor.duration == -1
458+
459+
with pytest.raises(ProgrammingError, match='Connection closed'):
460+
mocked_connection.close()
461+
cursor.execute("")
417462

418463

419464
def test_cursor_closes_access(mocked_connection):

0 commit comments

Comments
 (0)