10000 Fix failing tests (#643) · ammogcoder/PyMySQL@9f29c16 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f29c16

Browse files
authored
Fix failing tests (PyMySQL#643)
1 parent 9dde50a commit 9f29c16

File tree

4 files changed

+74
-49
lines changed

4 files changed

+74
-49
lines changed

pymysql/tests/base.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,33 @@ def mysql_server_is(self, conn, version_tuple):
4040
)
4141
return server_version_tuple >= version_tuple
4242

43-
def setUp(self):
44-
self.connections = []
45-
for params in self.databases:
46-
self.connections.append(pymysql.connect(**params))
47-
self.addCleanup(self._teardown_connections)
43+
_connections = None
44+
45+
@property
46+
def connections(self):
47+
if self._connections is None:
48+
self._connections = []
49+
for params in self.databases:
50+
self._connections.append(pymysql.connect(**params))
51+
self.addCleanup(self._teardown_connections)
52+
return self._connections
53+
54+
def connect(self, **params):
55+
p = self.databases[0].copy()
56+
p.update(params)
57+
conn = pymysql.connect(**p)
58+
@self.addCleanup
59+
def teardown():
60+
if conn.open:
61+
conn.close()
62+
return conn
4863

4964
def _teardown_connections(self):
50-
for connection in self.connections:
51-
connection.close()
65+
if self._connections:
66+
for connection in self._connections:
67+
if connection.open:
68+
connection.close()
69+
self._connections = None
5270

5371
def safe_create_table(self, connection, tablename, ddl, cleanup=True):
5472
"""create a table.

pymysql/tests/test_SSCursor.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
try:
44
from pymysql.tests import base
55
import pymysql.cursors
6+
from pymysql.constants import CLIENT
67
except Exception:
78
# For local testing from top-level directory, without installing
89
sys.path.append('../pymysql')
910
from pymysql.tests import base
1011
import pymysql.cursors
12+
from pymysql.constants import CLIENT
1113

1214
class TestSSCursor(base.PyMySQLTestCase):
1315
def test_SSCursor(self):
1416
affected_rows = 18446744073709551615
1517

16-
conn = self.connections[0]
18+
conn = self.connect(client_flag=CLIENT.MULTI_STATEMENTS)
1719
data = [
1820
('America', '', 'America/Jamaica'),
1921
('America', '', 'America/Los_Angeles'),
@@ -30,10 +32,10 @@ def test_SSCursor(self):
3032
cursor = conn.cursor(pymysql.cursors.SSCursor)
3133

3234
# Create table
33-
cursor.execute(('CREATE TABLE tz_data ('
35+
cursor.execute('CREATE TABLE tz_data ('
3436
'region VARCHAR(64),'
3537
'zone VARCHAR(64),'
36-
'name VARCHAR(64))'))
38+
'name VARCHAR(64))')
3739

3840
conn.begin()
3941
# Test INSERT
@@ -100,7 +102,7 @@ def test_SSCursor(self):
100102
self.assertFalse(cursor.nextset())
101103

102104
finally:
103-
cursor.execute('DROP TABLE tz_data')
105+
cursor.execute('DROP TABLE IF EXISTS tz_data')
104106
cursor.close()
105107

106108
__all__ = ["TestSSCursor"]

pymysql/tests/test_connection.py

+29-28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pymysql
66
from pymysql.tests import base
77
from pymysql._compat import text_type
8+
from pymysql.constants import CLIENT
89

910

1011
class TempUser:
@@ -411,7 +412,7 @@ def test_connection_gone_away(self):
411412
http://dev.mysql.com/doc/refman/5.0/en/gone-away.html
412413
http://dev.mysql.com/doc/refman/5.0/en/error-messages-client.html#error_cr_server_gone_error
413414
"""
414-
con = self.connections[0]
415+
con = self.connect()
415416
cur = con.cursor()
416417
cur.execute("SET wait_timeout=1")
417418
time.sleep(2)
@@ -422,10 +423,9 @@ def test_connection_gone_away(self):
422423
self.assertIn(cm.exception.args[0], (2006, 2013))
423424

424425
def test_init_command(self):
425-
conn = pymysql.connect(
426+
conn = self.connect(
426427
init_command='SELECT "bar"; SELECT "baz"',
427-
**self.databases[0]
428-
)
428+
client_flag=CLIENT.MULTI_STATEMENTS)
429429
c = conn.cursor()
430430
c.execute('select "foobar";')
431431
self.assertEqual(('foobar',), c.fetchone())
@@ -434,22 +434,21 @@ def test_init_command(self):
434434
conn.ping(reconnect=False)
435435

436436
def test_read_default_group(self):
437-
conn = pymysql.connect(
437+
conn = self.connect(
438438
read_default_group='client',
439-
**self.databases[0]
440439
)
441440
self.assertTrue(conn.open)
442441

443442
def test_context(self):
444443
with self.assertRaises(ValueError):
445-
c = pymysql.connect(**self.databases[0])
444+
c = self.connect()
446445
with c as cur:
447446
cur.execute('create table test ( a int )')
448447
c.begin()
449448
cur.execute('insert into test values ((1))')
450449
raise ValueError('pseudo abort')
451450
c.commit()
452-
c = pymysql.connect(**self.databases[0])
451+
c = self.connect()
453452
with c as cur:
454453
cur.execute('select count(*) from test')
455454
self.assertEqual(0, cur.fetchone()[0])
@@ -460,31 +459,31 @@ def test_context(self):
460459
cur.execute('drop table test')
461460

462461
def test_set_charset(self):
463-
c = pymysql.connect(**self.databases[0])
462+
c = self.connect()
464463
c.set_charset('utf8')
465464
# TODO validate setting here
466465

467466
def test_defer_connect(self):
468467
import socket
469-
for db in self.databases:
470-
d = db.copy()
468+
469+
d = self.databases[0].copy()
470+
try:
471+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
472+
sock.connect(d['unix_socket'])
473+
except KeyError:
474+
sock = socket.create_connection(
475+
(d.get('host', 'localhost'), d.get('port', 3306)))
476+
for k in ['unix_socket', 'host', 'port']:
471477
try:
472-
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
473-
sock.connect(d['unix_socket'])
478+
del d[k]
474479
except KeyError:
475-
sock = socket.create_connection(
476-
(d.get('host', 'localhost'), d.get('port', 3306)))
477-
for k in ['unix_socket', 'host', 'port']:
478-
try:
479-
del d[k]
480-
except KeyError:
481-
pass
482-
483-
c = pymysql.connect(defer_connect=True, **d)
484-
self.assertFalse(c.open)
485-
c.connect(sock)
486-
c.close()
487-
sock.close()
480+
pass
481+
482+
c = pymysql.connect(defer_connect=True, **d)
483+
self.assertFalse(c.open)
484+
c.connect(sock)
485+
c.close()
486+
sock.close()
488487

489488
@unittest2.skipUnless(sys.version_info[0:2] >= (3,2), "required py-3.2")
490489
def test_no_delay_warning(self):
@@ -560,15 +559,17 @@ def test_escape_list_item(self):
560559
self.assertEqual(con.escape([Foo()], mapping), "(bar)")
561560

562561
def test_previous_cursor_not_closed(self):
563-
con = self.connections[0]
562+
con = self.connect(
563+
init_command='SELECT "bar"; SELECT "baz"',
564+
client_flag=CLIENT.MULTI_STATEMENTS)
564565
cur1 = con.cursor()
565566
cur1.execute("SELECT 1; SELECT 2")
566567
cur2 = con.cursor()
567568
cur2.execute("SELECT 3")
568569
self.assertEqual(cur2.fetchone()[0], 3)
569570

570571
def test_commit_during_multi_result(self):
571-
con = self.connections[0]
572+
con = self.connect(client_flag=CLIENT.MULTI_STATEMENTS)
572573
cur = con.cursor()
573574
cur.execute("SELECT 1; SELECT 2")
574575
con.commit()

pymysql/tests/test_nextset.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
from pymysql.tests import base
44
from pymysql import util
5+
from pymysql.constants import CLIENT
56

67

78
class TestNextset(base.PyMySQLTestCase):
89

9-
def setUp(self):
10-
super(TestNextset, self).setUp()
11-
self.con = self.connections[0]
12-
1310
def test_nextset(self):
14-
cur = self.con.cursor()
11+
con = self.connect(
12+
init_command='SELECT "bar"; SELECT "baz"',
13+
client_flag=CLIENT.MULTI_STATEMENTS)
14+
cur = con.cursor()
1515
cur.execute("SELECT 1; SELECT 2;")
1616
self.assertEqual([(1,)], list(cur))
1717

@@ -22,15 +22,15 @@ def test_nextset(self):
2222
self.assertIsNone(cur.nextset())
2323

2424
def test_skip_nextset(self):
25-
cur = self.con.cursor()
25+
cur = self.connect(client_flag=CLIENT.MULTI_STATEMENTS).cursor()
2626
cur.execute("SELECT 1; SELECT 2;")
2727
self.assertEqual([(1,)], list(cur))
2828

2929
cur.execute("SELECT 42")
3030
self.assertEqual([(42,)], list(cur))
3131

3232
def test_ok_and_next(self):
33-
cur = self.con.cursor()
33+
cur = self.connect(client_flag=CLIENT.MULTI_STATEMENTS).cursor()
3434
cur.execute("SELECT 1; commit; SELECT 2;")
3535
self.assertEqual([(1,)], list(cur))
3636
self.assertTrue(cur.nextset())
@@ -40,8 +40,9 @@ def test_ok_and_next(self):
4040

4141
@unittest2.expectedFailure
4242
def test_multi_cursor(self):
43-
cur1 = self.con.cursor()
44-
cur2 = self.con.cursor()
43+
con = self.connect(client_flag=CLIENT.MULTI_STATEMENTS)
44+
cur1 = con.cursor()
45+
cur2 = con.cursor()
4546

4647
cur1.execute("SELECT 1; SELECT 2;")
4748
cur2.execute("SELECT 42")
@@ -56,7 +57,10 @@ def test_multi_cursor(self):
5657
self.assertIsNone(cur1.nextset())
5758

5859
def test_multi_statement_warnings(self):
59-
cursor = self.con.cursor()
60+
con = self.connect(
61+
init_command='SELECT "bar"; SELECT "baz"',
62+
client_flag=CLIENT.MULTI_STATEMENTS)
63+
cursor = con.cursor()
6064

6165
try:
6266
cursor.execute('DROP TABLE IF EXISTS a; '

0 commit comments

Comments
 (0)
0