File: test_mysqlx_errors.py

package info (click to toggle)
mysql-connector-python 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,768 kB
  • sloc: python: 83,598; sql: 47,030; ansic: 3,494; cpp: 860; sh: 394; makefile: 208; javascript: 2
file content (210 lines) | stat: -rw-r--r-- 7,643 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
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
# Copyright (c) 2016, 2024, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0, as
# published by the Free Software Foundation.
#
# This program is designed to work with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an
# additional permission to link the program and your derivative works
# with the separately licensed software that they have either included with
# the program or referenced in the documentation.
#
# Without limiting anything contained in the foregoing, this file,
# which is part of MySQL Connector/Python, is also subject to the
# Universal FOSS Exception, version 1.0, a copy of which can be found at
# http://oss.oracle.com/licenses/universal-foss-exception.
#
# 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, version 2.0, 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 mysqlx.errors
"""

import unittest

import tests

from mysqlx import errors


@unittest.skipIf(tests.MYSQL_VERSION < (5, 7, 14), "XPlugin not compatible")
class ErrorsTests(tests.MySQLxTests):
    def test_get_mysql_exception(self):
        tests = {
            errors.ProgrammingError: (
                "24",
                "25",
                "26",
                "27",
                "28",
                "2A",
                "2C",
                "34",
                "35",
                "37",
                "3C",
                "3D",
                "3F",
                "42",
            ),
            errors.DataError: ("02", "21", "22"),
            errors.NotSupportedError: ("0A",),
            errors.IntegrityError: ("23", "XA"),
            errors.InternalError: ("40", "44"),
            errors.OperationalError: ("08", "HZ", "0K"),
            errors.DatabaseError: ("07", "2B", "2D", "2E", "33", "ZZ", "HY"),
        }

        msg = "Ham"
        for exp, errlist in tests.items():
            for sqlstate in errlist:
                errno = 1000
                res = errors.get_mysql_exception(errno, msg, sqlstate)
                self.assertTrue(
                    isinstance(res, exp),
                    "SQLState {0} should be {1}".format(sqlstate, exp.__name__),
                )
                self.assertEqual(sqlstate, res.sqlstate)
                self.assertEqual(
                    "{0} ({1}): {2}".format(errno, sqlstate, msg), str(res)
                )

        errno = 1064
        sqlstate = "42000"
        msg = "You have an error in your SQL syntax"
        exp = "1064 (42000): You have an error in your SQL syntax"
        err = errors.get_mysql_exception(errno, msg, sqlstate)
        self.assertEqual(exp, str(err))

        # Hardcoded exceptions
        self.assertTrue(isinstance(errors._ERROR_EXCEPTIONS, dict))
        self.assertTrue(
            isinstance(
                errors.get_mysql_exception(1243, None, None),
                errors.ProgrammingError,
            )
        )

    def test_get_exception(self):
        ok_packet = bytearray(b"\x07\x00\x00\x01\x00\x01\x00\x00\x00\x01\x00")
        err_packet = bytearray(
            b"\x47\x00\x00\x02\xff\x15\x04\x23\x32\x38\x30\x30\x30"
            b"\x41\x63\x63\x65\x73\x73\x20\x64\x65\x6e\x69\x65\x64"
            b"\x20\x66\x6f\x72\x20\x75\x73\x65\x72\x20\x27\x68\x61"
            b"\x6d\x27\x40\x27\x6c\x6f\x63\x61\x6c\x68\x6f\x73\x74"
            b"\x27\x20\x28\x75\x73\x69\x6e\x67\x20\x70\x61\x73\x73"
            b"\x77\x6f\x72\x64\x3a\x20\x59\x45\x53\x29"
        )
        self.assertTrue(
            isinstance(errors.get_exception(err_packet), errors.ProgrammingError)
        )

        self.assertRaises(ValueError, errors.get_exception, ok_packet)

        res = errors.get_exception(bytearray(b"\x47\x00\x00\x02\xff\x15"))
        self.assertTrue(isinstance(res, errors.InterfaceError))


class ErrorTest(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.Error, Exception))

        err = errors.Error()
        self.assertEqual(-1, err.errno)
        self.assertEqual("Unknown error", err.msg)
        self.assertEqual(None, err.sqlstate)

        msg = "Ham"
        err = errors.Error(msg, errno=1)
        self.assertEqual(1, err.errno)
        self.assertEqual("1: {0}".format(msg), err._full_msg)
        self.assertEqual(msg, err.msg)

        err = errors.Error("Ham", errno=1, sqlstate="SPAM")
        self.assertEqual(1, err.errno)
        self.assertEqual("1 (SPAM): Ham", err._full_msg)
        self.assertEqual("1 (SPAM): Ham", str(err))

        err = errors.Error(errno=2000)
        self.assertEqual("Unknown MySQL error", err.msg)
        self.assertEqual("2000: Unknown MySQL error", err._full_msg)

        err = errors.Error(errno=2003, values=("localhost", 3306, 2))
        self.assertEqual(
            "2003: Can't connect to MySQL server on 'localhost:3306' (2)",
            err._full_msg,
        )
        self.assertEqual(
            "Can't connect to MySQL server on 'localhost:3306' (2)", err.msg
        )

        err = errors.Error(errno=2001, values=("ham",))
        if "(Warning:" in str(err):
            self.fail("Found %d in error message.")

        err = errors.Error(errno=2003, values=("ham",))
        self.assertEqual(
            "2003: Can't connect to MySQL server on '%-.100s:%u' (%s) "
            "(Warning: not enough arguments for format string)",
            err._full_msg,
        )

    def test___str__(self):
        msg = "Spam"
        self.assertEqual("Spam", str(errors.Error(msg)))
        self.assertEqual("1: Spam", str(errors.Error(msg, 1)))
        self.assertEqual("1 (XYZ): Spam", str(errors.Error(msg, 1, sqlstate="XYZ")))


class InterfaceErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.InterfaceError, errors.Error))


class DatabaseErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.DatabaseError, errors.Error))


class InternalErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.InternalError, errors.DatabaseError))


class OperationalErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.OperationalError, errors.DatabaseError))


class ProgrammingErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.ProgrammingError, errors.DatabaseError))


class IntegrityErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.IntegrityError, errors.DatabaseError))


class DataErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.DataError, errors.DatabaseError))


class NotSupportedErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.NotSupportedError, errors.DatabaseError))


class PoolErrorTests(tests.MySQLxTests):
    def test___init__(self):
        self.assertTrue(issubclass(errors.PoolError, errors.Error))