79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
import re
|
|
from dataclasses import dataclass
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from ploop.api.app import create_app
|
|
from ploop.domain.catalog.models import PublicDatasetProjection
|
|
from tests.api.conftest import DATASET_ONE, FakeReadPort
|
|
|
|
|
|
def test_dataset_list_uses_safe_envelope_and_opaque_pagination(client: TestClient) -> None:
|
|
response = client.get("/api/v1/datasets?limit=1")
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
assert payload["data"] == [DATASET_ONE]
|
|
assert payload["meta"]["total"] == 2
|
|
cursor = payload["meta"]["next_cursor"]
|
|
assert re.fullmatch(r"[A-Za-z0-9_-]+", cursor)
|
|
assert cursor not in {"dataset-1", "dataset-2"}
|
|
second = client.get("/api/v1/datasets", params={"limit": 1, "cursor": cursor})
|
|
assert second.status_code == 200
|
|
assert second.json() == {
|
|
"data": [{**DATASET_ONE, "id": "dataset-2"}],
|
|
"meta": {"next_cursor": None, "total": 2},
|
|
}
|
|
|
|
|
|
def test_dataset_detail_has_only_public_artifact_metadata(client: TestClient) -> None:
|
|
response = client.get("/api/v1/datasets/dataset-1")
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"data": {
|
|
**DATASET_ONE,
|
|
"public_artifacts": [
|
|
{"id": "artifact-1", "role": "public_request", "digest": "4" * 64, "size_bytes": 42}
|
|
],
|
|
},
|
|
"meta": {"next_cursor": None, "total": 1},
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("resource", ["datasets", "suts", "runs"])
|
|
def test_list_defaults_to_25_items_and_supports_the_next_page(resource: str) -> None:
|
|
client = TestClient(create_app(FakeReadPort(count=26)))
|
|
first = client.get(f"/api/v1/{resource}")
|
|
assert first.status_code == 200
|
|
assert len(first.json()["data"]) == 25
|
|
assert first.json()["meta"]["total"] == 26
|
|
second = client.get(
|
|
f"/api/v1/{resource}", params={"cursor": first.json()["meta"]["next_cursor"]}
|
|
)
|
|
assert second.status_code == 200
|
|
assert len(second.json()["data"]) == 1
|
|
assert second.json()["data"][0]["id"] not in {row["id"] for row in first.json()["data"]}
|
|
assert second.json()["meta"] == {"next_cursor": None, "total": 26}
|
|
|
|
|
|
@pytest.mark.parametrize("resource", ["datasets", "suts", "runs"])
|
|
def test_empty_list_is_a_success(resource: str) -> None:
|
|
response = TestClient(create_app(FakeReadPort(count=0))).get(f"/api/v1/{resource}")
|
|
assert response.status_code == 200
|
|
assert response.json() == {"data": [], "meta": {"next_cursor": None, "total": 0}}
|
|
|
|
|
|
def test_projection_extension_is_not_automatically_serialized() -> None:
|
|
@dataclass(frozen=True)
|
|
class ExtendedDataset(PublicDatasetProjection):
|
|
internal_note: str = "must-stay-internal"
|
|
|
|
port = FakeReadPort(count=1)
|
|
port.datasets = (ExtendedDataset("dataset-1", "demo", "en", 2, "1" * 64, "2" * 64, "3" * 64),)
|
|
client = TestClient(create_app(port))
|
|
for url in ("/api/v1/datasets", "/api/v1/datasets/dataset-1"):
|
|
response = client.get(url)
|
|
assert response.status_code == 200
|
|
assert "internal_note" not in response.text
|
|
assert "must-stay-internal" not in response.text
|