From 7ec9b9272ff78ed03618be7e8f43ade59e6c1bef Mon Sep 17 00:00:00 2001 From: Stanislav Bondarenko Date: Thu, 11 Feb 2021 17:30:30 +0200 Subject: [PATCH] fix datetime tests --- src/codemagic/apple/resources/resource.py | 11 ++++++--- src/codemagic/utilities/argument_utils.py | 2 +- .../app_store_connect/builds/conftest.py | 23 ------------------- tests/apple/resources/conftest.py | 4 ++++ tests/apple/resources/test_build_resource.py | 8 +++++++ tests/apple/resources/test_resource.py | 3 ++- 6 files changed, 23 insertions(+), 28 deletions(-) delete mode 100644 tests/apple/app_store_connect/builds/conftest.py create mode 100644 tests/apple/resources/test_build_resource.py diff --git a/src/codemagic/apple/resources/resource.py b/src/codemagic/apple/resources/resource.py index 054c9a59..ea451ff3 100644 --- a/src/codemagic/apple/resources/resource.py +++ b/src/codemagic/apple/resources/resource.py @@ -4,6 +4,7 @@ import re from dataclasses import dataclass from datetime import datetime +from datetime import timezone from typing import Any from typing import Dict from typing import List @@ -73,8 +74,10 @@ class Links(DictSerializable): _OMIT_IF_NONE_KEYS = ('related',) def __init__(_self, self: Optional[str] = None, related: Optional[str] = None): - _self.self = self - _self.related = related + if self: + _self.self = self + if related: + _self.related = related class ResourceLinks(DictSerializable): @@ -221,7 +224,9 @@ def to_iso_8601(cls, dt: datetime) -> str: def to_iso_8601(cls, dt: Optional[datetime]): if dt is None: return None - return dt.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + '+0000' + if dt.tzinfo == timezone.utc: + return dt.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + '+0000' + return dt.isoformat() def _format_attribute_name(self, name: str) -> str: type_prefix = self.type.value.rstrip('s') diff --git a/src/codemagic/utilities/argument_utils.py b/src/codemagic/utilities/argument_utils.py index 3c15b8f4..6ea04879 100644 --- a/src/codemagic/utilities/argument_utils.py +++ b/src/codemagic/utilities/argument_utils.py @@ -5,4 +5,4 @@ def get_binary_arguments_value(value, not_value): if not value and not not_value: return None - return value or not not_value \ No newline at end of file + return value or not not_value diff --git a/tests/apple/app_store_connect/builds/conftest.py b/tests/apple/app_store_connect/builds/conftest.py deleted file mode 100644 index bb4c1ea0..00000000 --- a/tests/apple/app_store_connect/builds/conftest.py +++ /dev/null @@ -1,23 +0,0 @@ -import json -import pathlib -from typing import Dict - -import pytest - - -_RESOURCE_MOCKS_DIR = pathlib.Path(__file__).parent.parent.parent / 'resources' / 'mocks' - - -class MockResponse: - def __init__(self, mock_data): - self.data = {'data': mock_data} - - def json(self): - return self.data - - -@pytest.fixture -def build_response() -> MockResponse: - mock_path = _RESOURCE_MOCKS_DIR / 'build.json' - mock_data = json.loads(mock_path.read_text()) - return MockResponse(mock_data) diff --git a/tests/apple/resources/conftest.py b/tests/apple/resources/conftest.py index 42bebbc6..73ebaef9 100644 --- a/tests/apple/resources/conftest.py +++ b/tests/apple/resources/conftest.py @@ -4,6 +4,10 @@ import pytest +@pytest.fixture +def api_build() -> Dict: + mock_path = pathlib.Path(__file__).parent / 'mocks' / 'build.json' + return json.loads(mock_path.read_text()) @pytest.fixture def api_bundle_id() -> Dict: diff --git a/tests/apple/resources/test_build_resource.py b/tests/apple/resources/test_build_resource.py new file mode 100644 index 00000000..b1cc5a19 --- /dev/null +++ b/tests/apple/resources/test_build_resource.py @@ -0,0 +1,8 @@ +from __future__ import annotations + +from codemagic.apple.resources import Build + + +def test_build_initialization(api_build): + build = Build(api_build) + assert build.dict() == api_build diff --git a/tests/apple/resources/test_resource.py b/tests/apple/resources/test_resource.py index acfc35f2..82f1a2bf 100644 --- a/tests/apple/resources/test_resource.py +++ b/tests/apple/resources/test_resource.py @@ -1,6 +1,7 @@ from __future__ import annotations from datetime import datetime +from datetime import timedelta from datetime import timezone import pytest @@ -22,7 +23,7 @@ def test_to_iso_8601(iso_8601_timestamp, expected_datetime): @pytest.mark.parametrize('given_datetime, expected_iso_8601_timestamp', [ (None, None), ('2020-08-04T11:44:12.000+0000', datetime(2020, 8, 4, 11, 44, 12, tzinfo=timezone.utc)), - ('2020-08-04T11:44:12+0000', datetime(2020, 8, 4, 11, 44, 12, tzinfo=timezone.utc)), + ('2021-01-28T06:01:32-08:00', datetime(2021, 1, 28, 6, 1, 32, tzinfo=timezone(timedelta(days=-1, seconds=57600)))), ('1970-01-01T00:00:00.000+0000', datetime(1970, 1, 1, 0, 0, 0, tzinfo=timezone.utc)), ]) def test_from_iso_8601(given_datetime, expected_iso_8601_timestamp):