Skip to content

Commit 915b7af

Browse files
committed
test(utils): Add test for datetime_from_isoformat
Fixes GH-3515
1 parent eee4cac commit 915b7af

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

tests/test_utils.py

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,55 +61,72 @@ def _normalize_distribution_name(name):
6161
return re.sub(r"[-_.]+", "-", name).lower()
6262

6363

64-
@pytest.mark.parametrize(
65-
("input_str", "expected_output"),
64+
isoformat_inputs_and_datetime_outputs = (
6665
(
67-
(
68-
"2021-01-01T00:00:00.000000Z",
69-
datetime(2021, 1, 1, tzinfo=timezone.utc),
70-
), # UTC time
71-
(
72-
"2021-01-01T00:00:00.000000",
73-
datetime(2021, 1, 1).astimezone(timezone.utc),
74-
), # No TZ -- assume local but convert to UTC
75-
(
76-
"2021-01-01T00:00:00Z",
77-
datetime(2021, 1, 1, tzinfo=timezone.utc),
78-
), # UTC - No milliseconds
79-
(
80-
"2021-01-01T00:00:00.000000+00:00",
81-
datetime(2021, 1, 1, tzinfo=timezone.utc),
82-
),
83-
(
84-
"2021-01-01T00:00:00.000000-00:00",
85-
datetime(2021, 1, 1, tzinfo=timezone.utc),
86-
),
87-
(
88-
"2021-01-01T00:00:00.000000+0000",
89-
datetime(2021, 1, 1, tzinfo=timezone.utc),
90-
),
91-
(
92-
"2021-01-01T00:00:00.000000-0000",
93-
datetime(2021, 1, 1, tzinfo=timezone.utc),
94-
),
95-
(
96-
"2020-12-31T00:00:00.000000+02:00",
97-
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=2))),
98-
), # UTC+2 time
99-
(
100-
"2020-12-31T00:00:00.000000-0200",
101-
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
102-
), # UTC-2 time
103-
(
104-
"2020-12-31T00:00:00-0200",
105-
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
106-
), # UTC-2 time - no milliseconds
66+
"2021-01-01T00:00:00.000000Z",
67+
datetime(2021, 1, 1, tzinfo=timezone.utc),
68+
), # UTC time
69+
(
70+
"2021-01-01T00:00:00.000000",
71+
datetime(2021, 1, 1).astimezone(timezone.utc),
72+
), # No TZ -- assume local but convert to UTC
73+
(
74+
"2021-01-01T00:00:00Z",
75+
datetime(2021, 1, 1, tzinfo=timezone.utc),
76+
), # UTC - No milliseconds
77+
(
78+
"2021-01-01T00:00:00.000000+00:00",
79+
datetime(2021, 1, 1, tzinfo=timezone.utc),
80+
),
81+
(
82+
"2021-01-01T00:00:00.000000-00:00",
83+
datetime(2021, 1, 1, tzinfo=timezone.utc),
84+
),
85+
(
86+
"2021-01-01T00:00:00.000000+0000",
87+
datetime(2021, 1, 1, tzinfo=timezone.utc),
88+
),
89+
(
90+
"2021-01-01T00:00:00.000000-0000",
91+
datetime(2021, 1, 1, tzinfo=timezone.utc),
10792
),
93+
(
94+
"2020-12-31T00:00:00.000000+02:00",
95+
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=2))),
96+
), # UTC+2 time
97+
(
98+
"2020-12-31T00:00:00.000000-0200",
99+
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
100+
), # UTC-2 time
101+
(
102+
"2020-12-31T00:00:00-0200",
103+
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
104+
), # UTC-2 time - no milliseconds
105+
)
106+
107+
108+
@pytest.mark.parametrize(
109+
("input_str", "expected_output"),
110+
isoformat_inputs_and_datetime_outputs,
108111
)
109112
def test_datetime_from_isoformat(input_str, expected_output):
110113
assert datetime_from_isoformat(input_str) == expected_output, input_str
111114

112115

116+
@pytest.mark.parametrize(
117+
("input_str", "expected_output"),
118+
isoformat_inputs_and_datetime_outputs,
119+
)
120+
def test_datetime_from_isoformat_with_py_36_or_lower(input_str, expected_output):
121+
"""
122+
`fromisoformat` was added in Python version 3.7
123+
"""
124+
with mock.patch("sentry_sdk.utils.datetime") as datetime_mocked:
125+
datetime_mocked.fromisoformat.side_effect = AttributeError()
126+
datetime_mocked.strptime = datetime.strptime
127+
assert datetime_from_isoformat(input_str) == expected_output, input_str
128+
129+
113130
@pytest.mark.parametrize(
114131
"env_var_value,strict,expected",
115132
[

0 commit comments

Comments
 (0)