Skip to content

Commit

Permalink
Return only votes in API responses that have a display title
Browse files Browse the repository at this point in the history
  • Loading branch information
tillprochaska committed Nov 16, 2024
1 parent eda4337 commit c64ce06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion backend/howtheyvote/api/votes_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TypeVar

from flask import Blueprint, Response, abort, jsonify, request
from sqlalchemy import select
from sqlalchemy import or_, select
from structlog import get_logger

from ..db import Session
Expand Down Expand Up @@ -102,6 +102,7 @@ def index() -> Response:
query = query.page_size(request.args.get("page_size", type=int))
query = query.sort("timestamp", Order.DESC)
query = query.filter("is_main", True)
query = query.where(or_(Vote.title != None, Vote.procedure_title != None)) # noqa: E711

response = query.handle()
results: list[BaseVoteDict] = [
Expand Down
30 changes: 30 additions & 0 deletions backend/tests/api/test_votes_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ def test_votes_api_index(db_session, api):
assert res.json["results"][0]["display_title"] == "Vote One"


def test_votes_api_index_empty_title(db_session, api):
empty_title = Vote(
id=1,
timestamp=datetime.datetime(2024, 1, 1, 0, 0, 0),
is_main=True,
)

non_empty_vote_title = Vote(
id=2,
timestamp=datetime.datetime(2024, 1, 2, 0, 0, 0),
title="Vote title",
is_main=True,
)

non_empty_procedure_title = Vote(
id=3,
timestamp=datetime.datetime(2024, 1, 3, 0, 0, 0),
title="Procedure title",
is_main=True,
)

db_session.add_all([empty_title, non_empty_vote_title, non_empty_procedure_title])
db_session.commit()

res = api.get("/api/votes")
assert len(res.json["results"]) == 2
assert res.json["results"][0]["display_title"] == "Procedure title"
assert res.json["results"][1]["display_title"] == "Vote title"


def test_votes_api_search(db_session, search_index, api):
one = Vote(
id=1,
Expand Down

0 comments on commit c64ce06

Please sign in to comment.