|
38 | 38 | from crate.client.converter import DataType, DefaultTypeConverter |
39 | 39 |
|
40 | 40 |
|
| 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 | + |
41 | 80 | def test_create_with_timezone_as_datetime_object(mocked_connection): |
42 | 81 | """ |
43 | 82 | The cursor can return timezone-aware `datetime` objects when requested. |
@@ -141,6 +180,7 @@ def test_execute_with_args(mocked_connection): |
141 | 180 | cursor.execute(statement, 1) |
142 | 181 | mocked_connection.client.sql.assert_called_once_with(statement, 1, None) |
143 | 182 |
|
| 183 | + |
144 | 184 | def test_execute_with_bulk_args(mocked_connection): |
145 | 185 | """ |
146 | 186 | Verify that `cursor.execute` is called with the right parameters |
@@ -414,6 +454,11 @@ def test_cursor_close(mocked_connection): |
414 | 454 |
|
415 | 455 | assert cursor._closed is True |
416 | 456 | 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("") |
417 | 462 |
|
418 | 463 |
|
419 | 464 | def test_cursor_closes_access(mocked_connection): |
|
0 commit comments