diff --git a/src/elections/urls.py b/src/elections/urls.py index 68e34e9..a1ee8b2 100644 --- a/src/elections/urls.py +++ b/src/elections/urls.py @@ -76,7 +76,6 @@ def _raise_if_bad_election_data( "", description="Return a list of all elections, their statuses and nominees (if requested)", response_model=list[ElectionResponse], - responses={status.HTTP_404_NOT_FOUND: {"description": "No election found", "model": DetailModel}}, operation_id="get_all_elections", ) async def list_elections( @@ -91,15 +90,11 @@ async def list_elections( election_responses = await elections.crud.get_all_elections_with_nominees( db_session, current_time, has_permission ) - if not election_responses: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="no election found") election_metadata_list = [ election.model_dump(mode="json", exclude_none=True) for election in election_responses ] else: election_list = await elections.crud.get_all_elections(db_session) - if election_list is None or len(election_list) == 0: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="no election found") election_metadata_list = [] for election in election_list: if has_permission: diff --git a/tests/unit/test_elections.py b/tests/unit/test_elections.py new file mode 100644 index 0000000..e389035 --- /dev/null +++ b/tests/unit/test_elections.py @@ -0,0 +1,31 @@ +from unittest.mock import AsyncMock + +import pytest +from fastapi.responses import JSONResponse + +import elections.crud +from elections.urls import list_elections + +pytestmark = pytest.mark.unit + + +async def test__list_elections_returns_empty_list_when_none_exist(monkeypatch: pytest.MonkeyPatch): + db_session = AsyncMock() + monkeypatch.setattr(elections.crud, "get_all_elections", AsyncMock(return_value=[])) + + response = await list_elections(computing_id=None, db_session=db_session, with_nominees=False) + + assert isinstance(response, JSONResponse) + assert response.status_code == 200 + assert response.body == b"[]" + + +async def test__list_elections_with_nominees_returns_empty_list_when_none_exist(monkeypatch: pytest.MonkeyPatch): + db_session = AsyncMock() + monkeypatch.setattr(elections.crud, "get_all_elections_with_nominees", AsyncMock(return_value=[])) + + response = await list_elections(computing_id=None, db_session=db_session, with_nominees=True) + + assert isinstance(response, JSONResponse) + assert response.status_code == 200 + assert response.body == b"[]"