Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/tests/test_serializers.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
import json | import json | ||||
from typing import Any, Callable, List, Tuple, Union | from typing import Any, Callable, List, Tuple, Union | ||||
from uuid import UUID | from uuid import UUID | ||||
import arrow | |||||
from arrow import Arrow | |||||
import pytest | import pytest | ||||
import requests | import requests | ||||
from requests.exceptions import ConnectionError | from requests.exceptions import ConnectionError | ||||
from swh.core.api.classes import PagedResult | from swh.core.api.classes import PagedResult | ||||
from swh.core.api.serializers import ( | from swh.core.api.serializers import ( | ||||
ENCODERS, | ENCODERS, | ||||
SWHJSONDecoder, | SWHJSONDecoder, | ||||
Show All 40 Lines | |||||
} | } | ||||
DATA_TIMEDELTA = datetime.timedelta(64) | DATA_TIMEDELTA = datetime.timedelta(64) | ||||
ENCODED_DATA_TIMEDELTA = { | ENCODED_DATA_TIMEDELTA = { | ||||
"swhtype": "timedelta", | "swhtype": "timedelta", | ||||
"d": {"days": 64, "seconds": 0, "microseconds": 0}, | "d": {"days": 64, "seconds": 0, "microseconds": 0}, | ||||
} | } | ||||
DATA_ARROW = arrow.get("2018-04-25T16:17:53.533672+00:00") | |||||
ENCODED_DATA_ARROW = {"swhtype": "arrow", "d": "2018-04-25T16:17:53.533672+00:00"} | |||||
DATA_UUID = UUID("cdd8f804-9db6-40c3-93ab-5955d3836234") | DATA_UUID = UUID("cdd8f804-9db6-40c3-93ab-5955d3836234") | ||||
ENCODED_DATA_UUID = {"swhtype": "uuid", "d": "cdd8f804-9db6-40c3-93ab-5955d3836234"} | ENCODED_DATA_UUID = {"swhtype": "uuid", "d": "cdd8f804-9db6-40c3-93ab-5955d3836234"} | ||||
# For test demonstration purposes | # For test demonstration purposes | ||||
TestPagedResultStr = PagedResult[ | TestPagedResultStr = PagedResult[ | ||||
Union[UUID, datetime.datetime, datetime.timedelta], str | Union[UUID, datetime.datetime, datetime.timedelta], str | ||||
] | ] | ||||
DATA_PAGED_RESULT = TestPagedResultStr( | DATA_PAGED_RESULT = TestPagedResultStr( | ||||
results=[DATA_UUID, DATA_DATETIME, DATA_TIMEDELTA], next_page_token="10", | results=[DATA_UUID, DATA_DATETIME, DATA_TIMEDELTA], next_page_token="10", | ||||
) | ) | ||||
ENCODED_DATA_PAGED_RESULT = { | ENCODED_DATA_PAGED_RESULT = { | ||||
"d": { | "d": { | ||||
"results": [ENCODED_DATA_UUID, ENCODED_DATA_DATETIME, ENCODED_DATA_TIMEDELTA,], | "results": [ENCODED_DATA_UUID, ENCODED_DATA_DATETIME, ENCODED_DATA_TIMEDELTA,], | ||||
"next_page_token": "10", | "next_page_token": "10", | ||||
}, | }, | ||||
"swhtype": "paged_result", | "swhtype": "paged_result", | ||||
} | } | ||||
TestPagedResultTuple = PagedResult[Union[str, bytes, Arrow], List[Union[str, UUID]]] | TestPagedResultTuple = PagedResult[ | ||||
Union[str, bytes, datetime.datetime], List[Union[str, UUID]] | |||||
] | |||||
DATA_PAGED_RESULT2 = TestPagedResultTuple( | DATA_PAGED_RESULT2 = TestPagedResultTuple( | ||||
results=["data0", DATA_BYTES, DATA_ARROW], next_page_token=["10", DATA_UUID], | results=["data0", DATA_BYTES, DATA_DATETIME], next_page_token=["10", DATA_UUID], | ||||
) | ) | ||||
ENCODED_DATA_PAGED_RESULT2 = { | ENCODED_DATA_PAGED_RESULT2 = { | ||||
"d": { | "d": { | ||||
"results": ["data0", ENCODED_DATA_BYTES, ENCODED_DATA_ARROW,], | "results": ["data0", ENCODED_DATA_BYTES, ENCODED_DATA_DATETIME,], | ||||
"next_page_token": ["10", ENCODED_DATA_UUID], | "next_page_token": ["10", ENCODED_DATA_UUID], | ||||
}, | }, | ||||
"swhtype": "paged_result", | "swhtype": "paged_result", | ||||
} | } | ||||
DATA = { | DATA = { | ||||
"bytes": DATA_BYTES, | "bytes": DATA_BYTES, | ||||
"datetime_tz": DATA_DATETIME, | "datetime_tz": DATA_DATETIME, | ||||
"datetime_utc": datetime.datetime( | "datetime_utc": datetime.datetime( | ||||
2015, 3, 4, 18, 25, 13, 1234, tzinfo=datetime.timezone.utc | 2015, 3, 4, 18, 25, 13, 1234, tzinfo=datetime.timezone.utc | ||||
), | ), | ||||
"datetime_delta": DATA_TIMEDELTA, | "datetime_delta": DATA_TIMEDELTA, | ||||
"arrow_date": DATA_ARROW, | |||||
"swhtype": "fake", | "swhtype": "fake", | ||||
"swh_dict": {"swhtype": 42, "d": "test"}, | "swh_dict": {"swhtype": 42, "d": "test"}, | ||||
"random_dict": {"swhtype": 43}, | "random_dict": {"swhtype": 43}, | ||||
"uuid": DATA_UUID, | "uuid": DATA_UUID, | ||||
"paged-result": DATA_PAGED_RESULT, | "paged-result": DATA_PAGED_RESULT, | ||||
"paged-result2": DATA_PAGED_RESULT2, | "paged-result2": DATA_PAGED_RESULT2, | ||||
} | } | ||||
ENCODED_DATA = { | ENCODED_DATA = { | ||||
"bytes": ENCODED_DATA_BYTES, | "bytes": ENCODED_DATA_BYTES, | ||||
"datetime_tz": ENCODED_DATA_DATETIME, | "datetime_tz": ENCODED_DATA_DATETIME, | ||||
"datetime_utc": {"swhtype": "datetime", "d": "2015-03-04T18:25:13.001234+00:00",}, | "datetime_utc": {"swhtype": "datetime", "d": "2015-03-04T18:25:13.001234+00:00",}, | ||||
"datetime_delta": ENCODED_DATA_TIMEDELTA, | "datetime_delta": ENCODED_DATA_TIMEDELTA, | ||||
"arrow_date": ENCODED_DATA_ARROW, | |||||
"swhtype": "fake", | "swhtype": "fake", | ||||
"swh_dict": {"swhtype": 42, "d": "test"}, | "swh_dict": {"swhtype": 42, "d": "test"}, | ||||
"random_dict": {"swhtype": 43}, | "random_dict": {"swhtype": 43}, | ||||
"uuid": ENCODED_DATA_UUID, | "uuid": ENCODED_DATA_UUID, | ||||
"paged-result": ENCODED_DATA_PAGED_RESULT, | "paged-result": ENCODED_DATA_PAGED_RESULT, | ||||
"paged-result2": ENCODED_DATA_PAGED_RESULT2, | "paged-result2": ENCODED_DATA_PAGED_RESULT2, | ||||
} | } | ||||
▲ Show 20 Lines • Show All 111 Lines • Show Last 20 Lines |