|
1 | 1 | import threading
|
2 | 2 | import re
|
3 | 3 | import sys
|
4 |
| -from datetime import timedelta |
| 4 | +from datetime import timedelta, datetime, timezone |
5 | 5 | from unittest import mock
|
6 | 6 |
|
7 | 7 | import pytest
|
|
13 | 13 | Components,
|
14 | 14 | Dsn,
|
15 | 15 | env_to_bool,
|
| 16 | + format_timestamp, |
16 | 17 | get_current_thread_meta,
|
17 | 18 | get_default_release,
|
18 | 19 | get_error_message,
|
@@ -950,3 +951,39 @@ def target():
|
950 | 951 | thread.start()
|
951 | 952 | thread.join()
|
952 | 953 | assert (main_thread.ident, main_thread.name) == results.get(timeout=1)
|
| 954 | + |
| 955 | + |
| 956 | +@pytest.mark.parametrize( |
| 957 | + ("datetime_object", "expected_output"), |
| 958 | + ( |
| 959 | + ( |
| 960 | + datetime(2021, 1, 1, tzinfo=timezone.utc), |
| 961 | + "2021-01-01T00:00:00.000000Z", |
| 962 | + ), # UTC time |
| 963 | + ( |
| 964 | + datetime(2021, 1, 1, tzinfo=timezone(timedelta(hours=2))), |
| 965 | + "2020-12-31T22:00:00.000000Z", |
| 966 | + ), # UTC+2 time |
| 967 | + ( |
| 968 | + datetime(2021, 1, 1, tzinfo=timezone(timedelta(hours=-7))), |
| 969 | + "2021-01-01T07:00:00.000000Z", |
| 970 | + ), # UTC-7 time |
| 971 | + ( |
| 972 | + datetime(2021, 2, 3, 4, 56, 7, 890123, tzinfo=timezone.utc), |
| 973 | + "2021-02-03T04:56:07.890123Z", |
| 974 | + ), # UTC time all non-zero fields |
| 975 | + ), |
| 976 | +) |
| 977 | +def test_format_timestamp(datetime_object, expected_output): |
| 978 | + formatted = format_timestamp(datetime_object) |
| 979 | + |
| 980 | + assert formatted == expected_output |
| 981 | + |
| 982 | + |
| 983 | +def test_format_timestamp_naive(): |
| 984 | + datetime_object = datetime(2021, 1, 1) |
| 985 | + timestamp_regex = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6}Z" |
| 986 | + |
| 987 | + # Ensure that some timestamp is returned, without error. We currently treat these as local time, but this is an |
| 988 | + # implementation detail which we should not assert here. |
| 989 | + assert re.fullmatch(timestamp_regex, format_timestamp(datetime_object)) |
0 commit comments