Skip to content

Commit

Permalink
Add test for discard partial result
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed May 16, 2023
1 parent 0220f42 commit 23d77df
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion tests/test_cursor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# import pytest
import pytest
import MySQLdb.cursors
from configdb import connection_factory

Expand Down Expand Up @@ -186,3 +186,39 @@ def test_mogrify_with_dict_args():

assert mogrified_query == "SELECT 1, 2"
assert mogrified_query == cursor._executed.decode()


# Test that cursor can be used without reading whole resultset.
@pytest.mark.parametrize("Cursor", [MySQLdb.cursors.Cursor, MySQLdb.cursors.SSCursor])
def test_cursor_discard_result(Cursor):
conn = connect()
cursor = conn.cursor(Cursor)

cursor.execute(
"""\
CREAATE TABLE test_cursor_discard_result (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
data VARCHAR(100)
)"""
)
_tables.append("test_cursor_discard_result")

cursor.executemany(
"INSERT INTO test_cursor_discard_result (id, data) VALUES (%s, %s)",
[(i, f"row {i}") for i in range(1, 31)],
)

cursor.execute(
"""\
SELECT * FROM test_cursor_discard_result WHERE id <= 10;
SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 11 AND 20;
SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 21 AND 30;
"""
)
cursor.nextset()
assert cursor.fetchone() == (11, "row 11")

cursor.execute(
"SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 31 AND 40"
)
assert cursor.fetchone() == (31, "row 31")

0 comments on commit 23d77df

Please sign in to comment.