def testLargePackets(self): cnn = client.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWD, db=DB_DB) cnn.query("truncate tbltest") c = cnn.buffer.capacity 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: self.assertEquals(row[0], len(row[1])) self.assertEquals(blob[:row[0]], row[1]) rs.close() #reread, second time, oversize packet is already present rs = cnn.query("select test_id, test_blob from tbltest") for row in rs: self.assertEquals(row[0], len(row[1])) self.assertEquals(blob[:row[0]], row[1]) rs.close() cnn.close() #have a very low max packet size for oversize packets #and check that exception is thrown when trying to read larger packets from concurrence.database.mysql import _mysql _mysql.MAX_PACKET_SIZE = 1024 * 4 cnn = client.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWD, db=DB_DB) try: rs = cnn.query("select test_id, test_blob from tbltest") for row in rs: self.assertEquals(row[0], len(row[1])) self.assertEquals(blob[:row[0]], row[1]) self.fail() except PacketReadError: pass finally: try: rs.close() except: pass cnn.close()
def testLargePackets(self): cnn = client.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = DB_DB) cnn.query("truncate tbltest") c = cnn.buffer.capacity 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: self.assertEquals(row[0], len(row[1])) self.assertEquals(blob[:row[0]], row[1]) rs.close() #reread, second time, oversize packet is already present rs = cnn.query("select test_id, test_blob from tbltest") for row in rs: self.assertEquals(row[0], len(row[1])) self.assertEquals(blob[:row[0]], row[1]) rs.close() cnn.close() #have a very low max packet size for oversize packets #and check that exception is thrown when trying to read larger packets from concurrence.database.mysql import _mysql _mysql.MAX_PACKET_SIZE = 1024 * 4 cnn = client.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = DB_DB) try: rs = cnn.query("select test_id, test_blob from tbltest") for row in rs: self.assertEquals(row[0], len(row[1])) self.assertEquals(blob[:row[0]], row[1]) self.fail() except PacketReadError: pass finally: try: rs.close() except: pass cnn.close()
def testMySQLClient2(self): cnn = client.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWD, db=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 try: rs.close() self.fail('expected exception') except client.ClientProgrammingError: pass for i, row in enumerate(rs): self.assertEquals((i, 'test%d' % i), row) rs.close() cnn.close()
def testFetchUnicode(self): cnn = client.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWD, db=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_string from tbltest where test_id = 1") s = list(rs)[0][0] self.assertTrue(type(s) == str) rs.close() cnn.set_charset('latin1') rs = cnn.query("select test_string from tbltest where test_id = 1") s = list(rs)[0][0] self.assertTrue(type(s) == unicode) rs.close() cnn.close()
def testConnectNoDb(self): cnn = client.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD) rs = cnn.query("select 1") self.assertEqual([('1',)], list(rs)) rs.close() cnn.close()
def testConnectNoDb(self): cnn = client.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWD) rs = cnn.query("select 1") self.assertEqual([('1', )], list(rs)) rs.close() cnn.close()
def main(): i = 0 cnn = None while True: try: print "iteration" if not cnn or not cnn.is_connected(): cnn = client.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = DB_DB) res = cnn.query("select %d" % i) print list(res)[0][0] res.close() res = cnn.query("select sleep(1)") list(res)[0][0] res.close() except Exception as e: traceback.print_exc() else: i += 1
def testMySQLTimeout(self): cnn = client.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = DB_DB) rs = cnn.query("select sleep(2)") list(rs) rs.close() from concurrence import TimeoutError from concurrence.timer import Timeout start = time.time() try: with Timeout.push(2.0): cnn.query("select sleep(4)") self.fail('expected timeout') except TimeoutError, e: end = time.time() self.assertAlmostEqual(2.0, end - start, places = 1)
def testFetchUnicode(self): cnn = client.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = 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_string from tbltest where test_id = 1") s = list(rs)[0][0] self.assertTrue(type(s) == str) rs.close() cnn.set_charset('latin1') rs = cnn.query("select test_string from tbltest where test_id = 1") s = list(rs)[0][0] self.assertTrue(type(s) == unicode) rs.close() cnn.close()
def testMySQLClient2(self): cnn = client.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = 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 try: rs.close() self.fail('expected exception') except client.ClientProgrammingError: pass for i, row in enumerate(rs): self.assertEquals((i, 'test%d' % i), row) rs.close() cnn.close()