Skip to content

Commit

Permalink
Extend config endpoint with document and event settings (#26)
Browse files Browse the repository at this point in the history
* Extend config endpoint with document and event settings

* update unit tests
  • Loading branch information
diversemix authored Oct 19, 2023
1 parent daa458c commit 8fbfe9e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/model/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 42 additions & 3 deletions app/repository/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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 = {}

Expand All @@ -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,
)
15 changes: 15 additions & 0 deletions integration_tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
10 changes: 8 additions & 2 deletions unit_tests/mocks/services/config_service.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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")
2 changes: 2 additions & 0 deletions unit_tests/routers/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 8fbfe9e

Please sign in to comment.