|
| 1 | +# Copyright 2016 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from datetime import datetime |
| 17 | + |
| 18 | +from .dateutil import UTC, TimezoneInfo, format_rfc3339, parse_rfc3339 |
| 19 | + |
| 20 | + |
| 21 | +class DateUtilTest(unittest.TestCase): |
| 22 | + |
| 23 | + def _parse_rfc3339_test(self, st, y, m, d, h, mn, s, us): |
| 24 | + actual = parse_rfc3339(st) |
| 25 | + expected = datetime(y, m, d, h, mn, s, us, UTC) |
| 26 | + self.assertEqual(expected, actual) |
| 27 | + |
| 28 | + def test_parse_rfc3339(self): |
| 29 | + self._parse_rfc3339_test("2017-07-25T04:44:21Z", |
| 30 | + 2017, 7, 25, 4, 44, 21, 0) |
| 31 | + self._parse_rfc3339_test("2017-07-25 04:44:21Z", |
| 32 | + 2017, 7, 25, 4, 44, 21, 0) |
| 33 | + self._parse_rfc3339_test("2017-07-25T04:44:21", |
| 34 | + 2017, 7, 25, 4, 44, 21, 0) |
| 35 | + self._parse_rfc3339_test("2017-07-25T04:44:21z", |
| 36 | + 2017, 7, 25, 4, 44, 21, 0) |
| 37 | + self._parse_rfc3339_test("2017-07-25T04:44:21+03:00", |
| 38 | + 2017, 7, 25, 1, 44, 21, 0) |
| 39 | + self._parse_rfc3339_test("2017-07-25T04:44:21-03:00", |
| 40 | + 2017, 7, 25, 7, 44, 21, 0) |
| 41 | + |
| 42 | + self._parse_rfc3339_test("2017-07-25T04:44:21,005Z", |
| 43 | + 2017, 7, 25, 4, 44, 21, 5000) |
| 44 | + self._parse_rfc3339_test("2017-07-25T04:44:21.005Z", |
| 45 | + 2017, 7, 25, 4, 44, 21, 5000) |
| 46 | + self._parse_rfc3339_test("2017-07-25 04:44:21.0050Z", |
| 47 | + 2017, 7, 25, 4, 44, 21, 5000) |
| 48 | + self._parse_rfc3339_test("2017-07-25T04:44:21.5", |
| 49 | + 2017, 7, 25, 4, 44, 21, 500000) |
| 50 | + self._parse_rfc3339_test("2017-07-25T04:44:21.005z", |
| 51 | + 2017, 7, 25, 4, 44, 21, 5000) |
| 52 | + self._parse_rfc3339_test("2017-07-25T04:44:21.005+03:00", |
| 53 | + 2017, 7, 25, 1, 44, 21, 5000) |
| 54 | + self._parse_rfc3339_test("2017-07-25T04:44:21.005-03:00", |
| 55 | + 2017, 7, 25, 7, 44, 21, 5000) |
| 56 | + |
| 57 | + def test_format_rfc3339(self): |
| 58 | + self.assertEqual( |
| 59 | + format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0, UTC)), |
| 60 | + "2017-07-25T04:44:21Z") |
| 61 | + self.assertEqual( |
| 62 | + format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0, |
| 63 | + TimezoneInfo(2, 0))), |
| 64 | + "2017-07-25T02:44:21Z") |
| 65 | + self.assertEqual( |
| 66 | + format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0, |
| 67 | + TimezoneInfo(-2, 30))), |
| 68 | + "2017-07-25T07:14:21Z") |
| 69 | + |
| 70 | + def test_parse_rfc3339_invalid_formats(self): |
| 71 | + """Test that invalid RFC3339 formats raise ValueError""" |
| 72 | + invalid_inputs = [ |
| 73 | + "2025-13-02T13:37:00Z", # Invalid month |
| 74 | + "2025-12-32T13:37:00Z", # Invalid day |
| 75 | + "2025-12-02T25:00:00Z", # Invalid hour |
| 76 | + "2025-12-02T13:60:00Z", # Invalid minute |
| 77 | + "2025-12-02T13:37:60Z", # Invalid second |
| 78 | + "not-a-valid-date", # Completely invalid |
| 79 | + "", # Empty string |
| 80 | + "2025-12-02Z13:37:00", # Timezone before time |
| 81 | + ] |
| 82 | + |
| 83 | + for invalid_input in invalid_inputs: |
| 84 | + with self.assertRaises(ValueError): |
| 85 | + parse_rfc3339(invalid_input) |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + def test_parse_rfc3339_with_whitespace(self): |
| 90 | + """Test that leading/trailing whitespace is handled""" |
| 91 | + actual = parse_rfc3339(" 2017-07-25T04:44:21Z ") |
| 92 | + expected = datetime(2017, 7, 25, 4, 44, 21, 0, UTC) |
| 93 | + self.assertEqual(expected, actual) |
| 94 | + |
| 95 | + def test_parse_rfc3339_error_message_clarity(self): |
| 96 | + """Test that error messages are clear and helpful""" |
| 97 | + try: |
| 98 | + parse_rfc3339("invalid-date-format") |
| 99 | + except ValueError as e: |
| 100 | + error_msg = str(e) |
| 101 | + # Verify error message contains helpful information |
| 102 | + self.assertIn("Invalid RFC3339", error_msg) |
| 103 | + self.assertIn("YYYY-MM-DD", error_msg) |
| 104 | + self.assertIn("expected", error_msg) |
| 105 | + |
| 106 | + def test_parse_rfc3339_handles_none_from_timezone_regex(self): |
| 107 | + """Test parse_rfc3339 handles timezone regex returning None. |
| 108 | +
|
| 109 | + This test addresses the GitHub issue where parse_rfc3339 was |
| 110 | + calling .groups() on None when the timezone regex failed to match, |
| 111 | + causing: 'NoneType' object has no attribute 'groups' |
| 112 | +
|
| 113 | + The fix adds a check to ensure _re_timezone.search() result is |
| 114 | + not None before calling .groups(), and provides a clear error |
| 115 | + message. |
| 116 | + """ |
| 117 | + # The main RFC3339 regex allows space in timezone position: [zZ ] |
| 118 | + # If a space ends up in groups[7], it should be handled gracefully |
| 119 | + # Since the current code uses strip(), trailing spaces are removed, |
| 120 | + # but the fix ensures robustness for any edge case |
| 121 | + |
| 122 | + # Test that space in timezone is treated as UTC (like Z/z) |
| 123 | + actual = parse_rfc3339("2017-07-25 04:44:21") |
| 124 | + expected = datetime(2017, 7, 25, 4, 44, 21, 0, UTC) |
| 125 | + self.assertEqual(expected, actual) |
0 commit comments