Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add setting
disposition_at
field for files under retention (#710
- Loading branch information
1 parent
426b2c5
commit 91b1373
Showing
7 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from datetime import datetime | ||
from typing import Union | ||
|
||
from dateutil import parser | ||
|
||
|
||
def normalize_date_to_rfc3339_format(date: Union[datetime, str]) -> str: | ||
""" | ||
Normalizes any datetime string or object to rfc3339 format. | ||
:param date: datetime str or datetime object | ||
:return: date-time str in rfc3339 format | ||
""" | ||
if isinstance(date, str): | ||
date = parser.parse(date) | ||
|
||
if not isinstance(date, datetime): | ||
raise TypeError(f"Got unsupported type {date.__class__.__name__!r} for date.") | ||
|
||
timezone_aware_datetime = date if date.tzinfo is not None else date.astimezone() | ||
return timezone_aware_datetime.isoformat(timespec='seconds') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from unittest.mock import Mock | ||
|
||
import datetime | ||
import pytest | ||
|
||
from pytest_lazyfixture import lazy_fixture | ||
|
||
from boxsdk.util import datetime_formatter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"valid_datetime_format", | ||
( | ||
"2035-03-04T10:14:24+14:00", | ||
"2035-03-04T10:14:24-04:00", | ||
lazy_fixture("mock_datetime_rfc3339_str"), | ||
), | ||
) | ||
def test_leave_datetime_string_unchanged_when_rfc3339_formatted_str_provided( | ||
valid_datetime_format, | ||
): | ||
formatted_str = datetime_formatter.normalize_date_to_rfc3339_format( | ||
valid_datetime_format | ||
) | ||
assert formatted_str == valid_datetime_format | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"other_datetime_format", | ||
( | ||
"2035-03-04T10:14:24.000+14:00", | ||
"2035-03-04 10:14:24.000+14:00", | ||
"2035/03/04 10:14:24.000+14:00", | ||
"2035/03/04T10:14:24+14:00", | ||
"2035/3/4T10:14:24+14:00", | ||
lazy_fixture('mock_timezone_aware_datetime_obj'), | ||
), | ||
) | ||
def test_normalize_date_to_rfc3339_format_timezone_aware_datetime( | ||
other_datetime_format, | ||
mock_datetime_rfc3339_str, | ||
): | ||
formatted_str = datetime_formatter.normalize_date_to_rfc3339_format( | ||
other_datetime_format | ||
) | ||
assert formatted_str == mock_datetime_rfc3339_str | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"timezone_naive_datetime", | ||
( | ||
"2035-03-04T10:14:24.000", | ||
"2035-03-04T10:14:24", | ||
lazy_fixture('mock_timezone_naive_datetime_obj') | ||
), | ||
) | ||
def test_add_timezone_info_when_timezone_naive_datetime_provided( | ||
timezone_naive_datetime, | ||
mock_timezone_naive_datetime_obj, | ||
): | ||
formatted_str = datetime_formatter.normalize_date_to_rfc3339_format( | ||
timezone_naive_datetime | ||
) | ||
|
||
local_timezone = datetime.datetime.now().tzinfo | ||
expected_datetime = mock_timezone_naive_datetime_obj.astimezone( | ||
tz=local_timezone | ||
).isoformat(timespec="seconds") | ||
assert formatted_str == expected_datetime | ||
|
||
|
||
@pytest.mark.parametrize("inavlid_datetime_object", (None, Mock())) | ||
def test_throw_type_error_when_invalid_datetime_object_provided( | ||
inavlid_datetime_object, | ||
): | ||
with pytest.raises(TypeError): | ||
datetime_formatter.normalize_date_to_rfc3339_format(inavlid_datetime_object) |