Fix python tests

This commit is contained in:
Deanna Garcia 2022-02-02 18:48:58 +00:00
parent e56d6936f3
commit d2ca349c2a

View File

@ -36,8 +36,6 @@ import collections.abc as collections_abc
import datetime import datetime
import unittest import unittest
import dateutil.tz
from google.protobuf import any_pb2 from google.protobuf import any_pb2
from google.protobuf import duration_pb2 from google.protobuf import duration_pb2
from google.protobuf import field_mask_pb2 from google.protobuf import field_mask_pb2
@ -268,37 +266,25 @@ class TimeUtilTest(TimeUtilTestBase):
message.FromDatetime(naive_end_of_time) message.FromDatetime(naive_end_of_time)
self.assertEqual(naive_end_of_time, message.ToDatetime()) self.assertEqual(naive_end_of_time, message.ToDatetime())
# Two hours after the Unix Epoch, around the world. def testDatetimeConversionWithTimezone(self):
@_parameterized.named_parameters( class TZ(datetime.tzinfo):
('London', [1970, 1, 1, 2], dateutil.tz.UTC),
('Tokyo', [1970, 1, 1, 11], dateutil.tz.gettz('Japan')),
('LA', [1969, 12, 31, 18], dateutil.tz.gettz('US/Pacific')),
)
def testTimezoneAwareDatetimeConversion(self, date_parts, tzinfo):
original_datetime = datelib.CreateDatetime(*date_parts, tzinfo=tzinfo)
message = timestamp_pb2.Timestamp() def utcoffset(self, _):
message.FromDatetime(original_datetime) return datetime.timedelta(hours=1)
self.assertEqual(7200, message.seconds)
self.assertEqual(0, message.nanos)
# ToDatetime() with no parameters produces a naive UTC datetime, i.e. it not def dst(self, _):
# only loses the original timezone information (e.g. US/Pacific) as it's return datetime.timedelta(0)
# "normalised" to UTC, but also drops the information that the datetime
# represents a UTC one.
naive_datetime = message.ToDatetime()
self.assertEqual(datetime.datetime(1970, 1, 1, 2), naive_datetime)
self.assertIsNone(naive_datetime.tzinfo)
self.assertNotEqual(original_datetime, naive_datetime) # not even for UTC!
# In contrast, ToDatetime(tzinfo=) produces an aware datetime in the given def tzname(self, _):
# timezone. return 'UTC+1'
aware_datetime = message.ToDatetime(tzinfo=tzinfo)
self.assertEqual(original_datetime, aware_datetime) message1 = timestamp_pb2.Timestamp()
self.assertEqual( dt = datetime.datetime(1970, 1, 1, 1, tzinfo=TZ())
datelib.CreateDatetime(1970, 1, 1, 2, tzinfo=dateutil.tz.UTC), message1.FromDatetime(dt)
aware_datetime) message2 = timestamp_pb2.Timestamp()
self.assertEqual(tzinfo, aware_datetime.tzinfo) dt = datetime.datetime(1970, 1, 1, 0)
message2.FromDatetime(dt)
self.assertEqual(message1, message2)
def testTimedeltaConversion(self): def testTimedeltaConversion(self):
message = duration_pb2.Duration() message = duration_pb2.Duration()