Update tzlocal to v1.5.1

This commit is contained in:
JonnyWong16 2018-10-23 22:40:49 -07:00
parent f42581a5a6
commit dee544c951
9 changed files with 447 additions and 273 deletions

View file

@ -1,11 +1,19 @@
import sys
import mock
import os
from datetime import datetime
import unittest
import pytz
import sys
import tzlocal.unix
import unittest
from datetime import datetime
class TzLocalTests(unittest.TestCase):
def setUp(self):
if 'TZ' in os.environ:
del os.environ['TZ']
self.path = os.path.split(__file__)[0]
def test_env(self):
tz_harare = tzlocal.unix._tz_from_env(':Africa/Harare')
@ -15,8 +23,7 @@ class TzLocalTests(unittest.TestCase):
tz_harare = tzlocal.unix._tz_from_env('Africa/Harare')
self.assertEqual(tz_harare.zone, 'Africa/Harare')
local_path = os.path.split(__file__)[0]
tz_local = tzlocal.unix._tz_from_env(':' + os.path.join(local_path, 'test_data', 'Harare'))
tz_local = tzlocal.unix._tz_from_env(':' + os.path.join(self.path, 'test_data', 'Harare'))
self.assertEqual(tz_local.zone, 'local')
# Make sure the local timezone is the same as the Harare one above.
# We test this with a past date, so that we don't run into future changes
@ -27,31 +34,68 @@ class TzLocalTests(unittest.TestCase):
# Non-zoneinfo timezones are not supported in the TZ environment.
self.assertRaises(pytz.UnknownTimeZoneError, tzlocal.unix._tz_from_env, 'GMT+03:00')
# Test the _try function
os.environ['TZ'] = 'Africa/Harare'
tz_harare = tzlocal.unix._try_tz_from_env()
self.assertEqual(tz_harare.zone, 'Africa/Harare')
# With a zone that doesn't exist
os.environ['TZ'] = 'Just Nonsense'
tz_harare = tzlocal.unix._try_tz_from_env()
self.assertIsNone(tz_harare)
def test_timezone(self):
# Most versions of Ubuntu
local_path = os.path.split(__file__)[0]
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone'))
tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'timezone'))
self.assertEqual(tz.zone, 'Africa/Harare')
def test_zone_setting(self):
# A ZONE setting in /etc/sysconfig/clock, f ex CentOS
local_path = os.path.split(__file__)[0]
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'zone_setting'))
tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'zone_setting'))
self.assertEqual(tz.zone, 'Africa/Harare')
def test_timezone_setting(self):
# A ZONE setting in /etc/conf.d/clock, f ex Gentoo
local_path = os.path.split(__file__)[0]
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone_setting'))
tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'timezone_setting'))
self.assertEqual(tz.zone, 'Africa/Harare')
def test_symlink_localtime(self):
# A ZONE setting in the target path of a symbolic linked localtime, f ex systemd distributions
tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'symlink_localtime'))
self.assertEqual(tz.zone, 'Africa/Harare')
def test_vardbzoneinfo_setting(self):
# A ZONE setting in /etc/conf.d/clock, f ex Gentoo
tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'vardbzoneinfo'))
self.assertEqual(tz.zone, 'Africa/Harare')
def test_only_localtime(self):
local_path = os.path.split(__file__)[0]
tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'localtime'))
tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'localtime'))
self.assertEqual(tz.zone, 'local')
dt = datetime(2012, 1, 1, 5)
self.assertEqual(pytz.timezone('Africa/Harare').localize(dt), tz.localize(dt))
def test_get_reload(self):
os.environ['TZ'] = 'Africa/Harare'
tz_harare = tzlocal.unix.get_localzone()
self.assertEqual(tz_harare.zone, 'Africa/Harare')
# Changing the TZ makes no difference, because it's cached
os.environ['TZ'] = 'Africa/Johannesburg'
tz_harare = tzlocal.unix.get_localzone()
self.assertEqual(tz_harare.zone, 'Africa/Harare')
# So we reload it
tz_harare = tzlocal.unix.reload_localzone()
self.assertEqual(tz_harare.zone, 'Africa/Johannesburg')
def test_fail(self):
with self.assertRaises(pytz.exceptions.UnknownTimeZoneError):
tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data'))
if sys.platform == 'win32':
import tzlocal.win32
@ -60,5 +104,31 @@ if sys.platform == 'win32':
def test_win32(self):
tzlocal.win32.get_localzone()
else:
class TzWin32Tests(unittest.TestCase):
def test_win32_on_unix(self):
# Yes, winreg is all mocked out, but this test means we at least
# catch syntax errors, etc.
winreg = mock.MagicMock()
winreg.OpenKey = mock.MagicMock()
winreg.OpenKey.close = mock.MagicMock()
winreg.QueryInfoKey = mock.MagicMock(return_value=(1, 1))
winreg.EnumValue = mock.MagicMock(
return_value=('TimeZoneKeyName','Belarus Standard Time'))
winreg.EnumKey = mock.Mock(return_value='Bahia Standard Time')
sys.modules['winreg'] = winreg
import tzlocal.win32
tz = tzlocal.win32.get_localzone()
self.assertEqual(tz.zone, 'Europe/Minsk')
tzlocal.win32.valuestodict = mock.Mock(return_value={
'StandardName': 'Mocked Standard Time',
'Std': 'Mocked Standard Time',
})
tz = tzlocal.win32.reload_localzone()
self.assertEqual(tz.zone, 'America/Bahia')
if __name__ == '__main__':
unittest.main()