mirror of
https://github.com/mariadb-corporation/mariadb-connector-python.git
synced 2025-08-11 02:43:15 +00:00

Fixed various coding style stuff detected by flake8. Added .pre-commit-config.yaml: With command pre_commit install a hook for flake8 will be installed.
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
#!/usr/bin/env python -O
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import unittest
|
|
from datetime import datetime
|
|
import mariadb
|
|
|
|
from test.base_test import create_connection
|
|
|
|
|
|
class TestException(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.connection = create_connection()
|
|
|
|
def tearDown(self):
|
|
del self.connection
|
|
|
|
def test_exception(self):
|
|
cursor = self.connection.cursor()
|
|
try:
|
|
cursor.execute("WRONG QUERY")
|
|
except mariadb.ProgrammingError as err:
|
|
self.assertEqual(err.sqlstate, "42000")
|
|
self.assertEqual(err.errno, 1064)
|
|
self.assertTrue(err.errmsg.find("You have an error "
|
|
"in your SQL syntax") > -1)
|
|
pass
|
|
|
|
del cursor
|
|
|
|
def test_db_unknown_exception(self):
|
|
try:
|
|
create_connection({"database": "unknown"})
|
|
except mariadb.ProgrammingError as err:
|
|
self.assertEqual(err.sqlstate, "42000")
|
|
self.assertEqual(err.errno, 1049)
|
|
self.assertTrue(err.errmsg.find("Unknown database 'unknown'") > -1)
|
|
pass
|
|
|
|
def test_conn_timeout_exception(self):
|
|
start = datetime.today()
|
|
try:
|
|
create_connection({"connect_timeout": 1, "host": "8.8.8.8"})
|
|
except mariadb.OperationalError as err:
|
|
self.assertEqual(err.sqlstate, "HY000")
|
|
self.assertEqual(err.errno, 2002)
|
|
self.assertTrue(err.errmsg.find("server on '8.8.8.8'") > -1)
|
|
end = datetime.today()
|
|
difference = end - start
|
|
self.assertEqual(difference.days, 0)
|
|
self.assertTrue(difference.seconds, 1)
|
|
pass
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|