-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathtimestamp_test.py
More file actions
251 lines (215 loc) · 10.5 KB
/
timestamp_test.py
File metadata and controls
251 lines (215 loc) · 10.5 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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
"""Unit tests for time utilities."""
# pytype: skip-file
import datetime
import unittest
import pytz
from google.protobuf import duration_pb2
from google.protobuf import timestamp_pb2
from apache_beam.utils.timestamp import Duration
from apache_beam.utils.timestamp import Timestamp
class TimestampTest(unittest.TestCase):
def test_of(self):
interval = Timestamp(123)
self.assertEqual(id(interval), id(Timestamp.of(interval)))
self.assertEqual(interval, Timestamp.of(123.0))
with self.assertRaises(TypeError):
Timestamp.of(Duration(10))
def test_precision(self):
self.assertEqual(Timestamp(10000000) % 0.1, 0)
self.assertEqual(Timestamp(10000000) % 0.05, 0)
self.assertEqual(Timestamp(10000000) % 0.000005, 0)
self.assertEqual(Timestamp(10000000) % Duration(0.1), 0)
self.assertEqual(Timestamp(10000000) % Duration(0.05), 0)
self.assertEqual(Timestamp(10000000) % Duration(0.000005), 0)
def test_utc_timestamp(self):
self.assertEqual(Timestamp(10000000).to_rfc3339(), '1970-04-26T17:46:40Z')
self.assertEqual(
Timestamp(10000000.000001).to_rfc3339(), '1970-04-26T17:46:40.000001Z')
self.assertEqual(
Timestamp(1458343379.123456).to_rfc3339(),
'2016-03-18T23:22:59.123456Z')
def test_from_rfc3339(self):
test_cases = [
(10000000, '1970-04-26T17:46:40Z'),
(10000000.000001, '1970-04-26T17:46:40.000001Z'),
(1458343379.123456, '2016-03-18T23:22:59.123456Z'),
]
for seconds_float, rfc3339_str in test_cases:
self.assertEqual(
Timestamp(seconds_float), Timestamp.from_rfc3339(rfc3339_str))
self.assertEqual(
rfc3339_str, Timestamp.from_rfc3339(rfc3339_str).to_rfc3339())
def test_from_rfc3339_with_timezone(self):
test_cases = [
(1458328979.123456, '2016-03-18T23:22:59.123456+04:00'),
(1458357779.123456, '2016-03-18T23:22:59.123456-04:00'),
]
for seconds_float, rfc3339_str in test_cases:
self.assertEqual(
Timestamp(seconds_float), Timestamp.from_rfc3339(rfc3339_str))
def test_from_rfc3339_failure(self):
with self.assertRaisesRegex(ValueError, 'parse'):
Timestamp.from_rfc3339('not rfc3339')
with self.assertRaisesRegex(ValueError, 'parse'):
Timestamp.from_rfc3339('2016-03-18T23:22:59.123456Z unparseable')
def test_from_utc_datetime(self):
self.assertEqual(
Timestamp.from_utc_datetime(
datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)),
Timestamp(0))
with self.assertRaisesRegex(ValueError, r'UTC'):
Timestamp.from_utc_datetime(
datetime.datetime(1970, 1, 1, tzinfo=pytz.timezone('US/Eastern')))
with self.assertRaisesRegex(ValueError, r'dt has no timezone info'):
Timestamp.from_utc_datetime(datetime.datetime(1970, 1, 1, tzinfo=None))
def test_from_to_utc_datetime(self):
timestamp = Timestamp(seconds=1458343379.123456)
dt = timestamp.to_utc_datetime(has_tz=True)
self.assertEqual(timestamp, Timestamp.from_utc_datetime(dt))
def test_arithmetic(self):
# Supported operations.
self.assertEqual(Timestamp(123) + 456, 579)
self.assertEqual(Timestamp(123) + Duration(456), 579)
self.assertEqual(456 + Timestamp(123), 579)
self.assertEqual(Duration(456) + Timestamp(123), 579)
self.assertEqual(Timestamp(123) - 456, -333)
self.assertEqual(Timestamp(123) - Duration(456), -333)
self.assertEqual(Timestamp(1230) % 456, 318)
self.assertEqual(Timestamp(1230) % Duration(456), 318)
self.assertEqual(Timestamp(123) - Timestamp(100), 23)
# Check that direct comparison of Timestamp and Duration is allowed.
self.assertTrue(Duration(123) == Timestamp(123))
self.assertTrue(Timestamp(123) == Duration(123))
self.assertFalse(Duration(123) == Timestamp(1230))
self.assertFalse(Timestamp(123) == Duration(1230))
# Check return types.
self.assertEqual((Timestamp(123) + 456).__class__, Timestamp)
self.assertEqual((Timestamp(123) + Duration(456)).__class__, Timestamp)
self.assertEqual((456 + Timestamp(123)).__class__, Timestamp)
self.assertEqual((Duration(456) + Timestamp(123)).__class__, Timestamp)
self.assertEqual((Timestamp(123) - 456).__class__, Timestamp)
self.assertEqual((Timestamp(123) - Duration(456)).__class__, Timestamp)
self.assertEqual((Timestamp(1230) % 456).__class__, Duration)
self.assertEqual((Timestamp(1230) % Duration(456)).__class__, Duration)
self.assertEqual((Timestamp(123) - Timestamp(100)).__class__, Duration)
# Unsupported operations.
with self.assertRaises(TypeError):
self.assertEqual(Timestamp(123) * 456, 56088)
with self.assertRaises(TypeError):
self.assertEqual(Timestamp(123) * Duration(456), 56088)
with self.assertRaises(TypeError):
self.assertEqual(456 * Timestamp(123), 56088)
with self.assertRaises(TypeError):
self.assertEqual(Duration(456) * Timestamp(123), 56088)
with self.assertRaises(TypeError):
self.assertEqual(456 - Timestamp(123), 333)
with self.assertRaises(TypeError):
self.assertEqual(Duration(456) - Timestamp(123), 333)
with self.assertRaises(TypeError):
self.assertEqual(-Timestamp(123), -123) # pylint: disable=invalid-unary-operand-type
with self.assertRaises(TypeError):
self.assertEqual(-Timestamp(123), -Duration(123)) # pylint: disable=invalid-unary-operand-type
with self.assertRaises(TypeError):
self.assertEqual(1230 % Timestamp(456), 318)
with self.assertRaises(TypeError):
self.assertEqual(Duration(1230) % Timestamp(456), 318)
def test_sort_order(self):
self.assertEqual([-63, Timestamp(-3), 2, 9, Timestamp(292.3), 500],
sorted([9, 2, Timestamp(-3), Timestamp(292.3), -63, 500]))
self.assertEqual([4, 5, Timestamp(6), Timestamp(7), 8, 9],
sorted([9, 8, Timestamp(7), Timestamp(6), 5, 4]))
def test_str(self):
self.assertEqual('Timestamp(1.234567)', str(Timestamp(1.234567)))
self.assertEqual('Timestamp(-1.234567)', str(Timestamp(-1.234567)))
self.assertEqual(
'Timestamp(-999999999.900000)', str(Timestamp(-999999999.9)))
self.assertEqual('Timestamp(999999999)', str(Timestamp(999999999)))
self.assertEqual('Timestamp(-999999999)', str(Timestamp(-999999999)))
def test_now(self):
now = Timestamp.now()
self.assertTrue(isinstance(now, Timestamp))
def test_from_proto(self):
ts_proto = timestamp_pb2.Timestamp(seconds=1234, nanos=56000)
actual_ts = Timestamp.from_proto(ts_proto)
expected_ts = Timestamp(seconds=1234, micros=56)
self.assertEqual(actual_ts, expected_ts)
def test_from_proto_fails_with_truncation(self):
# TODO(https://github.com/apache/beam/issues/19922): Better define
# timestamps.
with self.assertRaises(ValueError):
Timestamp.from_proto(timestamp_pb2.Timestamp(seconds=1234, nanos=56789))
def test_to_proto(self):
ts = Timestamp(seconds=1234, micros=56)
actual_ts_proto = Timestamp.to_proto(ts)
expected_ts_proto = timestamp_pb2.Timestamp(seconds=1234, nanos=56000)
self.assertEqual(actual_ts_proto, expected_ts_proto)
def test_equality(self):
for min_val in (Timestamp(1), Duration(1), 1, 1.1):
for max_val in (Timestamp(123), Duration(123), 123, 123.4):
self.assertTrue(min_val < max_val, "%s < %s" % (min_val, max_val))
self.assertTrue(min_val <= max_val, "%s <= %s" % (min_val, max_val))
self.assertTrue(max_val > min_val, "%s > %s" % (max_val, min_val))
self.assertTrue(max_val >= min_val, "%s >= %s" % (max_val, min_val))
class DurationTest(unittest.TestCase):
def test_of(self):
interval = Duration(123)
self.assertEqual(id(interval), id(Duration.of(interval)))
self.assertEqual(interval, Duration.of(123.0))
with self.assertRaises(TypeError):
Duration.of(Timestamp(10))
def test_precision(self):
self.assertEqual(Duration(10000000) % 0.1, 0)
self.assertEqual(Duration(10000000) % 0.05, 0)
self.assertEqual(Duration(10000000) % 0.000005, 0)
def test_arithmetic(self):
self.assertEqual(Duration(123) + 456, 579)
self.assertEqual(456 + Duration(123), 579)
self.assertEqual(Duration(123) * 456, 56088)
self.assertEqual(456 * Duration(123), 56088)
self.assertEqual(Duration(123) - 456, -333)
self.assertEqual(456 - Duration(123), 333)
self.assertEqual(-Duration(123), -123)
def test_sort_order(self):
self.assertEqual([-63, Duration(-3), 2, 9, Duration(292.3), 500],
sorted([9, 2, Duration(-3), Duration(292.3), -63, 500]))
self.assertEqual([4, 5, Duration(6), Duration(7), 8, 9],
sorted([9, 8, Duration(7), Duration(6), 5, 4]))
def test_str(self):
self.assertEqual('Duration(1.234567)', str(Duration(1.234567)))
self.assertEqual('Duration(-1.234567)', str(Duration(-1.234567)))
self.assertEqual('Duration(-999999999.900000)', str(Duration(-999999999.9)))
self.assertEqual('Duration(999999999)', str(Duration(999999999)))
self.assertEqual('Duration(-999999999)', str(Duration(-999999999)))
def test_from_proto(self):
dur_proto = duration_pb2.Duration(seconds=1234, nanos=56000)
actual_dur = Duration.from_proto(dur_proto)
expected_dur = Duration(seconds=1234, micros=56)
self.assertEqual(actual_dur, expected_dur)
def test_from_proto_fails_with_truncation(self):
# TODO(https://github.com/apache/beam/issues/19922): Better define
# durations.
with self.assertRaises(ValueError):
Duration.from_proto(duration_pb2.Duration(seconds=1234, nanos=56789))
def test_to_proto(self):
dur = Duration(seconds=1234, micros=56)
actual_dur_proto = Duration.to_proto(dur)
expected_dur_proto = duration_pb2.Duration(seconds=1234, nanos=56000)
self.assertEqual(actual_dur_proto, expected_dur_proto)
if __name__ == '__main__':
unittest.main()