|
| 1 | +import re |
| 2 | +from typing import Annotated, Any, Iterator, Literal |
| 3 | + |
| 4 | +import pytest |
| 5 | +import respx |
| 6 | +from faker import Faker |
| 7 | +from httpx import AsyncClient |
| 8 | +from pydantic import BaseModel, Field, HttpUrl, ValidationError |
| 9 | +from servicelib.aiohttp import status |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_itis_vip_downloadables_api(faker: Faker) -> Iterator[respx.MockRouter]: |
| 14 | + api_url = "http://testserver" |
| 15 | + response_data = { |
| 16 | + "msg": 0, |
| 17 | + "availableDownloads": [ |
| 18 | + { |
| 19 | + "ID": faker.random_int(min=70, max=90), |
| 20 | + "Description": faker.sentence(), |
| 21 | + "Thumbnail": faker.image_url(), |
| 22 | + "Features": f"{{name: {faker.name()} Right Hand, version: V{faker.pyint()}.0, sex: {faker.gender()}, age: {faker.age()} years,date: {faker.date()}, ethnicity: Caucasian, functionality: Posable}}", |
| 23 | + "DOI": None, |
| 24 | + "LicenseKey": faker.bothify(text="MODEL_????_V#"), |
| 25 | + "LicenseVersion": "V1.0", |
| 26 | + "Protection": "Code", |
| 27 | + "AvailableFromURL": None, |
| 28 | + } |
| 29 | + for _ in range(8) |
| 30 | + ], |
| 31 | + } |
| 32 | + |
| 33 | + with respx.mock(base_url=api_url) as mock: |
| 34 | + mock.post("getDownloadableItems/ComputationalPantom").respond( |
| 35 | + status_code=200, json=response_data |
| 36 | + ) |
| 37 | + yield mock |
| 38 | + |
| 39 | + |
| 40 | +def descriptor_to_dict(descriptor: str) -> dict[str, Any]: |
| 41 | + pattern = r"(\w+): ([^,]+)" |
| 42 | + matches = re.findall(pattern, descriptor) |
| 43 | + return {key: value for key, value in matches} |
| 44 | + |
| 45 | + |
| 46 | +class AvailableDownload(BaseModel): |
| 47 | + id: Annotated[int, Field(alias="ID")] |
| 48 | + description: Annotated[str, Field(alias="Description")] |
| 49 | + thumbnail: Annotated[str, Field(alias="Thumbnail")] |
| 50 | + features: Annotated[dict[str, Any], Field(alias="Features")] |
| 51 | + doi: Annotated[str, Field(alias="DOI")] |
| 52 | + license_key: Annotated[str | None, Field(alias="LicenseKey")] |
| 53 | + license_version: Annotated[str | None, Field(alias="LicenseVersion")] |
| 54 | + protection: Annotated[Literal["Code", "PayPal"], Field(alias="Protection")] |
| 55 | + available_from_url: Annotated[HttpUrl | None, Field(alias="AvailableFromURL")] |
| 56 | + |
| 57 | + |
| 58 | +class ResponseData(BaseModel): |
| 59 | + msg: int = -1 |
| 60 | + available_downloads: Annotated[ |
| 61 | + list[AvailableDownload], Field(alias="availableDownloads") |
| 62 | + ] |
| 63 | + |
| 64 | + |
| 65 | +async def test_computational_pantom_api( |
| 66 | + mock_itis_vip_downloadables_api: respx.MockRouter, |
| 67 | +): |
| 68 | + async with AsyncClient(base_url="http://testserver") as client: |
| 69 | + response = await client.post("getDownloadableItems/ComputationalPantom") |
| 70 | + assert response.status_code == status.HTTP_200_OK |
| 71 | + response_json = response.json() |
| 72 | + |
| 73 | + try: |
| 74 | + validated_data = ResponseData(**response_json) |
| 75 | + except ValidationError as e: |
| 76 | + pytest.fail(f"Response validation failed: {e}") |
| 77 | + |
| 78 | + assert validated_data.msg == 0 |
| 79 | + assert len(validated_data.availableDownloads) == 8 |
| 80 | + |
| 81 | + assert ( |
| 82 | + validated_data.availableDownloads[0].Features["functionality"] == "Posable" |
| 83 | + ) |
| 84 | + |
| 85 | + print(validated_data.model_dump_json()) |
0 commit comments