2
2
import datetime
3
3
from operator import attrgetter
4
4
5
+ import pytest
5
6
import pytest_asyncio
6
7
from pydantic import validator
7
8
@@ -19,11 +20,11 @@ class BaseModel(base_model, abc.ABC):
19
20
class Meta :
20
21
global_key_prefix = key_prefix
21
22
22
- class Post (BaseModel ):
23
+ class PostDatetime (BaseModel ):
23
24
created : datetime .datetime = Field (index = True )
24
25
25
26
await Migrator ().run ()
26
- return Post
27
+ return PostDatetime
27
28
28
29
29
30
@py_test_mark_asyncio
@@ -88,11 +89,11 @@ class BaseModel(base_model, abc.ABC):
88
89
class Meta :
89
90
global_key_prefix = key_prefix
90
91
91
- class Post (BaseModel ):
92
+ class PostDate (BaseModel ):
92
93
created : datetime .date = Field (index = True )
93
94
94
95
await Migrator ().run ()
95
- return Post
96
+ return PostDate
96
97
97
98
98
99
@py_test_mark_asyncio
@@ -145,7 +146,7 @@ class BaseModel(base_model, abc.ABC):
145
146
class Meta :
146
147
global_key_prefix = key_prefix
147
148
148
- class Post (BaseModel ):
149
+ class PostTime (BaseModel ):
149
150
created : datetime .time = Field (index = True )
150
151
151
152
# TODO: Find better / more correct approach!!!!!!!!!!
@@ -167,7 +168,7 @@ def time_validator(cls, value):
167
168
return value
168
169
169
170
await Migrator ().run ()
170
- return Post
171
+ return PostTime
171
172
172
173
173
174
@py_test_mark_asyncio
@@ -219,3 +220,65 @@ async def test_time(post_model_time):
219
220
.all ()
220
221
== []
221
222
)
223
+
224
+
225
+ @pytest .fixture (
226
+ params = [
227
+ datetime .timezone .utc ,
228
+ datetime .timezone (datetime .timedelta (hours = 1 )),
229
+ ],
230
+ ids = ["UTC" , "UTC+1" ],
231
+ )
232
+ def timezone (request ):
233
+ return request .param
234
+
235
+
236
+ @py_test_mark_asyncio
237
+ async def test_mixing (post_model_time , post_model_date , post_model_datetime , timezone ):
238
+ now = datetime .datetime (1980 , 1 , 1 , hour = 2 , second = 20 , tzinfo = timezone )
239
+ now_date , now_time = now .date (), now .time ().replace (tzinfo = timezone )
240
+
241
+ await post_model_datetime (created = now ).save ()
242
+ obj = await post_model_datetime .find ().first ()
243
+ assert obj .created == now
244
+
245
+ await post_model_date (created = now_date ).save ()
246
+ obj_date = await post_model_date .find ().first ()
247
+ assert obj_date .created == now_date
248
+
249
+ await post_model_time (created = now_time ).save ()
250
+ obj_time = await post_model_time .find ().first ()
251
+ assert obj_time .created == now_time
252
+
253
+ restored = datetime .datetime .combine (obj_date .created , obj_time .created )
254
+ assert restored == now
255
+
256
+
257
+ @py_test_mark_asyncio
258
+ async def test_precision (post_model_datetime ):
259
+ now = datetime .datetime (
260
+ 1980 , 1 , 1 , hour = 2 , second = 20 , microsecond = 123457 , tzinfo = datetime .timezone .utc
261
+ )
262
+ await post_model_datetime (created = now ).save ()
263
+ obj = await post_model_datetime .find ().first ()
264
+ obj_now = obj .created
265
+
266
+ # Test seconds
267
+ assert obj_now .replace (microsecond = 0 ) == now .replace (microsecond = 0 )
268
+
269
+ # Test milliseconds
270
+ assert obj_now .replace (microsecond = obj_now .microsecond // 1000 ) == now .replace (
271
+ microsecond = now .microsecond // 1000
272
+ )
273
+
274
+ # Test microseconds
275
+ # Our precision is millisecond
276
+ with pytest .raises (AssertionError ):
277
+ assert obj_now == now
278
+
279
+ # We should be in 1000 microsecond range
280
+ assert (
281
+ datetime .timedelta (microseconds = - 1000 )
282
+ <= obj_now - now
283
+ <= datetime .timedelta (microseconds = 1000 )
284
+ )
0 commit comments