Skip to content

Commit

Permalink
Feature/pdct 455 Updated geography summary route to take geography co…
Browse files Browse the repository at this point in the history
…de or slug (#175)
  • Loading branch information
katybaulch authored Oct 12, 2023
1 parent 99d21ea commit 1870874
Showing 3 changed files with 57 additions and 5 deletions.
13 changes: 11 additions & 2 deletions app/api/api_v1/routers/summaries.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

from app.api.api_v1.schemas.search import GeographySummaryFamilyResponse
from app.core.browse import BrowseArgs, browse_rds_families
from app.core.lookups import get_country_slug_from_country_code, is_country_code
from app.db.models.law_policy import FamilyCategory
from app.db.session import get_db

@@ -17,16 +18,24 @@


@summary_router.get(
"/summaries/geography/{geography_slug}",
"/summaries/geography/{geography_string}",
summary="Gets a summary of the documents associated with a geography.",
response_model=GeographySummaryFamilyResponse,
)
def search_by_geography(
request: Request,
geography_slug: str,
geography_string: str,
db=Depends(get_db),
):
"""Searches the documents filtering by geography and grouping by category."""

geography_slug = None
if is_country_code(db, geography_string):
geography_slug = get_country_slug_from_country_code(db, geography_string)

if geography_slug is None:
geography_slug = geography_string

_LOGGER.info(
f"Getting geography summary for {geography_slug}",
extra={"props": {"geography_slug": geography_slug}},
23 changes: 23 additions & 0 deletions app/core/lookups.py
Original file line number Diff line number Diff line change
@@ -68,3 +68,26 @@ def get_country_by_slug(db: Session, country_slug: str) -> Optional[Geography]:
return None

return geography


def get_country_slug_from_country_code(
db: Session, country_code: str
) -> Optional[Geography]:
geography_slug = (
db.query(Geography.slug).filter_by(value=country_code).one_or_none()
)
if geography_slug is None:
return None

return geography_slug


def is_country_code(db: Session, country_code: str) -> bool:
EXPECTED_GEO_CODE_LENGTH = 3
if len(country_code) != EXPECTED_GEO_CODE_LENGTH:
return False

country_code = (
db.query(Geography).filter(Geography.value == country_code).one_or_none()
)
return bool(country_code is not None)
26 changes: 23 additions & 3 deletions tests/routes/test_geography_summaries.py
Original file line number Diff line number Diff line change
@@ -3,11 +3,11 @@
import pytest # noqa: F401


def _url_under_test(slug: str) -> str:
return f"/api/v1/summaries/geography/{slug}"
def _url_under_test(geography: str) -> str:
return f"/api/v1/summaries/geography/{geography}"


def test_endpoint_returns_families_ok(client):
def test_endpoint_returns_families_ok_with_slug(client):
"""Test the endpoint returns an empty sets of data"""
response = client.get(_url_under_test("moldova"))
assert response.status_code == OK
@@ -27,6 +27,26 @@ def test_endpoint_returns_families_ok(client):
assert len(resp["targets"]) == 0


def test_endpoint_returns_families_ok_with_code(client):
"""Test the endpoint returns an empty sets of data"""
response = client.get(_url_under_test("MDA"))
assert response.status_code == OK
resp = response.json()

assert resp["family_counts"]["Executive"] == 0
assert resp["family_counts"]["Legislative"] == 0
assert resp["family_counts"]["UNFCCC"] == 0

assert len(resp["top_families"]["Executive"]) == 0
assert len(resp["top_families"]["Legislative"]) == 0
assert len(resp["top_families"]["UNFCCC"]) == 0

assert len(resp["family_counts"]) == 3
assert len(resp["top_families"]) == 3

assert len(resp["targets"]) == 0


def test_geography_with_families_ordered(client, summary_geography_family_data):
"""Test that all the data is returned ordered by published date"""
geography_slug = summary_geography_family_data["geos"][0].slug

0 comments on commit 1870874

Please sign in to comment.