|
8 | 8 | import traceback |
9 | 9 | from collections import deque |
10 | 10 | from dataclasses import dataclass |
11 | | -from datetime import datetime, timezone |
| 11 | +from datetime import datetime, timedelta, timezone |
12 | 12 | from enum import Enum, IntEnum |
13 | 13 | from typing import ( |
14 | 14 | Any, |
@@ -86,6 +86,11 @@ class MyDataClass: |
86 | 86 | baz: SerializableEnum |
87 | 87 |
|
88 | 88 |
|
| 89 | +@dataclass |
| 90 | +class DatetimeClass: |
| 91 | + datetime: datetime |
| 92 | + |
| 93 | + |
89 | 94 | async def test_converter_default(): |
90 | 95 | async def assert_payload( |
91 | 96 | input, |
@@ -178,6 +183,38 @@ async def assert_payload( |
178 | 183 | type_hint=RawValue, |
179 | 184 | ) |
180 | 185 |
|
| 186 | + # Without type hint, it is deserialized as a str |
| 187 | + await assert_payload( |
| 188 | + datetime(2020, 1, 1, 1, 1, 1), |
| 189 | + "json/plain", |
| 190 | + '"2020-01-01T01:01:01"', |
| 191 | + expected_decoded_input="2020-01-01T01:01:01", |
| 192 | + ) |
| 193 | + |
| 194 | + # With type hint, it is deserialized as a datetime |
| 195 | + await assert_payload( |
| 196 | + datetime(2020, 1, 1, 1, 1, 1, 1), |
| 197 | + "json/plain", |
| 198 | + '"2020-01-01T01:01:01.000001"', |
| 199 | + type_hint=datetime, |
| 200 | + ) |
| 201 | + |
| 202 | + # Timezones work |
| 203 | + await assert_payload( |
| 204 | + datetime(2020, 1, 1, 1, 1, 1, tzinfo=timezone(timedelta(hours=5))), |
| 205 | + "json/plain", |
| 206 | + '"2020-01-01T01:01:01+05:00"', |
| 207 | + type_hint=datetime, |
| 208 | + ) |
| 209 | + |
| 210 | + # Data class with datetime |
| 211 | + await assert_payload( |
| 212 | + DatetimeClass(datetime=datetime(2020, 1, 1, 1, 1, 1)), |
| 213 | + "json/plain", |
| 214 | + '{"datetime":"2020-01-01T01:01:01"}', |
| 215 | + type_hint=DatetimeClass, |
| 216 | + ) |
| 217 | + |
181 | 218 |
|
182 | 219 | def test_binary_proto(): |
183 | 220 | # We have to test this separately because by default it never encodes |
|
0 commit comments