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
|
#!/usr/bin/env python -O
# -*- coding: utf-8 -*-
import datetime
import unittest
from mariadb.constants import FIELD_TYPE
from test.base_test import create_connection
class foo(int):
def bar(self): pass
def timedelta_to_time(s):
return (datetime.datetime.min + s).time()
def long_minus(s):
return s - 1
def none_to_string(s):
if s is None:
return "None"
return s
conversions = {
**{FIELD_TYPE.TIME: timedelta_to_time},
**{FIELD_TYPE.LONG: long_minus},
**{FIELD_TYPE.NULL: none_to_string},
**{FIELD_TYPE.LONGLONG: long_minus},
}
class TestConversion(unittest.TestCase):
def setUp(self):
self.connection = create_connection({"converter": conversions})
self.connection.autocommit = False
def tearDown(self):
del self.connection
def test_convert_time(self):
cursor = self.connection.cursor()
a = datetime.time(12, 29, 21)
cursor.execute("SELECT cast(? as time)", (a,))
row = cursor.fetchone()
self.assertEqual(row[0], a)
del cursor
def test_convert_long(self):
cursor = self.connection.cursor()
a = 12345
cursor.execute("SELECT CAST(? AS SIGNED)", (12345,))
row = cursor.fetchone()
self.assertEqual(row[0], a - 1)
del cursor
def test_convert_none(self):
cursor = self.connection.cursor()
cursor.execute("SELECT NULL")
row = cursor.fetchone()
self.assertEqual(row[0], "None")
cursor.execute("SELECT ?", (None,))
row = cursor.fetchone()
self.assertEqual(row[0], "None")
del cursor
if __name__ == '__main__':
unittest.main()
|