Skip to content

Commit 1fc8661

Browse files
committed
update datetime_helpers
* 'if-else' statement restored to throw an exception * wrote additional tests to cover the exception
1 parent 447ed3e commit 1fc8661

2 files changed

Lines changed: 40 additions & 13 deletions

File tree

api_core/google/api_core/datetime_helpers.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def from_rfc3339(value):
132132
133133
"""
134134
with_nanos = _RFC3339_NANOS.match(value)
135+
136+
if with_nanos is None:
137+
raise ValueError(
138+
"Timestamp: {!r}, does not match pattern: {!r}".format(
139+
value, _RFC3339_NANOS.pattern
140+
)
141+
)
142+
135143
bare_seconds = datetime.datetime.strptime(
136144
with_nanos.group("no_fraction"), _RFC3339_NO_FRACTION
137145
)
@@ -228,7 +236,7 @@ def rfc3339(self):
228236
"""
229237
if self._nanosecond == 0:
230238
return to_rfc3339(self)
231-
nanos = str(self._nanosecond).rjust(9, '0').rstrip("0")
239+
nanos = str(self._nanosecond).rjust(9, "0").rstrip("0")
232240
return "{}.{}Z".format(self.strftime(_RFC3339_NO_FRACTION), nanos)
233241

234242
@classmethod

api_core/tests/unit/test_datetime_helpers.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525

2626
ONE_MINUTE_IN_MICROSECONDS = 60 * 1e6
27-
MESSAGE = ("The `from_rfc3339_nanos` function is deprecated"
28-
" use `from_rfc3339` instead.")
27+
MESSAGE = (
28+
"The `from_rfc3339_nanos` function is deprecated" " use `from_rfc3339` instead."
29+
)
2930

3031

3132
def test_utcnow():
@@ -159,6 +160,18 @@ def test_from_rfc3339_nanos_with_truncated_nanos(truncated, micros):
159160
)
160161

161162

163+
def test_from_rfc3339_wo_nanos_raise_exception():
164+
value = "2009-12-17T12:44:32"
165+
with pytest.raises(ValueError):
166+
datetime_helpers.from_rfc3339(value)
167+
168+
169+
def test_from_rfc3339_w_nanos_raise_exception():
170+
value = "2009-12-17T12:44:32.123456"
171+
with pytest.raises(ValueError):
172+
datetime_helpers.from_rfc3339(value)
173+
174+
162175
def test_to_rfc3339():
163176
value = datetime.datetime(2016, 4, 5, 13, 30, 0)
164177
expected = "2016-04-05T13:30:00.000000Z"
@@ -186,10 +199,11 @@ def test_to_rfc3339_with_non_utc_ignore_zone():
186199

187200

188201
class Test_DateTimeWithNanos(object):
189-
190202
@staticmethod
191203
def test_ctor_wo_nanos():
192-
stamp = datetime_helpers.DatetimeWithNanoseconds(2016, 12, 20, 21, 13, 47, 123456)
204+
stamp = datetime_helpers.DatetimeWithNanoseconds(
205+
2016, 12, 20, 21, 13, 47, 123456
206+
)
193207
assert stamp.year == 2016
194208
assert stamp.month == 12
195209
assert stamp.day == 20
@@ -229,7 +243,9 @@ def test_ctor_w_micros_keyword_and_nanos():
229243

230244
@staticmethod
231245
def test_rfc3339_wo_nanos():
232-
stamp = datetime_helpers.DatetimeWithNanoseconds(2016, 12, 20, 21, 13, 47, 123456)
246+
stamp = datetime_helpers.DatetimeWithNanoseconds(
247+
2016, 12, 20, 21, 13, 47, 123456
248+
)
233249
assert stamp.rfc3339() == "2016-12-20T21:13:47.123456Z"
234250

235251
@staticmethod
@@ -314,12 +330,16 @@ def test_from_rfc3339_w_full_precision():
314330
)
315331
def test_from_rfc3339_test_nanoseconds(fractional, nanos):
316332
value = "2009-12-17T12:44:32.{}Z".format(fractional)
317-
assert datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(value).nanosecond == nanos
333+
assert (
334+
datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(value).nanosecond
335+
== nanos
336+
)
318337

319338
@staticmethod
320339
def test_timestamp_pb_wo_nanos_naive():
321340
stamp = datetime_helpers.DatetimeWithNanoseconds(
322-
2016, 12, 20, 21, 13, 47, 123456)
341+
2016, 12, 20, 21, 13, 47, 123456
342+
)
323343
delta = stamp.replace(tzinfo=pytz.UTC) - datetime_helpers._UTC_EPOCH
324344
seconds = int(delta.total_seconds())
325345
nanos = 123456000
@@ -333,7 +353,8 @@ def test_timestamp_pb_w_nanos():
333353
)
334354
delta = stamp - datetime_helpers._UTC_EPOCH
335355
timestamp = timestamp_pb2.Timestamp(
336-
seconds=int(delta.total_seconds()), nanos=123456789)
356+
seconds=int(delta.total_seconds()), nanos=123456789
357+
)
337358
assert stamp.timestamp_pb() == timestamp
338359

339360
@staticmethod
@@ -343,8 +364,7 @@ def test_from_timestamp_pb_wo_nanos():
343364
seconds = int(delta.total_seconds())
344365
timestamp = timestamp_pb2.Timestamp(seconds=seconds)
345366

346-
stamp = datetime_helpers.DatetimeWithNanoseconds.from_timestamp_pb(
347-
timestamp)
367+
stamp = datetime_helpers.DatetimeWithNanoseconds.from_timestamp_pb(timestamp)
348368

349369
assert _to_seconds(when) == _to_seconds(stamp)
350370
assert stamp.microsecond == 0
@@ -358,8 +378,7 @@ def test_from_timestamp_pb_w_nanos():
358378
seconds = int(delta.total_seconds())
359379
timestamp = timestamp_pb2.Timestamp(seconds=seconds, nanos=123456789)
360380

361-
stamp = datetime_helpers.DatetimeWithNanoseconds.from_timestamp_pb(
362-
timestamp)
381+
stamp = datetime_helpers.DatetimeWithNanoseconds.from_timestamp_pb(timestamp)
363382

364383
assert _to_seconds(when) == _to_seconds(stamp)
365384
assert stamp.microsecond == 123456

0 commit comments

Comments
 (0)