4
from tests import skipped, skip_unless, using_pyevent, get_database_auth, LimitedTestCase
6
from eventlet import event
8
from eventlet.green import MySQLdb
12
def mysql_requirement(_f):
13
"""We want to skip tests if using pyevent, MySQLdb is not installed, or if
14
there is no database running on the localhost that the auth file grants
17
This errs on the side of skipping tests if everything is not right, but
18
it's better than a million tests failing when you don't care about mysql
23
print "Skipping mysql tests, MySQLdb not importable"
26
auth = get_database_auth()['MySQLdb'].copy()
27
MySQLdb.connect(**auth)
29
except MySQLdb.OperationalError:
30
print "Skipping mysql tests, error when connecting:"
34
class MySQLdbTester(LimitedTestCase):
36
self._auth = get_database_auth()['MySQLdb']
38
self.connection = None
39
self.connection = MySQLdb.connect(**self._auth)
40
cursor = self.connection.cursor()
41
cursor.execute("""CREATE TABLE gargleblatz
45
self.connection.commit()
50
self.connection.close()
53
@skip_unless(mysql_requirement)
55
auth = self._auth.copy()
60
dbname = 'test_%d_%d' % (os.getpid(), int(time.time()*1000))
61
db = MySQLdb.connect(**auth).cursor()
62
db.execute("create database "+dbname)
64
self._auth['db'] = dbname
68
db = MySQLdb.connect(**self._auth).cursor()
69
db.execute("drop database "+self._auth['db'])
73
def set_up_dummy_table(self, connection=None):
74
close_connection = False
75
if connection is None:
76
close_connection = True
77
if self.connection is None:
78
connection = MySQLdb.connect(**self._auth)
80
connection = self.connection
82
cursor = connection.cursor()
83
cursor.execute(self.dummy_table_sql)
89
dummy_table_sql = """CREATE TEMPORARY TABLE test_table
91
row_id INTEGER PRIMARY KEY AUTO_INCREMENT,
94
value_string VARCHAR(200),
97
value_binary_string VARCHAR(200) BINARY,
98
value_enum ENUM('Y','N'),
102
def assert_cursor_yields(self, curs):
108
gt = eventlet.spawn(tick)
109
curs.execute("select 1")
110
rows = curs.fetchall()
111
self.assertEqual(rows, ((1L,),))
112
self.assert_(counter[0] > 0, counter[0])
115
def assert_cursor_works(self, cursor):
116
cursor.execute("select 1")
117
rows = cursor.fetchall()
118
self.assertEqual(rows, ((1L,),))
119
self.assert_cursor_yields(cursor)
121
def assert_connection_works(self, conn):
123
self.assert_cursor_works(curs)
125
def test_module_attributes(self):
126
import MySQLdb as orig
127
for key in dir(orig):
128
if key not in ('__author__', '__path__', '__revision__',
129
'__version__', '__loader__'):
130
self.assert_(hasattr(MySQLdb, key), "%s %s" % (key, getattr(orig, key)))
132
def test_connecting(self):
133
self.assert_(self.connection is not None)
135
def test_connecting_annoyingly(self):
136
self.assert_connection_works(MySQLdb.Connect(**self._auth))
137
self.assert_connection_works(MySQLdb.Connection(**self._auth))
138
self.assert_connection_works(MySQLdb.connections.Connection(**self._auth))
140
def test_create_cursor(self):
141
cursor = self.connection.cursor()
144
def test_run_query(self):
145
cursor = self.connection.cursor()
146
self.assert_cursor_works(cursor)
149
def test_run_bad_query(self):
150
cursor = self.connection.cursor()
152
cursor.execute("garbage blah blah")
154
except AssertionError:
160
def fill_up_table(self, conn):
162
for i in range(1000):
163
curs.execute('insert into test_table (value_int) values (%s)' % i)
166
def test_yields(self):
167
conn = self.connection
168
self.set_up_dummy_table(conn)
169
self.fill_up_table(conn)
172
SHORT_QUERY = "select * from test_table"
175
self.assert_cursor_works(curs)
176
curs.execute(SHORT_QUERY)
179
eventlet.spawn(a_query)
181
self.assertEqual([1], results)
183
self.assertEqual([1, 2], results)
185
def test_visibility_from_other_connections(self):
186
conn = MySQLdb.connect(**self._auth)
187
conn2 = MySQLdb.connect(**self._auth)
190
curs2 = conn2.cursor()
191
curs2.execute("insert into gargleblatz (a) values (%s)" % (314159))
192
self.assertEqual(curs2.rowcount, 1)
194
selection_query = "select * from gargleblatz"
195
curs2.execute(selection_query)
196
self.assertEqual(curs2.rowcount, 1)
198
# create a new connection, it should see the addition
199
conn3 = MySQLdb.connect(**self._auth)
200
curs3 = conn3.cursor()
201
curs3.execute(selection_query)
202
self.assertEqual(curs3.rowcount, 1)
203
# now, does the already-open connection see it?
204
curs.execute(selection_query)
205
self.assertEqual(curs.rowcount, 1)
209
curs.execute("delete from gargleblatz where a=314159")
212
from tests import patcher_test
214
class MonkeyPatchTester(patcher_test.ProcessBase):
215
@skip_unless(mysql_requirement)
216
def test_monkey_patching(self):
217
output, lines = self.run_script("""
218
from eventlet import patcher
220
from eventlet.green import MySQLdb as gm
221
patcher.monkey_patch(all=True, MySQLdb=True)
222
print "mysqltest", ",".join(sorted(patcher.already_patched.keys()))
223
print "connect", m.connect == gm.connect
225
self.assertEqual(len(lines), 3)
226
self.assertEqual(lines[0].replace("psycopg,", ""),
227
'mysqltest MySQLdb,os,select,socket,thread,time')
228
self.assertEqual(lines[1], "connect True")