@@ -82,18 +82,18 @@ def test_from_rfc3339():
82
82
)
83
83
84
84
85
- def test_from_rfc3339_with_bad_tz ():
86
- value = "2009-12-17T12:44:32.123456BAD"
87
-
88
- with pytest .raises (ValueError ):
89
- datetime_helpers .from_rfc3339 (value )
90
-
85
+ def test_from_rfc3339_nanos ():
86
+ value = "2009-12-17T12:44:32.123456Z"
87
+ assert datetime_helpers .from_rfc3339_nanos (value ) == datetime .datetime (
88
+ 2009 , 12 , 17 , 12 , 44 , 32 , 123456 , pytz .utc
89
+ )
91
90
92
- def test_from_rfc3339_with_nanos ():
93
- value = "2009-12-17T12:44:32.123456789Z"
94
91
95
- with pytest .raises (ValueError ):
96
- datetime_helpers .from_rfc3339 (value )
92
+ def test_from_rfc3339_without_nanos ():
93
+ value = "2009-12-17T12:44:32Z"
94
+ assert datetime_helpers .from_rfc3339 (value ) == datetime .datetime (
95
+ 2009 , 12 , 17 , 12 , 44 , 32 , 0 , pytz .utc
96
+ )
97
97
98
98
99
99
def test_from_rfc3339_nanos_without_nanos ():
@@ -103,11 +103,33 @@ def test_from_rfc3339_nanos_without_nanos():
103
103
)
104
104
105
105
106
- def test_from_rfc3339_nanos_with_bad_tz ():
107
- value = "2009-12-17T12:44:32.123456789BAD"
106
+ @pytest .mark .parametrize (
107
+ "truncated, micros" ,
108
+ [
109
+ ("12345678" , 123456 ),
110
+ ("1234567" , 123456 ),
111
+ ("123456" , 123456 ),
112
+ ("12345" , 123450 ),
113
+ ("1234" , 123400 ),
114
+ ("123" , 123000 ),
115
+ ("12" , 120000 ),
116
+ ("1" , 100000 ),
117
+ ],
118
+ )
119
+ def test_from_rfc3339_with_truncated_nanos (truncated , micros ):
120
+ value = "2009-12-17T12:44:32.{}Z" .format (truncated )
121
+ assert datetime_helpers .from_rfc3339 (value ) == datetime .datetime (
122
+ 2009 , 12 , 17 , 12 , 44 , 32 , micros , pytz .utc
123
+ )
124
+
125
+
126
+ def test_from_rfc3339_nanos_is_deprecated ():
127
+ value = "2009-12-17T12:44:32.123456Z"
108
128
109
- with pytest .raises (ValueError ):
110
- datetime_helpers .from_rfc3339_nanos (value )
129
+ result = datetime_helpers .from_rfc3339 (value )
130
+ result_nanos = datetime_helpers .from_rfc3339_nanos (value )
131
+
132
+ assert result == result_nanos
111
133
112
134
113
135
@pytest .mark .parametrize (
@@ -130,6 +152,18 @@ def test_from_rfc3339_nanos_with_truncated_nanos(truncated, micros):
130
152
)
131
153
132
154
155
+ def test_from_rfc3339_wo_nanos_raise_exception ():
156
+ value = "2009-12-17T12:44:32"
157
+ with pytest .raises (ValueError ):
158
+ datetime_helpers .from_rfc3339 (value )
159
+
160
+
161
+ def test_from_rfc3339_w_nanos_raise_exception ():
162
+ value = "2009-12-17T12:44:32.123456"
163
+ with pytest .raises (ValueError ):
164
+ datetime_helpers .from_rfc3339 (value )
165
+
166
+
133
167
def test_to_rfc3339 ():
134
168
value = datetime .datetime (2016 , 4 , 5 , 13 , 30 , 0 )
135
169
expected = "2016-04-05T13:30:00.000000Z"
@@ -157,10 +191,11 @@ def test_to_rfc3339_with_non_utc_ignore_zone():
157
191
158
192
159
193
class Test_DateTimeWithNanos (object ):
160
-
161
194
@staticmethod
162
195
def test_ctor_wo_nanos ():
163
- stamp = datetime_helpers .DatetimeWithNanoseconds (2016 , 12 , 20 , 21 , 13 , 47 , 123456 )
196
+ stamp = datetime_helpers .DatetimeWithNanoseconds (
197
+ 2016 , 12 , 20 , 21 , 13 , 47 , 123456
198
+ )
164
199
assert stamp .year == 2016
165
200
assert stamp .month == 12
166
201
assert stamp .day == 20
@@ -200,7 +235,9 @@ def test_ctor_w_micros_keyword_and_nanos():
200
235
201
236
@staticmethod
202
237
def test_rfc3339_wo_nanos ():
203
- stamp = datetime_helpers .DatetimeWithNanoseconds (2016 , 12 , 20 , 21 , 13 , 47 , 123456 )
238
+ stamp = datetime_helpers .DatetimeWithNanoseconds (
239
+ 2016 , 12 , 20 , 21 , 13 , 47 , 123456
240
+ )
204
241
assert stamp .rfc3339 () == "2016-12-20T21:13:47.123456Z"
205
242
206
243
@staticmethod
@@ -285,12 +322,16 @@ def test_from_rfc3339_w_full_precision():
285
322
)
286
323
def test_from_rfc3339_test_nanoseconds (fractional , nanos ):
287
324
value = "2009-12-17T12:44:32.{}Z" .format (fractional )
288
- assert datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (value ).nanosecond == nanos
325
+ assert (
326
+ datetime_helpers .DatetimeWithNanoseconds .from_rfc3339 (value ).nanosecond
327
+ == nanos
328
+ )
289
329
290
330
@staticmethod
291
331
def test_timestamp_pb_wo_nanos_naive ():
292
332
stamp = datetime_helpers .DatetimeWithNanoseconds (
293
- 2016 , 12 , 20 , 21 , 13 , 47 , 123456 )
333
+ 2016 , 12 , 20 , 21 , 13 , 47 , 123456
334
+ )
294
335
delta = stamp .replace (tzinfo = pytz .UTC ) - datetime_helpers ._UTC_EPOCH
295
336
seconds = int (delta .total_seconds ())
296
337
nanos = 123456000
@@ -304,7 +345,8 @@ def test_timestamp_pb_w_nanos():
304
345
)
305
346
delta = stamp - datetime_helpers ._UTC_EPOCH
306
347
timestamp = timestamp_pb2 .Timestamp (
307
- seconds = int (delta .total_seconds ()), nanos = 123456789 )
348
+ seconds = int (delta .total_seconds ()), nanos = 123456789
349
+ )
308
350
assert stamp .timestamp_pb () == timestamp
309
351
310
352
@staticmethod
@@ -314,8 +356,7 @@ def test_from_timestamp_pb_wo_nanos():
314
356
seconds = int (delta .total_seconds ())
315
357
timestamp = timestamp_pb2 .Timestamp (seconds = seconds )
316
358
317
- stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (
318
- timestamp )
359
+ stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (timestamp )
319
360
320
361
assert _to_seconds (when ) == _to_seconds (stamp )
321
362
assert stamp .microsecond == 0
@@ -329,8 +370,7 @@ def test_from_timestamp_pb_w_nanos():
329
370
seconds = int (delta .total_seconds ())
330
371
timestamp = timestamp_pb2 .Timestamp (seconds = seconds , nanos = 123456789 )
331
372
332
- stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (
333
- timestamp )
373
+ stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (timestamp )
334
374
335
375
assert _to_seconds (when ) == _to_seconds (stamp )
336
376
assert stamp .microsecond == 123456
0 commit comments