test_timezone.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2021, Brandon Nielsen
  3. # All rights reserved.
  4. #
  5. # This software may be modified and distributed under the terms
  6. # of the BSD license. See the LICENSE file for details.
  7. import unittest
  8. import aniso8601
  9. from aniso8601.exceptions import ISOFormatError
  10. from aniso8601.tests.compat import mock
  11. from aniso8601.timezone import parse_timezone
  12. class TestTimezoneParserFunctions(unittest.TestCase):
  13. def test_parse_timezone(self):
  14. testtuples = (
  15. ("Z", {"negative": False, "Z": True, "name": "Z"}),
  16. ("+00:00", {"negative": False, "hh": "00", "mm": "00", "name": "+00:00"}),
  17. ("+01:00", {"negative": False, "hh": "01", "mm": "00", "name": "+01:00"}),
  18. ("-01:00", {"negative": True, "hh": "01", "mm": "00", "name": "-01:00"}),
  19. ("+00:12", {"negative": False, "hh": "00", "mm": "12", "name": "+00:12"}),
  20. ("+01:23", {"negative": False, "hh": "01", "mm": "23", "name": "+01:23"}),
  21. ("-01:23", {"negative": True, "hh": "01", "mm": "23", "name": "-01:23"}),
  22. ("+0000", {"negative": False, "hh": "00", "mm": "00", "name": "+0000"}),
  23. ("+0100", {"negative": False, "hh": "01", "mm": "00", "name": "+0100"}),
  24. ("-0100", {"negative": True, "hh": "01", "mm": "00", "name": "-0100"}),
  25. ("+0012", {"negative": False, "hh": "00", "mm": "12", "name": "+0012"}),
  26. ("+0123", {"negative": False, "hh": "01", "mm": "23", "name": "+0123"}),
  27. ("-0123", {"negative": True, "hh": "01", "mm": "23", "name": "-0123"}),
  28. ("+00", {"negative": False, "hh": "00", "mm": None, "name": "+00"}),
  29. ("+01", {"negative": False, "hh": "01", "mm": None, "name": "+01"}),
  30. ("-01", {"negative": True, "hh": "01", "mm": None, "name": "-01"}),
  31. ("+12", {"negative": False, "hh": "12", "mm": None, "name": "+12"}),
  32. ("-12", {"negative": True, "hh": "12", "mm": None, "name": "-12"}),
  33. )
  34. for testtuple in testtuples:
  35. with mock.patch.object(
  36. aniso8601.timezone.PythonTimeBuilder, "build_timezone"
  37. ) as mockBuildTimezone:
  38. mockBuildTimezone.return_value = testtuple[1]
  39. result = parse_timezone(testtuple[0])
  40. self.assertEqual(result, testtuple[1])
  41. mockBuildTimezone.assert_called_once_with(**testtuple[1])
  42. def test_parse_timezone_badtype(self):
  43. testtuples = (None, 1, False, 1.234)
  44. for testtuple in testtuples:
  45. with self.assertRaises(ValueError):
  46. parse_timezone(testtuple, builder=None)
  47. def test_parse_timezone_badstr(self):
  48. testtuples = (
  49. "+1",
  50. "-00",
  51. "-0000",
  52. "-00:00",
  53. "01",
  54. "0123",
  55. "@12:34",
  56. "Y",
  57. " Z",
  58. "Z ",
  59. " Z ",
  60. "bad",
  61. "",
  62. )
  63. for testtuple in testtuples:
  64. with self.assertRaises(ISOFormatError):
  65. parse_timezone(testtuple, builder=None)
  66. def test_parse_timezone_mockbuilder(self):
  67. mockBuilder = mock.Mock()
  68. expectedargs = {"negative": False, "Z": True, "name": "Z"}
  69. mockBuilder.build_timezone.return_value = expectedargs
  70. result = parse_timezone("Z", builder=mockBuilder)
  71. self.assertEqual(result, expectedargs)
  72. mockBuilder.build_timezone.assert_called_once_with(**expectedargs)
  73. mockBuilder = mock.Mock()
  74. expectedargs = {"negative": False, "hh": "00", "mm": "00", "name": "+00:00"}
  75. mockBuilder.build_timezone.return_value = expectedargs
  76. result = parse_timezone("+00:00", builder=mockBuilder)
  77. self.assertEqual(result, expectedargs)
  78. mockBuilder.build_timezone.assert_called_once_with(**expectedargs)
  79. mockBuilder = mock.Mock()
  80. expectedargs = {"negative": True, "hh": "01", "mm": "23", "name": "-01:23"}
  81. mockBuilder.build_timezone.return_value = expectedargs
  82. result = parse_timezone("-01:23", builder=mockBuilder)
  83. self.assertEqual(result, expectedargs)
  84. mockBuilder.build_timezone.assert_called_once_with(**expectedargs)
  85. def test_parse_timezone_negativezero(self):
  86. # A 0 offset cannot be negative
  87. with self.assertRaises(ISOFormatError):
  88. parse_timezone("-00:00", builder=None)
  89. with self.assertRaises(ISOFormatError):
  90. parse_timezone("-0000", builder=None)
  91. with self.assertRaises(ISOFormatError):
  92. parse_timezone("-00", builder=None)