@@ -27,6 +27,7 @@ class PostDatetime(BaseModel):
27
27
return PostDatetime
28
28
29
29
30
+ # TODO: code duplication
30
31
@py_test_mark_asyncio
31
32
async def test_datetime (post_model_datetime ):
32
33
now = datetime .datetime (1980 , 1 , 1 , hour = 2 , second = 20 , tzinfo = datetime .timezone .utc )
@@ -35,6 +36,7 @@ async def test_datetime(post_model_datetime):
35
36
36
37
next_hour_timezone = datetime .timezone (datetime .timedelta (hours = 1 ))
37
38
now_01_00 = now .replace (hour = 3 , tzinfo = next_hour_timezone )
39
+ # Sanity check
38
40
assert now == now_01_00
39
41
40
42
posts = [
@@ -46,20 +48,25 @@ async def test_datetime(post_model_datetime):
46
48
47
49
expected_sorted_posts = sorted (posts , key = attrgetter ("created" ))
48
50
51
+ # Check all
49
52
assert (
50
53
await post_model_datetime .find ().sort_by ("created" ).all ()
51
54
== expected_sorted_posts
52
55
)
56
+ # Check one
53
57
assert await post_model_datetime .find (post_model_datetime .created == now ).all () == [
54
58
posts [0 ]
55
59
]
60
+ # Check one using different timezone but the same time
56
61
assert await post_model_datetime .find (
57
62
post_model_datetime .created == now_01_00
58
63
).all () == [posts [0 ]]
59
64
65
+ # Check one
60
66
post = await post_model_datetime .find (post_model_datetime .created == now ).first ()
61
67
assert post .created == now == now_01_00
62
68
69
+ # Check index comparison
63
70
assert (
64
71
await post_model_datetime .find (post_model_datetime .created < now_p10 )
65
72
.sort_by ("created" )
@@ -96,6 +103,7 @@ class PostDate(BaseModel):
96
103
return PostDate
97
104
98
105
106
+ # TODO: code duplication
99
107
@py_test_mark_asyncio
100
108
async def test_date (post_model_date ):
101
109
now = datetime .date (1980 , 1 , 2 )
@@ -110,13 +118,16 @@ async def test_date(post_model_date):
110
118
111
119
expected_sorted_posts = sorted (posts , key = attrgetter ("created" ))
112
120
121
+ # Check all
113
122
assert (
114
123
await post_model_date .find ().sort_by ("created" ).all () == expected_sorted_posts
115
124
)
125
+ # Check one
116
126
assert await post_model_date .find (post_model_date .created == now ).all () == [
117
127
posts [0 ]
118
128
]
119
129
130
+ # Check index comparison
120
131
assert (
121
132
await post_model_date .find (post_model_date .created < now_next )
122
133
.sort_by ("created" )
@@ -171,6 +182,7 @@ def time_validator(cls, value):
171
182
return PostTime
172
183
173
184
185
+ # TODO: code duplication
174
186
@py_test_mark_asyncio
175
187
async def test_time (post_model_time ):
176
188
now = datetime .time (hour = 2 , second = 20 , tzinfo = datetime .timezone .utc )
@@ -179,6 +191,7 @@ async def test_time(post_model_time):
179
191
180
192
next_hour_timezone = datetime .timezone (datetime .timedelta (hours = 1 ))
181
193
now_01_00 = now .replace (hour = 3 , tzinfo = next_hour_timezone )
194
+ # Sanity check
182
195
assert now == now_01_00
183
196
184
197
posts = [
@@ -189,19 +202,24 @@ async def test_time(post_model_time):
189
202
190
203
expected_sorted_posts = sorted (posts , key = attrgetter ("created" ))
191
204
205
+ # Check all
192
206
assert (
193
207
await post_model_time .find ().sort_by ("created" ).all () == expected_sorted_posts
194
208
)
209
+ # Check one
195
210
assert await post_model_time .find (post_model_time .created == now ).all () == [
196
211
posts [0 ]
197
212
]
213
+ # Check one using different timezone but the same time
198
214
assert await post_model_time .find (post_model_time .created == now_01_00 ).all () == [
199
215
posts [0 ]
200
216
]
201
217
218
+ # Check one
202
219
post = await post_model_time .find (post_model_time .created == now ).first ()
203
220
assert post .created == now == now_01_00
204
221
222
+ # Check index comparison
205
223
assert (
206
224
await post_model_time .find (post_model_time .created < now_p10 )
207
225
.sort_by ("created" )
@@ -225,9 +243,10 @@ async def test_time(post_model_time):
225
243
@pytest .fixture (
226
244
params = [
227
245
datetime .timezone .utc ,
228
- datetime .timezone (datetime .timedelta (hours = 1 )),
246
+ datetime .timezone (datetime .timedelta (hours = 2 )),
247
+ datetime .timezone (datetime .timedelta (hours = - 5 )),
229
248
],
230
- ids = ["UTC" , "UTC+1 " ],
249
+ ids = ["UTC" , "UTC+2" , "UTC-5 " ],
231
250
)
232
251
def timezone (request ):
233
252
return request .param
@@ -238,18 +257,22 @@ async def test_mixing(post_model_time, post_model_date, post_model_datetime, tim
238
257
now = datetime .datetime (1980 , 1 , 1 , hour = 2 , second = 20 , tzinfo = timezone )
239
258
now_date , now_time = now .date (), now .time ().replace (tzinfo = timezone )
240
259
260
+ # Serialize + Deserialize datetime.datetime
241
261
await post_model_datetime (created = now ).save ()
242
262
obj = await post_model_datetime .find ().first ()
243
263
assert obj .created == now
244
264
265
+ # Serialize + Deserialize datetime.date
245
266
await post_model_date (created = now_date ).save ()
246
267
obj_date = await post_model_date .find ().first ()
247
268
assert obj_date .created == now_date
248
269
270
+ # Serialize + Deserialize datetime.time
249
271
await post_model_time (created = now_time ).save ()
250
272
obj_time = await post_model_time .find ().first ()
251
273
assert obj_time .created == now_time
252
274
275
+ # Combine deserialized and compare to expected
253
276
restored = datetime .datetime .combine (obj_date .created , obj_time .created )
254
277
assert restored == now
255
278
@@ -259,6 +282,7 @@ async def test_precision(post_model_datetime):
259
282
now = datetime .datetime (
260
283
1980 , 1 , 1 , hour = 2 , second = 20 , microsecond = 123457 , tzinfo = datetime .timezone .utc
261
284
)
285
+ # Serialize + Deserialize datetime.datetime
262
286
await post_model_datetime (created = now ).save ()
263
287
obj = await post_model_datetime .find ().first ()
264
288
obj_now = obj .created
0 commit comments