From fa677bca3777a2338ebe7b1417c460db2b485a8a Mon Sep 17 00:00:00 2001 From: Ilya Khait Date: Wed, 11 Dec 2024 01:30:51 +0000 Subject: [PATCH] Update & add test --- ebl/app.py | 6 +++ ebl/context.py | 4 +- ...r_repository.py => dossiers_repository.py} | 4 +- ...sitory.py => mongo_dossiers_repository.py} | 4 +- ebl/dossiers/web/bootstrap.py | 6 +-- ebl/dossiers/web/dossier_records.py | 14 +++---- ebl/tests/conftest.py | 12 +++--- ebl/tests/dossiers/test_dossier.py | 6 ++- ebl/tests/dossiers/test_dossier_repository.py | 10 ----- ebl/tests/dossiers/test_dossier_route.py | 42 ------------------- .../dossiers/test_dossiers_repository.py | 10 +++++ ebl/tests/dossiers/test_dossiers_route.py | 28 +++++++++++++ 12 files changed, 70 insertions(+), 76 deletions(-) rename ebl/dossiers/application/{dossier_repository.py => dossiers_repository.py} (72%) rename ebl/dossiers/infrastructure/{mongo_dossier_repository.py => mongo_dossiers_repository.py} (94%) delete mode 100644 ebl/tests/dossiers/test_dossier_repository.py delete mode 100644 ebl/tests/dossiers/test_dossier_route.py create mode 100644 ebl/tests/dossiers/test_dossiers_repository.py create mode 100644 ebl/tests/dossiers/test_dossiers_route.py diff --git a/ebl/app.py b/ebl/app.py index d90037892..849d9cc08 100644 --- a/ebl/app.py +++ b/ebl/app.py @@ -43,6 +43,7 @@ from ebl.signs.infrastructure.mongo_sign_repository import MongoSignRepository from ebl.signs.web.bootstrap import create_signs_routes from ebl.afo_register.web.bootstrap import create_afo_register_routes +from ebl.dossiers.web.bootstrap import create_dossiers_routes from ebl.transliteration.application.parallel_line_injector import ParallelLineInjector from ebl.transliteration.infrastructure.mongo_parallel_repository import ( MongoParallelRepository, @@ -50,6 +51,9 @@ from ebl.afo_register.infrastructure.mongo_afo_register_repository import ( MongoAfoRegisterRepository, ) +from ebl.dossiers.infrastructure.mongo_dossiers_repository import ( + MongoDossiersRepository, +) from ebl.users.domain.user import Guest from ebl.users.infrastructure.auth0 import Auth0Backend from ebl.fragmentarium.infrastructure.mongo_findspot_repository import ( @@ -99,6 +103,7 @@ def create_context(): annotations_repository=MongoAnnotationsRepository(database), lemma_repository=MongoLemmaRepository(database), afo_register_repository=MongoAfoRegisterRepository(database), + dossiers_repository=MongoDossiersRepository(database), findspot_repository=MongoFindspotRepository(database), custom_cache=custom_cache, cache=cache, @@ -128,6 +133,7 @@ def create_app(context: Context, issuer: str = "", audience: str = ""): create_lemmatization_routes(api, context) create_markup_routes(api, context) create_afo_register_routes(api, context) + create_dossiers_routes(api, context) return api diff --git a/ebl/context.py b/ebl/context.py index 28511dbd6..c43cc9df8 100644 --- a/ebl/context.py +++ b/ebl/context.py @@ -29,7 +29,7 @@ from ebl.fragmentarium.infrastructure.mongo_findspot_repository import ( MongoFindspotRepository, ) -from ebl.dossiers.application.dossier_repository import DossierRepository +from ebl.dossiers.application.dossiers_repository import DossiersRepository @attr.s(auto_attribs=True, frozen=True) @@ -54,7 +54,7 @@ class Context: cache: Cache parallel_line_injector: ParallelLineInjector afo_register_repository: AfoRegisterRepository - dossier_repository: DossierRepository + dossiers_repository: DossiersRepository def get_bibliography(self): return Bibliography(self.bibliography_repository, self.changelog) diff --git a/ebl/dossiers/application/dossier_repository.py b/ebl/dossiers/application/dossiers_repository.py similarity index 72% rename from ebl/dossiers/application/dossier_repository.py rename to ebl/dossiers/application/dossiers_repository.py index 3bebcb852..c70a7d41e 100644 --- a/ebl/dossiers/application/dossier_repository.py +++ b/ebl/dossiers/application/dossiers_repository.py @@ -5,9 +5,9 @@ ) -class DossierRepository(ABC): +class DossiersRepository(ABC): @abstractmethod - def fetch(self, name: str) -> DossierRecord: ... + def fetch(self, id: str) -> DossierRecord: ... @abstractmethod def create(self, dossier_record: DossierRecord) -> str: ... diff --git a/ebl/dossiers/infrastructure/mongo_dossier_repository.py b/ebl/dossiers/infrastructure/mongo_dossiers_repository.py similarity index 94% rename from ebl/dossiers/infrastructure/mongo_dossier_repository.py rename to ebl/dossiers/infrastructure/mongo_dossiers_repository.py index 78be66f5b..0f4c2ccc3 100644 --- a/ebl/dossiers/infrastructure/mongo_dossier_repository.py +++ b/ebl/dossiers/infrastructure/mongo_dossiers_repository.py @@ -4,7 +4,7 @@ from ebl.dossiers.domain.dossier_record import ( DossierRecord, ) -from ebl.dossiers.application.dossier_repository import DossierRepository +from ebl.dossiers.application.dossiers_repository import DossiersRepository from ebl.common.domain.provenance import Provenance from ebl.fragmentarium.application.fragment_schema import ScriptSchema from ebl.schemas import NameEnumField @@ -46,7 +46,7 @@ def make_record(self, data, **kwargs): return DossierRecord(**data) -class MongoDossierRepository(DossierRepository): +class MongoDossiersRepository(DossiersRepository): def __init__(self, database: Database): self._dossier = MongoCollection(database, COLLECTION) diff --git a/ebl/dossiers/web/bootstrap.py b/ebl/dossiers/web/bootstrap.py index b843ca2ac..5a4133168 100644 --- a/ebl/dossiers/web/bootstrap.py +++ b/ebl/dossiers/web/bootstrap.py @@ -2,10 +2,10 @@ from ebl.context import Context from ebl.dossiers.web.dossier_records import ( - DossierResource, + DossiersResource, ) -def create_afo_register_routes(api: falcon.App, context: Context): - dossier_resourse = DossierResource(context.dossier_repository) +def create_dossiers_routes(api: falcon.App, context: Context): + dossier_resourse = DossiersResource(context.dossiers_repository) api.add_route("/dossiers", dossier_resourse) diff --git a/ebl/dossiers/web/dossier_records.py b/ebl/dossiers/web/dossier_records.py index c3a71a39f..402092e37 100644 --- a/ebl/dossiers/web/dossier_records.py +++ b/ebl/dossiers/web/dossier_records.py @@ -1,21 +1,21 @@ from falcon import Request, Response from ebl.errors import NotFoundError -from ebl.dossiers.application.dossier_repository import DossierRepository -from ebl.dossiers.infrastructure.mongo_dossier_repository import ( +from ebl.dossiers.application.dossiers_repository import DossiersRepository +from ebl.dossiers.infrastructure.mongo_dossiers_repository import ( DossierRecordSchema, ) -class DossierResource: - def __init__(self, _dossierRepository: DossierRepository): - self._dossierRepository = _dossierRepository +class DossiersResource: + def __init__(self, _dossiersRepository: DossiersRepository): + self._dossiersRepository = _dossiersRepository def on_get(self, req: Request, resp: Response) -> None: try: - response = self._dossierRepository.fetch(req.params) + dossiers = self._dossiersRepository.fetch(req.params["id"]) except ValueError as error: raise NotFoundError( f"No dossier records matching {str(req.params)} found." ) from error - resp.media = DossierRecordSchema().dump(response, many=True) + resp.media = DossierRecordSchema().dump([dossiers], many=True) diff --git a/ebl/tests/conftest.py b/ebl/tests/conftest.py index af5d5dde6..6b809a59c 100644 --- a/ebl/tests/conftest.py +++ b/ebl/tests/conftest.py @@ -84,8 +84,8 @@ from ebl.afo_register.infrastructure.mongo_afo_register_repository import ( MongoAfoRegisterRepository, ) -from ebl.dossiers.infrastructure.mongo_dossier_repository import ( - MongoDossierRepository, +from ebl.dossiers.infrastructure.mongo_dossiers_repository import ( + MongoDossiersRepository, ) from ebl.users.domain.user import Guest, User from ebl.users.infrastructure.auth0 import Auth0User @@ -438,8 +438,8 @@ def user() -> User: @pytest.fixture -def dossier_repository(database): - return MongoDossierRepository(database) +def dossiers_repository(database): + return MongoDossiersRepository(database) @pytest.fixture @@ -459,7 +459,7 @@ def context( annotations_repository, lemma_repository, afo_register_repository, - dossier_repository, + dossiers_repository, findspot_repository, user, parallel_line_injector, @@ -482,7 +482,7 @@ def context( annotations_repository=annotations_repository, lemma_repository=lemma_repository, afo_register_repository=afo_register_repository, - dossier_repository=dossier_repository, + dossiers_repository=dossiers_repository, findspot_repository=findspot_repository, cache=Cache({"CACHE_TYPE": "null"}), custom_cache=ChapterCache(mongo_cache_repository), diff --git a/ebl/tests/dossiers/test_dossier.py b/ebl/tests/dossiers/test_dossier.py index 9d6f59d86..7146aaae4 100644 --- a/ebl/tests/dossiers/test_dossier.py +++ b/ebl/tests/dossiers/test_dossier.py @@ -2,7 +2,7 @@ from ebl.dossiers.domain.dossier_record import ( DossierRecord, ) -from ebl.dossiers.infrastructure.mongo_dossier_repository import ( +from ebl.dossiers.infrastructure.mongo_dossiers_repository import ( DossierRecordSchema, ) from ebl.tests.factories.dossier import DossierRecordFactory @@ -54,7 +54,9 @@ def test_dossier_record_to_dict( "yearRangeFrom": dossier_record.year_range_from, "yearRangeTo": dossier_record.year_range_to, "relatedKings": dossier_record.related_kings, - "provenance": dossier_record.provenance.long_name, + "provenance": dossier_record.provenance.long_name + if dossier_record.provenance + else None, "script": ScriptSchema().dump(dossier_record.script), "references": [ str(reference).replace("ReferenceType.", "") diff --git a/ebl/tests/dossiers/test_dossier_repository.py b/ebl/tests/dossiers/test_dossier_repository.py deleted file mode 100644 index b510137cc..000000000 --- a/ebl/tests/dossiers/test_dossier_repository.py +++ /dev/null @@ -1,10 +0,0 @@ -from ebl.tests.factories.dossier import DossierRecordFactory -from ebl.dossiers.application.dossier_repository import DossierRepository - - -def test_fetch(dossier_repository: DossierRepository): - dossier_record = DossierRecordFactory.build() - dossier_repository.create(dossier_record) - dossier_repository.create(DossierRecordFactory.build()) - - assert dossier_repository.fetch(dossier_record.id) == dossier_record diff --git a/ebl/tests/dossiers/test_dossier_route.py b/ebl/tests/dossiers/test_dossier_route.py deleted file mode 100644 index 0ec79acd0..000000000 --- a/ebl/tests/dossiers/test_dossier_route.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -import falcon -import pytest -import json - -from ebl.afo_register.domain.afo_register_record import AfoRegisterRecord -from ebl.tests.factories.afo_register import ( - AfoRegisterRecordFactory, - AfoRegisterRecordSuggestionFactory, -) -from ebl.afo_register.application.afo_register_repository import ( - AfoRegisterRepository, -) -from ebl.afo_register.infrastructure.mongo_afo_register_repository import ( - AfoRegisterRecordSchema, - AfoRegisterRecordSuggestionSchema, -) - - -@pytest.fixture -def afo_register_record() -> AfoRegisterRecord: - return AfoRegisterRecordFactory.build() - - -def test_search_afo_register_record_route( - afo_register_record, afo_register_repository: AfoRegisterRepository, client -) -> None: - params = { - "afoNumber": afo_register_record.afo_number, - "page": afo_register_record.page, - "text": afo_register_record.text, - "textNumber": afo_register_record.text_number, - "linesDiscussed": afo_register_record.lines_discussed, - "discussedBy": afo_register_record.discussed_by, - "discussedByNotes": afo_register_record.discussed_by_notes, - } - afo_register_repository.create(afo_register_record) - get_result = client.simulate_get("/afo-register", params=params) - - assert get_result.status == falcon.HTTP_OK - assert get_result.json == [AfoRegisterRecordSchema().dump(afo_register_record)] -""" diff --git a/ebl/tests/dossiers/test_dossiers_repository.py b/ebl/tests/dossiers/test_dossiers_repository.py new file mode 100644 index 000000000..da66f9589 --- /dev/null +++ b/ebl/tests/dossiers/test_dossiers_repository.py @@ -0,0 +1,10 @@ +from ebl.tests.factories.dossier import DossierRecordFactory +from ebl.dossiers.application.dossiers_repository import DossiersRepository + + +def test_fetch(dossiers_repository: DossiersRepository): + dossier_record = DossierRecordFactory.build() + dossiers_repository.create(dossier_record) + dossiers_repository.create(DossierRecordFactory.build()) + + assert dossiers_repository.fetch(dossier_record.id) == dossier_record diff --git a/ebl/tests/dossiers/test_dossiers_route.py b/ebl/tests/dossiers/test_dossiers_route.py new file mode 100644 index 000000000..dea6aaa19 --- /dev/null +++ b/ebl/tests/dossiers/test_dossiers_route.py @@ -0,0 +1,28 @@ +import falcon +import pytest + +from ebl.dossiers.domain.dossier_record import DossierRecord +from ebl.tests.factories.dossier import ( + DossierRecordFactory, +) +from ebl.dossiers.application.dossiers_repository import ( + DossiersRepository, +) +from ebl.dossiers.infrastructure.mongo_dossiers_repository import ( + DossierRecordSchema, +) + + +@pytest.fixture +def dossier_record() -> DossierRecord: + return DossierRecordFactory.build() + + +def test_fetch_dossier_record_route( + dossier_record, dossiers_repository: DossiersRepository, client +) -> None: + dossiers_repository.create(dossier_record) + get_result = client.simulate_get("/dossiers", params={"id": dossier_record.id}) + + assert get_result.status == falcon.HTTP_OK + assert get_result.json[0] == DossierRecordSchema().dump(dossier_record)