diff --git a/app/api/api_v1/routers/summaries.py b/app/api/api_v1/routers/summaries.py index 098c6182..f950a36b 100644 --- a/app/api/api_v1/routers/summaries.py +++ b/app/api/api_v1/routers/summaries.py @@ -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}}, diff --git a/app/core/lookups.py b/app/core/lookups.py index 0e1ab05f..60fca46f 100644 --- a/app/core/lookups.py +++ b/app/core/lookups.py @@ -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) diff --git a/tests/routes/test_geography_summaries.py b/tests/routes/test_geography_summaries.py index 18d70ec0..373315ed 100644 --- a/tests/routes/test_geography_summaries.py +++ b/tests/routes/test_geography_summaries.py @@ -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