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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
|
# -*- coding: utf-8 -*-
# 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, Incur., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""Test module for bugs
Bug test cases specific to a particular Python (major) version are loaded
from py2.bugs or py3.bugs.
This module was originally located in python2/tests and python3/tests. It
should contain bug test cases which work for both Python v2 and v3.
Whenever a bug is bout to a specific Python version, put the test cases
in tests/py2/bugs.py or tests/py3/bugs.py. It might be that these files need
to be created first.
"""
import datetime
from collections import namedtuple
from decimal import Decimal
import re
import time
from . import PY2
import tests
from mysql.connector import (connection, cursor, errors)
class CursorModule(tests.MySQLConnectorTests):
"""
Tests for the cursor module functions and attributes
"""
def test_RE_SQL_INSERT_VALUES(self):
regex = cursor.RE_SQL_INSERT_VALUES
cases = [
("(%s, %s)",
"INSERT INTO t1 VALUES (%s, %s)"),
("( %s, \n %s )",
"INSERT INTO t1 VALUES ( %s, \n %s )"),
("(%(c1)s, %(c2)s)",
"INSERT INTO t1 VALUES (%(c1)s, %(c2)s)"),
("(\n%(c1)s\n, \n%(c2)s\n)",
"INSERT INTO t1 VALUES \n(\n%(c1)s\n, \n%(c2)s\n)"),
("( %(c1)s , %(c2)s )",
"INSERT INTO t1 VALUES ( %(c1)s , %(c2)s ) ON DUPLICATE"),
("(%s, %s, NOW())",
"INSERT INTO t1 VALUES (%s, %s, NOW())"),
("(%s, CONCAT('a', 'b'), %s, NOW())",
"INSERT INTO t1 VALUES (%s, CONCAT('a', 'b'), %s, NOW())"),
("( NOW(), %s, \n, CONCAT('a', 'b'), %s )",
"INSERT INTO t1 VALUES "
" ( NOW(), %s, \n, CONCAT('a', 'b'), %s )"),
("(%(c1)s, NOW(6), %(c2)s)",
"INSERT INTO t1 VALUES (%(c1)s, NOW(6), %(c2)s)"),
("(\n%(c1)s\n, \n%(c2)s, REPEAT('a', 20)\n)",
"INSERT INTO t1 VALUES "
"\n(\n%(c1)s\n, \n%(c2)s, REPEAT('a', 20)\n)"),
("( %(c1)s ,NOW(),REPEAT('a', 20)\n), %(c2)s )",
"INSERT INTO t1 VALUES "
" ( %(c1)s ,NOW(),REPEAT('a', 20)\n), %(c2)s ) ON DUPLICATE"),
("( %(c1)s, %(c2)s )",
"INSERT INTO `values` VALUES "
" ( %(c1)s, %(c2)s ) ON DUPLICATE"),
]
for exp, stmt in cases:
self.assertEqual(exp, re.search(regex, stmt).group(1))
class CursorBaseTests(tests.MySQLConnectorTests):
def setUp(self):
self.cur = cursor.CursorBase()
def test___init__(self):
exp = {
'_description': None,
'_rowcount': -1,
'arraysize': 1,
}
for key, value in exp.items():
self.assertEqual(value, getattr(self.cur, key),
msg="Default for '%s' did not match." % key)
def test_callproc(self):
"""CursorBase object callproc()-method"""
self.check_method(self.cur, 'callproc')
try:
self.cur.callproc('foo', args=(1, 2, 3))
except (SyntaxError, TypeError):
self.fail("Cursor callproc(): wrong arguments")
def test_close(self):
"""CursorBase object close()-method"""
self.check_method(self.cur, 'close')
def test_execute(self):
"""CursorBase object execute()-method"""
self.check_method(self.cur, 'execute')
try:
self.cur.execute('select', params=(1, 2, 3))
except (SyntaxError, TypeError):
self.fail("Cursor execute(): wrong arguments")
def test_executemany(self):
"""CursorBase object executemany()-method"""
self.check_method(self.cur, 'executemany')
try:
self.cur.executemany('select', [()])
except (SyntaxError, TypeError):
self.fail("Cursor executemany(): wrong arguments")
def test_fetchone(self):
"""CursorBase object fetchone()-method"""
self.check_method(self.cur, 'fetchone')
def test_fetchmany(self):
"""CursorBase object fetchmany()-method"""
self.check_method(self.cur, 'fetchmany')
try:
self.cur.fetchmany(size=1)
except (SyntaxError, TypeError):
self.fail("Cursor fetchmany(): wrong arguments")
def test_fetchall(self):
"""CursorBase object fetchall()-method"""
self.check_method(self.cur, 'fetchall')
def test_nextset(self):
"""CursorBase object nextset()-method"""
self.check_method(self.cur, 'nextset')
def test_setinputsizes(self):
"""CursorBase object setinputsizes()-method"""
self.check_method(self.cur, 'setinputsizes')
try:
self.cur.setinputsizes((1,))
except (SyntaxError, TypeError):
self.fail("CursorBase setinputsizes(): wrong arguments")
def test_setoutputsize(self):
"""CursorBase object setoutputsize()-method"""
self.check_method(self.cur, 'setoutputsize')
try:
self.cur.setoutputsize(1, column=None)
except (SyntaxError, TypeError):
self.fail("CursorBase setoutputsize(): wrong arguments")
def test_description(self):
self.assertEqual(None, self.cur.description)
self.assertEqual(self.cur._description, self.cur.description)
self.cur._description = 'ham'
self.assertEqual('ham', self.cur.description)
if tests.OLD_UNITTEST:
try:
self.cur.description = 'spam'
except AttributeError as err:
# Exception should be raised
pass
else:
self.fail("AttributeError was not raised")
else:
with self.assertRaises(AttributeError):
self.cur.description = 'spam'
def test_rowcount(self):
self.assertEqual(-1, self.cur.rowcount)
self.assertEqual(self.cur._rowcount, self.cur.rowcount)
self.cur._rowcount = 2
self.assertEqual(2, self.cur.rowcount)
if tests.OLD_UNITTEST:
try:
self.cur.description = 'spam'
except AttributeError as err:
# Exception should be raised
pass
else:
self.fail("AttributeError was not raised")
else:
with self.assertRaises(AttributeError):
self.cur.rowcount = 3
def test_last_insert_id(self):
self.assertEqual(None, self.cur.lastrowid)
self.assertEqual(self.cur._last_insert_id, self.cur.lastrowid)
self.cur._last_insert_id = 2
self.assertEqual(2, self.cur.lastrowid)
if tests.OLD_UNITTEST:
try:
self.cur.description = 'spam'
except AttributeError as err:
# Exception should be raised
pass
else:
self.fail("AttributeError was not raised")
else:
with self.assertRaises(AttributeError):
self.cur.lastrowid = 3
class MySQLCursorTests(tests.TestsCursor):
def setUp(self):
self.cur = cursor.MySQLCursor(connection=None)
self.cnx = None
def test_init(self):
"""MySQLCursor object init"""
try:
cur = cursor.MySQLCursor(connection=None)
except (SyntaxError, TypeError) as err:
self.fail("Failed initializing MySQLCursor; {0}".format(err))
exp = {
'_connection': None,
'_stored_results': [],
'_nextrow': (None, None),
'_warnings': None,
'_warning_count': 0,
'_executed': None,
'_executed_list': [],
}
for key, value in exp.items():
self.assertEqual(
value, getattr(cur, key),
msg="Default for '{0}' did not match.".format(key))
self.assertRaises(errors.InterfaceError, cursor.MySQLCursor,
connection='foo')
def test__set_connection(self):
"""MySQLCursor object _set_connection()-method"""
self.check_method(self.cur, '_set_connection')
self.assertRaises(errors.InterfaceError,
self.cur._set_connection, 'foo')
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur._set_connection(self.cnx)
self.cur.close()
def test__reset_result(self):
"""MySQLCursor object _reset_result()-method"""
self.check_method(self.cur, '_reset_result')
def reset(self):
self._test = "Reset called"
self.cur.reset = reset.__get__(self.cur, cursor.MySQLCursor)
exp = {
'rowcount': -1,
'_stored_results': [],
'_nextrow': (None, None),
'_warnings': None,
'_warning_count': 0,
'_executed': None,
'_executed_list': [],
}
self.cur._reset_result()
for key, value in exp.items():
self.assertEqual(value, getattr(self.cur, key),
msg="'{0}' was not reset.".format(key))
# MySQLCursor._reset_result() must call MySQLCursor.reset()
self.assertEqual('Reset called',
self.cur._test) # pylint: disable=E1103
def test__have_unread_result(self):
"""MySQLCursor object _have_unread_result()-method"""
self.check_method(self.cur, '_have_unread_result')
class FakeConnection(object):
def __init__(self):
self.unread_result = False
self.cur = cursor.MySQLCursor()
self.cur._connection = FakeConnection()
self.cur._connection.unread_result = True
self.assertTrue(self.cur._have_unread_result())
self.cur._connection.unread_result = False
self.assertFalse(self.cur._have_unread_result())
def test_next(self):
"""MySQLCursor object next()-method"""
self.check_method(self.cur, 'next')
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = cursor.MySQLCursor(self.cnx)
self.assertRaises(StopIteration, self.cur.__next__)
self.cur.execute("SELECT BINARY 'ham'")
exp = (b'ham',)
self.assertEqual(exp, next(self.cur))
self.cur.close()
def test_close(self):
"""MySQLCursor object close()-method"""
self.check_method(self.cur, 'close')
self.assertEqual(False, self.cur.close(),
"close() should return False with no connection")
self.assertEqual(None, self.cur._connection)
def test__process_params(self):
"""MySQLCursor object _process_params()-method"""
self.check_method(self.cur, '_process_params')
self.assertRaises(
errors.ProgrammingError, self.cur._process_params, 'foo')
self.assertRaises(errors.ProgrammingError, self.cur._process_params, ())
st_now = time.localtime()
data = (
None,
int(128),
int(1281288),
float(3.14),
Decimal('3.14'),
r'back\slash',
'newline\n',
'return\r',
"'single'",
'"double"',
'windows\032',
"Strings are sexy",
u'\u82b1',
datetime.datetime(2008, 5, 7, 20, 1, 23),
datetime.date(2008, 5, 7),
datetime.time(20, 3, 23),
st_now,
datetime.timedelta(hours=40, minutes=30, seconds=12),
'foo %(t)s',
'foo %(s)s',
)
exp = (
b'NULL',
b'128',
b'1281288',
repr(float(3.14)) if PY2 else b'3.14',
b"'3.14'",
b"'back\\\\slash'",
b"'newline\\n'",
b"'return\\r'",
b"'\\'single\\''",
b'\'\\"double\\"\'',
b"'windows\\\x1a'",
b"'Strings are sexy'",
b"'\xe8\x8a\xb1'",
b"'2008-05-07 20:01:23'",
b"'2008-05-07'",
b"'20:03:23'",
b"'" + time.strftime('%Y-%m-%d %H:%M:%S', st_now).encode('ascii')
+ b"'",
b"'40:30:12'",
b"'foo %(t)s'",
b"'foo %(s)s'",
)
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = self.cnx.cursor()
self.assertEqual((), self.cur._process_params(()),
"_process_params() should return a tuple")
res = self.cur._process_params(data)
for (i, exped) in enumerate(exp):
self.assertEqual(exped, res[i])
self.cur.close()
def test__process_params_dict(self):
"""MySQLCursor object _process_params_dict()-method"""
self.check_method(self.cur, '_process_params')
self.assertRaises(
errors.ProgrammingError, self.cur._process_params, 'foo')
self.assertRaises(errors.ProgrammingError, self.cur._process_params, ())
st_now = time.localtime()
data = {
'a': None,
'b': int(128),
'c': int(1281288),
'd': float(3.14),
'e': Decimal('3.14'),
'f': 'back\slash', # pylint: disable=W1401
'g': 'newline\n',
'h': 'return\r',
'i': "'single'",
'j': '"double"',
'k': 'windows\032',
'l': str("Strings are sexy"),
'm': u'\u82b1',
'n': datetime.datetime(2008, 5, 7, 20, 1, 23),
'o': datetime.date(2008, 5, 7),
'p': datetime.time(20, 3, 23),
'q': st_now,
'r': datetime.timedelta(hours=40, minutes=30, seconds=12),
's': 'foo %(t)s',
't': 'foo %(s)s',
}
exp = {
b'a': b'NULL',
b'b': b'128',
b'c': b'1281288',
b'd': repr(float(3.14)) if PY2 else b'3.14',
b'e': b"'3.14'",
b'f': b"'back\\\\slash'",
b'g': b"'newline\\n'",
b'h': b"'return\\r'",
b'i': b"'\\'single\\''",
b'j': b'\'\\"double\\"\'',
b'k': b"'windows\\\x1a'",
b'l': b"'Strings are sexy'",
b'm': b"'\xe8\x8a\xb1'",
b'n': b"'2008-05-07 20:01:23'",
b'o': b"'2008-05-07'",
b'p': b"'20:03:23'",
b'q': b"'" +
time.strftime('%Y-%m-%d %H:%M:%S', st_now).encode('ascii')
+ b"'",
b'r': b"'40:30:12'",
b's': b"'foo %(t)s'",
b't': b"'foo %(s)s'",
}
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = self.cnx.cursor()
self.assertEqual({}, self.cur._process_params_dict({}),
"_process_params_dict() should return a dict")
self.assertEqual(exp, self.cur._process_params_dict(data))
self.cur.close()
def test__fetch_warnings(self):
"""MySQLCursor object _fetch_warnings()-method"""
self.check_method(self.cur, '_fetch_warnings')
self.assertRaises(errors.InterfaceError, self.cur._fetch_warnings)
config = tests.get_mysql_config()
config['get_warnings'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
self.cur.execute("SELECT 'a' + 'b'")
self.cur.fetchone()
exp = [
('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'"),
('Warning', 1292, "Truncated incorrect DOUBLE value: 'b'")
]
self.assertTrue(tests.cmp_result(exp, self.cur._fetch_warnings()))
self.assertEqual(len(exp), self.cur._warning_count)
def test__handle_noresultset(self):
"""MySQLCursor object _handle_noresultset()-method"""
self.check_method(self.cur, '_handle_noresultset')
self.assertRaises(errors.ProgrammingError,
self.cur._handle_noresultset, None)
data = {
'affected_rows': 1,
'insert_id': 10,
'warning_count': 100,
'server_status': 8,
}
config = tests.get_mysql_config()
config['get_warnings'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
self.cur._handle_noresultset(data)
self.assertEqual(data['affected_rows'], self.cur.rowcount)
self.assertEqual(data['insert_id'], self.cur.lastrowid)
self.assertEqual(data['warning_count'], self.cur._warning_count)
def test__handle_result(self):
"""MySQLCursor object _handle_result()-method"""
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = self.cnx.cursor()
self.assertRaises(errors.InterfaceError, self.cur._handle_result, None)
self.assertRaises(errors.InterfaceError, self.cur._handle_result,
'spam')
self.assertRaises(errors.InterfaceError, self.cur._handle_result,
{'spam': 5})
cases = [
{'affected_rows': 99999,
'insert_id': 10,
'warning_count': 100,
'server_status': 8,
},
{'eof': {'status_flag': 0, 'warning_count': 0},
'columns': [('1', 8, None, None, None, None, 0, 129)]
},
]
self.cur._handle_result(cases[0])
self.assertEqual(cases[0]['affected_rows'], self.cur.rowcount)
self.assertFalse(self.cur._connection.unread_result)
self.assertFalse(self.cur._have_unread_result())
self.cur._handle_result(cases[1])
self.assertEqual(cases[1]['columns'], self.cur.description)
self.assertTrue(self.cur._connection.unread_result)
self.assertTrue(self.cur._have_unread_result())
def test_execute(self):
"""MySQLCursor object execute()-method"""
self.check_method(self.cur, 'execute')
self.assertEqual(None, self.cur.execute(None, None))
config = tests.get_mysql_config()
config['get_warnings'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
self.assertRaises(errors.ProgrammingError, self.cur.execute,
'SELECT %s,%s,%s', ('foo', 'bar',))
self.cur.execute("SELECT 'a' + 'b'")
self.cur.fetchone()
exp = [
('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'"),
('Warning', 1292, "Truncated incorrect DOUBLE value: 'b'")
]
self.assertTrue(tests.cmp_result(exp, self.cur._warnings))
self.cur.execute("SELECT BINARY 'ham'")
exp = [(b'ham',)]
self.assertEqual(exp, self.cur.fetchall())
self.cur.close()
tbl = 'myconnpy_cursor'
self._test_execute_setup(self.cnx, tbl)
stmt_insert = "INSERT INTO {0} (col1,col2) VALUES (%s,%s)".format(tbl)
self.cur = self.cnx.cursor()
res = self.cur.execute(stmt_insert, (1, 100))
self.assertEqual(None, res, "Return value of execute() is wrong.")
stmt_select = "SELECT col1,col2 FROM {0} ORDER BY col1".format(tbl)
self.cur.execute(stmt_select)
self.assertEqual([(1, '100')],
self.cur.fetchall(), "Insert test failed")
data = {'id': 2}
stmt = "SELECT col1,col2 FROM {0} WHERE col1 <= %(id)s".format(tbl)
self.cur.execute(stmt, data)
self.assertEqual([(1, '100')], self.cur.fetchall())
self._test_execute_cleanup(self.cnx, tbl)
self.cur.close()
self.cur = self.cnx.cursor()
self.cur.execute("DROP PROCEDURE IF EXISTS multi_results")
procedure = (
"CREATE PROCEDURE multi_results () "
"BEGIN SELECT 1; SELECT 'ham'; END"
)
self.cur.execute(procedure)
exp_stmt = "CALL multi_results()"
if not PY2:
exp_stmt = b"CALL multi_results()"
exp_result = [[(1,)], [(u'ham',)]]
results = []
for result in self.cur.execute(exp_stmt, multi=True):
if result.with_rows:
self.assertEqual(exp_stmt, result._executed)
results.append(result.fetchall())
self.assertEqual(exp_result, results)
self.cur.execute("DROP PROCEDURE multi_results")
def test_executemany(self):
"""MySQLCursor object executemany()-method"""
self.check_method(self.cur, 'executemany')
self.assertEqual(None, self.cur.executemany(None, []))
config = tests.get_mysql_config()
config['get_warnings'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
self.assertRaises(errors.ProgrammingError, self.cur.executemany,
'programming error with string', 'foo')
self.assertRaises(errors.ProgrammingError, self.cur.executemany,
'programming error with 1 element list', ['foo'])
self.assertEqual(None, self.cur.executemany('empty params', []))
self.assertEqual(None, self.cur.executemany('params is None', None))
self.assertRaises(errors.ProgrammingError, self.cur.executemany,
'foo', ['foo'])
self.assertRaises(errors.ProgrammingError, self.cur.executemany,
'SELECT %s', [('foo',), 'foo'])
self.assertRaises(errors.ProgrammingError,
self.cur.executemany,
"INSERT INTO t1 1 %s", [(1,), (2,)])
self.cur.executemany("SELECT SHA1(%s)", [('foo',), ('bar',)])
self.assertEqual(None, self.cur.fetchone())
self.cur.close()
tbl = 'myconnpy_cursor'
self._test_execute_setup(self.cnx, tbl)
stmt_insert = "INSERT INTO {0} (col1,col2) VALUES (%s,%s)".format(tbl)
stmt_select = "SELECT col1,col2 FROM {0} ORDER BY col1".format(tbl)
self.cur = self.cnx.cursor()
res = self.cur.executemany(stmt_insert, [(1, 100), (2, 200), (3, 300)])
self.assertEqual(3, self.cur.rowcount)
res = self.cur.executemany("SELECT %s", [('f',), ('o',), ('o',)])
self.assertEqual(3, self.cur.rowcount)
data = [{'id': 2}, {'id': 3}]
stmt = "SELECT * FROM {0} WHERE col1 <= %(id)s".format(tbl)
self.cur.executemany(stmt, data)
self.assertEqual(5, self.cur.rowcount)
self.cur.execute(stmt_select)
self.assertEqual([(1, '100'), (2, '200'), (3, '300')],
self.cur.fetchall(), "Multi insert test failed")
data = [{'id': 2}, {'id': 3}]
stmt = "DELETE FROM {0} WHERE col1 = %(id)s".format(tbl)
self.cur.executemany(stmt, data)
self.assertEqual(2, self.cur.rowcount)
stmt = "TRUNCATE TABLE {0}".format(tbl)
self.cur.execute(stmt)
stmt = (
"/*comment*/INSERT/*comment*/INTO/*comment*/{0}(col1,col2)VALUES"
"/*comment*/(%s,%s/*comment*/)/*comment()*/ON DUPLICATE KEY UPDATE"
" col1 = VALUES(col1)"
).format(tbl)
self.cur.executemany(stmt, [(4, 100), (5, 200), (6, 300)])
self.assertEqual(3, self.cur.rowcount)
self.cur.execute(stmt_select)
self.assertEqual([(4, '100'), (5, '200'), (6, '300')],
self.cur.fetchall(), "Multi insert test failed")
stmt = "TRUNCATE TABLE {0}".format(tbl)
self.cur.execute(stmt)
stmt = (
"INSERT INTO/*comment*/{0}(col1,col2)VALUES"
"/*comment*/(%s,'/*100*/')/*comment()*/ON DUPLICATE KEY UPDATE "
"col1 = VALUES(col1)"
).format(tbl)
self.cur.executemany(stmt, [(4,), (5,)])
self.assertEqual(2, self.cur.rowcount)
self.cur.execute(stmt_select)
self.assertEqual([(4, '/*100*/'), (5, '/*100*/')],
self.cur.fetchall(), "Multi insert test failed")
self._test_execute_cleanup(self.cnx, tbl)
self.cur.close()
def test_fetchwarnings(self):
"""MySQLCursor object fetchwarnings()-method"""
self.check_method(self.cur, 'fetchwarnings')
self.assertEqual(
None, self.cur.fetchwarnings(),
"There should be no warnings after initiating cursor.")
exp = ['A warning']
self.cur._warnings = exp
self.cur._warning_count = len(self.cur._warnings)
self.assertEqual(exp, self.cur.fetchwarnings())
self.cur.close()
def test_stored_results(self):
"""MySQLCursor object stored_results()-method"""
self.check_method(self.cur, 'stored_results')
self.assertEqual([], self.cur._stored_results)
self.assertTrue(hasattr(self.cur.stored_results(), '__iter__'))
self.cur._stored_results.append('abc')
self.assertEqual('abc', next(self.cur.stored_results()))
try:
_ = next(self.cur.stored_results())
except StopIteration:
pass
except:
self.fail("StopIteration not raised")
def _test_callproc_setup(self, cnx):
self._test_callproc_cleanup(cnx)
stmt_create1 = (
"CREATE PROCEDURE myconnpy_sp_1 "
"(IN pFac1 INT, IN pFac2 INT, OUT pProd INT) "
"BEGIN SET pProd := pFac1 * pFac2; END;")
stmt_create2 = (
"CREATE PROCEDURE myconnpy_sp_2 "
"(IN pFac1 INT, IN pFac2 INT, OUT pProd INT) "
"BEGIN SELECT 'abc'; SELECT 'def'; SET pProd := pFac1 * pFac2; "
"END;")
stmt_create3 = (
"CREATE PROCEDURE myconnpy_sp_3"
"(IN pStr1 VARCHAR(20), IN pStr2 VARCHAR(20), "
"OUT pConCat VARCHAR(100)) "
"BEGIN SET pConCat := CONCAT(pStr1, pStr2); END;")
stmt_create4 = (
"CREATE PROCEDURE myconnpy_sp_4"
"(IN pStr1 VARCHAR(20), INOUT pStr2 VARCHAR(20), "
"OUT pConCat VARCHAR(100)) "
"BEGIN SET pConCat := CONCAT(pStr1, pStr2); END;")
try:
cur = cnx.cursor()
cur.execute(stmt_create1)
cur.execute(stmt_create2)
cur.execute(stmt_create3)
cur.execute(stmt_create4)
except errors.Error as err:
self.fail("Failed setting up test stored routine; {0}".format(err))
cur.close()
def _test_callproc_cleanup(self, cnx):
sp_names = ('myconnpy_sp_1', 'myconnpy_sp_2', 'myconnpy_sp_3',
'myconnpy_sp_4')
stmt_drop = "DROP PROCEDURE IF EXISTS {procname}"
try:
cur = cnx.cursor()
for sp_name in sp_names:
cur.execute(stmt_drop.format(procname=sp_name))
except errors.Error as err:
self.fail(
"Failed cleaning up test stored routine; {0}".format(err))
cur.close()
def test_callproc(self):
"""MySQLCursor object callproc()-method"""
self.check_method(self.cur, 'callproc')
self.assertRaises(ValueError, self.cur.callproc, None)
self.assertRaises(ValueError, self.cur.callproc, 'sp1', None)
config = tests.get_mysql_config()
config['get_warnings'] = True
self.cnx = connection.MySQLConnection(**config)
self._test_callproc_setup(self.cnx)
self.cur = self.cnx.cursor()
if tests.MYSQL_VERSION < (5, 1):
exp = ('5', '4', b'20')
else:
exp = (5, 4, 20)
result = self.cur.callproc('myconnpy_sp_1', (exp[0], exp[1], 0))
self.assertEqual([], self.cur._stored_results)
self.assertEqual(exp, result)
if tests.MYSQL_VERSION < (5, 1):
exp = ('6', '5', b'30')
else:
exp = (6, 5, 30)
result = self.cur.callproc('myconnpy_sp_2', (exp[0], exp[1], 0))
self.assertTrue(isinstance(self.cur._stored_results, list))
self.assertEqual(exp, result)
exp_results = [
('abc',),
('def',)
]
for result, exp in zip(self.cur.stored_results(),
iter(exp_results)):
self.assertEqual(exp, result.fetchone())
exp = ('ham', 'spam', 'hamspam')
result = self.cur.callproc('myconnpy_sp_3', (exp[0], exp[1], 0))
self.assertTrue(isinstance(self.cur._stored_results, list))
self.assertEqual(exp, result)
exp = ('ham', 'spam', 'hamspam')
result = self.cur.callproc('myconnpy_sp_4',
(exp[0], (exp[1], 'CHAR'), (0, 'CHAR')))
self.assertTrue(isinstance(self.cur._stored_results, list))
self.assertEqual(exp, result)
self._test_callproc_cleanup(self.cnx)
self.cur.close()
def test_fetchone(self):
"""MySQLCursor object fetchone()-method"""
self.check_method(self.cur, 'fetchone')
self.assertEqual(None, self.cur.fetchone())
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = self.cnx.cursor()
self.cur.execute("SELECT BINARY 'ham'")
exp = (b'ham',)
self.assertEqual(exp, self.cur.fetchone())
self.assertEqual(None, self.cur.fetchone())
self.cur.close()
def test_fetchmany(self):
"""MySQLCursor object fetchmany()-method"""
self.check_method(self.cur, 'fetchmany')
self.assertEqual([], self.cur.fetchmany())
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
tbl = 'myconnpy_fetch'
self._test_execute_setup(self.cnx, tbl)
stmt_insert = (
"INSERT INTO {table} (col1,col2) "
"VALUES (%s,%s)".format(table=tbl))
stmt_select = (
"SELECT col1,col2 FROM {table} "
"ORDER BY col1 DESC".format(table=tbl))
self.cur = self.cnx.cursor()
nrrows = 10
data = [(i, str(i * 100)) for i in range(0, nrrows)]
self.cur.executemany(stmt_insert, data)
self.cur.execute(stmt_select)
exp = [(9, '900'), (8, '800'), (7, '700'), (6, '600')]
rows = self.cur.fetchmany(4)
self.assertTrue(tests.cmp_result(exp, rows),
"Fetching first 4 rows test failed.")
exp = [(5, '500'), (4, '400'), (3, '300')]
rows = self.cur.fetchmany(3)
self.assertTrue(tests.cmp_result(exp, rows),
"Fetching next 3 rows test failed.")
exp = [(2, '200'), (1, '100'), (0, '0')]
rows = self.cur.fetchmany(3)
self.assertTrue(tests.cmp_result(exp, rows),
"Fetching next 3 rows test failed.")
self.assertEqual([], self.cur.fetchmany())
self._test_execute_cleanup(self.cnx, tbl)
self.cur.close()
def test_fetchall(self):
"""MySQLCursor object fetchall()-method"""
self.check_method(self.cur, 'fetchall')
self.assertRaises(errors.InterfaceError, self.cur.fetchall)
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
tbl = 'myconnpy_fetch'
self._test_execute_setup(self.cnx, tbl)
stmt_insert = (
"INSERT INTO {table} (col1,col2) "
"VALUES (%s,%s)".format(table=tbl))
stmt_select = (
"SELECT col1,col2 FROM {table} "
"ORDER BY col1 ASC".format(table=tbl))
self.cur = self.cnx.cursor()
self.cur.execute("SELECT * FROM {table}".format(table=tbl))
self.assertEqual([], self.cur.fetchall(),
"fetchall() with empty result should return []")
nrrows = 10
data = [(i, str(i * 100)) for i in range(0, nrrows)]
self.cur.executemany(stmt_insert, data)
self.cur.execute(stmt_select)
self.assertTrue(tests.cmp_result(data, self.cur.fetchall()),
"Fetching all rows failed.")
self.assertEqual(None, self.cur.fetchone())
self._test_execute_cleanup(self.cnx, tbl)
self.cur.close()
def test_raise_on_warning(self):
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cnx.raise_on_warnings = True
self.cur = self.cnx.cursor()
try:
self.cur.execute("SELECT 'a' + 'b'")
self.cur.fetchall()
except errors.Error:
pass
else:
self.fail("Did not get exception while raising warnings.")
def test__str__(self):
"""MySQLCursor object __str__()-method"""
self.assertEqual("MySQLCursor: (Nothing executed yet)",
self.cur.__str__())
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = self.cnx.cursor()
self.cur.execute("SELECT VERSION()")
self.cur.fetchone()
self.assertEqual("MySQLCursor: SELECT VERSION()",
self.cur.__str__())
stmt = "SELECT VERSION(),USER(),CURRENT_TIME(),NOW(),SHA1('myconnpy')"
self.cur.execute(stmt)
self.cur.fetchone()
self.assertEqual("MySQLCursor: {0}..".format(stmt[:40]),
self.cur.__str__())
self.cur.close()
def test_column_names(self):
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = self.cnx.cursor()
stmt = "SELECT NOW() as now, 'The time' as label, 123 FROM dual"
exp = ('now', 'label', '123')
self.cur.execute(stmt)
self.cur.fetchone()
self.assertEqual(exp, self.cur.column_names)
self.cur.close()
def test_statement(self):
self.cur = cursor.MySQLCursor()
exp = 'SELECT * FROM ham'
self.cur._executed = exp
self.assertEqual(exp, self.cur.statement)
self.cur._executed = ' ' + exp + ' '
self.assertEqual(exp, self.cur.statement)
self.cur._executed = b'SELECT * FROM ham'
self.assertEqual(exp, self.cur.statement)
def test_with_rows(self):
self.cur = cursor.MySQLCursor()
self.assertFalse(self.cur.with_rows)
self.cur._description = ('ham', 'spam')
self.assertTrue(self.cur.with_rows)
def test_unicode(self):
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
self.cur = self.cnx.cursor()
stmt = "DROP TABLE IF EXISTS test_unicode"
self.cur.execute(stmt)
stmt = (
"CREATE TABLE test_unicode(`aé` INTEGER AUTO_INCREMENT, "
"`測試` INTEGER, PRIMARY KEY (`aé`))ENGINE=InnoDB"
)
self.cur.execute(stmt)
stmt = "INSERT INTO test_unicode(`aé`, `測試`) VALUES (%(aé)s, %(測試)s)"
params = {'aé': 1, '測試': 2}
self.cur.execute(stmt, params)
stmt = "SELECT * FROM test_unicode"
self.cur.execute(stmt)
exp = [(1, 2)]
self.assertEqual(exp, self.cur.fetchall())
stmt = "DROP TABLE IF EXISTS test_unicode"
self.cur.execute(stmt)
class MySQLCursorBufferedTests(tests.TestsCursor):
def setUp(self):
self.cur = cursor.MySQLCursorBuffered(connection=None)
self.cnx = None
def tearDown(self):
if self.cnx:
self.cnx.close()
def test_init(self):
"""MySQLCursorBuffered object init"""
try:
cur = cursor.MySQLCursorBuffered(connection=None)
except (SyntaxError, TypeError) as err:
self.fail("Failed initializing MySQLCursorBuffered; {0}".format(
err))
else:
cur.close()
self.assertRaises(errors.InterfaceError,
cursor.MySQLCursorBuffered, connection='foo')
def test__next_row(self):
"""MySQLCursorBuffered object _next_row-attribute"""
self.check_attr(self.cur, '_next_row', 0)
def test__rows(self):
"""MySQLCursorBuffered object _rows-attribute"""
self.check_attr(self.cur, '_rows', None)
def test_execute(self):
"""MySQLCursorBuffered object execute()-method
"""
self.check_method(self.cur, 'execute')
self.assertEqual(None, self.cur.execute(None, None))
config = tests.get_mysql_config()
config['buffered'] = True
config['get_warnings'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
self.assertEqual(True,
isinstance(self.cur, cursor.MySQLCursorBuffered))
self.cur.execute("SELECT 1")
self.assertEqual([(b'1',)], self.cur._rows)
def test_raise_on_warning(self):
config = tests.get_mysql_config()
config['buffered'] = True
config['raise_on_warnings'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
try:
self.cur.execute("SELECT 'a' + 'b'")
except errors.Error:
pass
else:
self.fail("Did not get exception while raising warnings.")
def test_with_rows(self):
cur = cursor.MySQLCursorBuffered()
self.assertFalse(cur.with_rows)
cur._rows = [('ham',)]
self.assertTrue(cur.with_rows)
class MySQLCursorRawTests(tests.TestsCursor):
def setUp(self):
config = tests.get_mysql_config()
config['raw'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
def tearDown(self):
self.cur.close()
self.cnx.close()
def test_fetchone(self):
self.check_method(self.cur, 'fetchone')
self.assertEqual(None, self.cur.fetchone())
self.cur.execute("SELECT 1, 'string', MAKEDATE(2010,365), 2.5")
exp = (b'1', b'string', b'2010-12-31', b'2.5')
self.assertEqual(exp, self.cur.fetchone())
class MySQLCursorRawBufferedTests(tests.TestsCursor):
def setUp(self):
config = tests.get_mysql_config()
config['raw'] = True
config['buffered'] = True
self.cnx = connection.MySQLConnection(**config)
self.cur = self.cnx.cursor()
def tearDown(self):
self.cur.close()
self.cnx.close()
def test_fetchone(self):
self.check_method(self.cur, 'fetchone')
self.assertEqual(None, self.cur.fetchone())
self.cur.execute("SELECT 1, 'string', MAKEDATE(2010,365), 2.5")
exp = (b'1', b'string', b'2010-12-31', b'2.5')
self.assertEqual(exp, self.cur.fetchone())
def test_fetchall(self):
self.check_method(self.cur, 'fetchall')
self.assertRaises(errors.InterfaceError, self.cur.fetchall)
self.cur.execute("SELECT 1, 'string', MAKEDATE(2010,365), 2.5")
exp = [(b'1', b'string', b'2010-12-31', b'2.5')]
self.assertEqual(exp, self.cur.fetchall())
class MySQLCursorPreparedTests(tests.TestsCursor):
def setUp(self):
config = tests.get_mysql_config()
config['raw'] = True
config['buffered'] = True
self.cnx = connection.MySQLConnection(**config)
def test_callproc(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
self.assertRaises(errors.NotSupportedError, cur.callproc)
def test_close(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
cur.close()
self.assertEqual(None, cur._prepared)
def test_fetch_row(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
self.assertEqual(None, cur._fetch_row())
cur._description = [('c1', 5, None, None, None, None, 1, 128)]
# Monkey patch the get_row method of the connection for testing
def _get_row(binary, columns): # pylint: disable=W0613
try:
row = self.cnx._test_fetch_row[0]
self.cnx._test_fetch_row = self.cnx._test_fetch_row[1:]
except IndexError:
return None
return row
self.cnx.get_row = _get_row
eof_info = {'status_flag': 0, 'warning_count': 2}
self.cnx.unread_result = True
self.cnx._test_fetch_row = [(b'1', None), (None, eof_info)]
self.assertEqual(b'1', cur._fetch_row())
self.assertEqual((None, None), cur._nextrow)
self.assertEqual(eof_info['warning_count'], cur._warning_count)
cur._reset_result()
self.cnx.unread_result = True
self.cnx._test_fetch_row = [(None, eof_info)]
self.assertEqual(None, cur._fetch_row())
self.assertEqual((None, None), cur._nextrow)
self.assertEqual(eof_info['warning_count'], cur._warning_count)
def test_execute(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
cur2 = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
# No 1
stmt = "SELECT (? * 2) AS c1"
cur.execute(stmt, (5,))
self.assertEqual(stmt, cur._executed)
exp = {
'num_params': 1, 'statement_id': 1,
'parameters': [('?', 253, None, None, None, None, 1, 128)],
'warning_count': 0, 'num_columns': 1,
'columns': [('c1', 5, None, None, None, None, 1, 128)]
}
self.assertEqual(exp, cur._prepared)
# No 2
stmt = "SELECT (? * 3) AS c2"
# first, execute should fail, because unread results of No 1
self.assertRaises(errors.InternalError, cur2.execute, stmt)
cur.fetchall()
# We call with wrong number of values for paramaters
self.assertRaises(errors.ProgrammingError, cur2.execute, stmt, (1, 3))
cur2.execute(stmt, (5,))
self.assertEqual(stmt, cur2._executed)
exp = {
'num_params': 1, 'statement_id': 2,
'parameters': [('?', 253, None, None, None, None, 1, 128)],
'warning_count': 0, 'num_columns': 1,
'columns': [('c2', 5, None, None, None, None, 1, 128)]
}
self.assertEqual(exp, cur2._prepared)
self.assertEqual([(15,)], cur2.fetchall())
# No 3
data = (3, 4)
exp = [(5.0,)]
stmt = "SELECT SQRT(POW(?, 2) + POW(?, 2)) AS hypotenuse"
cur.execute(stmt, data)
self.assertEqual(3, cur._prepared['statement_id'])
self.assertEqual(exp, cur.fetchall())
# Execute the already prepared statement
data = (4, 5)
exp = (6.4031242374328485,)
cur.execute(stmt, data)
self.assertEqual(3, cur._prepared['statement_id'])
self.assertEqual(exp, cur.fetchone())
def test_executemany(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
self.assertEqual(None, cur.executemany(None, []))
self.assertRaises(errors.InterfaceError, cur.executemany,
'ham', None)
self.assertRaises(errors.ProgrammingError, cur.executemany,
'ham', 'ham')
self.assertEqual(None, cur.executemany('ham', []))
self.assertRaises(errors.ProgrammingError, cur.executemany,
'ham', ['ham'])
cur.executemany("SELECT SHA1(%s)", [('ham',), ('bar',)])
self.assertEqual(None, cur.fetchone())
cur.close()
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
tbl = 'myconnpy_cursor'
self._test_execute_setup(self.cnx, tbl)
stmt_insert = "INSERT INTO {table} (col1,col2) VALUES (%s, %s)".format(
table=tbl)
stmt_select = "SELECT col1,col2 FROM {table} ORDER BY col1".format(
table=tbl)
cur.executemany(stmt_insert, [(1, 100), (2, 200), (3, 300)])
self.assertEqual(3, cur.rowcount)
cur.executemany("SELECT %s", [('h',), ('a',), ('m',)])
self.assertEqual(3, cur.rowcount)
cur.execute(stmt_select)
self.assertEqual([(1, b'100'), (2, b'200'), (3, b'300')],
cur.fetchall(), "Multi insert test failed")
data = [(2,), (3,)]
stmt = "DELETE FROM {table} WHERE col1 = %s".format(table=tbl)
cur.executemany(stmt, data)
self.assertEqual(2, cur.rowcount)
self._test_execute_cleanup(self.cnx, tbl)
cur.close()
def test_fetchone(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
def _fetch_row():
try:
row = cur._test_fetch_row[0]
cur._test_fetch_row = cur._test_fetch_row[1:]
except IndexError:
return None
return row
cur._fetch_row = _fetch_row
cur._test_fetch_row = [(b'ham',)]
self.assertEqual((b'ham',), cur.fetchone())
self.assertEqual(None, cur.fetchone())
def test_fetchmany(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
def _fetch_row():
try:
row = cur._test_fetch_row[0]
cur._test_fetch_row = cur._test_fetch_row[1:]
except IndexError:
return None
return row
cur._fetch_row = _fetch_row
rows = [(1, b'100'), (2, b'200'), (3, b'300')]
cur._test_fetch_row = rows
self.cnx.unread_result = True
self.assertEqual(rows[0:2], cur.fetchmany(2))
self.assertEqual([rows[2]], cur.fetchmany(2))
self.assertEqual([], cur.fetchmany())
def test_fetchall(self):
cur = self.cnx.cursor(cursor_class=cursor.MySQLCursorPrepared)
def _get_rows(binary, columns): # pylint: disable=W0613
self.unread_result = False # pylint: disable=W0201
return (
self.cnx._test_fetch_row,
{'status_flag': 0, 'warning_count': 3}
)
self.cnx.get_rows = _get_rows
rows = [(1, 100), (2, 200), (3, 300)]
self.cnx._test_fetch_row = rows
self.cnx.unread_result = True
self.assertEqual(rows, cur.fetchall())
self.assertEqual(len(rows), cur._rowcount)
self.assertEqual(3, cur._warning_count)
self.assertRaises(errors.InterfaceError, cur.fetchall)
class MySQLCursorDictTests(tests.TestsCursor):
def setUp(self):
config = tests.get_mysql_config()
self.connection = connection.MySQLConnection(**config)
self.cur = self.connection.cursor(dictionary=True)
self.cur.execute('DROP TABLE IF EXISTS MySQLCursorDictTests')
self.cur.execute('CREATE TABLE MySQLCursorDictTests(id INT(10), name '
'VARCHAR(20), city VARCHAR(20))')
def tearDown(self):
self.cur.execute('DROP TABLE IF EXISTS MySQLCursorDictTests')
self.cur.close()
self.connection.close()
def test_fetchone(self):
self.check_method(self.cur, 'fetchone')
self.assertEqual(None, self.cur.fetchone())
self.cur.execute("INSERT INTO MySQLCursorDictTests VALUES(%s, %s, %s)",
(1, 'ham', 'spam'))
self.cur.execute("SELECT * FROM MySQLCursorDictTests")
exp = {u'id': 1, u'name': u'ham', u'city': u'spam'}
self.assertEqual(exp, self.cur.fetchone())
class MySQLCursorBufferedDictTests(tests.TestsCursor):
def setUp(self):
config = tests.get_mysql_config()
self.connection = connection.MySQLConnection(**config)
self.cur = self.connection.cursor(dictionary=True, buffered=True)
self.cur.execute('DROP TABLE IF EXISTS MySQLCursorBufferedDictTests')
self.cur.execute('CREATE TABLE MySQLCursorBufferedDictTests(id INT(10),'
'name VARCHAR(20), city VARCHAR(20))')
def tearDown(self):
self.cur.close()
self.connection.close()
def test_fetchone(self):
self.check_method(self.cur, 'fetchone')
self.assertEqual(None, self.cur.fetchone())
self.cur.execute("INSERT INTO MySQLCursorBufferedDictTests VALUE"
"(%s, %s, %s)", (1, 'ham', 'spam'))
self.cur.execute("SELECT * FROM MySQLCursorBufferedDictTests")
exp = {u'id': 1, u'name': u'ham', u'city': u'spam'}
self.assertEqual(exp, self.cur.fetchone())
def test_fetchall(self):
self.check_method(self.cur, 'fetchall')
self.assertRaises(errors.InterfaceError, self.cur.fetchall)
self.cur.execute("INSERT INTO MySQLCursorBufferedDictTests VALUE"
"(%s, %s, %s)", (1, 'ham', 'spam'))
self.cur.execute("SELECT * FROM MySQLCursorBufferedDictTests")
exp = [{u'id': 1, u'name': u'ham', u'city': u'spam'}]
self.assertEqual(exp, self.cur.fetchall())
class MySQLCursorNamedTupleTests(tests.TestsCursor):
def setUp(self):
config = tests.get_mysql_config()
self.connection = connection.MySQLConnection(**config)
self.cur = self.connection.cursor(named_tuple=True)
self.cur.execute('DROP TABLE IF EXISTS MySQLCursorNamedTupleTests')
self.cur.execute('CREATE TABLE MySQLCursorNamedTupleTests(id INT(10),'
'name VARCHAR(20), city VARCHAR(20))')
def tearDown(self):
self.cur.execute('DROP TABLE IF EXISTS MySQLCursorNamedTupleTests')
self.cur.close()
self.connection.close()
def test_fetchone(self):
self.check_method(self.cur, 'fetchone')
self.assertEqual(None, self.cur.fetchone())
self.cur.execute("INSERT INTO MySQLCursorNamedTupleTests VALUES"
"(%s, %s, %s)", (1, 'ham', 'spam'))
self.cur.execute("SELECT * FROM MySQLCursorNamedTupleTests")
named_tuple = namedtuple('Row', ['id', 'name', 'city'])
exp = named_tuple(1, u'ham', u'spam')
row = self.cur.fetchone()
self.assertEqual(exp.id, row.id)
self.assertEqual(exp.name, row.name)
self.assertEqual(exp.city, row.city)
class MySQLCursorBufferedNamedTupleTests(tests.TestsCursor):
def setUp(self):
config = tests.get_mysql_config()
self.connection = connection.MySQLConnection(**config)
self.cur = self.connection.cursor(named_tuple=True, buffered=True)
self.cur.execute('DROP TABLE IF EXISTS '
'MySQLCursorBufferedNamedTupleTests')
self.cur.execute('CREATE TABLE MySQLCursorBufferedNamedTupleTests('
'id INT(10), name VARCHAR(20), city VARCHAR(20))')
def tearDown(self):
self.cur.close()
self.connection.close()
def test_fetchone(self):
self.check_method(self.cur, 'fetchone')
self.assertEqual(None, self.cur.fetchone())
self.cur.execute("INSERT INTO MySQLCursorBufferedNamedTupleTests VALUES"
"(%s, %s, %s)", (1, 'ham', 'spam'))
self.cur.execute("SELECT * FROM MySQLCursorBufferedNamedTupleTests")
named_tuple = namedtuple('Row', ['id', 'name', 'city'])
exp = named_tuple(1, u'ham', u'spam')
row = self.cur.fetchone()
self.assertEqual(exp.id, row.id)
self.assertEqual(exp.name, row.name)
self.assertEqual(exp.city, row.city)
def test_fetchall(self):
self.check_method(self.cur, 'fetchall')
self.assertRaises(errors.InterfaceError, self.cur.fetchall)
self.cur.execute("INSERT INTO MySQLCursorBufferedNamedTupleTests VALUES"
"(%s, %s, %s)", (1, 'ham', 'spam'))
self.cur.execute("SELECT * FROM MySQLCursorBufferedNamedTupleTests")
named_tuple = namedtuple('Row', ['id', 'name', 'city'])
exp = named_tuple(1, u'ham', u'spam')
row = self.cur.fetchall()
self.assertEqual(exp.id, row[0].id)
self.assertEqual(exp.name, row[0].name)
self.assertEqual(exp.city, row[0].city)
|