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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
|
# -*- coding: utf-8 -*-
# MySQL Connector/Python - MySQL driver written in Python.
# Copyright (c) 2009, 2016, 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.connector.conversion
"""
from decimal import Decimal
import datetime
import time
import uuid
import tests
from mysql.connector import conversion, constants
from mysql.connector.catch23 import PY2
class MySQLConverterBaseTests(tests.MySQLConnectorTests):
def test_init(self):
cnv = conversion.MySQLConverterBase()
self.assertEqual('utf8', cnv.charset)
self.assertEqual(True, cnv.use_unicode)
def test_init2(self):
cnv = conversion.MySQLConverterBase(charset='latin1',
use_unicode=False)
self.assertEqual('latin1', cnv.charset)
self.assertEqual(False, cnv.use_unicode)
def test_set_charset(self):
cnv = conversion.MySQLConverterBase()
cnv.set_charset('latin2')
self.assertEqual('latin2', cnv.charset)
def test_set_useunicode(self):
cnv = conversion.MySQLConverterBase()
cnv.set_unicode(False)
self.assertEqual(False, cnv.use_unicode)
def test_to_mysql(self):
cnv = conversion.MySQLConverterBase()
self.assertEqual('a value', cnv.to_mysql('a value'))
def test_to_python(self):
cnv = conversion.MySQLConverterBase()
self.assertEqual('a value', cnv.to_python('nevermind', 'a value'))
def test_escape(self):
cnv = conversion.MySQLConverterBase()
self.assertEqual("'a value'", cnv.escape("'a value'"))
def test_quote(self):
cnv = conversion.MySQLConverterBase()
self.assertEqual("'a value'", cnv.escape("'a value'"))
class MySQLConverterTests(tests.MySQLConnectorTests):
_to_python_data = [
(b'3.14', ('float', constants.FieldType.FLOAT)),
(b'128', ('int', constants.FieldType.TINY)),
(b'1281288', ('long', constants.FieldType.LONG)),
(b'3.14', ('decimal', constants.FieldType.DECIMAL)),
(b'2008-05-07', ('date', constants.FieldType.DATE)),
(b'45:34:10', ('time', constants.FieldType.TIME)),
(b'2008-05-07 22:34:10',
('datetime', constants.FieldType.DATETIME)),
(b'val1,val2', ('set', constants.FieldType.SET, None,
None, None, None, True, constants.FieldFlag.SET)),
('2008', ('year', constants.FieldType.YEAR)),
(b'\x80\x00\x00\x00', ('bit', constants.FieldType.BIT)),
(b'\xc3\xa4 utf8 string', ('utf8', constants.FieldType.STRING,
None, None, None, None, True, 0)),
]
_to_python_exp = (
float(_to_python_data[0][0]),
int(_to_python_data[1][0]),
int(_to_python_data[2][0]),
Decimal('3.14'),
datetime.date(2008, 5, 7),
datetime.timedelta(hours=45, minutes=34, seconds=10),
datetime.datetime(2008, 5, 7, 22, 34, 10),
set(['val1', 'val2']),
int(_to_python_data[8][0]),
2147483648,
unicode(b'\xc3\xa4 utf8 string', 'utf8') if PY2 \
else str(b'\xc3\xa4 utf8 string', 'utf8')
)
def setUp(self):
self.cnv = conversion.MySQLConverter()
def tearDown(self):
pass
def test_init(self):
pass
def test_escape(self):
"""Making strings ready for MySQL operations"""
data = (
None, # should stay the same
int(128), # should stay the same
int(1281288), # should stay the same
float(3.14), # should stay the same
Decimal('3.14'), # should stay a Decimal
r'back\slash',
'newline\n',
'return\r',
"'single'",
'"double"',
'windows\032',
)
exp = (
None,
128,
1281288,
float(3.14),
Decimal("3.14"),
'back\\\\slash',
'newline\\n',
'return\\r',
"\\'single\\'",
'\\"double\\"',
'windows\\\x1a'
)
res = tuple([self.cnv.escape(v) for v in data])
self.assertTrue(res, exp)
def test_quote(self):
"""Quote values making them ready for MySQL operations."""
data = [
None,
int(128),
int(1281288),
float(3.14),
Decimal('3.14'),
b'string A',
b"string B",
]
exp = (
b'NULL',
b'128',
b'1281288',
repr(float(3.14)) if PY2 else b'3.14',
b'3.14',
b"'string A'",
b"'string B'",
)
res = tuple([self.cnv.quote(value) for value in data])
self.assertEqual(res, exp)
def test_to_mysql(self):
"""Convert Python types to MySQL types using helper method"""
st_now = time.localtime()
data = (
128, # int
1281288, # long
float(3.14), # float
'Strings are sexy',
r'\u82b1',
None,
datetime.datetime(2008, 5, 7, 20, 0o1, 23),
datetime.date(2008, 5, 7),
datetime.time(20, 0o3, 23),
st_now,
datetime.timedelta(hours=40, minutes=30, seconds=12),
Decimal('3.14'),
)
exp = (
data[0],
data[1],
data[2],
self.cnv._str_to_mysql(data[3]),
self.cnv._str_to_mysql(data[4]),
None,
b'2008-05-07 20:01:23',
b'2008-05-07',
b'20:03:23',
time.strftime('%Y-%m-%d %H:%M:%S', st_now).encode('ascii'),
b'40:30:12',
b'3.14',
)
res = tuple([self.cnv.to_mysql(value) for value in data])
self.assertEqual(res, exp)
self.assertRaises(TypeError, self.cnv.to_mysql, uuid.uuid4())
def test__str_to_mysql(self):
"""A Python string becomes bytes."""
data = 'This is a string'
exp = data.encode()
res = self.cnv._str_to_mysql(data)
self.assertEqual(exp, res)
def test__bytes_to_mysql(self):
"""A Python bytes stays bytes."""
data = b'This is a bytes'
exp = data
res = self.cnv._bytes_to_mysql(data)
self.assertEqual(exp, res)
def test__bytearray_to_mysql(self):
"""A Python bytearray becomes bytes."""
data = bytearray(b'This is a bytearray',)
exp = bytes(data)
res = self.cnv._bytearray_to_mysql(data)
self.assertEqual(exp, res)
def test__nonetype_to_mysql(self):
"""Python None stays None for MySQL."""
data = None
res = self.cnv._nonetype_to_mysql(data)
self.assertEqual(data, res)
def test__datetime_to_mysql(self):
"""A datetime.datetime becomes formatted like Y-m-d H:M:S[.f]"""
cases = [
(datetime.datetime(2008, 5, 7, 20, 1, 23),
b'2008-05-07 20:01:23'),
(datetime.datetime(2012, 5, 2, 20, 1, 23, 10101),
b'2012-05-02 20:01:23.010101')
]
for data, exp in cases:
self.assertEqual(exp, self.cnv._datetime_to_mysql(data))
def test__date_to_mysql(self):
"""A datetime.date becomes formatted like Y-m-d"""
data = datetime.date(2008, 5, 7)
res = self.cnv._date_to_mysql(data)
exp = data.strftime('%Y-%m-%d').encode('ascii')
self.assertEqual(exp, res)
def test__time_to_mysql(self):
"""A datetime.time becomes formatted like Y-m-d H:M:S[.f]"""
cases = [
(datetime.time(20, 3, 23), b'20:03:23'),
(datetime.time(20, 3, 23, 10101), b'20:03:23.010101'),
]
for data, exp in cases:
self.assertEqual(exp, self.cnv._time_to_mysql(data))
def test__struct_time_to_mysql(self):
"""A time.struct_time becomes formatted like Y-m-d H:M:S"""
data = time.localtime()
res = self.cnv._struct_time_to_mysql(data)
exp = time.strftime('%Y-%m-%d %H:%M:%S', data).encode('ascii')
self.assertEqual(exp, res)
def test__timedelta_to_mysql(self):
"""A datetime.timedelta becomes format like 'H:M:S[.f]'"""
cases = [
(datetime.timedelta(hours=40, minutes=30, seconds=12),
b'40:30:12'),
(datetime.timedelta(hours=-40, minutes=30, seconds=12),
b'-39:29:48'),
(datetime.timedelta(hours=40, minutes=-1, seconds=12),
b'39:59:12'),
(datetime.timedelta(hours=-40, minutes=60, seconds=12),
b'-38:59:48'),
(datetime.timedelta(hours=40, minutes=30, seconds=12,
microseconds=10101),
b'40:30:12.010101'),
(datetime.timedelta(hours=-40, minutes=30, seconds=12,
microseconds=10101),
b'-39:29:47.989899'),
(datetime.timedelta(hours=40, minutes=-1, seconds=12,
microseconds=10101),
b'39:59:12.010101'),
(datetime.timedelta(hours=-40, minutes=60, seconds=12,
microseconds=10101),
b'-38:59:47.989899'),
]
for i, case in enumerate(cases):
data, exp = case
self.assertEqual(exp, self.cnv._timedelta_to_mysql(data),
"Case {0} failed: {1}; got {2}".format(
i + 1, repr(data),
self.cnv._timedelta_to_mysql(data)))
def test__decimal_to_mysql(self):
"""A decimal.Decimal becomes a string."""
data = Decimal('3.14')
self.assertEqual(b'3.14', self.cnv._decimal_to_mysql(data))
def test_to_python(self):
"""Convert MySQL data to Python types using helper method"""
res = tuple(
[self.cnv.to_python(v[1], v[0]) for v in self._to_python_data])
self.assertEqual(res, tuple(self._to_python_exp))
def test_row_to_python(self):
data = [v[0] for v in self._to_python_data]
description = [v[1] for v in self._to_python_data]
res = self.cnv.row_to_python(data, description)
self.assertEqual(res, self._to_python_exp)
def test__FLOAT_to_python(self):
"""Convert a MySQL FLOAT/DOUBLE to a Python float type"""
data = b'3.14'
exp = float(data)
res = self.cnv._FLOAT_to_python(data)
self.assertEqual(exp, res)
self.assertEqual(self.cnv._FLOAT_to_python,
self.cnv._DOUBLE_to_python)
def test__INT_to_python(self):
"""Convert a MySQL TINY/SHORT/INT24/INT to a Python int type"""
data = b'128'
exp = int(data)
res = self.cnv._INT_to_python(data)
self.assertEqual(exp, res)
self.assertEqual(self.cnv._INT_to_python, self.cnv._TINY_to_python)
self.assertEqual(self.cnv._INT_to_python, self.cnv._SHORT_to_python)
self.assertEqual(self.cnv._INT_to_python, self.cnv._INT24_to_python)
def test__LONG_to_python(self):
"""Convert a MySQL LONG/LONGLONG to a Python long type"""
data = b'1281288'
exp = int(data)
res = self.cnv._LONG_to_python(data)
self.assertEqual(exp, res)
self.assertEqual(self.cnv._LONG_to_python,
self.cnv._LONGLONG_to_python)
def test__DECIMAL_to_python(self):
"""Convert a MySQL DECIMAL to a Python decimal.Decimal type"""
data = b'3.14'
exp = Decimal('3.14')
res = self.cnv._DECIMAL_to_python(data)
self.assertEqual(exp, res)
self.assertEqual(self.cnv._DECIMAL_to_python,
self.cnv._NEWDECIMAL_to_python)
def test__BIT_to_python(self):
"""Convert a MySQL BIT to Python int"""
data = [
b'\x80',
b'\x80\x00',
b'\x80\x00\x00',
b'\x80\x00\x00\x00',
b'\x80\x00\x00\x00\x00',
b'\x80\x00\x00\x00\x00\x00',
b'\x80\x00\x00\x00\x00\x00\x00',
b'\x80\x00\x00\x00\x00\x00\x00\x00',
]
exp = [128, 32768, 8388608, 2147483648, 549755813888,
140737488355328, 36028797018963968, 9223372036854775808]
for i, buf in enumerate(data):
self.assertEqual(self.cnv._BIT_to_python(buf), exp[i])
def test__DATE_to_python(self):
"""Convert a MySQL DATE to a Python datetime.date type"""
data = b'2008-05-07'
exp = datetime.date(2008, 5, 7)
res = self.cnv._DATE_to_python(data)
self.assertEqual(exp, res)
res = self.cnv._DATE_to_python(b'0000-00-00')
self.assertEqual(None, res)
res = self.cnv._DATE_to_python(b'1000-00-00')
self.assertEqual(None, res)
def test__TIME_to_python(self):
"""Convert a MySQL TIME to a Python datetime.time type"""
cases = [
(b'45:34:10',
datetime.timedelta(hours=45, minutes=34, seconds=10)),
(b'-45:34:10',
datetime.timedelta(-2, 8750)),
(b'45:34:10.010101',
datetime.timedelta(hours=45, minutes=34, seconds=10,
microseconds=10101)),
(b'-45:34:10.010101',
datetime.timedelta(-2, 8749, 989899)),
]
for i, case in enumerate(cases):
data, exp = case
self.assertEqual(exp, self.cnv._TIME_to_python(data),
"Case {0} failed: {1}; got {2}".format(
i + 1, repr(data),
repr(self.cnv._TIME_to_python(data))))
def test__DATETIME_to_python(self):
"""Convert a MySQL DATETIME to a Python datetime.datetime type"""
cases = [
(b'2008-05-07 22:34:10',
datetime.datetime(2008, 5, 7, 22, 34, 10)),
(b'2008-05-07 22:34:10.010101',
datetime.datetime(2008, 5, 7, 22, 34, 10, 10101)),
(b'0000-00-00 00:00:00', None),
(b'1000-00-00 00:00:00', None),
]
for data, exp in cases:
self.assertEqual(exp, self.cnv._DATETIME_to_python(data))
def test__YEAR_to_python(self):
"""Convert a MySQL YEAR to Python int"""
data = '2008'
exp = 2008
self.assertEqual(exp, self.cnv._YEAR_to_python(data))
data = 'foobar'
self.assertRaises(ValueError, self.cnv._YEAR_to_python, data)
def test__SET_to_python(self):
"""Convert a MySQL SET type to a Python sequence
This actually calls hte _STRING_to_python() method since a SET is
returned as string by MySQL. However, the description of the field
has in it's field flags that the string is a SET.
"""
data = b'val1,val2'
exp = set(['val1', 'val2'])
desc = ('foo', constants.FieldType.STRING,
2, 3, 4, 5, 6, constants.FieldFlag.SET)
res = self.cnv._STRING_to_python(data, desc)
self.assertEqual(exp, res)
def test__STRING_to_python_utf8(self):
"""Convert a UTF-8 MySQL STRING/VAR_STRING to a Python Unicode type"""
self.cnv.set_charset('utf8') # default
data = b'\xc3\xa4 utf8 string'
exp = data.decode('utf-8')
res = self.cnv._STRING_to_python(data)
self.assertEqual(exp, res)
def test__STRING_to_python_latin1(self):
"""Convert a ISO-8859-1 MySQL STRING/VAR_STRING to a Python str"""
self.cnv.set_charset('latin1')
self.cnv.set_unicode(False)
data = b'\xe4 latin string'
exp = data
res = self.cnv._STRING_to_python(data)
self.assertEqual(exp, res)
exp = data.decode('latin1')
self.cnv.set_unicode(True)
res = self.cnv._STRING_to_python(data)
self.assertEqual(exp, res)
self.cnv.set_charset('utf8')
self.cnv.set_unicode(True)
def test__STRING_to_python_binary(self):
"""Convert a STRING BINARY to Python bytes type"""
data = b'\x33\xfd\x34\xed'
desc = ('foo', constants.FieldType.STRING,
2, 3, 4, 5, 6, constants.FieldFlag.BINARY)
res = self.cnv._STRING_to_python(data, desc)
self.assertEqual(data, res)
def test__BLOB_to_python_binary(self):
"""Convert a BLOB BINARY to Python bytes type"""
data = b'\x33\xfd\x34\xed'
desc = ('foo', constants.FieldType.BLOB,
2, 3, 4, 5, 6, constants.FieldFlag.BINARY)
res = self.cnv._BLOB_to_python(data, desc)
self.assertEqual(data, res)
|