From 9352da58811eb38c7b67fce30fb8c75256bd405e Mon Sep 17 00:00:00 2001 From: Peter Hooper Date: Tue, 21 Mar 2023 11:12:03 +0000 Subject: [PATCH] Update config endpoint (#61) * Update config endpoint * update test --- app/api/api_v1/routers/lookups/config.py | 10 ++++------ app/api/api_v1/schemas/metadata.py | 9 ++++++--- app/core/lookups.py | 12 ++++++++++++ app/core/organisation.py | 15 ++++++--------- tests/routes/test_config.py | 21 +++++++++++++++++---- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/app/api/api_v1/routers/lookups/config.py b/app/api/api_v1/routers/lookups/config.py index 14f36c2e..3155fda9 100644 --- a/app/api/api_v1/routers/lookups/config.py +++ b/app/api/api_v1/routers/lookups/config.py @@ -2,16 +2,14 @@ from fastapi import Depends, Request, Response -from app.api.api_v1.schemas.metadata import Config, TaxonomyConfig -from app.core.lookups import get_metadata +from app.api.api_v1.schemas.metadata import Config, ApplicationConfig +from app.core.lookups import get_config, get_metadata from app.db.session import get_db from app.db.crud.deprecated_document import get_document_ids from .router import lookups_router -from app.core.organisation import get_organisation_taxonomy_by_name - -@lookups_router.get("/config", response_model=Union[Config, TaxonomyConfig]) +@lookups_router.get("/config", response_model=Union[Config, ApplicationConfig]) def lookup_config( request: Request, db=Depends(get_db), @@ -21,7 +19,7 @@ def lookup_config( if not group_documents: return get_metadata(db) else: - return get_organisation_taxonomy_by_name(db=db, org_name="CCLW") + return get_config(db) @lookups_router.get( diff --git a/app/api/api_v1/schemas/metadata.py b/app/api/api_v1/schemas/metadata.py index 15b0a6c2..381b363e 100644 --- a/app/api/api_v1/schemas/metadata.py +++ b/app/api/api_v1/schemas/metadata.py @@ -124,8 +124,11 @@ class Config(BaseModel): metadata: SourceCollections -class TaxonomyConfig(BaseModel): +TaxonomyData = Mapping[str, Mapping[str, Union[str, Sequence[str]]]] + + +class ApplicationConfig(BaseModel): """Definition of the new Config which just includes taxonomy.""" - organisation: str - taxonomy: Mapping[str, Mapping[str, Union[str, Sequence[str]]]] + geographies: list[dict] + taxonomies: Mapping[str, TaxonomyData] diff --git a/app/core/lookups.py b/app/core/lookups.py index 1ddc90d5..671ac442 100644 --- a/app/core/lookups.py +++ b/app/core/lookups.py @@ -1,6 +1,8 @@ from typing import Optional, Sequence, cast from sqlalchemy.orm import Session +from app.api.api_v1.schemas.metadata import ApplicationConfig +from app.core.organisation import get_organisation_taxonomy_by_name from app.core.util import tree_table_to_json, table_to_json from app.db.models.law_policy import Geography @@ -38,6 +40,16 @@ def get_metadata(db: Session): return {"metadata": source_collections} +def get_config(db: Session) -> ApplicationConfig: + ORG_NAME = "CCLW" + return ApplicationConfig( + geographies=tree_table_to_json(table=Geography, db=db), + taxonomies={ + ORG_NAME: get_organisation_taxonomy_by_name(db=db, org_name=ORG_NAME) + }, + ) + + def get_countries_for_region(db: Session, region_slug: str) -> Sequence[Geography]: geography = db.query(Geography).filter(Geography.slug == region_slug).first() if geography is None: diff --git a/app/core/organisation.py b/app/core/organisation.py index a4f9e2b9..b30835ac 100644 --- a/app/core/organisation.py +++ b/app/core/organisation.py @@ -1,6 +1,6 @@ from dataclasses import asdict from sqlalchemy.orm import Session -from app.api.api_v1.schemas.metadata import TaxonomyConfig +from app.api.api_v1.schemas.metadata import TaxonomyData from app.db.models.app.users import Organisation from app.db.models.law_policy.family import FamilyEventType from app.db.models.law_policy.metadata import MetadataOrganisation, MetadataTaxonomy @@ -29,7 +29,7 @@ def get_organisation_taxonomy(db: Session, org_id: int) -> tuple[int, Taxonomy]: return taxonomy[0], {k: TaxonomyEntry(**v) for k, v in taxonomy[1].items()} -def get_organisation_taxonomy_by_name(db: Session, org_name: str) -> TaxonomyConfig: +def get_organisation_taxonomy_by_name(db: Session, org_name: str) -> TaxonomyData: """ Returns the TaxonomyConfig for the named organisation @@ -54,10 +54,7 @@ def get_organisation_taxonomy_by_name(db: Session, org_name: str) -> TaxonomyCon ) # The above line will throw if there is no taxonomy for the organisation - return TaxonomyConfig( - organisation=org_name, - taxonomy={ - **taxonomy[0], - "event_types": asdict(entry), - }, - ) + return { + **taxonomy[0], + "event_types": asdict(entry), + } diff --git a/tests/routes/test_config.py b/tests/routes/test_config.py index de9a78ba..56595462 100644 --- a/tests/routes/test_config.py +++ b/tests/routes/test_config.py @@ -5,7 +5,11 @@ import pytest from app.core.util import tree_table_to_json -from app.data_migrations import populate_event_type, populate_taxonomy +from app.data_migrations import ( + populate_event_type, + populate_geography, + populate_taxonomy, +) from app.db.session import SessionLocal from tests.routes.test_documents_deprecated import create_4_documents @@ -41,6 +45,7 @@ def test_endpoint_returns_taxonomy(client, test_db): """Tests whether we get the taxonomy when the /config endpoint is called.""" url_under_test = "/api/v1/config?group_documents=True" populate_taxonomy(test_db) + populate_geography(test_db) populate_event_type(test_db) test_db.flush() @@ -51,8 +56,16 @@ def test_endpoint_returns_taxonomy(client, test_db): response_json = response.json() assert response.status_code == OK - assert response_json["organisation"] == "CCLW" - assert set(response_json["taxonomy"]) == { + assert len(response_json) == 2 + + assert "geographies" in response_json + assert len(response_json["geographies"]) == 7 + + assert "taxonomies" in response_json + assert "CCLW" in response_json["taxonomies"] + tax = response_json["taxonomies"]["CCLW"] + + assert set(tax) == { "instrument", "keyword", "sector", @@ -62,7 +75,7 @@ def test_endpoint_returns_taxonomy(client, test_db): "hazard", "event_types", } - taxonomy_event_types = response_json["taxonomy"]["event_types"]["allowed_values"] + taxonomy_event_types = tax["event_types"]["allowed_values"] expected_event_types = [ "Amended", "Appealed",