From 8fbfe9efd90fb5fb86458811dca0b37d0f543349 Mon Sep 17 00:00:00 2001 From: Peter Hooper Date: Thu, 19 Oct 2023 11:22:34 +0100 Subject: [PATCH] Extend config endpoint with document and event settings (#26) * Extend config endpoint with document and event settings * update unit tests --- app/model/config.py | 16 ++++++++ app/repository/config.py | 45 +++++++++++++++++++-- integration_tests/test_config.py | 15 +++++++ unit_tests/mocks/services/config_service.py | 10 ++++- unit_tests/routers/test_config.py | 2 + 5 files changed, 83 insertions(+), 5 deletions(-) diff --git a/app/model/config.py b/app/model/config.py index 07227e40..68543043 100644 --- a/app/model/config.py +++ b/app/model/config.py @@ -6,9 +6,25 @@ TaxonomyData = Mapping[str, Mapping[str, Union[bool, str, Sequence[str]]]] +class EventConfig(BaseModel): + """Everything you need to know about events.""" + + types: Sequence[str] + + +class DocumentConfig(BaseModel): + """Everything you need to know about documents.""" + + roles: Sequence[str] + types: Sequence[str] + variants: Sequence[str] + + class ConfigReadDTO(BaseModel): """Definition of the new Config which just includes taxonomy.""" geographies: Sequence[dict] taxonomies: Mapping[str, TaxonomyData] languages: Mapping[str, str] + document: DocumentConfig + event: EventConfig diff --git a/app/repository/config.py b/app/repository/config.py index f7f13bf3..b5be4902 100644 --- a/app/repository/config.py +++ b/app/repository/config.py @@ -3,13 +3,19 @@ from sqlalchemy.orm import Session from app.clients.db.models.app.users import Organisation from app.clients.db.models.document.physical_document import Language +from app.clients.db.models.law_policy.family import ( + FamilyDocumentRole, + FamilyDocumentType, + FamilyEventType, + Variant, +) from app.clients.db.models.law_policy.geography import Geography from app.clients.db.models.law_policy.metadata import ( MetadataOrganisation, MetadataTaxonomy, ) from app.clients.db.session import AnyModel -from app.model.config import ConfigReadDTO, TaxonomyData +from app.model.config import ConfigReadDTO, DocumentConfig, EventConfig, TaxonomyData _LOGGER = logging.getLogger(__name__) @@ -72,7 +78,6 @@ def get(db: Session) -> ConfigReadDTO: :return ConfigReadDTO: The config data """ - # TODO: Return the event types too geographies = _tree_table_to_json(table=Geography, db=db) taxonomies = {} @@ -83,6 +88,40 @@ def get(db: Session) -> ConfigReadDTO: taxonomies[org.name] = tax languages = {lang.language_code: lang.name for lang in db.query(Language).all()} + + # Now Document config + doc_config = DocumentConfig( + roles=[ + doc_role.name + for doc_role in db.query(FamilyDocumentRole) + .order_by(FamilyDocumentRole.name) + .all() + ], + types=[ + doc_type.name + for doc_type in db.query(FamilyDocumentType) + .order_by(FamilyDocumentType.name) + .all() + ], + variants=[ + variant.variant_name + for variant in db.query(Variant).order_by(Variant.variant_name).all() + ], + ) + + # Now Event config + event_config = EventConfig( + types=[ + event_type.name + for event_type in db.query(FamilyEventType) + .order_by(FamilyEventType.name) + .all() + ] + ) return ConfigReadDTO( - geographies=geographies, taxonomies=taxonomies, languages=languages + geographies=geographies, + taxonomies=taxonomies, + languages=languages, + document=doc_config, + event=event_config, ) diff --git a/integration_tests/test_config.py b/integration_tests/test_config.py index 085d5b2d..8c97a3b3 100644 --- a/integration_tests/test_config.py +++ b/integration_tests/test_config.py @@ -17,3 +17,18 @@ def test_get_config(client: TestClient, test_db: Session, user_header_token): assert "geographies" in keys assert "taxonomies" in keys assert "languages" in keys + assert "document" in keys + assert "event" in keys + + # Now sanity check the data + assert data["geographies"][1]["node"]["slug"] == "europe-central-asia" + + assert "CCLW" in data["taxonomies"].keys() + + assert "aaa" in data["languages"].keys() + + assert "AMENDMENT" in data["document"]["roles"] + assert "Action Plan" in data["document"]["types"] + assert "Translation" in data["document"]["variants"] + + assert "Appealed" in data["event"]["types"] diff --git a/unit_tests/mocks/services/config_service.py b/unit_tests/mocks/services/config_service.py index bba2dcf2..9b3a3589 100644 --- a/unit_tests/mocks/services/config_service.py +++ b/unit_tests/mocks/services/config_service.py @@ -1,7 +1,7 @@ from pytest import MonkeyPatch from app.errors import RepositoryError -from app.model.config import ConfigReadDTO +from app.model.config import ConfigReadDTO, DocumentConfig, EventConfig def mock_config_service(config_service, monkeypatch: MonkeyPatch, mocker): @@ -13,7 +13,13 @@ def maybe_throw(): def mock_get_config() -> ConfigReadDTO: maybe_throw() - return ConfigReadDTO(geographies=[], taxonomies={}, languages={}) + return ConfigReadDTO( + geographies=[], + taxonomies={}, + languages={}, + document=DocumentConfig(roles=[], types=[], variants=[]), + event=EventConfig(types=[]), + ) monkeypatch.setattr(config_service, "get", mock_get_config) mocker.spy(config_service, "get") diff --git a/unit_tests/routers/test_config.py b/unit_tests/routers/test_config.py index a5209e00..6fe596ef 100644 --- a/unit_tests/routers/test_config.py +++ b/unit_tests/routers/test_config.py @@ -10,6 +10,8 @@ def test_get_when_ok(client: TestClient, user_header_token, config_service_mock) assert "geographies" in keys assert "taxonomies" in keys assert "languages" in keys + assert "event" in keys + assert "document" in keys assert config_service_mock.get.call_count == 1