Skip to content

Commit 57b0b4d

Browse files
committed
Add mixing time test, add time precision test
1 parent aca860d commit 57b0b4d

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

tests/test_time.py

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
from operator import attrgetter
44

5+
import pytest
56
import pytest_asyncio
67
from pydantic import validator
78

@@ -19,11 +20,11 @@ class BaseModel(base_model, abc.ABC):
1920
class Meta:
2021
global_key_prefix = key_prefix
2122

22-
class Post(BaseModel):
23+
class PostDatetime(BaseModel):
2324
created: datetime.datetime = Field(index=True)
2425

2526
await Migrator().run()
26-
return Post
27+
return PostDatetime
2728

2829

2930
@py_test_mark_asyncio
@@ -88,11 +89,11 @@ class BaseModel(base_model, abc.ABC):
8889
class Meta:
8990
global_key_prefix = key_prefix
9091

91-
class Post(BaseModel):
92+
class PostDate(BaseModel):
9293
created: datetime.date = Field(index=True)
9394

9495
await Migrator().run()
95-
return Post
96+
return PostDate
9697

9798

9899
@py_test_mark_asyncio
@@ -145,7 +146,7 @@ class BaseModel(base_model, abc.ABC):
145146
class Meta:
146147
global_key_prefix = key_prefix
147148

148-
class Post(BaseModel):
149+
class PostTime(BaseModel):
149150
created: datetime.time = Field(index=True)
150151

151152
# TODO: Find better / more correct approach!!!!!!!!!!
@@ -167,7 +168,7 @@ def time_validator(cls, value):
167168
return value
168169

169170
await Migrator().run()
170-
return Post
171+
return PostTime
171172

172173

173174
@py_test_mark_asyncio
@@ -219,3 +220,65 @@ async def test_time(post_model_time):
219220
.all()
220221
== []
221222
)
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

Comments
 (0)