|
1 |
| -import sys |
| 1 | +import pytest |
2 | 2 |
|
3 |
| -try: |
4 |
| - from pymysql.tests import base |
5 |
| - import pymysql.cursors |
6 |
| - from pymysql.constants import CLIENT, ER |
7 |
| -except Exception: |
8 |
| - # For local testing from top-level directory, without installing |
9 |
| - sys.path.append("../pymysql") |
10 |
| - from pymysql.tests import base |
11 |
| - import pymysql.cursors |
12 |
| - from pymysql.constants import CLIENT, ER |
| 3 | +from pymysql.tests import base |
| 4 | +import pymysql.cursors |
| 5 | +from pymysql.constants import CLIENT, ER |
13 | 6 |
|
14 | 7 |
|
15 | 8 | class TestSSCursor(base.PyMySQLTestCase):
|
@@ -122,6 +115,92 @@ def test_SSCursor(self):
|
122 | 115 | cursor.execute("DROP TABLE IF EXISTS tz_data")
|
123 | 116 | cursor.close()
|
124 | 117 |
|
| 118 | + def test_execution_time_limit(self): |
| 119 | + # this method is similarly implemented in test_cursor |
| 120 | + |
| 121 | + conn = self.connect() |
| 122 | + |
| 123 | + # table creation and filling is SSCursor only as it's not provided by self.setUp() |
| 124 | + self.safe_create_table( |
| 125 | + conn, |
| 126 | + "test", |
| 127 | + "create table test (data varchar(10))", |
| 128 | + ) |
| 129 | + with conn.cursor() as cur: |
| 130 | + cur.execute( |
| 131 | + "insert into test (data) values " |
| 132 | + "('row1'), ('row2'), ('row3'), ('row4'), ('row5')" |
| 133 | + ) |
| 134 | + conn.commit() |
| 135 | + |
| 136 | + db_type = self.get_mysql_vendor(conn) |
| 137 | + |
| 138 | + with conn.cursor(pymysql.cursors.SSCursor) as cur: |
| 139 | + # MySQL MAX_EXECUTION_TIME takes ms |
| 140 | + # MariaDB max_statement_time takes seconds as int/float, introduced in 10.1 |
| 141 | + |
| 142 | + # this will sleep 0.01 seconds per row |
| 143 | + if db_type == "mysql": |
| 144 | + sql = ( |
| 145 | + "SELECT /*+ MAX_EXECUTION_TIME(2000) */ data, sleep(0.01) FROM test" |
| 146 | + ) |
| 147 | + else: |
| 148 | + sql = "SET STATEMENT max_statement_time=2 FOR SELECT data, sleep(0.01) FROM test" |
| 149 | + |
| 150 | + cur.execute(sql) |
| 151 | + # unlike Cursor, SSCursor returns a list of tuples here |
| 152 | + self.assertEqual( |
| 153 | + cur.fetchall(), |
| 154 | + [ |
| 155 | + ("row1", 0), |
| 156 | + ("row2", 0), |
| 157 | + ("row3", 0), |
| 158 | + ("row4", 0), |
| 159 | + ("row5", 0), |
| 160 | + ], |
| 161 | + ) |
| 162 | + |
| 163 | + if db_type == "mysql": |
| 164 | + sql = ( |
| 165 | + "SELECT /*+ MAX_EXECUTION_TIME(2000) */ data, sleep(0.01) FROM test" |
| 166 | + ) |
| 167 | + else: |
| 168 | + sql = "SET STATEMENT max_statement_time=2 FOR SELECT data, sleep(0.01) FROM test" |
| 169 | + cur.execute(sql) |
| 170 | + self.assertEqual(cur.fetchone(), ("row1", 0)) |
| 171 | + |
| 172 | + # this discards the previous unfinished query and raises an |
| 173 | + # incomplete unbuffered query warning |
| 174 | + with pytest.warns(UserWarning): |
| 175 | + cur.execute("SELECT 1") |
| 176 | + self.assertEqual(cur.fetchone(), (1,)) |
| 177 | + |
| 178 | + # SSCursor will not read the EOF packet until we try to read |
| 179 | + # another row. Skipping this will raise an incomplete unbuffered |
| 180 | + # query warning in the next cur.execute(). |
| 181 | + self.assertEqual(cur.fetchone(), None) |
| 182 | + |
| 183 | + if db_type == "mysql": |
| 184 | + sql = "SELECT /*+ MAX_EXECUTION_TIME(1) */ data, sleep(1) FROM test" |
| 185 | + else: |
| 186 | + sql = "SET STATEMENT max_statement_time=0.001 FOR SELECT data, sleep(1) FROM test" |
| 187 | + with pytest.raises(pymysql.err.OperationalError) as cm: |
| 188 | + # in an unbuffered cursor the OperationalError may not show up |
| 189 | + # until fetching the entire result |
| 190 | + cur.execute(sql) |
| 191 | + cur.fetchall() |
| 192 | + |
| 193 | + if db_type == "mysql": |
| 194 | + # this constant was only introduced in MySQL 5.7, not sure |
| 195 | + # what was returned before, may have been ER_QUERY_INTERRUPTED |
| 196 | + self.assertEqual(cm.value.args[0], ER.QUERY_TIMEOUT) |
| 197 | + else: |
| 198 | + self.assertEqual(cm.value.args[0], ER.STATEMENT_TIMEOUT) |
| 199 | + |
| 200 | + # connection should still be fine at this point |
| 201 | + cur.execute("SELECT 1") |
| 202 | + self.assertEqual(cur.fetchone(), (1,)) |
| 203 | + |
125 | 204 | def test_warnings(self):
|
126 | 205 | con = self.connect()
|
127 | 206 | cur = con.cursor(pymysql.cursors.SSCursor)
|
|
0 commit comments