| 1 |
from datetime import datetime, timedelta, timezone |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from celery.exceptions import CPendingDeprecationWarning |
| 6 |
from celery.utils.iso8601 import parse_iso8601 |
| 7 |
|
| 8 |
|
| 9 |
def test_parse_iso8601_utc(): |
| 10 |
dt = parse_iso8601("2023-10-26T10:30:00Z") |
| 11 |
assert dt == datetime(2023, 10, 26, 10, 30, 0, tzinfo=timezone.utc) |
| 12 |
|
| 13 |
|
| 14 |
def test_parse_iso8601_positive_offset(): |
| 15 |
dt = parse_iso8601("2023-10-26T10:30:00+05:30") |
| 16 |
expected_tz = timezone(timedelta(hours=5, minutes=30)) |
| 17 |
assert dt == datetime(2023, 10, 26, 10, 30, 0, tzinfo=expected_tz) |
| 18 |
|
| 19 |
|
| 20 |
def test_parse_iso8601_negative_offset(): |
| 21 |
dt = parse_iso8601("2023-10-26T10:30:00-08:00") |
| 22 |
expected_tz = timezone(timedelta(hours=-8)) |
| 23 |
assert dt == datetime(2023, 10, 26, 10, 30, 0, tzinfo=expected_tz) |
| 24 |
|
| 25 |
|
| 26 |
def test_parse_iso8601_with_microseconds(): |
| 27 |
dt = parse_iso8601("2023-10-26T10:30:00.123456Z") |
| 28 |
assert dt == datetime(2023, 10, 26, 10, 30, 0, 123456, tzinfo=timezone.utc) |
| 29 |
|
| 30 |
|
| 31 |
def test_parse_iso8601_date_only(): |
| 32 |
dt = parse_iso8601("2023-10-26") |
| 33 |
assert dt == datetime(2023, 10, 26, 0, 0, 0) # Expects naive datetime |
| 34 |
|
| 35 |
|
| 36 |
def test_parse_iso8601_date_hour_minute_only(): |
| 37 |
# The regex uses '.' as a separator, often 'T' is used. |
| 38 |
# Let's test with 'T' as it's common in ISO8601. |
| 39 |
dt = parse_iso8601("2023-10-26T10:30") |
| 40 |
assert dt == datetime(2023, 10, 26, 10, 30, 0) # Expects naive datetime |
| 41 |
|
| 42 |
|
| 43 |
def test_parse_iso8601_invalid_string(): |
| 44 |
with pytest.raises(ValueError, match="unable to parse date string"): |
| 45 |
parse_iso8601("invalid-date-string") |
| 46 |
|
| 47 |
|
| 48 |
def test_parse_iso8601_malformed_strings(): |
| 49 |
# These strings match the regex but have invalid date/time component values |
| 50 |
invalid_component_strings = [ |
| 51 |
"2023-13-01T00:00:00Z", # Invalid month |
| 52 |
"2023-12-32T00:00:00Z", # Invalid day |
| 53 |
"2023-12-01T25:00:00Z", # Invalid hour |
| 54 |
"2023-12-01T00:60:00Z", # Invalid minute |
| 55 |
"2023-12-01T00:00:60Z", # Invalid second |
| 56 |
] |
| 57 |
for s in invalid_component_strings: |
| 58 |
# For these, the error comes from datetime constructor |
| 59 |
with pytest.raises(ValueError): |
| 60 |
parse_iso8601(s) |
| 61 |
|
| 62 |
# This string has a timezone format that is ignored by the parser, resulting in a naive datetime |
| 63 |
ignored_tz_string = "2023-10-26T10:30:00+05:AA" |
| 64 |
dt_ignored_tz = parse_iso8601(ignored_tz_string) |
| 65 |
assert dt_ignored_tz == datetime(2023, 10, 26, 10, 30, 0) |
| 66 |
assert dt_ignored_tz.tzinfo is None |
| 67 |
|
| 68 |
# This string does not match the main ISO8601_REGEX pattern correctly, leading to None groups |
| 69 |
unparseable_string = "20231026T103000Z" |
| 70 |
with pytest.raises(TypeError): # Expects TypeError due to int(None) |
| 71 |
parse_iso8601(unparseable_string) |
| 72 |
|
| 73 |
|
| 74 |
def test_parse_iso8601_deprecation_warning(): |
| 75 |
with pytest.warns(CPendingDeprecationWarning, match="parse_iso8601 is scheduled for deprecation"): |
| 76 |
parse_iso8601("2023-10-26T10:30:00Z") |