1 | import warnings |
---|
2 | |
---|
3 | from pymysql.tests import base |
---|
4 | import pymysql.cursors |
---|
5 | |
---|
6 | class CursorTest(base.PyMySQLTestCase): |
---|
7 | def setUp(self): |
---|
8 | super(CursorTest, self).setUp() |
---|
9 | |
---|
10 | conn = self.connections[0] |
---|
11 | self.safe_create_table( |
---|
12 | conn, |
---|
13 | "test", "create table test (data varchar(10))", |
---|
14 | ) |
---|
15 | cursor = conn.cursor() |
---|
16 | cursor.execute( |
---|
17 | "insert into test (data) values " |
---|
18 | "('row1'), ('row2'), ('row3'), ('row4'), ('row5')") |
---|
19 | cursor.close() |
---|
20 | self.test_connection = pymysql.connect(**self.databases[0]) |
---|
21 | self.addCleanup(self.test_connection.close) |
---|
22 | |
---|
23 | def test_cleanup_rows_unbuffered(self): |
---|
24 | conn = self.test_connection |
---|
25 | cursor = conn.cursor(pymysql.cursors.SSCursor) |
---|
26 | |
---|
27 | cursor.execute("select * from test as t1, test as t2") |
---|
28 | for counter, row in enumerate(cursor): |
---|
29 | if counter > 10: |
---|
30 | break |
---|
31 | |
---|
32 | del cursor |
---|
33 | self.safe_gc_collect() |
---|
34 | |
---|
35 | c2 = conn.cursor() |
---|
36 | |
---|
37 | c2.execute("select 1") |
---|
38 | self.assertEqual(c2.fetchone(), (1,)) |
---|
39 | self.assertIsNone(c2.fetchone()) |
---|
40 | |
---|
41 | def test_cleanup_rows_buffered(self): |
---|
42 | conn = self.test_connection |
---|
43 | cursor = conn.cursor(pymysql.cursors.Cursor) |
---|
44 | |
---|
45 | cursor.execute("select * from test as t1, test as t2") |
---|
46 | for counter, row in enumerate(cursor): |
---|
47 | if counter > 10: |
---|
48 | break |
---|
49 | |
---|
50 | del cursor |
---|
51 | self.safe_gc_collect() |
---|
52 | |
---|
53 | c2 = conn.cursor() |
---|
54 | |
---|
55 | c2.execute("select 1") |
---|
56 | |
---|
57 | self.assertEqual( |
---|
58 | c2.fetchone(), (1,) |
---|
59 | ) |
---|
60 | self.assertIsNone(c2.fetchone()) |
---|
61 | |
---|
62 | def test_executemany(self): |
---|
63 | conn = self.test_connection |
---|
64 | cursor = conn.cursor(pymysql.cursors.Cursor) |
---|
65 | |
---|
66 | m = pymysql.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%s, %s)") |
---|
67 | self.assertIsNotNone(m, 'error parse %s') |
---|
68 | self.assertEqual(m.group(3), '', 'group 3 not blank, bug in RE_INSERT_VALUES?') |
---|
69 | |
---|
70 | m = pymysql.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%(id)s, %(name)s)") |
---|
71 | self.assertIsNotNone(m, 'error parse %(name)s') |
---|
72 | self.assertEqual(m.group(3), '', 'group 3 not blank, bug in RE_INSERT_VALUES?') |
---|
73 | |
---|
74 | m = pymysql.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s)") |
---|
75 | self.assertIsNotNone(m, 'error parse %(id_name)s') |
---|
76 | self.assertEqual(m.group(3), '', 'group 3 not blank, bug in RE_INSERT_VALUES?') |
---|
77 | |
---|
78 | m = pymysql.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s) ON duplicate update") |
---|
79 | self.assertIsNotNone(m, 'error parse %(id_name)s') |
---|
80 | self.assertEqual(m.group(3), ' ON duplicate update', 'group 3 not ON duplicate update, bug in RE_INSERT_VALUES?') |
---|
81 | |
---|
82 | # cursor._executed must bee "insert into test (data) values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)" |
---|
83 | # list args |
---|
84 | data = range(10) |
---|
85 | cursor.executemany("insert into test (data) values (%s)", data) |
---|
86 | self.assertTrue(cursor._executed.endswith(b",(7),(8),(9)"), 'execute many with %s not in one query') |
---|
87 | |
---|
88 | # dict args |
---|
89 | data_dict = [{'data': i} for i in range(10)] |
---|
90 | cursor.executemany("insert into test (data) values (%(data)s)", data_dict) |
---|
91 | self.assertTrue(cursor._executed.endswith(b",(7),(8),(9)"), 'execute many with %(data)s not in one query') |
---|
92 | |
---|
93 | # %% in column set |
---|
94 | cursor.execute("""\ |
---|
95 | CREATE TABLE percent_test ( |
---|
96 | `A%` INTEGER, |
---|
97 | `B%` INTEGER)""") |
---|
98 | try: |
---|
99 | q = "INSERT INTO percent_test (`A%%`, `B%%`) VALUES (%s, %s)" |
---|
100 | self.assertIsNotNone(pymysql.cursors.RE_INSERT_VALUES.match(q)) |
---|
101 | cursor.executemany(q, [(3, 4), (5, 6)]) |
---|
102 | self.assertTrue(cursor._executed.endswith(b"(3, 4),(5, 6)"), "executemany with %% not in one query") |
---|
103 | finally: |
---|
104 | cursor.execute("DROP TABLE IF EXISTS percent_test") |
---|