From 3dd5a0aab40594e84fa79b3b833232997a87e58a Mon Sep 17 00:00:00 2001 From: Harsh Raj Singhania <40535627+HarshRajSinghania@users.noreply.github.com> Date: Sat, 19 Sep 2026 15:02:18 +0530 Subject: [PATCH 1/2] test(elections): assert GET /election returns 200 with empty list --- tests/unit/test_elections.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/unit/test_elections.py 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"[]" From 9a96a6d55be9f9225cd76ecec329d0e8fe45446f Mon Sep 17 00:00:00 2001 From: Harsh Raj Singhania Date: Sat, 19 Sep 2026 15:02:36 +0530 Subject: [PATCH 2/2] fix(elections): return 200 and empty list when no elections exist --- src/elections/urls.py | 5 ----- 1 file changed, 5 deletions(-) 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: