Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions bases/ecoindex/backend/routers/ecoindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,27 @@ async def get_ecoindex_analysis_by_id(
@router.get(
name="Get ecoindex analysis requests by id",
path="/{id}/requests",
response_model=RequestsDetailResponse | None,
response_model=RequestsDetailResponse,
response_description="Request details of the ecoindex analysis",
responses={status.HTTP_404_NOT_FOUND: example_ecoindex_not_found},
responses={
status.HTTP_204_NO_CONTENT: {
"description": (
"Analysis exists but request details were not collected"
)
},
status.HTTP_404_NOT_FOUND: example_ecoindex_not_found,
},
description=(
"This returns the detailed list of requests made by the page, "
"aggregated by category and by domain. Returns `null` when the "
"aggregated by category and by domain. Returns 204 when the "
"analysis exists but request details were not collected."
),
)
async def get_ecoindex_analysis_requests_by_id(
id: IdParameter,
version: VersionParameter = Version.v1,
session: AsyncSession = Depends(get_session),
) -> RequestsDetailResponse | None:
) -> RequestsDetailResponse | Response:
ecoindex = await get_ecoindex_result_by_id_db(
session=session, id=id, version=version
)
Expand All @@ -160,7 +167,7 @@ async def get_ecoindex_analysis_requests_by_id(
session=session, analysis_id=id
)
if not request_rows:
return None
return Response(status_code=status.HTTP_204_NO_CONTENT)

return aggregate_request_details(
[
Expand Down
15 changes: 5 additions & 10 deletions projects/ecoindex_api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@
},
"/{version}/ecoindexes/{id}/requests": {
"get": {
"description": "This returns the detailed list of requests made by the page, aggregated by category and by domain. Returns `null` when the analysis exists but request details were not collected.",
"description": "This returns the detailed list of requests made by the page, aggregated by category and by domain. Returns 204 when the analysis exists but request details were not collected.",
"operationId": "Get_ecoindex_analysis_requests_by_id__version__ecoindexes__id__requests_get",
"parameters": [
{
Expand Down Expand Up @@ -2069,20 +2069,15 @@
"content": {
"application/json": {
"schema": {
"anyOf": [
{
"$ref": "#/components/schemas/RequestsDetailResponse"
},
{
"type": "null"
}
],
"title": "Response Get Ecoindex Analysis Requests By Id Version Ecoindexes Id Requests Get"
"$ref": "#/components/schemas/RequestsDetailResponse"
}
}
},
"description": "Request details of the ecoindex analysis"
},
"204": {
"description": "Analysis exists but request details were not collected"
},
"404": {
"content": {
"application/json": {
Expand Down
90 changes: 90 additions & 0 deletions test/bases/ecoindex/backend/test_ecoindex_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from unittest.mock import AsyncMock
from uuid import uuid4

import pytest
from ecoindex.backend.routers.ecoindex import get_ecoindex_analysis_requests_by_id
from ecoindex.models.enums import Version
from ecoindex.models.scraper import RequestDetail, RequestsDetailResponse
from fastapi import HTTPException, Response, status


@pytest.mark.asyncio
async def test_get_requests_returns_204_when_analysis_exists_without_rows(
monkeypatch,
):
monkeypatch.setattr(
"ecoindex.backend.routers.ecoindex.get_ecoindex_result_by_id_db",
AsyncMock(return_value=object()),
)
monkeypatch.setattr(
"ecoindex.backend.routers.ecoindex.get_requests_by_analysis_id_db",
AsyncMock(return_value=[]),
)

result = await get_ecoindex_analysis_requests_by_id(
id=uuid4(),
version=Version.v1,
session=AsyncMock(),
)

assert isinstance(result, Response)
assert result.status_code == status.HTTP_204_NO_CONTENT
assert not result.body


@pytest.mark.asyncio
async def test_get_requests_raises_404_when_analysis_is_missing(monkeypatch):
analysis_id = uuid4()
monkeypatch.setattr(
"ecoindex.backend.routers.ecoindex.get_ecoindex_result_by_id_db",
AsyncMock(return_value=None),
)
get_requests = AsyncMock()
monkeypatch.setattr(
"ecoindex.backend.routers.ecoindex.get_requests_by_analysis_id_db",
get_requests,
)

with pytest.raises(HTTPException) as exc_info:
await get_ecoindex_analysis_requests_by_id(
id=analysis_id,
version=Version.v1,
session=AsyncMock(),
)

assert exc_info.value.status_code == status.HTTP_404_NOT_FOUND
assert str(analysis_id) in str(exc_info.value.detail)
get_requests.assert_not_called()


@pytest.mark.asyncio
async def test_get_requests_returns_payload_when_rows_exist(monkeypatch):
analysis_id = uuid4()
monkeypatch.setattr(
"ecoindex.backend.routers.ecoindex.get_ecoindex_result_by_id_db",
AsyncMock(return_value=object()),
)
monkeypatch.setattr(
"ecoindex.backend.routers.ecoindex.get_requests_by_analysis_id_db",
AsyncMock(
return_value=[
RequestDetail(
category="html",
domain="www.ecoindex.fr",
status=200,
url="https://www.ecoindex.fr/",
size=1000,
)
]
),
)

result = await get_ecoindex_analysis_requests_by_id(
id=analysis_id,
version=Version.v1,
session=AsyncMock(),
)

assert isinstance(result, RequestsDetailResponse)
assert len(result.items) == 1
assert result.items[0].domain == "www.ecoindex.fr"
Loading