From 2cd5f3914de80bd3e830bbcf2c9b1d27ce7e5a97 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Sun, 30 Sep 2018 15:45:18 +1300 Subject: [PATCH 1/4] Add unit tests for timezone.get_local_time_zone --- isodatetime/tests.py | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/isodatetime/tests.py b/isodatetime/tests.py index ab965cf..44063fb 100644 --- a/isodatetime/tests.py +++ b/isodatetime/tests.py @@ -21,12 +21,14 @@ import copy import multiprocessing import unittest +import mock from . import data from . import dumpers from . import parsers from . import parser_spec from . import util +from . import timezone def get_timeduration_tests(): @@ -1569,6 +1571,68 @@ def sum(self, x, y): # in total, we have only three calls, as 2 were cached! self.assertEqual(3, temp_class.times_called) + # data provider for the test test_get_local_time_zone_no_dst + # the format for the parameters is [tz_seconds, expected_hours, expected_minutes]] + get_local_time_zone_no_dst = [ + [45900, 12, 45], # pacific/chatham, +12:45 + [20700, 5, 45], # asia/kathmandu, +05:45 + [3600, 1, 0], # arctic/longyearbyen, +01:00 + [0, 0, 0], # UTC + [-10800, -3, 0], # america/sao_paulo, -03:00 + [-12600, -3, 30] # america/st_johns, -03:30 + ] + + @mock.patch('isodatetime.timezone.time') + def test_get_local_time_zone_no_dst(self, mock_time): + """Test that the hour/minute returned is correct. + + Parts of the time module are mocked so that we can specify scenarios + without daylight saving time.""" + for tz_seconds, expected_hours, expected_minutes in self.get_local_time_zone_no_dst: + # for a pre-defined timezone + mock_time.timezone.__neg__.return_value = tz_seconds + # time without dst + mock_time.daylight = False + # and localtime also without dst + mock_localtime = mock.Mock() + mock_time.localtime.return_value = mock_localtime + mock_localtime.tm_isdst = 0 + hours, minutes = timezone.get_local_time_zone() + self.assertEqual(expected_hours, hours) + self.assertEqual(expected_minutes, minutes) + + # data provider for the test test_get_local_time_zone_with_dst + # the format for the parameters is [tz_seconds, tz_alt_seconds, expected_hours, expected_minutes] + get_local_time_zone_with_dst = [ + [45900, 49500, 13, 45], # pacific/chatham, +12:45 and +13:45 + [43200, 46800, 13, 0], # pacific/auckland, +12:00 and +13:00 + [3600, 7200, 2, 0], # arctic/longyearbyen, +01:00 + [0, 0, 0, 0], # UTC + [-10800, -7200, -2, 0], # america/sao_paulo, -03:00 and -02:00, + [-12600, -9000, -2, 30] # america/st_johns, -03:30 and -02:30 + ] + + @mock.patch('isodatetime.timezone.time') + def test_get_local_time_zone_with_dst(self, mock_time): + """Test that the hour/minute returned is correct + + Parts of the time module are mocked so that we can specify scenarios + with daylight saving time.""" + for tz_seconds, tz_alt_seconds, expected_hours, expected_minutes in self.get_local_time_zone_with_dst: + # for a pre-defined timezone + mock_time.timezone.__neg__.return_value = tz_seconds + # time without dst + mock_time.daylight = True + # and localtime also without dst + mock_localtime = mock.MagicMock() + mock_time.localtime.return_value = mock_localtime + mock_localtime.tm_isdst = 1 + # and finally with the following alternative time for when dst is set + mock_time.altzone.__neg__.return_value = tz_alt_seconds + hours, minutes = timezone.get_local_time_zone() + self.assertEqual(expected_hours, hours) + self.assertEqual(expected_minutes, minutes) + def assert_equal(data1, data2): """A function-level equivalent of the unittest method.""" From 26a966014e30054dcbc211b7a8e2cfabdd4e29ba Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Mon, 1 Oct 2018 11:15:42 +1300 Subject: [PATCH 2/4] Add unit tests for timezone.get_local_time_zone_format --- isodatetime/tests.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/isodatetime/tests.py b/isodatetime/tests.py index 44063fb..30aa75b 100644 --- a/isodatetime/tests.py +++ b/isodatetime/tests.py @@ -1633,6 +1633,46 @@ def test_get_local_time_zone_with_dst(self, mock_time): self.assertEqual(expected_hours, hours) self.assertEqual(expected_minutes, minutes) + # data provider for the test test_get_local_time_zone_format + # the format for the parameters is [tz_seconds, extended_mode, reduced_mode, expected_format] + get_test_get_local_time_zone_format = [ + # UTC + [0, False, False, "Z"], # UTC, returns Z, the flags are never used for UTC + # Positive values, some with minutes != 0 + [28800, False, False, "+0800"], # asia/macao, +08:00 + [28800, True, False, "+08:00"], # asia/macao, +08:00 + [28800, False, True, "+08"], # asia/macao, +08:00 + [45900, False, False, "+1245"], # pacific/chatham, +12:45 + [45900, True, False, "+12:45"], # pacific/chatham, +12:45 + [45900, False, True, "+1245"], # pacific/chatham, +12:45 (attention as it's different in this case) + # Negative values, some with minutes != 0 + [-10800, False, False, "-0300"], # america/buenos_aires, -03:00 + [-10800, True, False, "-03:00"], # america/buenos_aires, -03:00 + [-10800, False, True, "-03"], # america/buenos_aires, -03:00 + [-12600, False, False, "-0330"], # america/st_johns, -03:30 + [-12600, True, False, "-03:30"], # america/st_johns, -03:30 + [-12600, False, True, "-0330"] # america/st_johns, -03:30 + ] + + @mock.patch('isodatetime.timezone.time') + def test_get_local_time_zone_format(self, mock_time): + """Test that the UTC offset string format is correct + + Parts of the time module are mocked so that we can specify scenarios + with certain timezone seconds offsets. Daylight saving time is not really + important for this test case""" + for tz_seconds, extended_mode, reduced_mode, expected_format in self.get_test_get_local_time_zone_format: + # for a pre-defined timezone + mock_time.timezone.__neg__.return_value = tz_seconds + # time without dst + mock_time.daylight = False + # and localtime also without dst + mock_localtime = mock.Mock() + mock_time.localtime.return_value = mock_localtime + mock_localtime.tm_isdst = 0 + tz_format = timezone.get_local_time_zone_format(extended_mode, reduced_mode) + self.assertEqual(expected_format, tz_format) + def assert_equal(data1, data2): """A function-level equivalent of the unittest method.""" From ed81b0f9e8a5611bf8990df0147c56813337c84e Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Tue, 2 Oct 2018 10:18:36 +1300 Subject: [PATCH 3/4] Fix pycodestyle warnings --- isodatetime/tests.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/isodatetime/tests.py b/isodatetime/tests.py index 30aa75b..0e8563a 100644 --- a/isodatetime/tests.py +++ b/isodatetime/tests.py @@ -1572,7 +1572,8 @@ def sum(self, x, y): self.assertEqual(3, temp_class.times_called) # data provider for the test test_get_local_time_zone_no_dst - # the format for the parameters is [tz_seconds, expected_hours, expected_minutes]] + # the format for the parameters is + # [tz_seconds, expected_hours, expected_minutes]] get_local_time_zone_no_dst = [ [45900, 12, 45], # pacific/chatham, +12:45 [20700, 5, 45], # asia/kathmandu, +05:45 @@ -1588,7 +1589,8 @@ def test_get_local_time_zone_no_dst(self, mock_time): Parts of the time module are mocked so that we can specify scenarios without daylight saving time.""" - for tz_seconds, expected_hours, expected_minutes in self.get_local_time_zone_no_dst: + for tz_seconds, expected_hours, expected_minutes in \ + self.get_local_time_zone_no_dst: # for a pre-defined timezone mock_time.timezone.__neg__.return_value = tz_seconds # time without dst @@ -1602,7 +1604,8 @@ def test_get_local_time_zone_no_dst(self, mock_time): self.assertEqual(expected_minutes, minutes) # data provider for the test test_get_local_time_zone_with_dst - # the format for the parameters is [tz_seconds, tz_alt_seconds, expected_hours, expected_minutes] + # the format for the parameters is + # [tz_seconds, tz_alt_seconds, expected_hours, expected_minutes] get_local_time_zone_with_dst = [ [45900, 49500, 13, 45], # pacific/chatham, +12:45 and +13:45 [43200, 46800, 13, 0], # pacific/auckland, +12:00 and +13:00 @@ -1618,7 +1621,8 @@ def test_get_local_time_zone_with_dst(self, mock_time): Parts of the time module are mocked so that we can specify scenarios with daylight saving time.""" - for tz_seconds, tz_alt_seconds, expected_hours, expected_minutes in self.get_local_time_zone_with_dst: + for tz_seconds, tz_alt_seconds, expected_hours, expected_minutes in \ + self.get_local_time_zone_with_dst: # for a pre-defined timezone mock_time.timezone.__neg__.return_value = tz_seconds # time without dst @@ -1627,24 +1631,25 @@ def test_get_local_time_zone_with_dst(self, mock_time): mock_localtime = mock.MagicMock() mock_time.localtime.return_value = mock_localtime mock_localtime.tm_isdst = 1 - # and finally with the following alternative time for when dst is set + # and with the following alternative time for when dst is set mock_time.altzone.__neg__.return_value = tz_alt_seconds hours, minutes = timezone.get_local_time_zone() self.assertEqual(expected_hours, hours) self.assertEqual(expected_minutes, minutes) # data provider for the test test_get_local_time_zone_format - # the format for the parameters is [tz_seconds, extended_mode, reduced_mode, expected_format] + # the format for the parameters is + # [tz_seconds, extended_mode, reduced_mode, expected_format] get_test_get_local_time_zone_format = [ # UTC - [0, False, False, "Z"], # UTC, returns Z, the flags are never used for UTC + [0, False, False, "Z"], # UTC, returns Z, flags are never used for UTC # Positive values, some with minutes != 0 [28800, False, False, "+0800"], # asia/macao, +08:00 [28800, True, False, "+08:00"], # asia/macao, +08:00 [28800, False, True, "+08"], # asia/macao, +08:00 [45900, False, False, "+1245"], # pacific/chatham, +12:45 [45900, True, False, "+12:45"], # pacific/chatham, +12:45 - [45900, False, True, "+1245"], # pacific/chatham, +12:45 (attention as it's different in this case) + [45900, False, True, "+1245"], # pacific/chatham, +12:45 # Negative values, some with minutes != 0 [-10800, False, False, "-0300"], # america/buenos_aires, -03:00 [-10800, True, False, "-03:00"], # america/buenos_aires, -03:00 @@ -1659,9 +1664,10 @@ def test_get_local_time_zone_format(self, mock_time): """Test that the UTC offset string format is correct Parts of the time module are mocked so that we can specify scenarios - with certain timezone seconds offsets. Daylight saving time is not really + with certain timezone seconds offsets. DST is not really important for this test case""" - for tz_seconds, extended_mode, reduced_mode, expected_format in self.get_test_get_local_time_zone_format: + for tz_seconds, extended_mode, reduced_mode, expected_format in \ + self.get_test_get_local_time_zone_format: # for a pre-defined timezone mock_time.timezone.__neg__.return_value = tz_seconds # time without dst @@ -1670,7 +1676,8 @@ def test_get_local_time_zone_format(self, mock_time): mock_localtime = mock.Mock() mock_time.localtime.return_value = mock_localtime mock_localtime.tm_isdst = 0 - tz_format = timezone.get_local_time_zone_format(extended_mode, reduced_mode) + tz_format = timezone.get_local_time_zone_format(extended_mode, + reduced_mode) self.assertEqual(expected_format, tz_format) From d103bdb9f32deba4b9fd6e7155147ff471f4dda1 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Thu, 4 Oct 2018 23:42:27 +1300 Subject: [PATCH 4/4] Use the new object/enum for tz format mode in the tests --- isodatetime/tests.py | 46 ++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/isodatetime/tests.py b/isodatetime/tests.py index 0e8563a..63da800 100644 --- a/isodatetime/tests.py +++ b/isodatetime/tests.py @@ -1639,24 +1639,37 @@ def test_get_local_time_zone_with_dst(self, mock_time): # data provider for the test test_get_local_time_zone_format # the format for the parameters is - # [tz_seconds, extended_mode, reduced_mode, expected_format] + # [tz_seconds, tz_format_mode, expected_format] get_test_get_local_time_zone_format = [ # UTC - [0, False, False, "Z"], # UTC, returns Z, flags are never used for UTC + # UTC, returns Z, flags are never used for UTC + [0, timezone.TimeZoneFormatMode.normal, "Z"], # Positive values, some with minutes != 0 - [28800, False, False, "+0800"], # asia/macao, +08:00 - [28800, True, False, "+08:00"], # asia/macao, +08:00 - [28800, False, True, "+08"], # asia/macao, +08:00 - [45900, False, False, "+1245"], # pacific/chatham, +12:45 - [45900, True, False, "+12:45"], # pacific/chatham, +12:45 - [45900, False, True, "+1245"], # pacific/chatham, +12:45 + # asia/macao, +08:00 + [28800, timezone.TimeZoneFormatMode.normal, "+0800"], + # asia/macao, +08:00 + [28800, timezone.TimeZoneFormatMode.extended, "+08:00"], + # asia/macao, +08:00 + [28800, timezone.TimeZoneFormatMode.reduced, "+08"], + # pacific/chatham, +12:45 + [45900, timezone.TimeZoneFormatMode.normal, "+1245"], + # pacific/chatham, +12:45 + [45900, timezone.TimeZoneFormatMode.extended, "+12:45"], + # pacific/chatham, +12:45 + [45900, timezone.TimeZoneFormatMode.reduced, "+1245"], # Negative values, some with minutes != 0 - [-10800, False, False, "-0300"], # america/buenos_aires, -03:00 - [-10800, True, False, "-03:00"], # america/buenos_aires, -03:00 - [-10800, False, True, "-03"], # america/buenos_aires, -03:00 - [-12600, False, False, "-0330"], # america/st_johns, -03:30 - [-12600, True, False, "-03:30"], # america/st_johns, -03:30 - [-12600, False, True, "-0330"] # america/st_johns, -03:30 + # america/buenos_aires, -03:00 + [-10800, timezone.TimeZoneFormatMode.normal, "-0300"], + # america/buenos_aires, -03:00 + [-10800, timezone.TimeZoneFormatMode.extended, "-03:00"], + # america/buenos_aires, -03:00 + [-10800, timezone.TimeZoneFormatMode.reduced, "-03"], + # america/st_johns, -03:30 + [-12600, timezone.TimeZoneFormatMode.normal, "-0330"], + # america/st_johns, -03:30 + [-12600, timezone.TimeZoneFormatMode.extended, "-03:30"], + # america/st_johns, -03:30 + [-12600, timezone.TimeZoneFormatMode.reduced, "-0330"] ] @mock.patch('isodatetime.timezone.time') @@ -1666,7 +1679,7 @@ def test_get_local_time_zone_format(self, mock_time): Parts of the time module are mocked so that we can specify scenarios with certain timezone seconds offsets. DST is not really important for this test case""" - for tz_seconds, extended_mode, reduced_mode, expected_format in \ + for tz_seconds, tz_format_mode, expected_format in \ self.get_test_get_local_time_zone_format: # for a pre-defined timezone mock_time.timezone.__neg__.return_value = tz_seconds @@ -1676,8 +1689,7 @@ def test_get_local_time_zone_format(self, mock_time): mock_localtime = mock.Mock() mock_time.localtime.return_value = mock_localtime mock_localtime.tm_isdst = 0 - tz_format = timezone.get_local_time_zone_format(extended_mode, - reduced_mode) + tz_format = timezone.get_local_time_zone_format(tz_format_mode) self.assertEqual(expected_format, tz_format)