1 | import sys |
---|
2 | |
---|
3 | try: |
---|
4 | from pymysql.tests import base |
---|
5 | import pymysql.cursors |
---|
6 | except Exception: |
---|
7 | # For local testing from top-level directory, without installing |
---|
8 | sys.path.append('../pymysql') |
---|
9 | from pymysql.tests import base |
---|
10 | import pymysql.cursors |
---|
11 | |
---|
12 | class TestSSCursor(base.PyMySQLTestCase): |
---|
13 | def test_SSCursor(self): |
---|
14 | affected_rows = 18446744073709551615 |
---|
15 | |
---|
16 | conn = self.connections[0] |
---|
17 | data = [ |
---|
18 | ('America', '', 'America/Jamaica'), |
---|
19 | ('America', '', 'America/Los_Angeles'), |
---|
20 | ('America', '', 'America/Lima'), |
---|
21 | ('America', '', 'America/New_York'), |
---|
22 | ('America', '', 'America/Menominee'), |
---|
23 | ('America', '', 'America/Havana'), |
---|
24 | ('America', '', 'America/El_Salvador'), |
---|
25 | ('America', '', 'America/Costa_Rica'), |
---|
26 | ('America', '', 'America/Denver'), |
---|
27 | ('America', '', 'America/Detroit'),] |
---|
28 | |
---|
29 | try: |
---|
30 | cursor = conn.cursor(pymysql.cursors.SSCursor) |
---|
31 | |
---|
32 | # Create table |
---|
33 | cursor.execute(('CREATE TABLE tz_data (' |
---|
34 | 'region VARCHAR(64),' |
---|
35 | 'zone VARCHAR(64),' |
---|
36 | 'name VARCHAR(64))')) |
---|
37 | |
---|
38 | conn.begin() |
---|
39 | # Test INSERT |
---|
40 | for i in data: |
---|
41 | cursor.execute('INSERT INTO tz_data VALUES (%s, %s, %s)', i) |
---|
42 | self.assertEqual(conn.affected_rows(), 1, 'affected_rows does not match') |
---|
43 | conn.commit() |
---|
44 | |
---|
45 | # Test fetchone() |
---|
46 | iter = 0 |
---|
47 | cursor.execute('SELECT * FROM tz_data') |
---|
48 | while True: |
---|
49 | row = cursor.fetchone() |
---|
50 | if row is None: |
---|
51 | break |
---|
52 | iter += 1 |
---|
53 | |
---|
54 | # Test cursor.rowcount |
---|
55 | self.assertEqual(cursor.rowcount, affected_rows, |
---|
56 | 'cursor.rowcount != %s' % (str(affected_rows))) |
---|
57 | |
---|
58 | # Test cursor.rownumber |
---|
59 | self.assertEqual(cursor.rownumber, iter, |
---|
60 | 'cursor.rowcount != %s' % (str(iter))) |
---|
61 | |
---|
62 | # Test row came out the same as it went in |
---|
63 | self.assertEqual((row in data), True, |
---|
64 | 'Row not found in source data') |
---|
65 | |
---|
66 | # Test fetchall |
---|
67 | cursor.execute('SELECT * FROM tz_data') |
---|
68 | self.assertEqual(len(cursor.fetchall()), len(data), |
---|
69 | 'fetchall failed. Number of rows does not match') |
---|
70 | |
---|
71 | # Test fetchmany |
---|
72 | cursor.execute('SELECT * FROM tz_data') |
---|
73 | self.assertEqual(len(cursor.fetchmany(2)), 2, |
---|
74 | 'fetchmany failed. Number of rows does not match') |
---|
75 | |
---|
76 | # So MySQLdb won't throw "Commands out of sync" |
---|
77 | while True: |
---|
78 | res = cursor.fetchone() |
---|
79 | if res is None: |
---|
80 | break |
---|
81 | |
---|
82 | # Test update, affected_rows() |
---|
83 | cursor.execute('UPDATE tz_data SET zone = %s', ['Foo']) |
---|
84 | conn.commit() |
---|
85 | self.assertEqual(cursor.rowcount, len(data), |
---|
86 | 'Update failed. affected_rows != %s' % (str(len(data)))) |
---|
87 | |
---|
88 | # Test executemany |
---|
89 | cursor.executemany('INSERT INTO tz_data VALUES (%s, %s, %s)', data) |
---|
90 | self.assertEqual(cursor.rowcount, len(data), |
---|
91 | 'executemany failed. cursor.rowcount != %s' % (str(len(data)))) |
---|
92 | |
---|
93 | # Test multiple datasets |
---|
94 | cursor.execute('SELECT 1; SELECT 2; SELECT 3') |
---|
95 | self.assertListEqual(list(cursor), [(1, )]) |
---|
96 | self.assertTrue(cursor.nextset()) |
---|
97 | self.assertListEqual(list(cursor), [(2, )]) |
---|
98 | self.assertTrue(cursor.nextset()) |
---|
99 | self.assertListEqual(list(cursor), [(3, )]) |
---|
100 | self.assertFalse(cursor.nextset()) |
---|
101 | |
---|
102 | finally: |
---|
103 | cursor.execute('DROP TABLE tz_data') |
---|
104 | cursor.close() |
---|
105 | |
---|
106 | __all__ = ["TestSSCursor"] |
---|
107 | |
---|
108 | if __name__ == "__main__": |
---|
109 | import unittest |
---|
110 | unittest.main() |
---|