Fix breaking unit tests due to iso8601 changes

The move from iso8601===0.1.11 to iso8601===0.1.12 broke unit
tests in oslo.utils.

iso8601 used to do:
    from datetime import datetime

But now they call datetime.datetime():
    import datetime
    datetime.datetime()

Unfortunately the unit tests that mocked datetime.datetime() are now
mocking the one in iso8601. This causes a failure in the unit tests.

Fix this by using the 'wraps' argument to mock. So that the calls will
get passed through to datetime.datetime. Also changed to using the
decorator style of mock.

In addition Python 3 unit tests were broken due to changing how the
UTC time zone is represented from 'UTC' to 'UTC+00:00'.

Closes-Bug: #1747575
Closes-Bug: #1744160
Change-Id: Ia80ffb5e571cc5366bef2bc1a32c457a3c16843d
This commit is contained in:
John L. Villalovos 2018-02-05 22:29:38 -08:00
parent 3094ac66ba
commit 010fe3b102
2 changed files with 31 additions and 30 deletions

View File

@ -87,20 +87,18 @@ class TimeUtilsTest(test_base.BaseTestCase):
t = timeutils.parse_strtime(s)
self.assertEqual(orig_t, t)
def _test_is_older_than(self, fn):
strptime = datetime.datetime.strptime
with mock.patch('datetime.datetime') as datetime_mock:
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
datetime_mock.strptime = strptime
expect_true = timeutils.is_older_than(fn(self.one_minute_before),
59)
self.assertTrue(expect_true)
expect_false = timeutils.is_older_than(fn(self.one_minute_before),
60)
self.assertFalse(expect_false)
expect_false = timeutils.is_older_than(fn(self.one_minute_before),
61)
self.assertFalse(expect_false)
@mock.patch('datetime.datetime', wraps=datetime.datetime)
def _test_is_older_than(self, fn, datetime_mock):
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
expect_true = timeutils.is_older_than(fn(self.one_minute_before),
59)
self.assertTrue(expect_true)
expect_false = timeutils.is_older_than(fn(self.one_minute_before),
60)
self.assertFalse(expect_false)
expect_false = timeutils.is_older_than(fn(self.one_minute_before),
61)
self.assertFalse(expect_false)
def test_is_older_than_datetime(self):
self._test_is_older_than(lambda x: x)
@ -118,20 +116,18 @@ class TimeUtilsTest(test_base.BaseTestCase):
tzinfo=iso8601.iso8601.FixedOffset(1, 0, 'foo')).replace(
hour=7))
def _test_is_newer_than(self, fn):
strptime = datetime.datetime.strptime
with mock.patch('datetime.datetime') as datetime_mock:
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
datetime_mock.strptime = strptime
expect_true = timeutils.is_newer_than(fn(self.one_minute_after),
59)
self.assertTrue(expect_true)
expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
60)
self.assertFalse(expect_false)
expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
61)
self.assertFalse(expect_false)
@mock.patch('datetime.datetime', wraps=datetime.datetime)
def _test_is_newer_than(self, fn, datetime_mock):
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
expect_true = timeutils.is_newer_than(fn(self.one_minute_after),
59)
self.assertTrue(expect_true)
expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
60)
self.assertFalse(expect_false)
expect_false = timeutils.is_newer_than(fn(self.one_minute_after),
61)
self.assertFalse(expect_false)
def test_is_newer_than_datetime(self):
self._test_is_newer_than(lambda x: x)

View File

@ -55,7 +55,8 @@ def isotime(at=None, subsecond=False):
if not subsecond
else _ISO8601_TIME_FORMAT_SUBSECOND)
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
st += ('Z' if tz == 'UTC' else tz)
# Need to handle either iso8601 or python UTC format
st += ('Z' if tz in ('UTC', 'UTC+00:00') else tz)
return st
@ -256,7 +257,9 @@ def marshall_now(now=None):
minute=now.minute, second=now.second,
microsecond=now.microsecond)
if now.tzinfo:
d['tzname'] = now.tzinfo.tzname(None)
# Need to handle either iso8601 or python UTC format
tzname = now.tzinfo.tzname(None)
d['tzname'] = 'UTC' if tzname == 'UTC+00:00' else tzname
return d
@ -283,6 +286,8 @@ def unmarshall_time(tyme):
microsecond=tyme['microsecond'])
tzname = tyme.get('tzname')
if tzname:
# Need to handle either iso8601 or python UTC format
tzname = 'UTC' if tzname == 'UTC+00:00' else tzname
tzinfo = pytz.timezone(tzname)
dt = tzinfo.localize(dt)
return dt