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
|
# MySQL Connector/Python - MySQL driver written in Python.
# Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
# MySQL Connector/Python is licensed under the terms of the GPLv2
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
# MySQL Connectors. There are special exceptions to the terms and
# conditions of the GPLv2 as it is applied to this software, see the
# FOSS License Exception
# <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""Unittests for MySQL data types
"""
from decimal import Decimal
import time
import datetime
from mysql.connector import connection
import tests
def _get_insert_stmt(tbl, cols):
insert = "INSERT INTO {table} ({columns}) values ({values})".format(
table=tbl,
columns=','.join(cols),
values=','.join(['%s'] * len(cols))
)
return insert
def _get_select_stmt(tbl, cols):
select = "SELECT {columns} FROM {table} ORDER BY id".format(
columns=','.join(cols),
table=tbl
)
return select
class TestsDataTypes(tests.MySQLConnectorTests):
tables = {
'bit': 'myconnpy_mysql_bit',
'int': 'myconnpy_mysql_int',
'bool': 'myconnpy_mysql_bool',
'float': 'myconnpy_mysql_float',
'decimal': 'myconnpy_mysql_decimal',
'temporal': 'myconnpy_mysql_temporal',
'temporal_year': 'myconnpy_mysql_temporal_year',
}
def compare(self, name, val1, val2):
self.assertEqual(val1, val2, "%s %s != %s" % (name, val1, val2))
def drop_tables(self, cnx):
cur = cnx.cursor()
table_names = self.tables.values()
cur.execute("DROP TABLE IF EXISTS {tables}".format(
tables=','.join(table_names))
)
cur.close()
class TestsCursor(TestsDataTypes):
def setUp(self):
config = tests.get_mysql_config()
self.cnx = connection.MySQLConnection(**config)
self.drop_tables(self.cnx)
def tearDown(self):
self.drop_tables(self.cnx)
self.cnx.close()
def test_numeric_int(self):
"""MySQL numeric integer data types"""
cur = self.cnx.cursor()
columns = [
'tinyint_signed',
'tinyint_unsigned',
'bool_signed',
'smallint_signed',
'smallint_unsigned',
'mediumint_signed',
'mediumint_unsigned',
'int_signed',
'int_unsigned',
'bigint_signed',
'bigint_unsigned',
]
cur.execute((
"CREATE TABLE {table} ("
"`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,"
"`tinyint_signed` TINYINT SIGNED,"
"`tinyint_unsigned` TINYINT UNSIGNED,"
"`bool_signed` BOOL,"
"`smallint_signed` SMALLINT SIGNED,"
"`smallint_unsigned` SMALLINT UNSIGNED,"
"`mediumint_signed` MEDIUMINT SIGNED,"
"`mediumint_unsigned` MEDIUMINT UNSIGNED,"
"`int_signed` INT SIGNED,"
"`int_unsigned` INT UNSIGNED,"
"`bigint_signed` BIGINT SIGNED,"
"`bigint_unsigned` BIGINT UNSIGNED,"
"PRIMARY KEY (id))"
).format(table=self.tables['int'])
)
data = [
(
-128, # tinyint signed
0, # tinyint unsigned
0, # boolean
-32768, # smallint signed
0, # smallint unsigned
-8388608, # mediumint signed
0, # mediumint unsigned
-2147483648, # int signed
0, # int unsigned
-9223372036854775808, # big signed
0, # big unsigned
),
(
127, # tinyint signed
255, # tinyint unsigned
127, # boolean
32767, # smallint signed
65535, # smallint unsigned
8388607, # mediumint signed
16777215, # mediumint unsigned
2147483647, # int signed
4294967295, # int unsigned
9223372036854775807, # big signed
18446744073709551615, # big unsigned
)
]
insert = _get_insert_stmt(self.tables['int'], columns)
select = _get_select_stmt(self.tables['int'], columns)
cur.executemany(insert, data)
cur.execute(select)
rows = cur.fetchall()
for i, col in enumerate(columns):
self.compare(col, data[0][i], rows[0][i])
self.compare(col, data[1][i], rows[1][i])
cur.close()
def test_numeric_bit(self):
"""MySQL numeric bit data type"""
cur = self.cnx.cursor()
columns = [
'c8', 'c16', 'c24', 'c32',
'c40', 'c48', 'c56', 'c63',
'c64']
cur.execute((
"CREATE TABLE {table} ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`c8` bit(8) DEFAULT NULL,"
"`c16` bit(16) DEFAULT NULL,"
"`c24` bit(24) DEFAULT NULL,"
"`c32` bit(32) DEFAULT NULL,"
"`c40` bit(40) DEFAULT NULL,"
"`c48` bit(48) DEFAULT NULL,"
"`c56` bit(56) DEFAULT NULL,"
"`c63` bit(63) DEFAULT NULL,"
"`c64` bit(64) DEFAULT NULL,"
"PRIMARY KEY (id))"
).format(table=self.tables['bit'])
)
insert = _get_insert_stmt(self.tables['bit'], columns)
select = _get_select_stmt(self.tables['bit'], columns)
data = list()
data.append(tuple([0] * len(columns)))
values = list()
for col in columns:
values.append(1 << int(col.replace('c', '')) - 1)
data.append(tuple(values))
values = list()
for col in columns:
values.append((1 << int(col.replace('c', ''))) - 1)
data.append(tuple(values))
cur.executemany(insert, data)
cur.execute(select)
rows = cur.fetchall()
self.assertEqual(rows, data)
cur.close()
def test_numeric_float(self):
"""MySQL numeric float data type"""
cur = self.cnx.cursor()
columns = [
'float_signed',
'float_unsigned',
'double_signed',
'double_unsigned',
]
cur.execute((
"CREATE TABLE {table} ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`float_signed` FLOAT(6,5) SIGNED,"
"`float_unsigned` FLOAT(6,5) UNSIGNED,"
"`double_signed` DOUBLE(15,10) SIGNED,"
"`double_unsigned` DOUBLE(15,10) UNSIGNED,"
"PRIMARY KEY (id))"
).format(table=self.tables['float'])
)
insert = _get_insert_stmt(self.tables['float'], columns)
select = _get_select_stmt(self.tables['float'], columns)
data = [
(-3.402823466, 0, -1.7976931348623157, 0,),
(-1.175494351, 3.402823466,
1.7976931348623157, 2.2250738585072014),
(-1.23455678, 2.999999, -1.3999999999999999, 1.9999999999999999),
]
cur.executemany(insert, data)
cur.execute(select)
rows = cur.fetchall()
for j in range(0, len(data)):
for i, col in enumerate(columns[0:2]):
self.compare(col, round(data[j][i], 5), rows[j][i])
for i, col in enumerate(columns[2:2]):
self.compare(col, round(data[j][i], 10), rows[j][i])
cur.close()
def test_numeric_decimal(self):
"""MySQL numeric decimal data type"""
cur = self.cnx.cursor()
columns = [
'decimal_signed',
'decimal_unsigned',
]
cur.execute((
"CREATE TABLE {table} ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`decimal_signed` DECIMAL(65,30) SIGNED,"
"`decimal_unsigned` DECIMAL(65,30) UNSIGNED,"
"PRIMARY KEY (id))"
).format(table=self.tables['decimal'])
)
insert = _get_insert_stmt(self.tables['decimal'], columns)
select = _get_select_stmt(self.tables['decimal'], columns)
data = [
(Decimal(
'-9999999999999999999999999.999999999999999999999999999999'),
Decimal(
'+9999999999999999999999999.999999999999999999999999999999')),
(Decimal('-1234567.1234'),
Decimal('+123456789012345.123456789012345678901')),
(Decimal(
'-1234567890123456789012345.123456789012345678901234567890'),
Decimal(
'+1234567890123456789012345.123456789012345678901234567890')),
]
cur.executemany(insert, data)
cur.execute(select)
rows = cur.fetchall()
self.assertEqual(data, rows)
cur.close()
def test_temporal_datetime(self):
"""MySQL temporal date/time data types"""
cur = self.cnx.cursor()
cur.execute("SET SESSION time_zone = '+00:00'")
columns = [
't_date',
't_datetime',
't_time',
't_timestamp',
't_year_4',
]
cur.execute((
"CREATE TABLE {table} ("
"`id` int NOT NULL AUTO_INCREMENT,"
"`t_date` DATE,"
"`t_datetime` DATETIME,"
"`t_time` TIME,"
"`t_timestamp` TIMESTAMP DEFAULT 0,"
"`t_year_4` YEAR(4),"
"PRIMARY KEY (id))"
).format(table=self.tables['temporal'])
)
insert = _get_insert_stmt(self.tables['temporal'], columns)
select = _get_select_stmt(self.tables['temporal'], columns)
data = [
(datetime.date(2010, 1, 17),
datetime.datetime(2010, 1, 17, 19, 31, 12),
datetime.timedelta(hours=43, minutes=32, seconds=21),
datetime.datetime(2010, 1, 17, 19, 31, 12),
0),
(datetime.date(1000, 1, 1),
datetime.datetime(1000, 1, 1, 0, 0, 0),
datetime.timedelta(hours=-838, minutes=59, seconds=59),
datetime.datetime(*time.gmtime(1)[:6]),
1901),
(datetime.date(9999, 12, 31),
datetime.datetime(9999, 12, 31, 23, 59, 59),
datetime.timedelta(hours=838, minutes=59, seconds=59),
datetime.datetime(2038, 1, 19, 3, 14, 7),
2155),
]
cur.executemany(insert, data)
cur.execute(select)
rows = cur.fetchall()
for j in (range(0, len(data))):
for i, col in enumerate(columns):
self.compare("{column} (data[{count}])".format(
column=col, count=j), data[j][i], rows[j][i])
# Testing YEAR(2), which is now obsolete since MySQL 5.6.6
tblname = self.tables['temporal_year']
cur.execute(
"CREATE TABLE {table} ("
"`id` int NOT NULL AUTO_INCREMENT KEY, "
"`t_year_2` YEAR(2))".format(table=tblname)
)
cur.execute(_get_insert_stmt(tblname, ['t_year_2']), (10,))
cur.execute(_get_select_stmt(tblname, ['t_year_2']))
row = cur.fetchone()
if tests.MYSQL_VERSION >= (5, 6, 6):
self.assertEqual(2010, row[0])
else:
self.assertEqual(10, row[0])
cur.close()
|