Skip to content

Commit 1aa356b

Browse files
committed
Add test for discard partial result
1 parent 0220f42 commit 1aa356b

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

tests/test_cursor.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# import pytest
1+
import pytest
22
import MySQLdb.cursors
33
from configdb import connection_factory
44

@@ -186,3 +186,39 @@ def test_mogrify_with_dict_args():
186186

187187
assert mogrified_query == "SELECT 1, 2"
188188
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, 31)],
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

Comments
 (0)