File: test_locales.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 (118 lines) | stat: -rw-r--r-- 4,106 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
# MySQL Connector/Python - MySQL driver written in Python.
# Copyright (c) 2012, 2014, 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.locales
"""

from datetime import datetime

import tests
from . import PY2
from mysql.connector import errorcode, locales


def _get_client_errors():
    errors = {}
    for name in dir(errorcode):
        if name.startswith('CR_'):
            errors[name] = getattr(errorcode, name)
    return errors


class LocalesModulesTests(tests.MySQLConnectorTests):

    def test_defaults(self):
        # There should always be 'eng'
        try:
            from mysql.connector.locales import eng  # pylint: disable=W0612
        except ImportError:
            self.fail("locales.eng could not be imported")

        # There should always be 'eng.client_error'
        some_error = None
        try:
            from mysql.connector.locales.eng import client_error
            some_error = client_error.CR_UNKNOWN_ERROR
        except ImportError:
            self.fail("locales.eng.client_error could not be imported")
        some_error = some_error + ''  # fool pylint

    def test_get_client_error(self):
        try:
            locales.get_client_error(2000, language='spam')
        except ImportError as err:
            self.assertEqual("No localization support for language 'spam'",
                             str(err))
        else:
            self.fail("ImportError not raised")

        exp = "Unknown MySQL error"
        self.assertEqual(exp, locales.get_client_error(2000))
        self.assertEqual(exp, locales.get_client_error('CR_UNKNOWN_ERROR'))

        try:
            locales.get_client_error(tuple())
        except ValueError as err:
            self.assertEqual(
                "error argument needs to be either an integer or string",
                str(err))
        else:
            self.fail("ValueError not raised")


class LocalesEngClientErrorTests(tests.MySQLConnectorTests):

    """Testing locales.eng.client_error"""

    def test__MYSQL_VERSION(self):
        try:
            from mysql.connector.locales.eng import client_error
        except ImportError:
            self.fail("locales.eng.client_error could not be imported")

        minimum = (5, 6, 6)
        self.assertTrue(isinstance(client_error._MYSQL_VERSION, tuple))
        self.assertTrue(len(client_error._MYSQL_VERSION) == 3)
        self.assertTrue(client_error._MYSQL_VERSION >= minimum)

    def test_messages(self):
        try:
            from mysql.connector.locales.eng import client_error
        except ImportError:
            self.fail("locales.eng.client_error could not be imported")

        errors = _get_client_errors()

        count = 0
        for name in dir(client_error):
            if name.startswith('CR_'):
                count += 1
        self.assertEqual(len(errors), count)

        if PY2:
            strtype = unicode  # pylint: disable=E0602
        else:
            strtype = str

        for name in errors.keys():
            self.assertTrue(isinstance(getattr(client_error, name), strtype))