Skip to content

Commit f43aeea

Browse files
committed
- Alter the datetime in test_basic->test_datatypes to be a
fixed value without microseconds, so that no truncation or rounding of microseconds takes place; whether they are truncated or rounded depends on backend. - improve the test for test_datetime to round trip the datetime value, passing it in the INSERT as well as getting it back from the SELECT. Use version detection to only run the test on compliant backends; don't catch and discard a ProgrammingError without knowing what it is.
1 parent 3eeb622 commit f43aeea

2 files changed

Lines changed: 40 additions & 18 deletions

File tree

pymysql/tests/base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import json
33
import pymysql
4+
import re
45
try:
56
import unittest2 as unittest
67
except ImportError:
@@ -19,6 +20,23 @@ class PyMySQLTestCase(unittest.TestCase):
1920
"passwd":"","db":"test_pymysql", "use_unicode": True, 'local_infile': True},
2021
{"host":"localhost","user":"root","passwd":"","db":"test_pymysql2"}]
2122

23+
def mysql_server_is(self, conn, version_tuple):
24+
"""Return True if the given connection is on the version given or
25+
greater.
26+
27+
e.g.::
28+
29+
if self.mysql_server_is(conn, (5, 6, 4)):
30+
# do something for MySQL 5.6.4 and above
31+
"""
32+
server_version = conn.get_server_info()
33+
server_version_tuple = tuple(
34+
(int(dig) if dig is not None else 0)
35+
for dig in
36+
re.match(r'(\d+)\.(\d+)\.(\d+)', server_version).group(1, 2, 3)
37+
)
38+
return server_version_tuple >= version_tuple
39+
2240
def setUp(self):
2341
self.connections = []
2442
for params in self.databases:

pymysql/tests/test_basic.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import datetime
99
import warnings
1010

11+
try:
12+
from unittest2 import SkipTest
13+
except:
14+
from unittest import SkipTest
15+
16+
1117
__all__ = ["TestConversion", "TestCursor", "TestBulkInserts"]
1218

1319

@@ -19,16 +25,13 @@ def test_datatypes(self):
1925
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)")
2026
try:
2127
# insert values
22-
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())
28+
29+
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())
2330
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)
2431
c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes")
2532
r = c.fetchone()
2633
self.assertEqual(util.int2byte(1), r[0])
27-
self.assertEqual(v[1:8], r[1:8])
28-
# mysql throws away microseconds so we need to check datetimes
29-
# specially. additionally times are turned into timedeltas.
30-
self.assertEqual(datetime.datetime(*v[8].timetuple()[:6]), r[8])
31-
self.assertEqual(v[9], r[9]) # just timedeltas
34+
self.assertEqual(v[1:10], r[1:10])
3235
self.assertEqual(datetime.timedelta(0, 60 * (v[10].hour * 60 + v[10].minute)), r[10])
3336
self.assertEqual(datetime.datetime(*v[-1][:6]), r[-1])
3437

@@ -123,23 +126,24 @@ def test_timedelta(self):
123126
-datetime.timedelta(0, 1800)),
124127
c.fetchone())
125128

126-
def test_datetime(self):
127-
""" test datetime conversion """
129+
def test_datetime_microseconds(self):
130+
""" test datetime conversion w microseconds"""
131+
128132
conn = self.connections[0]
133+
if not self.mysql_server_is(conn, (5, 6, 4)):
134+
raise SkipTest("target backend does not support microseconds")
129135
c = conn.cursor()
130-
dt = datetime.datetime(2013,11,12,9,9,9,123450)
136+
dt = datetime.datetime(2013, 11, 12, 9, 9, 9, 123450)
137+
c.execute("create table test_datetime (id int, ts datetime(6))")
131138
try:
132-
c.execute("create table test_datetime (id int, ts datetime(6))")
133-
c.execute("insert into test_datetime values (1,'2013-11-12 09:09:09.12345')")
139+
c.execute(
140+
"insert into test_datetime values (%s, %s)",
141+
(1, dt)
142+
)
134143
c.execute("select ts from test_datetime")
135-
self.assertEqual((dt,),c.fetchone())
136-
except ProgrammingError:
137-
# User is running a version of MySQL that doesn't support msecs within datetime
138-
pass
144+
self.assertEqual((dt,), c.fetchone())
139145
finally:
140-
with warnings.catch_warnings():
141-
warnings.filterwarnings("ignore")
142-
c.execute("drop table if exists test_datetime")
146+
c.execute("drop table test_datetime")
143147

144148

145149
class TestCursor(base.PyMySQLTestCase):

0 commit comments

Comments
 (0)