forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timestamptz.py
More file actions
94 lines (75 loc) · 2.59 KB
/
test_timestamptz.py
File metadata and controls
94 lines (75 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import datetime
from unittest import TestCase
from dateutil import tz
from piccolo.columns.column_types import Timestamptz
from piccolo.columns.defaults.timestamptz import (
TimestamptzCustom,
TimestamptzNow,
TimestamptzOffset,
)
from piccolo.table import Table
class MyTable(Table):
created_on = Timestamptz()
class MyTableDefault(Table):
"""
A table containing all of the possible `default` arguments for
`Timestamptz`.
"""
created_on = Timestamptz(default=TimestamptzNow())
created_on_offset = Timestamptz(default=TimestamptzOffset(days=1))
created_on_custom = Timestamptz(default=TimestamptzCustom(year=2021))
created_on_datetime = Timestamptz(
default=datetime.datetime(year=2020, month=1, day=1)
)
class CustomTimezone(datetime.tzinfo):
pass
class TestTimestamptz(TestCase):
def setUp(self):
MyTable.create_table().run_sync()
def tearDown(self):
MyTable.alter().drop_table().run_sync()
def test_timestamptz_timezone_aware(self):
"""
Test storing a timezone aware timestamp.
"""
for tzinfo in (
datetime.timezone.utc,
tz.gettz("America/New_York"),
):
created_on = datetime.datetime(
year=2020,
month=1,
day=1,
hour=12,
minute=0,
second=0,
tzinfo=tzinfo,
)
row = MyTable(created_on=created_on)
row.save().run_sync()
# Fetch it back from the database
result = (
MyTable.objects()
.where(MyTable.id == row.id)
.first()
.run_sync()
)
self.assertEqual(result.created_on, created_on)
# The database converts it to UTC
self.assertEqual(result.created_on.tzinfo, datetime.timezone.utc)
class TestTimestamptzDefault(TestCase):
def setUp(self):
MyTableDefault.create_table().run_sync()
def tearDown(self):
MyTableDefault.alter().drop_table().run_sync()
def test_timestamptz_default(self):
"""
Make sure the default value gets created, and can be retrieved.
"""
created_on = datetime.datetime.now(tz=datetime.timezone.utc)
row = MyTableDefault()
row.save().run_sync()
result = MyTableDefault.objects().first().run_sync()
delta = result.created_on - created_on
self.assertTrue(delta < datetime.timedelta(seconds=1))
self.assertEqual(result.created_on.tzinfo, datetime.timezone.utc)