|
1 |
| -# import pytest |
| 1 | +import pytest |
2 | 2 | import MySQLdb.cursors
|
3 | 3 | from configdb import connection_factory
|
4 | 4 |
|
@@ -186,3 +186,39 @@ def test_mogrify_with_dict_args():
|
186 | 186 |
|
187 | 187 | assert mogrified_query == "SELECT 1, 2"
|
188 | 188 | assert mogrified_query == cursor._executed.decode()
|
| 189 | + |
| 190 | + |
| 191 | +# Test that cursor can be used without reading whole resultset. |
| 192 | +@pytest.mark.parametrize("Cursor", [MySQLdb.cursors.Cursor, MySQLdb.cursors.SSCursor]) |
| 193 | +def test_cursor_discard_result(Cursor): |
| 194 | + conn = connect() |
| 195 | + cursor = conn.cursor(Cursor) |
| 196 | + |
| 197 | + cursor.execute( |
| 198 | + """\ |
| 199 | +CREATE TABLE test_cursor_discard_result ( |
| 200 | + id INTEGER PRIMARY KEY AUTO_INCREMENT, |
| 201 | + data VARCHAR(100) |
| 202 | +)""" |
| 203 | + ) |
| 204 | + _tables.append("test_cursor_discard_result") |
| 205 | + |
| 206 | + cursor.executemany( |
| 207 | + "INSERT INTO test_cursor_discard_result (id, data) VALUES (%s, %s)", |
| 208 | + [(i, f"row {i}") for i in range(1, 101)], |
| 209 | + ) |
| 210 | + |
| 211 | + cursor.execute( |
| 212 | + """\ |
| 213 | +SELECT * FROM test_cursor_discard_result WHERE id <= 10; |
| 214 | +SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 11 AND 20; |
| 215 | +SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 21 AND 30; |
| 216 | +""" |
| 217 | + ) |
| 218 | + cursor.nextset() |
| 219 | + assert cursor.fetchone() == (11, "row 11") |
| 220 | + |
| 221 | + cursor.execute( |
| 222 | + "SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 31 AND 40" |
| 223 | + ) |
| 224 | + assert cursor.fetchone() == (31, "row 31") |
0 commit comments