forked from esnme/ultramysql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
575 lines (429 loc) · 23.6 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# -*- coding: utf-8 -*-
"""
Copyright (c) 2011, Jonas Tarnstrom and ESN Social Software AB
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by ESN Social Software AB (www.esn.me).
4. Neither the name of the ESN Social Software AB nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ESN SOCIAL SOFTWARE AB ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Portions of code from gevent-MySQL
Copyright (C) 2010, Markus Thurlin
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Hyves (Startphone Ltd.) nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
from __future__ import with_statement
import time
import datetime
import logging
import unittest
import gevent
import gevent.socket
import amysql
DB_HOST = '127.0.0.1'
DB_PORT = 3306
DB_USER = 'gevent_test'
DB_PASSWD = 'gevent_test'
DB_DB = 'gevent_test'
class TestMySQL(unittest.TestCase):
log = logging.getLogger('TestMySQL')
def testMySQLTimeout(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
rs = cnn.query("select sleep(2)")
list(rs.rows)
from gevent import Timeout
start = time.time()
try:
def delay():
cnn.query("select sleep(4)")
self.fail('expected timeout')
gevent.with_timeout(2, delay)
except Timeout:
end = time.time()
self.assertAlmostEqual(2.0, end - start, places = 1)
cnn.close()
def testDoubleConnect(self):
cnn = amysql.Connection()
cnn.connect(DB_HOST, DB_PORT, DB_USER, DB_PASSWD, DB_DB)
time.sleep(11)
cnn.close()
time.sleep(1)
cnn = amysql.Connection()
cnn.connect(DB_HOST, DB_PORT, DB_USER, DB_PASSWD, DB_DB)
def testConnectTwice(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
try:
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
assert False, "Expected exception"
except(RuntimeError):
pass
pass
def testParallelQuery(self):
def query(s):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("select sleep(%d)" % s)
cnn.close()
start = time.time()
ch1 = gevent.spawn(query, 1)
ch2 = gevent.spawn(query, 2)
ch3 = gevent.spawn(query, 3)
gevent.joinall([ch1, ch2, ch3])
end = time.time()
self.assertAlmostEqual(3.0, end - start, places = 1)
def testConnectFails(self):
cnn = amysql.Connection()
try:
cnn.connect (DB_HOST, 31337, DB_USER, DB_PASSWD, DB_DB)
assert False, "Expected exception"
except(RuntimeError):
pass
pass
def testConnectDNSFails(self):
cnn = amysql.Connection()
try:
cnn.connect ("thisplaceisnowere", 31337, DB_USER, DB_PASSWD, DB_DB)
assert False, "Expected exception"
except(RuntimeError):
pass
pass
def testConnectClosed(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
self.assertEquals(True, cnn.is_connected())
cnn.close()
self.assertEquals(False, cnn.is_connected())
def testConnectCloseQuery(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
self.assertEquals(True, cnn.is_connected())
cnn.close()
self.assertEquals(False, cnn.is_connected())
try:
cnn.query("SELECT 1")
assert False, "Expected exception"
except(RuntimeError):
pass
def testMySQLClient(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
rs = cnn.query("select 1")
self.assertEqual([(1,)], rs.rows)
cnn.close()
def testConnectNoDb(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, "")
rs = cnn.query("select 1")
self.assertEqual([(1,)], rs.rows)
cnn.close()
def testConnectAutoCommitOff(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, "", False)
rs = cnn.query("select 1")
self.assertEqual([(1,)], rs.rows)
cnn.close()
def testMySQLClient2(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("truncate tbltest")
for i in range(10):
self.assertEquals((1, 0), cnn.query("insert into tbltest (test_id, test_string) values (%d, 'test%d')" % (i, i)))
rs = cnn.query("select test_id, test_string from tbltest")
#trying to close it now would give an error, e.g. we always need to read
#the result from the database otherwise connection would be in wrong stat
for i, row in enumerate(rs.rows):
self.assertEquals((i, 'test%d' % i), row)
cnn.close()
def testMySQLDBAPI(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("truncate tbltest")
for i in range(10):
cnn.query("insert into tbltest (test_id, test_string) values (%d, 'test%d')" % (i, i))
rs = cnn.query("select test_id, test_string from tbltest")
self.assertEquals((0, 'test0'), rs.rows[0])
#check that fetchall gets the remainder
self.assertEquals([(1, 'test1'), (2, 'test2'), (3, 'test3'), (4, 'test4'), (5, 'test5'), (6, 'test6'), (7, 'test7'), (8, 'test8'), (9, 'test9')], rs.rows[1:])
#another query on the same cursor should work
rs = cnn.query("select test_id, test_string from tbltest")
#fetch some but not all
self.assertEquals((0, 'test0'), rs.rows[0])
self.assertEquals((1, 'test1'), rs.rows[1])
self.assertEquals((2, 'test2'), rs.rows[2])
#this should not work, cursor was closed
def testLargePackets(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("truncate tbltest")
c = 16 * 1024
blob = '0123456789'
while 1:
cnn.query("insert into tbltest (test_id, test_blob) values (%d, '%s')" % (len(blob), blob))
if len(blob) > (c * 2): break
blob = blob * 2
rs = cnn.query("select test_id, test_blob from tbltest")
for row in rs.rows:
self.assertEquals(row[0], len(row[1]))
#self.assertEquals(blob[:row[0]], row[1])
#reread, second time, oversize packet is already present
rs = cnn.query("select test_id, test_blob from tbltest")
for row in rs.rows:
self.assertEquals(row[0], len(row[1]))
self.assertEquals(blob[:row[0]], row[1])
cnn.close()
#have a very low max packet size for oversize packets
#and check that exception is thrown when trying to read larger packets
def testEscapeArgs(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("truncate tbltest")
cnn.query("insert into tbltest (test_id, test_string) values (%s, %s)", (1, 'piet'))
cnn.query("insert into tbltest (test_id, test_string) values (%s, %s)", (2, 'klaas'))
cnn.query("insert into tbltest (test_id, test_string) values (%s, %s)", (3, "pi'et"))
#classic sql injection, would return all rows if no proper escaping is done
rs = cnn.query("select test_id, test_string from tbltest where test_string = %s", ("piet' OR 'a' = 'a",))
self.assertEquals([], rs.rows) #assert no rows are found
#but we should still be able to find the piet with the apostrophe in its name
rs = cnn.query("select test_id, test_string from tbltest where test_string = %s", ("pi'et",))
self.assertEquals([(3, "pi'et")], rs.rows)
#also we should be able to insert and retrieve blob/string with all possible bytes transparently
chars = ''.join([chr(i) for i in range(256)])
cnn.query("insert into tbltest (test_id, test_string, test_blob) values (%s, %s, %s)", (4, chars[:80], chars))
#cnn.query("insert into tbltest (test_id, test_blob) values (%s, %s)", (4, chars))
rs = cnn.query("select test_blob, test_string from tbltest where test_id = %s", (4,))
#self.assertEquals([(chars, chars)], cur.fetchall())
b, s = rs.rows[0]
#test blob
self.assertEquals(256, len(b))
self.assertEquals(chars, b)
self.assertEquals(80, len(s))
self.assertEquals(chars[:80], s)
cnn.close()
def testSelectUnicode(self):
s = u'r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s'
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("truncate tbltest")
cnn.query("insert into tbltest (test_id, test_string) values (%s, %s)", (1, 'piet'))
cnn.query("insert into tbltest (test_id, test_string) values (%s, %s)", (2, s))
cnn.query(u"insert into tbltest (test_id, test_string) values (%s, %s)", (3, s))
rs = cnn.query("select test_id, test_string from tbltest")
result = rs.rows
self.assertEquals([(1, u'piet'), (2, s), (3, s)], result)
#test that we can still cleanly roundtrip a blob, (it should not be encoded if we pass
#it as 'str' argument), eventhough we pass the qry itself as unicode
blob = ''.join([chr(i) for i in range(256)])
cnn.query(u"insert into tbltest (test_id, test_blob) values (%s, %s)", (4, blob))
rs = cnn.query("select test_blob from tbltest where test_id = %s", (4,))
b2 = rs.rows[0][0]
self.assertEquals(str, type(b2))
self.assertEquals(256, len(b2))
self.assertEquals(blob, b2)
def testAutoInc(self):
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("truncate tblautoincint")
cnn.query("ALTER TABLE tblautoincint AUTO_INCREMENT = 100")
rowcount, lastrowid = cnn.query("insert into tblautoincint (test_string) values (%s)", ('piet',))
self.assertEqual(1, rowcount)
self.assertEqual(100, lastrowid)
rowcount, lastrowid = cnn.query("insert into tblautoincint (test_string) values (%s)", ('piet',))
self.assertEqual(1, rowcount)
self.assertEqual(101, lastrowid)
cnn.query("ALTER TABLE tblautoincint AUTO_INCREMENT = 4294967294")
rowcount, lastrowid = cnn.query("insert into tblautoincint (test_string) values (%s)", ('piet',))
self.assertEqual(1, rowcount)
self.assertEqual(4294967294, lastrowid)
rowcount, lastrowid = cnn.query("insert into tblautoincint (test_string) values (%s)", ('piet',))
self.assertEqual(1, rowcount)
self.assertEqual(4294967295, lastrowid)
cnn.query("truncate tblautoincbigint")
cnn.query("ALTER TABLE tblautoincbigint AUTO_INCREMENT = 100")
rowcount, lastrowid = cnn.query("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
self.assertEqual(1, rowcount)
self.assertEqual(100, lastrowid)
rowcount, lastrowid = cnn.query("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
self.assertEqual(1, rowcount)
self.assertEqual(101, lastrowid)
cnn.query("ALTER TABLE tblautoincbigint AUTO_INCREMENT = 18446744073709551614")
rowcount, lastrowid = cnn.query("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
self.assertEqual(1, rowcount)
self.assertEqual(18446744073709551614, lastrowid)
#this fails on mysql, but that is a mysql problem
#cur.execute("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
#self.assertEqual(1, cur.rowcount)
#self.assertEqual(18446744073709551615, cur.lastrowid)
cnn.close()
def testBigInt(self):
#Tests the behaviour of insert/select with bigint/long.
BIGNUM = 112233445566778899
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tblbigint")
cnn.query("create table tblbigint (test_id int(11) DEFAULT NULL, test_bigint bigint DEFAULT NULL, test_bigint2 bigint DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1")
cnn.query("insert into tblbigint (test_id, test_bigint, test_bigint2) values (%s, " + str(BIGNUM) + ", %s)", (1, BIGNUM))
cnn.query(u"insert into tblbigint (test_id, test_bigint, test_bigint2) values (%s, " + str(BIGNUM) + ", %s)", (2, BIGNUM))
# Make sure both our inserts where correct (ie, the big number was not truncated/modified on insert)
rs = cnn.query("select test_id from tblbigint where test_bigint = test_bigint2")
result = rs.rows
self.assertEquals([(1, ), (2, )], result)
# Make sure select gets the right values (ie, the big number was not truncated/modified when retrieved)
rs = cnn.query("select test_id, test_bigint, test_bigint2 from tblbigint where test_bigint = test_bigint2")
result = rs.rows
self.assertEquals([(1, BIGNUM, BIGNUM), (2, BIGNUM, BIGNUM)], result)
def testDate(self):
# Tests the behaviour of insert/select with mysql/DATE <-> python/datetime.date
d_date = datetime.date(2010, 02, 11)
d_string = "2010-02-11"
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tbldate")
cnn.query("create table tbldate (test_id int(11) DEFAULT NULL, test_date date DEFAULT NULL, test_date2 date DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1")
cnn.query("insert into tbldate (test_id, test_date, test_date2) values (%s, '" + d_string + "', %s)", (1, d_date))
# Make sure our insert was correct
rs = cnn.query("select test_id from tbldate where test_date = test_date2")
result = rs.rows
self.assertEquals([(1, )], result)
# Make sure select gets the right value back
rs = cnn.query("select test_id, test_date, test_date2 from tbldate where test_date = test_date2")
result = rs.rows
self.assertEquals([(1, d_date, d_date)], result)
def testDateTime(self):
# Tests the behaviour of insert/select with mysql/DATETIME <-> python/datetime.datetime
d_date = datetime.datetime(2010, 02, 11, 13, 37, 42)
d_string = "2010-02-11 13:37:42"
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tbldate")
cnn.query("create table tbldate (test_id int(11) DEFAULT NULL, test_date datetime DEFAULT NULL, test_date2 datetime DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1")
cnn.query("insert into tbldate (test_id, test_date, test_date2) values (%s, '" + d_string + "', %s)", (1, d_date))
# Make sure our insert was correct
rs = cnn.query("select test_id from tbldate where test_date = test_date2")
result = rs.rows
self.assertEquals([(1, )], result)
# Make sure select gets the right value back
rs = cnn.query("select test_id, test_date, test_date2 from tbldate where test_date = test_date2")
result = rs.rows
self.assertEquals([(1, d_date, d_date)], result)
def testZeroDates(self):
# Tests the behaviour of zero dates
zero_datetime = "0000-00-00 00:00:00"
zero_date = "0000-00-00"
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tbldate")
cnn.query("create table tbldate (test_id int(11) DEFAULT NULL, test_date date DEFAULT NULL, test_datetime datetime DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1")
cnn.query("insert into tbldate (test_id, test_date, test_datetime) values (%s, %s, %s)", (1, zero_date, zero_datetime))
# Make sure we get None-values back
rs = cnn.query("select test_id, test_date, test_datetime from tbldate where test_id = 1")
result = rs.rows
self.assertEquals([(1, None, None)], result)
def testUnicodeUTF8(self):
peacesign_unicode = u"\u262e"
peacesign_utf8 = "\xe2\x98\xae"
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tblutf")
cnn.query("create table tblutf (test_id int(11) DEFAULT NULL, test_string VARCHAR(32) DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8")
cnn.query("insert into tblutf (test_id, test_string) values (%s, %s)", (1, peacesign_unicode)) # This should be encoded in utf8
cnn.query("insert into tblutf (test_id, test_string) values (%s, %s)", (2, peacesign_utf8))
rs = cnn.query("select test_id, test_string from tblutf")
result = rs.rows
# We expect unicode strings back
self.assertEquals([(1, peacesign_unicode), (2, peacesign_unicode)], result)
def testBinary(self):
peacesign_binary = "\xe2\x98\xae"
peacesign_binary2 = "\xe2\x98\xae" * 10
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tblbin")
cnn.query("create table tblbin (test_id int(11) DEFAULT NULL, test_binary VARBINARY(30) DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8")
cnn.query("insert into tblbin (test_id, test_binary) values (%s, %s)", (1, peacesign_binary))
cnn.query("insert into tblbin (test_id, test_binary) values (%s, %s)", (2, peacesign_binary2))
rs = cnn.query("select test_id, test_binary from tblbin")
result = rs.rows
# We expect binary strings back
self.assertEquals([(1, peacesign_binary),(2, peacesign_binary2)], result)
def testBlob(self):
peacesign_binary = "\xe2\x98\xae"
peacesign_binary2 = "\xe2\x98\xae" * 1024
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tblblob")
cnn.query("create table tblblob (test_id int(11) DEFAULT NULL, test_blob BLOB DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8")
cnn.query("insert into tblblob (test_id, test_blob) values (%s, %s)", (1, peacesign_binary))
cnn.query("insert into tblblob (test_id, test_blob) values (%s, %s)", (2, peacesign_binary2))
rs = cnn.query("select test_id, test_blob from tblblob")
result = rs.rows
# We expect binary strings back
self.assertEquals([(1, peacesign_binary),(2, peacesign_binary2)], result)
def testCharsets(self):
aumlaut_unicode = u"\u00e4"
aumlaut_utf8 = "\xc3\xa4"
aumlaut_latin1 = "\xe4"
cnn = amysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
cnn.query("drop table if exists tblutf")
cnn.query("create table tblutf (test_mode VARCHAR(32) DEFAULT NULL, test_utf VARCHAR(32) DEFAULT NULL, test_latin1 VARCHAR(32)) ENGINE=MyISAM DEFAULT CHARSET=utf8")
# We insert the same character using two different encodings
cnn.query("set names utf8")
cnn.query("insert into tblutf (test_mode, test_utf, test_latin1) values ('utf8', _utf8'" + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')")
cnn.query("set names latin1")
cnn.query("insert into tblutf (test_mode, test_utf, test_latin1) values ('latin1', _utf8'" + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')")
# We expect the driver to always give us unicode strings back
expected = [(u"utf8", aumlaut_unicode, aumlaut_unicode), (u"latin1", aumlaut_unicode, aumlaut_unicode)]
# Fetch and test with different charsets
for charset in ("latin1", "utf8", "cp1250"):
cnn.query("set names " + charset)
rs = cnn.query("select test_mode, test_utf, test_latin1 from tblutf")
result = rs.rows
self.assertEquals(result, expected)
if __name__ == '__main__':
unittest.main()
if __name__ == '__main__':
from guppy import hpy
hp = hpy()
hp.setrelheap()
while True:
unittest.main()
heap = hp.heapu()
print heap