forked from geldata/gel-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_datetime.py
More file actions
290 lines (246 loc) · 9.67 KB
/
test_datetime.py
File metadata and controls
290 lines (246 loc) · 9.67 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2019-present MagicStack Inc. and the EdgeDB authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
from datetime import timedelta
from gel import _testbase as tb
from edgedb import errors
from edgedb.datatypes.datatypes import RelativeDuration, DateDuration
USECS_PER_HOUR = 3600000000
USECS_PER_MINUTE = 60000000
USECS_PER_SEC = 1000000
class TestDatetimeTypes(tb.SyncQueryTestCase):
async def test_duration_01(self):
duration_kwargs = [
dict(),
dict(microseconds=1),
dict(microseconds=-1),
dict(days=1),
dict(days=-1),
dict(hours=1),
dict(seconds=-1),
dict(microseconds=1, days=1, hours=1),
dict(microseconds=-1, days=-1, hours=-1),
]
# Fuzz it!
for _ in range(5000):
duration_kwargs.append(
dict(
microseconds=random.randint(-1000000000, 1000000000),
hours=random.randint(-50, 50),
days=random.randint(-500, 500),
)
)
durs = [timedelta(**d) for d in duration_kwargs]
# Test encode/decode roundtrip
durs_from_db = self.client.query('''
WITH args := array_unpack(<array<duration>>$0)
SELECT args;
''', durs)
self.assertEqual(list(durs_from_db), durs)
async def test_duration_02(self):
# Make sure that when we break down the microseconds into the bigger
# components we still get consistent values.
tdn1h = timedelta(microseconds=-USECS_PER_HOUR)
tdn1m = timedelta(microseconds=-USECS_PER_MINUTE)
tdn1s = timedelta(microseconds=-USECS_PER_SEC)
tdn1us = timedelta(microseconds=-1)
durs = [
(
tdn1h, tdn1m,
timedelta(microseconds=-USECS_PER_HOUR - USECS_PER_MINUTE),
),
(
tdn1h, tdn1s,
timedelta(microseconds=-USECS_PER_HOUR - USECS_PER_SEC),
),
(
tdn1m, tdn1s,
timedelta(microseconds=-USECS_PER_MINUTE - USECS_PER_SEC),
),
(
tdn1h, tdn1us,
timedelta(microseconds=-USECS_PER_HOUR - 1),
),
(
tdn1m, tdn1us,
timedelta(microseconds=-USECS_PER_MINUTE - 1),
),
(
tdn1s, tdn1us,
timedelta(microseconds=-USECS_PER_SEC - 1),
),
]
# Test encode
durs_enc = self.client.query('''
WITH args := array_unpack(
<array<tuple<duration, duration, duration>>>$0)
SELECT args.0 + args.1 = args.2;
''', durs)
# Test decode
durs_dec = self.client.query('''
WITH args := array_unpack(
<array<tuple<duration, duration, duration>>>$0)
SELECT (args.0 + args.1, args.2);
''', durs)
self.assertEqual(durs_enc, [True] * len(durs))
self.assertEqual(list(durs_dec), [(d[2], d[2]) for d in durs])
async def test_relative_duration_01(self):
try:
self.client.query("SELECT <cal::relative_duration>'1y'")
except errors.InvalidReferenceError:
self.skipTest("feature not implemented")
delta_kwargs = [
dict(),
dict(microseconds=1),
dict(microseconds=-1),
dict(days=1),
dict(days=-1),
dict(months=1),
dict(months=-1),
dict(microseconds=1, days=1, months=1),
dict(microseconds=-1, days=-1, months=-1),
]
# Fuzz it!
for _ in range(5000):
delta_kwargs.append(
dict(
microseconds=random.randint(-1000000000, 1000000000),
days=random.randint(-500, 500),
months=random.randint(-50, 50)
)
)
durs = [RelativeDuration(**d) for d in delta_kwargs]
# Test that RelativeDuration.__str__ formats the
# same as <str><cal::relative_duration>
durs_as_text = self.client.query('''
WITH args := array_unpack(<array<cal::relative_duration>>$0)
SELECT <str>args;
''', durs)
# Test encode/decode roundtrip
durs_from_db = self.client.query('''
WITH args := array_unpack(<array<cal::relative_duration>>$0)
SELECT args;
''', durs)
self.assertEqual(durs_as_text, [str(d) for d in durs])
self.assertEqual(list(durs_from_db), durs)
async def test_relative_duration_02(self):
d1 = RelativeDuration(microseconds=1)
d2 = RelativeDuration(microseconds=2)
d3 = RelativeDuration(microseconds=1)
self.assertNotEqual(d1, d2)
self.assertEqual(d1, d3)
self.assertEqual(hash(d1), hash(d3))
if hash(1) != hash(2):
self.assertNotEqual(hash(d1), hash(d2))
self.assertEqual(d1.days, 0)
self.assertEqual(d1.months, 0)
self.assertEqual(d1.microseconds, 1)
self.assertEqual(repr(d1), '<gel.RelativeDuration "PT0.000001S">')
async def test_relative_duration_03(self):
# Make sure that when we break down the microseconds into the bigger
# components we still get the sign correctly in string
# representation.
durs = [
RelativeDuration(microseconds=-USECS_PER_HOUR),
RelativeDuration(microseconds=-USECS_PER_MINUTE),
RelativeDuration(microseconds=-USECS_PER_SEC),
RelativeDuration(microseconds=-USECS_PER_HOUR - USECS_PER_MINUTE),
RelativeDuration(microseconds=-USECS_PER_HOUR - USECS_PER_SEC),
RelativeDuration(microseconds=-USECS_PER_MINUTE - USECS_PER_SEC),
RelativeDuration(microseconds=-USECS_PER_HOUR - USECS_PER_MINUTE -
USECS_PER_SEC),
RelativeDuration(microseconds=-USECS_PER_HOUR - 1),
RelativeDuration(microseconds=-USECS_PER_MINUTE - 1),
RelativeDuration(microseconds=-USECS_PER_SEC - 1),
RelativeDuration(microseconds=-1),
]
# Test that RelativeDuration.__str__ formats the
# same as <str><cal::relative_duration>
durs_as_text = self.client.query('''
WITH args := array_unpack(<array<cal::relative_duration>>$0)
SELECT <str>args;
''', durs)
# Test encode/decode roundtrip
durs_from_db = self.client.query('''
WITH args := array_unpack(<array<cal::relative_duration>>$0)
SELECT args;
''', durs)
self.assertEqual(durs_as_text, [str(d) for d in durs])
self.assertEqual(list(durs_from_db), durs)
async def test_date_duration_01(self):
try:
self.client.query("SELECT <cal::date_duration>'1y'")
except errors.InvalidReferenceError:
self.skipTest("feature not implemented")
delta_kwargs = [
dict(),
dict(days=1),
dict(days=-1),
dict(months=1),
dict(months=-1),
dict(days=1, months=1),
dict(days=-1, months=-1),
]
# Fuzz it!
for _ in range(5000):
delta_kwargs.append(
dict(
days=random.randint(-500, 500),
months=random.randint(-50, 50)
)
)
durs = [DateDuration(**d) for d in delta_kwargs]
# Test that DateDuration.__str__ formats the
# same as <str><cal::relative_duration>
durs_as_text = self.client.query('''
WITH args := array_unpack(<array<cal::date_duration>>$0)
SELECT <str>args;
''', durs)
# Test encode/decode roundtrip
durs_from_db = self.client.query('''
WITH args := array_unpack(<array<cal::date_duration>>$0)
SELECT args;
''', durs)
for db_dur, client_dur in zip(durs_as_text, durs):
self.assertEqual(db_dur, str(client_dur))
self.assertEqual(list(durs_from_db), durs)
async def test_date_duration_02(self):
# Make sure that when we break down the microseconds into the bigger
# components we still get the sign correctly in string
# representation.
durs = [
DateDuration(months=11),
DateDuration(months=12),
DateDuration(months=13),
DateDuration(months=-11),
DateDuration(months=-12),
DateDuration(months=-13),
]
# Test that DateDuration.__str__ formats the
# same as <str><cal::date_duration>
durs_as_text = self.client.query('''
WITH args := array_unpack(<array<cal::date_duration>>$0)
SELECT <str>args;
''', durs)
# Test encode/decode roundtrip
durs_from_db = self.client.query('''
WITH args := array_unpack(<array<cal::date_duration>>$0)
SELECT args;
''', durs)
self.assertEqual(durs_as_text, [str(d) for d in durs])
self.assertEqual(list(durs_from_db), durs)