33from MySQLdb .constants import ER
44from configdb import connection_factory
55
6-
76_conns = []
87_tables = []
98
@@ -57,9 +56,9 @@ def test_executemany():
5756 "INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s) ON duplicate update"
5857 )
5958 assert m is not None , "error parse %(id_name)s"
60- assert m . group ( 3 ) == " ON duplicate update" , (
61- " group 3 not ON duplicate update, bug in RE_INSERT_VALUES? "
62- )
59+ assert (
60+ m . group ( 3 ) == " ON duplicate update"
61+ ), "group 3 not ON duplicate update, bug in RE_INSERT_VALUES?"
6362
6463 # https://github.com/PyMySQL/mysqlclient-python/issues/178
6564 m = MySQLdb .cursors .RE_INSERT_VALUES .match (
@@ -75,31 +74,29 @@ def test_executemany():
7574 # list args
7675 data = [(i ,) for i in range (10 )]
7776 cursor .executemany ("insert into test (data) values (%s)" , data )
78- assert cursor ._executed .endswith (b",(7),(8),(9)" ), (
79- "execute many with %s not in one query "
80- )
77+ assert cursor ._executed .endswith (
78+ b",(7),(8),(9) "
79+ ), "execute many with %s not in one query"
8180
8281 # dict args
8382 data_dict = [{"data" : i } for i in range (10 )]
8483 cursor .executemany ("insert into test (data) values (%(data)s)" , data_dict )
85- assert cursor ._executed .endswith (b",(7),(8),(9)" ), (
86- "execute many with %(data)s not in one query "
87- )
84+ assert cursor ._executed .endswith (
85+ b",(7),(8),(9) "
86+ ), "execute many with %(data)s not in one query"
8887
8988 # %% in column set
90- cursor .execute (
91- """\
89+ cursor .execute ("""\
9290 CREATE TABLE percent_test (
9391 `A%` INTEGER,
94- `B%` INTEGER)"""
95- )
92+ `B%` INTEGER)""" )
9693 try :
9794 q = "INSERT INTO percent_test (`A%%`, `B%%`) VALUES (%s, %s)"
9895 assert MySQLdb .cursors .RE_INSERT_VALUES .match (q ) is not None
9996 cursor .executemany (q , [(3 , 4 ), (5 , 6 )])
100- assert cursor ._executed .endswith (b"(3, 4),(5, 6)" ), (
101- "executemany with %% not in one query "
102- )
97+ assert cursor ._executed .endswith (
98+ b"(3, 4),(5, 6) "
99+ ), "executemany with %% not in one query"
103100 finally :
104101 cursor .execute ("DROP TABLE IF EXISTS percent_test" )
105102
@@ -195,27 +192,23 @@ def test_cursor_discard_result(Cursor):
195192 conn = connect ()
196193 cursor = conn .cursor (Cursor )
197194
198- cursor .execute (
199- """\
195+ cursor .execute ("""\
200196 CREATE TABLE test_cursor_discard_result (
201197 id INTEGER PRIMARY KEY AUTO_INCREMENT,
202198 data VARCHAR(100)
203- )"""
204- )
199+ )""" )
205200 _tables .append ("test_cursor_discard_result" )
206201
207202 cursor .executemany (
208203 "INSERT INTO test_cursor_discard_result (id, data) VALUES (%s, %s)" ,
209204 [(i , f"row { i } " ) for i in range (1 , 101 )],
210205 )
211206
212- cursor .execute (
213- """\
207+ cursor .execute ("""\
214208 SELECT * FROM test_cursor_discard_result WHERE id <= 10;
215209SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 11 AND 20;
216210SELECT * FROM test_cursor_discard_result WHERE id BETWEEN 21 AND 30;
217- """
218- )
211+ """ )
219212 cursor .nextset ()
220213 assert cursor .fetchone () == (11 , "row 11" )
221214
@@ -231,14 +224,12 @@ def test_binary_prefix():
231224 cursor = conn .cursor ()
232225
233226 cursor .execute ("DROP TABLE IF EXISTS test_binary_prefix" )
234- cursor .execute (
235- """\
227+ cursor .execute ("""\
236228 CREATE TABLE test_binary_prefix (
237229 id INTEGER NOT NULL AUTO_INCREMENT,
238230 json JSON NOT NULL,
239231 PRIMARY KEY (id)
240- ) CHARSET=utf8mb4"""
241- )
232+ ) CHARSET=utf8mb4""" )
242233
243234 cursor .executemany (
244235 "INSERT INTO test_binary_prefix (id, json) VALUES (%(id)s, %(json)s)" ,
@@ -284,3 +275,27 @@ def test_sscursor_warning_count():
284275 rows = cursor .fetchmany (2 )
285276 assert len (rows ) == 1
286277 assert cursor .warning_count == 1
278+
279+
280+ @pytest .mark .parametrize ("Cursor" , [MySQLdb .cursors .Cursor , MySQLdb .cursors .SSCursor ])
281+ def test_cursor_is_iterator (Cursor ):
282+ conn = connect ()
283+ cursor = conn .cursor (Cursor )
284+
285+ cursor .execute ("DROP TABLE IF EXISTS test_cursor_is_iterator" )
286+ cursor .execute (
287+ "CREATE TABLE test_cursor_is_iterator (id INT PRIMARY KEY, name VARCHAR(20))"
288+ )
289+ _tables .append ("test_cursor_is_iterator" )
290+ cursor .executemany (
291+ "INSERT INTO test_cursor_is_iterator (id, name) VALUES (%s, %s)" ,
292+ [(1 , "a" ), (2 , "b" ), (3 , "c" )],
293+ )
294+
295+ cursor .execute ("SELECT name FROM test_cursor_is_iterator ORDER BY id" )
296+ assert iter (cursor ) is cursor
297+ assert next (cursor ) == ("a" ,)
298+ assert next (cursor ) == ("b" ,)
299+ assert next (cursor ) == ("c" ,)
300+ with pytest .raises (StopIteration ):
301+ next (cursor )
0 commit comments