upgraded PyMysql to 0.7.9, thanks niphlod
This commit is contained in:
1 parent
a867a65ebe
commit
590a505c54
38 files changed
+28064
-1328
No files matched your search
@@ -1,8 +1,19 @@
|
||||
from pymysql.tests import base
|
||||
from pymysql import util
|
||||
|
||||
import time
|
||||
# coding: utf-8
|
||||
import datetime
|
||||
import json
|
||||
import time
|
||||
import warnings
|
||||
|
||||
from unittest2 import SkipTest
|
||||
|
||||
from pymysql import util
|
||||
import pymysql.cursors
|
||||
from pymysql.tests import base
|
||||
from pymysql.err import ProgrammingError
|
||||
|
||||
|
||||
__all__ = ["TestConversion", "TestCursor", "TestBulkInserts"]
|
||||
|
||||
|
||||
class TestConversion(base.PyMySQLTestCase):
|
||||
def test_datatypes(self):
|
||||
@@ -12,16 +23,13 @@ class TestConversion(base.PyMySQLTestCase):
|
||||
c.execute("create table test_datatypes (b bit, i int, l bigint, f real, s varchar(32), u varchar(32), bb blob, d date, dt datetime, ts timestamp, td time, t time, st datetime)")
|
||||
try:
|
||||
# insert values
|
||||
v = (True, -3, 123456789012, 5.7, "hello'\" world", u"Espa\xc3\xb1ol", "binary\x00data".encode(conn.charset), datetime.date(1988,2,2), datetime.datetime.now(), datetime.timedelta(5,6), datetime.time(16,32), time.localtime())
|
||||
|
||||
v = (True, -3, 123456789012, 5.7, "hello'\" world", u"Espa\xc3\xb1ol", "binary\x00data".encode(conn.charset), datetime.date(1988,2,2), datetime.datetime(2014, 5, 15, 7, 45, 57), datetime.timedelta(5,6), datetime.time(16,32), time.localtime())
|
||||
c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", v)
|
||||
c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes")
|
||||
r = c.fetchone()
|
||||
self.assertEqual(util.int2byte(1), r[0])
|
||||
self.assertEqual(v[1:8], r[1:8])
|
||||
# mysql throws away microseconds so we need to check datetimes
|
||||
# specially. additionally times are turned into timedeltas.
|
||||
self.assertEqual(datetime.datetime(*v[8].timetuple()[:6]), r[8])
|
||||
self.assertEqual(v[9], r[9]) # just timedeltas
|
||||
self.assertEqual(v[1:10], r[1:10])
|
||||
self.assertEqual(datetime.timedelta(0, 60 * (v[10].hour * 60 + v[10].minute)), r[10])
|
||||
self.assertEqual(datetime.datetime(*v[-1][:6]), r[-1])
|
||||
|
||||
@@ -35,11 +43,15 @@ class TestConversion(base.PyMySQLTestCase):
|
||||
|
||||
c.execute("delete from test_datatypes")
|
||||
|
||||
# check sequence type
|
||||
c.execute("insert into test_datatypes (i, l) values (2,4), (6,8), (10,12)")
|
||||
c.execute("select l from test_datatypes where i in %s order by i", ((2,6),))
|
||||
r = c.fetchall()
|
||||
self.assertEqual(((4,),(8,)), r)
|
||||
# check sequences type
|
||||
for seq_type in (tuple, list, set, frozenset):
|
||||
c.execute("insert into test_datatypes (i, l) values (2,4), (6,8), (10,12)")
|
||||
seq = seq_type([2,6])
|
||||
c.execute("select l from test_datatypes where i in %s order by i", (seq,))
|
||||
r = c.fetchall()
|
||||
self.assertEqual(((4,),(8,)), r)
|
||||
c.execute("delete from test_datatypes")
|
||||
|
||||
finally:
|
||||
c.execute("drop table test_datatypes")
|
||||
|
||||
@@ -79,20 +91,18 @@ class TestConversion(base.PyMySQLTestCase):
|
||||
finally:
|
||||
c.execute("drop table test_dict")
|
||||
|
||||
|
||||
def test_big_blob(self):
|
||||
""" test tons of data """
|
||||
def test_blob(self):
|
||||
"""test binary data"""
|
||||
data = bytes(bytearray(range(256)) * 4)
|
||||
conn = self.connections[0]
|
||||
c = conn.cursor()
|
||||
c.execute("create table test_big_blob (b blob)")
|
||||
try:
|
||||
data = "pymysql" * 1024
|
||||
c.execute("insert into test_big_blob (b) values (%s)", (data,))
|
||||
c.execute("select b from test_big_blob")
|
||||
self.assertEqual(data.encode(conn.charset), c.fetchone()[0])
|
||||
finally:
|
||||
c.execute("drop table test_big_blob")
|
||||
|
||||
self.safe_create_table(
|
||||
conn, "test_blob", "create table test_blob (b blob)")
|
||||
|
||||
with conn.cursor() as c:
|
||||
c.execute("insert into test_blob (b) values (%s)", (data,))
|
||||
c.execute("select b from test_blob")
|
||||
self.assertEqual(data, c.fetchone()[0])
|
||||
|
||||
def test_untyped(self):
|
||||
""" test conversion of null, empty string """
|
||||
conn = self.connections[0]
|
||||
@@ -101,17 +111,40 @@ class TestConversion(base.PyMySQLTestCase):
|
||||
self.assertEqual((None,u''), c.fetchone())
|
||||
c.execute("select '',null")
|
||||
self.assertEqual((u'',None), c.fetchone())
|
||||
|
||||
def test_datetime(self):
|
||||
""" test conversion of null, empty string """
|
||||
|
||||
def test_timedelta(self):
|
||||
""" test timedelta conversion """
|
||||
conn = self.connections[0]
|
||||
c = conn.cursor()
|
||||
c.execute("select time('12:30'), time('23:12:59'), time('23:12:59.05100')")
|
||||
c.execute("select time('12:30'), time('23:12:59'), time('23:12:59.05100'), time('-12:30'), time('-23:12:59'), time('-23:12:59.05100'), time('-00:30')")
|
||||
self.assertEqual((datetime.timedelta(0, 45000),
|
||||
datetime.timedelta(0, 83579),
|
||||
datetime.timedelta(0, 83579, 51000)),
|
||||
datetime.timedelta(0, 83579, 51000),
|
||||
-datetime.timedelta(0, 45000),
|
||||
-datetime.timedelta(0, 83579),
|
||||
-datetime.timedelta(0, 83579, 51000),
|
||||
-datetime.timedelta(0, 1800)),
|
||||
c.fetchone())
|
||||
|
||||
def test_datetime_microseconds(self):
|
||||
""" test datetime conversion w microseconds"""
|
||||
|
||||
conn = self.connections[0]
|
||||
if not self.mysql_server_is(conn, (5, 6, 4)):
|
||||
raise SkipTest("target backend does not support microseconds")
|
||||
c = conn.cursor()
|
||||
dt = datetime.datetime(2013, 11, 12, 9, 9, 9, 123450)
|
||||
c.execute("create table test_datetime (id int, ts datetime(6))")
|
||||
try:
|
||||
c.execute(
|
||||
"insert into test_datetime values (%s, %s)",
|
||||
(1, dt)
|
||||
)
|
||||
c.execute("select ts from test_datetime")
|
||||
self.assertEqual((dt,), c.fetchone())
|
||||
finally:
|
||||
c.execute("drop table test_datetime")
|
||||
|
||||
|
||||
class TestCursor(base.PyMySQLTestCase):
|
||||
# this test case does not work quite right yet, however,
|
||||
@@ -185,7 +218,7 @@ class TestCursor(base.PyMySQLTestCase):
|
||||
c = conn.cursor()
|
||||
try:
|
||||
c.execute('create table test_aggregates (i integer)')
|
||||
for i in xrange(0, 10):
|
||||
for i in range(0, 10):
|
||||
c.execute('insert into test_aggregates (i) values (%s)', (i,))
|
||||
c.execute('select sum(i) from test_aggregates')
|
||||
r, = c.fetchone()
|
||||
@@ -197,17 +230,150 @@ class TestCursor(base.PyMySQLTestCase):
|
||||
""" test a single tuple """
|
||||
conn = self.connections[0]
|
||||
c = conn.cursor()
|
||||
try:
|
||||
c.execute("create table mystuff (id integer primary key)")
|
||||
c.execute("insert into mystuff (id) values (1)")
|
||||
c.execute("insert into mystuff (id) values (2)")
|
||||
c.execute("select id from mystuff where id in %s", ((1,),))
|
||||
self.assertEqual([(1,)], list(c.fetchall()))
|
||||
finally:
|
||||
c.execute("drop table mystuff")
|
||||
self.safe_create_table(
|
||||
conn, 'mystuff',
|
||||
"create table mystuff (id integer primary key)")
|
||||
c.execute("insert into mystuff (id) values (1)")
|
||||
c.execute("insert into mystuff (id) values (2)")
|
||||
c.execute("select id from mystuff where id in %s", ((1,),))
|
||||
self.assertEqual([(1,)], list(c.fetchall()))
|
||||
c.close()
|
||||
|
||||
__all__ = ["TestConversion","TestCursor"]
|
||||
def test_json(self):
|
||||
args = self.databases[0].copy()
|
||||
args["charset"] = "utf8mb4"
|
||||
conn = pymysql.connect(**args)
|
||||
if not self.mysql_server_is(conn, (5, 7, 0)):
|
||||
raise SkipTest("JSON type is not supported on MySQL <= 5.6")
|
||||
|
||||
if __name__ == "__main__":
|
||||
import unittest
|
||||
unittest.main()
|
||||
self.safe_create_table(conn, "test_json", """\
|
||||
create table test_json (
|
||||
id int not null,
|
||||
json JSON not null,
|
||||
primary key (id)
|
||||
);""")
|
||||
cur = conn.cursor()
|
||||
|
||||
json_str = u'{"hello": "こんにちは"}'
|
||||
cur.execute("INSERT INTO test_json (id, `json`) values (42, %s)", (json_str,))
|
||||
cur.execute("SELECT `json` from `test_json` WHERE `id`=42")
|
||||
res = cur.fetchone()[0]
|
||||
self.assertEqual(json.loads(res), json.loads(json_str))
|
||||
|
||||
cur.execute("SELECT CAST(%s AS JSON) AS x", (json_str,))
|
||||
res = cur.fetchone()[0]
|
||||
self.assertEqual(json.loads(res), json.loads(json_str))
|
||||
|
||||
|
||||
class TestBulkInserts(base.PyMySQLTestCase):
|
||||
|
||||
cursor_type = pymysql.cursors.DictCursor
|
||||
|
||||
def setUp(self):
|
||||
super(TestBulkInserts, self).setUp()
|
||||
self.conn = conn = self.connections[0]
|
||||
c = conn.cursor(self.cursor_type)
|
||||
|
||||
# create a table ane some data to query
|
||||
self.safe_create_table(conn, 'bulkinsert', """\
|
||||
CREATE TABLE bulkinsert
|
||||
(
|
||||
id int(11),
|
||||
name char(20),
|
||||
age int,
|
||||
height int,
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
""")
|
||||
|
||||
def _verify_records(self, data):
|
||||
conn = self.connections[0]
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT id, name, age, height from bulkinsert")
|
||||
result = cursor.fetchall()
|
||||
self.assertEqual(sorted(data), sorted(result))
|
||||
|
||||
def test_bulk_insert(self):
|
||||
conn = self.connections[0]
|
||||
cursor = conn.cursor()
|
||||
|
||||
data = [(0, "bob", 21, 123), (1, "jim", 56, 45), (2, "fred", 100, 180)]
|
||||
cursor.executemany("insert into bulkinsert (id, name, age, height) "
|
||||
"values (%s,%s,%s,%s)", data)
|
||||
self.assertEqual(
|
||||
cursor._last_executed, bytearray(
|
||||
b"insert into bulkinsert (id, name, age, height) values "
|
||||
b"(0,'bob',21,123),(1,'jim',56,45),(2,'fred',100,180)"))
|
||||
cursor.execute('commit')
|
||||
self._verify_records(data)
|
||||
|
||||
def test_bulk_insert_multiline_statement(self):
|
||||
conn = self.connections[0]
|
||||
cursor = conn.cursor()
|
||||
data = [(0, "bob", 21, 123), (1, "jim", 56, 45), (2, "fred", 100, 180)]
|
||||
cursor.executemany("""insert
|
||||
into bulkinsert (id, name,
|
||||
age, height)
|
||||
values (%s,
|
||||
%s , %s,
|
||||
%s )
|
||||
""", data)
|
||||
self.assertEqual(cursor._last_executed.strip(), bytearray(b"""insert
|
||||
into bulkinsert (id, name,
|
||||
age, height)
|
||||
values (0,
|
||||
'bob' , 21,
|
||||
123 ),(1,
|
||||
'jim' , 56,
|
||||
45 ),(2,
|
||||
'fred' , 100,
|
||||
180 )"""))
|
||||
cursor.execute('commit')
|
||||
self._verify_records(data)
|
||||
|
||||
def test_bulk_insert_single_record(self):
|
||||
conn = self.connections[0]
|
||||
cursor = conn.cursor()
|
||||
data = [(0, "bob", 21, 123)]
|
||||
cursor.executemany("insert into bulkinsert (id, name, age, height) "
|
||||
"values (%s,%s,%s,%s)", data)
|
||||
cursor.execute('commit')
|
||||
self._verify_records(data)
|
||||
|
||||
def test_issue_288(self):
|
||||
"""executemany should work with "insert ... on update" """
|
||||
conn = self.connections[0]
|
||||
cursor = conn.cursor()
|
||||
data = [(0, "bob", 21, 123), (1, "jim", 56, 45), (2, "fred", 100, 180)]
|
||||
cursor.executemany("""insert
|
||||
into bulkinsert (id, name,
|
||||
age, height)
|
||||
values (%s,
|
||||
%s , %s,
|
||||
%s ) on duplicate key update
|
||||
age = values(age)
|
||||
""", data)
|
||||
self.assertEqual(cursor._last_executed.strip(), bytearray(b"""insert
|
||||
into bulkinsert (id, name,
|
||||
age, height)
|
||||
values (0,
|
||||
'bob' , 21,
|
||||
123 ),(1,
|
||||
'jim' , 56,
|
||||
45 ),(2,
|
||||
'fred' , 100,
|
||||
180 ) on duplicate key update
|
||||
age = values(age)"""))
|
||||
cursor.execute('commit')
|
||||
self._verify_records(data)
|
||||
|
||||
def test_warnings(self):
|
||||
con = self.connections[0]
|
||||
cur = con.cursor()
|
||||
with warnings.catch_warnings(record=True) as ws:
|
||||
warnings.simplefilter("always")
|
||||
cur.execute("drop table if exists no_exists_table")
|
||||
self.assertEqual(len(ws), 1)
|
||||
self.assertEqual(ws[0].category, pymysql.Warning)
|
||||
if u"no_exists_table" not in str(ws[0].message):
|
||||
self.fail("'no_exists_table' not in %s" % (str(ws[0].message),))
|
||||
Reference in new issue
Block a user