Skip to content

Commit

Permalink
Update config endpoint (#61)
Browse files Browse the repository at this point in the history
* Update config endpoint

* update test
  • Loading branch information
diversemix authored Mar 21, 2023
1 parent 47b152d commit 9352da5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
10 changes: 4 additions & 6 deletions app/api/api_v1/routers/lookups/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions app/api/api_v1/schemas/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
12 changes: 12 additions & 0 deletions app/core/lookups.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 6 additions & 9 deletions app/core/organisation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
}
21 changes: 17 additions & 4 deletions tests/routes/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 9352da5

Please sign in to comment.