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
|
# MySQL Connector/Python - MySQL driver written in Python.
# Copyright (c) 2014, 2017, 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.abstracts
"""
from decimal import Decimal
from operator import attrgetter
import unittest
import tests
from tests import PY2, foreach_cnx
from mysql.connector.connection import MySQLConnection
from mysql.connector.constants import RefreshOption
from mysql.connector import errors
try:
from mysql.connector.connection_cext import CMySQLConnection
except ImportError:
# Test without C Extension
CMySQLConnection = None
class ConnectionSubclasses(tests.MySQLConnectorTests):
"""Tests for any subclass of MySQLConnectionAbstract
"""
def asEq(self, exp, *cases):
for case in cases:
self.assertEqual(exp, case)
@foreach_cnx()
def test_properties_getter(self):
properties = [
(self.config['user'], 'user'),
(self.config['host'], 'server_host'),
(self.config['port'], 'server_port'),
(self.config['unix_socket'], 'unix_socket'),
(self.config['database'], 'database')
]
for exp, property in properties:
f = attrgetter(property)
self.asEq(exp, f(self.cnx))
@foreach_cnx()
def test_time_zone(self):
orig = self.cnx.info_query("SELECT @@session.time_zone")[0]
self.assertEqual(orig, self.cnx.time_zone)
self.cnx.time_zone = "+02:00"
self.assertEqual("+02:00", self.cnx.time_zone)
@foreach_cnx()
def test_sql_mode(self):
orig = self.cnx.info_query("SELECT @@session.sql_mode")[0]
self.assertEqual(orig, self.cnx.sql_mode)
try:
self.cnx.sql_mode = 'SPAM'
except errors.ProgrammingError:
pass # excepted
else:
self.fail("ProgrammingError not raises")
# Set SQL Mode to a list of modes
if tests.MYSQL_VERSION[0:3] < (5, 7, 4):
exp = ('STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,'
'NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,'
'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION')
else:
exp = ('STRICT_TRANS_TABLES,STRICT_ALL_TABLES,TRADITIONAL,'
'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION')
try:
self.cnx.sql_mode = exp
except errors.Error as err:
self.fail("Failed setting SQL Mode with multiple "
"modes: {0}".format(str(err)))
self.assertEqual(exp, self.cnx._sql_mode)
# SQL Modes must be empty
self.cnx.sql_mode = ''
self.assertEqual('', self.cnx.sql_mode)
# Set SQL Mode and check
sql_mode = exp = 'STRICT_ALL_TABLES'
self.cnx.sql_mode = sql_mode
self.assertEqual(exp, self.cnx.sql_mode)
# Unset the SQL Mode again
self.cnx.sql_mode = ''
self.assertEqual('', self.cnx.sql_mode)
@foreach_cnx()
def test_in_transaction(self):
self.cnx.cmd_query('START TRANSACTION')
self.assertTrue(self.cnx.in_transaction)
self.cnx.cmd_query('ROLLBACK')
self.assertFalse(self.cnx.in_transaction)
# AUTO_COMMIT turned ON
self.cnx.autocommit = True
self.assertFalse(self.cnx.in_transaction)
self.cnx.cmd_query('START TRANSACTION')
self.assertTrue(self.cnx.in_transaction)
@foreach_cnx()
def test_disconnect(self):
self.cnx.disconnect()
self.assertFalse(self.cnx.is_connected())
@foreach_cnx()
def test_is_connected(self):
"""Check connection to MySQL Server"""
self.assertEqual(True, self.cnx.is_connected())
self.cnx.disconnect()
self.assertEqual(False, self.cnx.is_connected())
@foreach_cnx()
def test_info_query(self):
queries = [
("SELECT 1",
(1,)),
("SELECT 'ham', 'spam'",
((u'ham', u'spam')))
]
for query, exp in queries:
self.assertEqual(exp, self.cnx.info_query(query))
@foreach_cnx()
def test_cmd_init_db(self):
self.assertRaises(errors.ProgrammingError,
self.cnx.cmd_init_db, 'unknown_database')
self.cnx.cmd_init_db(u'INFORMATION_SCHEMA')
self.assertEqual('INFORMATION_SCHEMA', self.cnx.database.upper())
self.cnx.cmd_init_db('mysql')
self.assertEqual(u'mysql', self.cnx.database)
self.cnx.cmd_init_db('myconnpy')
self.assertEqual(u'myconnpy', self.cnx.database)
@foreach_cnx()
def test_reset_session(self):
exp = [True, u'STRICT_ALL_TABLES', u'-09:00', 33]
self.cnx.autocommit = exp[0]
self.cnx.sql_mode = exp[1]
self.cnx.time_zone = exp[2]
self.cnx.set_charset_collation(exp[3])
user_variables = {'ham': '1', 'spam': '2'}
session_variables = {'wait_timeout': 100000}
self.cnx.reset_session(user_variables, session_variables)
self.assertEqual(exp, [self.cnx.autocommit, self.cnx.sql_mode,
self.cnx.time_zone, self.cnx._charset_id])
exp_user_variables = {'ham': '1', 'spam': '2'}
exp_session_variables = {'wait_timeout': 100000}
for key, value in exp_user_variables.items():
row = self.cnx.info_query("SELECT @{0}".format(key))
self.assertEqual(value, row[0])
for key, value in exp_session_variables.items():
row = self.cnx.info_query("SELECT @@session.{0}".format(key))
self.assertEqual(value, row[0])
@unittest.skipIf(tests.MYSQL_VERSION > (5, 7, 10),
"As of MySQL 5.7.11, mysql_refresh() is deprecated")
@foreach_cnx()
def test_cmd_refresh(self):
refresh = RefreshOption.LOG | RefreshOption.THREADS
exp = {'insert_id': 0, 'affected_rows': 0,
'field_count': 0, 'warning_count': 0,
'status_flag': 0}
result = self.cnx.cmd_refresh(refresh)
for key in set(result.keys()) ^ set(exp.keys()):
try:
del result[key]
except KeyError:
del exp[key]
self.assertEqual(exp, result)
query = "SHOW GLOBAL STATUS LIKE 'Uptime_since_flush_status'"
pre_flush = int(self.cnx.info_query(query)[1])
self.cnx.cmd_refresh(RefreshOption.STATUS)
post_flush = int(self.cnx.info_query(query)[1])
self.assertTrue(post_flush <= pre_flush)
@foreach_cnx()
def test_cmd_quit(self):
self.cnx.cmd_quit()
self.assertFalse(self.cnx.is_connected())
@foreach_cnx()
def test_cmd_shutdown(self):
server = tests.MYSQL_SERVERS[0]
# We make sure the connection is re-established.
self.cnx = self.cnx.__class__(**self.config)
self.cnx.cmd_shutdown()
if not server.wait_down():
self.fail("[{0}] ".format(self.cnx.__class__.__name__) +
"MySQL not shut down after cmd_shutdown()")
self.assertRaises(errors.Error, self.cnx.cmd_shutdown)
server.start()
if not server.wait_up():
self.fail("Failed restarting MySQL server after test")
@foreach_cnx()
def test_cmd_statistics(self):
exp = {
'Uptime': int,
'Open tables': int,
'Queries per second avg': Decimal,
'Slow queries': int,
'Threads': int,
'Questions': int,
'Flush tables': int,
'Opens': int
}
stat = self.cnx.cmd_statistics()
self.assertEqual(len(exp), len(stat))
for key, type_ in exp.items():
self.assertTrue(key in stat)
self.assertTrue(isinstance(stat[key], type_))
@foreach_cnx()
def test_cmd_process_info(self):
self.assertRaises(errors.NotSupportedError,
self.cnx.cmd_process_info)
@foreach_cnx()
def test_cmd_process_kill(self):
other_cnx = self.cnx.__class__(**self.config)
pid = other_cnx.connection_id
self.cnx.cmd_process_kill(pid)
self.assertFalse(other_cnx.is_connected())
@foreach_cnx()
def test_start_transaction(self):
self.cnx.start_transaction()
self.assertTrue(self.cnx.in_transaction)
self.cnx.rollback()
self.cnx.start_transaction(consistent_snapshot=True)
self.assertTrue(self.cnx.in_transaction)
self.assertRaises(errors.ProgrammingError,
self.cnx.start_transaction)
self.cnx.rollback()
levels = ['READ UNCOMMITTED', 'READ COMMITTED', 'REPEATABLE READ',
'SERIALIZABLE',
'READ-UNCOMMITTED', 'READ-COMMITTED', 'REPEATABLE-READ',
'SERIALIZABLE']
for level in levels:
level = level.replace(' ', '-')
self.cnx.start_transaction(isolation_level=level)
self.assertTrue(self.cnx.in_transaction)
self.cnx.rollback()
self.assertRaises(ValueError,
self.cnx.start_transaction,
isolation_level='spam')
|