1
# MySQL Connector/Python - MySQL driver written in Python.
2
# Copyright (c) 2009,2010, Oracle and/or its affiliates. All rights reserved.
3
# Use is subject to license terms. (See COPYING)
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation.
9
# There are special exceptions to the terms and conditions of the GNU
10
# General Public License as it is applied to this software. View the
11
# full text of the exception in file EXCEPTIONS-CLIENT in the directory
12
# of this software distribution or see the FOSS License Exception at
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU General Public License for more details.
20
# You should have received a copy of the GNU General Public License
21
# along with this program; if not, write to the Free Software
22
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
"""Unittests for MySQL data types
27
from decimal import Decimal
31
from mysql.connector import connection
34
class TestsDataTypes(tests.MySQLConnectorTests):
37
'bit': 'myconnpy_mysql_bit',
38
'int': 'myconnpy_mysql_int',
39
'bool': 'myconnpy_mysql_bool',
40
'float': 'myconnpy_mysql_float',
41
'decimal': 'myconnpy_mysql_decimal',
42
'temporal': 'myconnpy_mysql_temporal',
45
def _get_insert_stmt(self, tbl, cols):
46
insert = """insert into %s (%s) values (%s)""" % (
48
','.join(['%s']*len(cols)))
51
def _get_select_stmt(self, tbl, cols):
52
select = "SELECT %s FROM %s ORDER BY id" % (
56
class TestsCursor(TestsDataTypes):
59
config = self.getMySQLConfig()
60
self.db = connection.MySQLConnection(**config)
62
tblNames = self.tables.values()
63
c.execute("DROP TABLE IF EXISTS %s" % (','.join(tblNames)))
69
tblNames = self.tables.values()
70
c.execute("DROP TABLE IF EXISTS %s" % (','.join(tblNames)))
75
def test_numeric_int(self):
76
"""MySQL numeric integer data types"""
91
c.execute("""CREATE TABLE %s (
92
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
93
`tinyint_signed` TINYINT SIGNED,
94
`tinyint_unsigned` TINYINT UNSIGNED,
96
`smallint_signed` SMALLINT SIGNED,
97
`smallint_unsigned` SMALLINT UNSIGNED,
98
`mediumint_signed` MEDIUMINT SIGNED,
99
`mediumint_unsigned` MEDIUMINT UNSIGNED,
100
`int_signed` INT SIGNED,
101
`int_unsigned` INT UNSIGNED,
102
`bigint_signed` BIGINT SIGNED,
103
`bigint_unsigned` BIGINT UNSIGNED,
106
""" % (self.tables['int']))
110
-128, # tinyint signed
111
0, # tinyint unsigned
113
-32768, # smallint signed
114
0, # smallint unsigned
115
-8388608, # mediumint signed
116
0, # mediumint unsigned
117
-2147483648, # int signed
119
-9223372036854775808, # big signed
123
127, # tinyint signed
124
255, # tinyint unsigned
126
32767, # smallint signed
127
65535, # smallint unsigned
128
8388607, # mediumint signed
129
16777215, # mediumint unsigned
130
2147483647, # int signed
131
4294967295, # int unsigned
132
9223372036854775807, # big signed
133
18446744073709551615, # big unsigned
137
insert = self._get_insert_stmt(self.tables['int'],columns)
138
select = self._get_select_stmt(self.tables['int'],columns)
140
c.executemany(insert, data)
144
def compare(name, d, r):
145
self.assertEqual(d,r,"%s %s != %s" % (name,d,r))
147
for i,col in enumerate(columns):
148
compare(col,data[0][i],rows[0][i])
149
compare(col,data[1][i],rows[1][i])
153
def test_numeric_bit(self):
154
"""MySQL numeric bit data type"""
157
'c8','c16','c24','c32',
158
'c40','c48','c56','c63',
160
c.execute("""CREATE TABLE %s (
161
`id` int NOT NULL AUTO_INCREMENT,
162
`c8` bit(8) DEFAULT NULL,
163
`c16` bit(16) DEFAULT NULL,
164
`c24` bit(24) DEFAULT NULL,
165
`c32` bit(32) DEFAULT NULL,
166
`c40` bit(40) DEFAULT NULL,
167
`c48` bit(48) DEFAULT NULL,
168
`c56` bit(56) DEFAULT NULL,
169
`c63` bit(63) DEFAULT NULL,
170
`c64` bit(64) DEFAULT NULL,
173
""" % self.tables['bit'])
175
insert = self._get_insert_stmt(self.tables['bit'],columns)
176
select = self._get_select_stmt(self.tables['bit'],columns)
179
data.append(tuple([0]*len(columns)))
183
values.append( 1 << int(col.replace('c',''))-1)
184
data.append(tuple(values))
188
values.append( (1 << int(col.replace('c',''))) -1)
189
data.append(tuple(values))
191
c.executemany(insert, data)
195
self.assertEqual(rows, data)
198
def test_numeric_float(self):
199
"""MySQL numeric float data type"""
207
c.execute("""CREATE TABLE %s (
208
`id` int NOT NULL AUTO_INCREMENT,
209
`float_signed` FLOAT(6,5) SIGNED,
210
`float_unsigned` FLOAT(6,5) UNSIGNED,
211
`double_signed` DOUBLE(15,10) SIGNED,
212
`double_unsigned` DOUBLE(15,10) UNSIGNED,
214
)""" % (self.tables['float']))
216
insert = self._get_insert_stmt(self.tables['float'],columns)
217
select = self._get_select_stmt(self.tables['float'],columns)
220
(-3.402823466,0,-1.7976931348623157,0,),
221
(-1.175494351,3.402823466,1.7976931348623157,2.2250738585072014),
222
(-1.23455678,2.999999,-1.3999999999999999,1.9999999999999999),
224
c.executemany(insert, data)
228
def compare(name, d, r):
229
self.assertEqual(d,r,"%s %s != %s" % (name,d,r))
231
for j in (range(0,len(data))):
232
for i,col in enumerate(columns[0:2]):
233
compare(col,round(data[j][i],5),rows[j][i])
234
for i,col in enumerate(columns[2:2]):
235
compare(col,round(data[j][i],10),rows[j][i])
238
def test_numeric_decimal(self):
239
"""MySQL numeric decimal data type"""
245
c.execute("""CREATE TABLE %s (
246
`id` int NOT NULL AUTO_INCREMENT,
247
`decimal_signed` DECIMAL(65,30) SIGNED,
248
`decimal_unsigned` DECIMAL(65,30) UNSIGNED,
250
)""" % (self.tables['decimal']))
252
insert = self._get_insert_stmt(self.tables['decimal'],columns)
253
select = self._get_select_stmt(self.tables['decimal'],columns)
256
(Decimal('-9999999999999999999999999.999999999999999999999999999999'),
257
Decimal('+9999999999999999999999999.999999999999999999999999999999')),
258
(Decimal('-1234567.1234'),
259
Decimal('+123456789012345.123456789012345678901')),
260
(Decimal('-1234567890123456789012345.123456789012345678901234567890'),
261
Decimal('+1234567890123456789012345.123456789012345678901234567890')),
263
c.executemany(insert, data)
267
self.assertEqual(data,rows)
271
def test_temporal_datetime(self):
272
"""MySQL temporal date/time data types"""
274
c.execute("SET SESSION time_zone = '+00:00'")
283
c.execute("""CREATE TABLE %s (
284
`id` int NOT NULL AUTO_INCREMENT,
286
`t_datetime` DATETIME,
288
`t_timestamp` TIMESTAMP DEFAULT 0,
292
)""" % (self.tables['temporal']))
294
insert = self._get_insert_stmt(self.tables['temporal'],columns)
295
select = self._get_select_stmt(self.tables['temporal'],columns)
298
(datetime.date(2010,1,17),
299
datetime.datetime(2010,1,17,19,31,12),
300
datetime.timedelta(hours=43,minutes=32,seconds=21),
301
datetime.datetime(2010,1,17,19,31,12),
304
(datetime.date(1000,1,1),
305
datetime.datetime(1000,1,1,0,0,0),
306
datetime.timedelta(hours=-838,minutes=59,seconds=59),
307
datetime.datetime(*time.gmtime(1)[:6]),
310
(datetime.date(9999,12,31),
311
datetime.datetime(9999,12,31,23,59,59),
312
datetime.timedelta(hours=838,minutes=59,seconds=59),
313
datetime.datetime(2038,1,19,3,14,7),
318
c.executemany(insert, data)
322
def compare(name, d, r):
323
self.assertEqual(d,r,"%s %s != %s" % (name,d,r))
325
for j in (range(0,len(data))):
326
for i,col in enumerate(columns):
327
compare("%s (data[%d])" % (col,j),data[j][i],rows[j][i])