Skip to content

Commit 295fff2

Browse files
committed
TIME return datetime.time object
1 parent f589870 commit 295fff2

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### 0.2.4
66

77
- Fix `escape_string` for enum type. (#30)
8+
- `TIME` return `datetime.time` object.
89

910
### 0.2.3
1011

asyncmy/converters.pyx

+6-6
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,24 @@ cpdef str escape_time(obj, mapping=None):
102102
fmt = "'{0.hour:02}:{0.minute:02}:{0.second:02}'"
103103
return fmt.format(obj)
104104

105-
cpdef str escape_datetime(obj, mapping=None):
105+
cpdef str escape_datetime(obj, mapping=None):
106106
if obj.microsecond:
107107
fmt = "'{0.year:04}-{0.month:02}-{0.day:02} {0.hour:02}:{0.minute:02}:{0.second:02}.{0.microsecond:06}'"
108108
else:
109109
fmt = "'{0.year:04}-{0.month:02}-{0.day:02} {0.hour:02}:{0.minute:02}:{0.second:02}'"
110110
return fmt.format(obj)
111111

112-
cpdef str escape_date(obj, mapping=None):
112+
cpdef str escape_date(obj, mapping=None):
113113
fmt = "'{0.year:04}-{0.month:02}-{0.day:02}'"
114114
return fmt.format(obj)
115115

116-
cpdef str escape_struct_time(obj, mapping=None):
116+
cpdef str escape_struct_time(obj, mapping=None):
117117
return escape_datetime(datetime.datetime(*obj[:6]))
118118

119-
cpdef str decimal2literal(o, d):
119+
cpdef str decimal2literal(o, d):
120120
return format(o, "f")
121121

122-
cpdef int _convert_second_fraction(s):
122+
cpdef int _convert_second_fraction(s):
123123
if not s:
124124
return 0
125125
# Pad zeros to ensure the fraction length in microseconds
@@ -313,7 +313,7 @@ cdef dict decoders = {
313313
YEAR: int,
314314
TIMESTAMP: convert_datetime,
315315
DATETIME: convert_datetime,
316-
TIME: convert_timedelta,
316+
TIME: convert_time,
317317
DATE: convert_date,
318318
BLOB: through,
319319
TINY_BLOB: through,

conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async def initialize_tests(connection):
4646
`decimal` decimal(10,2) DEFAULT NULL,
4747
`date` date DEFAULT NULL,
4848
`datetime` datetime DEFAULT NULL,
49+
`time` time DEFAULT NULL,
4950
`float` float DEFAULT NULL,
5051
`string` varchar(200) DEFAULT NULL,
5152
`tinyint` tinyint DEFAULT NULL,

tests/test_cursor.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
from decimal import Decimal
13
from enum import Enum
24

35
import pytest
@@ -33,18 +35,33 @@ async def test_dict_cursor(connection):
3335
async def test_insert(connection):
3436
async with connection.cursor(cursor=DictCursor) as cursor:
3537
rows = await cursor.execute(
36-
"""INSERT INTO test.asyncmy(id,`decimal`, date, datetime, `float`, string, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s,%s)""",
38+
"""INSERT INTO test.asyncmy(id,`decimal`, date, datetime, time, `float`, string, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)""",
3739
(
38-
-1,
40+
1,
3941
1,
4042
"2020-08-08",
4143
"2020-08-08 00:00:00",
44+
"00:00:00",
4245
1,
4346
"1",
4447
1,
4548
),
4649
)
4750
assert rows == 1
51+
await cursor.execute("select * from test.asyncmy where id = %s", (cursor.lastrowid,))
52+
result = await cursor.fetchall()
53+
assert result == [
54+
{
55+
"id": 1,
56+
"decimal": Decimal("1.00"),
57+
"date": datetime.date(2020, 8, 8),
58+
"datetime": datetime.datetime(2020, 8, 8, 0, 0),
59+
"time": datetime.time.fromisoformat("00:00"),
60+
"float": 1.0,
61+
"string": "1",
62+
"tinyint": 1,
63+
}
64+
]
4865

4966

5067
@pytest.mark.asyncio
@@ -58,12 +75,13 @@ async def test_delete(connection):
5875
async def test_executemany(connection):
5976
async with connection.cursor(cursor=DictCursor) as cursor:
6077
rows = await cursor.executemany(
61-
"""INSERT INTO test.asyncmy(`decimal`, date, datetime, `float`, string, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s)""",
78+
"""INSERT INTO test.asyncmy(`decimal`, date, datetime, time, `float`, string, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s,%s)""",
6279
[
6380
(
6481
1,
6582
"2020-08-08",
6683
"2020-08-08 00:00:00",
84+
"00:00:00",
6785
1,
6886
"1",
6987
1,
@@ -72,6 +90,7 @@ async def test_executemany(connection):
7290
1,
7391
"2020-08-08",
7492
"2020-08-08 00:00:00",
93+
"00:00:00",
7594
1,
7695
"1",
7796
1,
@@ -105,12 +124,13 @@ class EnumValue(str, Enum):
105124
async def test_insert_enum(connection):
106125
async with connection.cursor(cursor=DictCursor) as cursor:
107126
rows = await cursor.execute(
108-
"""INSERT INTO test.asyncmy(id,`decimal`, date, datetime, `float`, string, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s,%s)""",
127+
"""INSERT INTO test.asyncmy(id, `decimal`, date, datetime, time, `float`, string, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)""",
109128
(
110129
-1,
111130
1,
112131
"2020-08-08",
113132
"2020-08-08 00:00:00",
133+
"00:00:00",
114134
1,
115135
EnumValue.VALUE,
116136
1,

0 commit comments

Comments
 (0)