Skip to content

fix(elections): return 200 with empty list when no elections exist - #190

Closed
HarshRajSinghania wants to merge 2 commits into
CSSS:mainfrom
HarshRajSinghania:fix/empty-elections-list-200
Closed

HarshRajSinghania wants to merge 2 commits into
CSSS:mainfrom
HarshRajSinghania:fix/empty-elections-list-200

Conversation

@HarshRajSinghania

Copy link
Copy Markdown

Fixes #189.

Summary

GET /api/election and GET /api/election?with_nominees=true returned HTTP 404 when the elections table was empty. An empty collection is a valid result, so the endpoint now returns 200 with [].

A 404 is still used for GET /api/election/{name} when a specific election slug does not exist.

Motivation

Issue #189: returning 404 when there are no rows is semantically incorrect and makes it look like the route or table is missing.

Implementation

  • Removed the empty-list 404 checks in list_elections (src/elections/urls.py).
  • Dropped the list-endpoint 404 OpenAPI response, since that status is no longer produced.
  • Added unit tests that mock an empty result from the CRUD layer and assert status 200 and body [].

Testing

Unit tests added:

  • tests/unit/test_elections.py::test__list_elections_returns_empty_list_when_none_exist
  • tests/unit/test_elections.py::test__list_elections_with_nominees_returns_empty_list_when_none_exist

These tests call list_elections directly with mocked CRUD and do not require a database. Existing integration tests for non-empty lists were not changed.

I did not run the full integration suite here because it needs the project's Postgres test database.

@jbriones1 jbriones1 closed this Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return status of GET elections incorrect

2 participants