Skip to content

Commit

Permalink
Merge branch 'main' into PDCT-313-endpoint-for-deleting-events
Browse files Browse the repository at this point in the history
  • Loading branch information
katybaulch committed Oct 23, 2023
2 parents 6dd2f52 + b1ef7cf commit 83c7295
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 20 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
69 changes: 58 additions & 11 deletions app/repository/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import logging
from typing import Any
from typing import Any, Optional
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 @@ -41,23 +47,27 @@ def _tree_table_to_json(
return json_out


def _get_organisation_taxonomy_by_name(db: Session, org_name: str) -> TaxonomyData:
def _get_organisation_taxonomy_by_name(
db: Session, org_name: str
) -> Optional[TaxonomyData]:
"""
Returns the TaxonomyConfig for the named organisation
:param Session db: connection to the database
:return TaxonomyConfig: the TaxonomyConfig from the db
"""
return (
metadata = (
db.query(MetadataTaxonomy.valid_metadata)
.join(
MetadataOrganisation,
MetadataOrganisation.taxonomy_id == MetadataTaxonomy.id,
)
.join(Organisation, Organisation.id == MetadataOrganisation.organisation_id)
.filter_by(name=org_name)
.one()[0]
.one_or_none()
)
if metadata is not None:
return metadata[0]


def get(db: Session) -> ConfigReadDTO:
Expand All @@ -68,13 +78,50 @@ 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 = {
org.name: _get_organisation_taxonomy_by_name(db=db, org_name=org.name)
for org in db.query(Organisation).all()
}
taxonomies = {}

# Be resilient to an organisation not having a taxonomy
for org in db.query(Organisation).all():
tax = _get_organisation_taxonomy_by_name(db=db, org_name=org.name)
if tax is not None:
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,
)
12 changes: 7 additions & 5 deletions app/repository/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,14 @@ def get_organisation(db: Session, family_import_id: str) -> Optional[Organisatio
:param str family_import_id: The family import_id in question
:return Optional[Organisation]: Any associated organisation
"""
family_org = db.query(FamilyOrganisation).filter(
FamilyOrganisation.family_import_id == family_import_id
)

# TODO - can this be improved - we get warnings on integration tests ?
return db.query(Organisation).select_from(family_org).one_or_none()
return (
db.query(Organisation)
.join(FamilyOrganisation, FamilyOrganisation.organisation_id == Organisation.id)
.filter(FamilyOrganisation.family_import_id == family_import_id)
.group_by(Organisation.id)
.one()
)


def count(db: Session) -> Optional[int]:
Expand Down
7 changes: 7 additions & 0 deletions integration_tests/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ def _setup_organisation(test_db: Session) -> int:
organisation_type="test organisation",
)
test_db.add(org)
test_db.add(
Organisation(
name="Another org",
description="because we will have more than one org",
organisation_type="test",
)
)
test_db.flush()
return cast(int, org.id)

Expand Down
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/repos/config_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from sqlalchemy import exc

from app.model.config import ConfigReadDTO
from app.model.config import ConfigReadDTO, DocumentConfig, EventConfig


def mock_config_repo(config_repo, monkeypatch: MonkeyPatch, mocker):
Expand All @@ -16,7 +16,13 @@ def maybe_throw():

def mock_get(_) -> Optional[ConfigReadDTO]:
maybe_throw()
return ConfigReadDTO(geographies=[], taxonomies={}, languages={})
return ConfigReadDTO(
geographies=[],
taxonomies={},
languages={},
document=DocumentConfig(roles=[], types=[], variants=[]),
event=EventConfig(types=[]),
)

monkeypatch.setattr(config_repo, "get", mock_get)
mocker.spy(config_repo, "get")
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 83c7295

Please sign in to comment.