Skip to content

Commit

Permalink
Update to match data
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Dec 24, 2024
1 parent c028dc0 commit 51ad64d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ebl/dossiers/domain/dossier_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ebl.common.domain.provenance import Provenance
from ebl.fragmentarium.domain.fragment import Script
from ebl.bibliography.domain.reference import ReferenceType
from ebl.bibliography.domain.reference import Reference


@attr.s(frozen=True, auto_attribs=True)
Expand All @@ -16,4 +16,4 @@ class DossierRecord:
related_kings: Sequence[float] = []
provenance: Optional[Provenance] = None
script: Optional[Script] = None
references: Sequence[ReferenceType] = []
references: Sequence[Reference] = ()
13 changes: 8 additions & 5 deletions ebl/dossiers/infrastructure/mongo_dossiers_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
from ebl.dossiers.application.dossiers_repository import DossiersRepository
from ebl.common.domain.provenance import Provenance
from ebl.fragmentarium.application.fragment_fields_schemas import ScriptSchema
from ebl.schemas import NameEnumField
from ebl.bibliography.domain.reference import ReferenceType
from ebl.bibliography.application.reference_schema import ReferenceSchema

COLLECTION = "dossier"
COLLECTION = "dossiers"

provenance_field = fields.Function(
lambda object_: getattr(object_.provenance, "long_name", None),
Expand Down Expand Up @@ -40,10 +39,13 @@ class Meta:
)
provenance = provenance_field
script = fields.Nested(ScriptSchema, allow_none=True, load_default=None)
references = fields.List(NameEnumField(ReferenceType), load_default=list)
references = fields.Nested(
ReferenceSchema, allow_none=True, many=True, load_default=()
)

@post_load
def make_record(self, data, **kwargs):
data["references"] = tuple(data["references"])
return DossierRecord(**data)


Expand All @@ -53,7 +55,8 @@ def __init__(self, database: Database):

def query_by_ids(self, ids: Sequence[str]) -> Sequence[DossierRecord]:
cursor = self._collection.find_many({"_id": {"$in": ids}})
return DossierRecordSchema(many=True).load(cursor)
records = DossierRecordSchema(many=True).load(cursor)
return records

def create(self, dossier_record: DossierRecord) -> str:
return self._collection.insert_one(DossierRecordSchema().dump(dossier_record))
13 changes: 9 additions & 4 deletions ebl/dossiers/web/dossier_records.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from falcon import Request, Response
from urllib.parse import parse_qs
from ebl.errors import NotFoundError
from marshmallow import EXCLUDE

Expand All @@ -14,11 +15,15 @@ def __init__(self, _dossiersRepository: DossiersRepository):

def on_get(self, req: Request, resp: Response) -> None:
try:
dossiers = self._dossiersRepository.query_by_ids(
req.params["ids"].split(",")
)
parsed_params = parse_qs(req.query_string)
ids = parsed_params.get("ids[]", [])
if not ids:
raise ValueError("No valid IDs provided in the request.")

dossiers = self._dossiersRepository.query_by_ids(ids)
except ValueError as error:
raise NotFoundError(
f"No dossier records matching {str(req.params)} found."
f"No dossier records matching {req.params} found."
) from error

resp.media = DossierRecordSchema(unknown=EXCLUDE, many=True).dump(dossiers)
12 changes: 5 additions & 7 deletions ebl/tests/dossiers/test_dossier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
)
from ebl.tests.factories.dossier import DossierRecordFactory
from ebl.fragmentarium.domain.fragment import Script
from ebl.fragmentarium.application.fragment_fields_schemas import ScriptSchema
from ebl.common.domain.provenance import Provenance
from ebl.fragmentarium.application.fragment_fields_schemas import ScriptSchema
from ebl.bibliography.application.reference_schema import ReferenceSchema


@pytest.fixture
Expand All @@ -27,7 +28,7 @@ def test_dossier_record_creation(
assert isinstance(dossier_record.related_kings, (list, type(None)))
assert isinstance(dossier_record.provenance, (Provenance, type(None)))
assert isinstance(dossier_record.script, (Script, type(None)))
assert isinstance(dossier_record.references, (list, type(None)))
assert isinstance(dossier_record.references, (tuple, type(None)))


def test_dossier_record_defaults() -> None:
Expand All @@ -41,7 +42,7 @@ def test_dossier_record_defaults() -> None:
assert blank_dossier_record.related_kings == []
assert blank_dossier_record.provenance is None
assert blank_dossier_record.script is None
assert blank_dossier_record.references == []
assert blank_dossier_record.references == ()


def test_dossier_record_to_dict(
Expand All @@ -58,10 +59,7 @@ def test_dossier_record_to_dict(
if dossier_record.provenance
else None,
"script": ScriptSchema().dump(dossier_record.script),
"references": [
str(reference).replace("ReferenceType.", "")
for reference in dossier_record.references
],
"references": ReferenceSchema().dump(dossier_record.references, many=True),
}


Expand Down
2 changes: 1 addition & 1 deletion ebl/tests/dossiers/test_dossiers_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_fetch_dossier_record_route(
dossiers_repository.create(unrelated_dossier_record)
get_result = client.simulate_get(
"/dossiers",
params={"ids": ",".join([dossier_record.id, another_dossier_record.id])},
params={"ids[]": [dossier_record.id, another_dossier_record.id]},
)

assert get_result.status == falcon.HTTP_OK
Expand Down
4 changes: 2 additions & 2 deletions ebl/tests/factories/dossier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ebl.common.domain.provenance import Provenance
from ebl.tests.factories.fragment import ScriptFactory
from ebl.chronology.chronology import chronology
from ebl.bibliography.domain.reference import ReferenceType
from ebl.tests.factories.bibliography import ReferenceFactory


class DossierRecordFactory(factory.Factory):
Expand All @@ -28,5 +28,5 @@ class Meta:
provenance = factory.fuzzy.FuzzyChoice(set(Provenance) - {Provenance.STANDARD_TEXT})
script = factory.SubFactory(ScriptFactory)
references = factory.LazyAttribute(
lambda _: list({list(ReferenceType)[i] for i in range(randint(1, 6))})
lambda _: tuple(ReferenceFactory() for _ in range(randint(1, 5)))
)

0 comments on commit 51ad64d

Please sign in to comment.