Skip to content
Closed
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
5 changes: 0 additions & 5 deletions src/elections/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test_elections.py
Original file line number Diff line number Diff line change
@@ -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"[]"