@@ -82,18 +82,18 @@ def test_from_rfc3339():
8282 )
8383
8484
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+ )
9190
92- def test_from_rfc3339_with_nanos ():
93- value = "2009-12-17T12:44:32.123456789Z"
9491
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+ )
9797
9898
9999def test_from_rfc3339_nanos_without_nanos ():
@@ -103,11 +103,33 @@ def test_from_rfc3339_nanos_without_nanos():
103103 )
104104
105105
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"
108128
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
111133
112134
113135@pytest .mark .parametrize (
@@ -130,6 +152,18 @@ def test_from_rfc3339_nanos_with_truncated_nanos(truncated, micros):
130152 )
131153
132154
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+
133167def test_to_rfc3339 ():
134168 value = datetime .datetime (2016 , 4 , 5 , 13 , 30 , 0 )
135169 expected = "2016-04-05T13:30:00.000000Z"
@@ -157,10 +191,11 @@ def test_to_rfc3339_with_non_utc_ignore_zone():
157191
158192
159193class Test_DateTimeWithNanos (object ):
160-
161194 @staticmethod
162195 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+ )
164199 assert stamp .year == 2016
165200 assert stamp .month == 12
166201 assert stamp .day == 20
@@ -200,7 +235,9 @@ def test_ctor_w_micros_keyword_and_nanos():
200235
201236 @staticmethod
202237 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+ )
204241 assert stamp .rfc3339 () == "2016-12-20T21:13:47.123456Z"
205242
206243 @staticmethod
@@ -285,12 +322,16 @@ def test_from_rfc3339_w_full_precision():
285322 )
286323 def test_from_rfc3339_test_nanoseconds (fractional , nanos ):
287324 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+ )
289329
290330 @staticmethod
291331 def test_timestamp_pb_wo_nanos_naive ():
292332 stamp = datetime_helpers .DatetimeWithNanoseconds (
293- 2016 , 12 , 20 , 21 , 13 , 47 , 123456 )
333+ 2016 , 12 , 20 , 21 , 13 , 47 , 123456
334+ )
294335 delta = stamp .replace (tzinfo = pytz .UTC ) - datetime_helpers ._UTC_EPOCH
295336 seconds = int (delta .total_seconds ())
296337 nanos = 123456000
@@ -304,7 +345,8 @@ def test_timestamp_pb_w_nanos():
304345 )
305346 delta = stamp - datetime_helpers ._UTC_EPOCH
306347 timestamp = timestamp_pb2 .Timestamp (
307- seconds = int (delta .total_seconds ()), nanos = 123456789 )
348+ seconds = int (delta .total_seconds ()), nanos = 123456789
349+ )
308350 assert stamp .timestamp_pb () == timestamp
309351
310352 @staticmethod
@@ -314,8 +356,7 @@ def test_from_timestamp_pb_wo_nanos():
314356 seconds = int (delta .total_seconds ())
315357 timestamp = timestamp_pb2 .Timestamp (seconds = seconds )
316358
317- stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (
318- timestamp )
359+ stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (timestamp )
319360
320361 assert _to_seconds (when ) == _to_seconds (stamp )
321362 assert stamp .microsecond == 0
@@ -329,8 +370,7 @@ def test_from_timestamp_pb_w_nanos():
329370 seconds = int (delta .total_seconds ())
330371 timestamp = timestamp_pb2 .Timestamp (seconds = seconds , nanos = 123456789 )
331372
332- stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (
333- timestamp )
373+ stamp = datetime_helpers .DatetimeWithNanoseconds .from_timestamp_pb (timestamp )
334374
335375 assert _to_seconds (when ) == _to_seconds (stamp )
336376 assert stamp .microsecond == 123456
0 commit comments