File: test_cext_connection.py

package info (click to toggle)
mysql-connector-python 2.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,968 kB
  • ctags: 4,120
  • sloc: python: 23,410; ansic: 2,621; makefile: 27; cpp: 1
file content (123 lines) | stat: -rw-r--r-- 4,342 bytes parent folder | download
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
# -*- coding: utf-8 -*-
# MySQL Connector/Python - MySQL driver written in Python.
# Copyright (c) 2014, 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

"""Testing connection.CMySQLConnection class using the C Extension
"""

import tests

from mysql.connector import errors
from mysql.connector.constants import ClientFlag, flag_is_set
from mysql.connector.connection import MySQLConnection
from mysql.connector.connection_cext import CMySQLConnection


class CMySQLConnectionTests(tests.MySQLConnectorTests):

    def setUp(self):
        config = tests.get_mysql_config()
        self.cnx = CMySQLConnection(**config)

        self.pcnx = MySQLConnection(**config)

    def test__info_query(self):
        query = "SELECT 1, 'a', 2, 'b'"
        exp = (1, 'a', 2, 'b')
        self.assertEqual(exp, self.cnx.info_query(query))

        self.assertRaises(errors.InterfaceError, self.cnx.info_query,
                          "SHOW VARIABLES LIKE '%char%'")

    def test_client_flags(self):
        defaults = ClientFlag.default
        set_flags = self.cnx._cmysql.st_client_flag()
        for flag in defaults:
            self.assertTrue(flag_is_set(flag, set_flags))

    def test_get_rows(self):
        self.assertRaises(errors.InternalError, self.cnx.get_rows)

        query = "SHOW STATUS LIKE 'Aborted_c%'"

        self.cnx.cmd_query(query)
        self.assertRaises(AttributeError, self.cnx.get_rows, 0)
        self.assertRaises(AttributeError, self.cnx.get_rows, -10)
        self.assertEqual(2, len(self.cnx.get_rows()))
        self.cnx.free_result()

        self.cnx.cmd_query(query)
        self.assertEqual(1, len(self.cnx.get_rows(count=1)))
        self.assertEqual(1, len(self.cnx.get_rows(count=1)))
        self.assertEqual([], self.cnx.get_rows(count=1))
        self.cnx.free_result()

    def test_cmd_init_db(self):
        query = "SELECT DATABASE()"
        self.cnx.cmd_init_db('mysql')
        self.assertEqual('mysql', self.cnx.info_query(query)[0])

        self.cnx.cmd_init_db('myconnpy')
        self.assertEqual('myconnpy', self.cnx.info_query(query)[0])

    def test_cmd_query(self):
        query = "SHOW STATUS LIKE 'Aborted_c%'"
        info = self.cnx.cmd_query(query)

        exp = {
            'eof': {'status_flag': 32, 'warning_count': 0},
            'columns': [
                ['Variable_name', 253, None, None, None, None, 0, 1],
                ('Value', 253, None, None, None, None, 1, 0)
            ]
        }

        if tests.MYSQL_VERSION >= (5, 7, 10):
            exp['columns'][0][7] = 4097
            exp['eof']['status_flag'] = 16385

        exp['columns'][0] = tuple(exp['columns'][0])

        self.assertEqual(exp, info)

        rows = self.cnx.get_rows()
        vars = [ row[0] for row in rows ]
        self.assertEqual(2, len(rows))

        vars.sort()
        exp = ['Aborted_clients', 'Aborted_connects']
        self.assertEqual(exp, vars)

        exp = ['Value', 'Variable_name']
        fields = [fld[0] for fld in info['columns']]
        fields.sort()
        self.assertEqual(exp, fields)

        self.cnx.free_result()

        info = self.cnx.cmd_query("SET @a = 1")
        exp = {
            'warning_count': 0, 'insert_id': 0, 'affected_rows': 0,
            'server_status': 0, 'field_count': 0
        }
        self.assertEqual(exp, info)