From 5415cd87012856b043d5d843602c6b08a6d3ef7b Mon Sep 17 00:00:00 2001 From: Ilya Khait Date: Tue, 10 Dec 2024 00:32:09 +0000 Subject: [PATCH] Update & fix test --- ebl/context.py | 2 +- .../application/dossier_repository.py | 2 +- .../domain/dossier_record.py | 8 ++-- .../mongo_dossier_repository.py | 23 ++--------- ebl/{dossier => dossiers}/web/bootstrap.py | 4 +- .../web/dossier_records.py | 6 +-- ebl/tests/conftest.py | 2 +- ebl/tests/dossier/test_dossier.py | 27 ------------- ebl/tests/dossier/test_dossier_repository.py | 2 +- ebl/tests/dossier/test_dossier_route.py | 38 ------------------- ebl/tests/factories/dossier.py | 9 ++--- 11 files changed, 21 insertions(+), 102 deletions(-) rename ebl/{dossier => dossiers}/application/dossier_repository.py (83%) rename ebl/{dossier => dossiers}/domain/dossier_record.py (74%) rename ebl/{dossier => dossiers}/infrastructure/mongo_dossier_repository.py (75%) rename ebl/{dossier => dossiers}/web/bootstrap.py (68%) rename ebl/{dossier => dossiers}/web/dossier_records.py (72%) diff --git a/ebl/context.py b/ebl/context.py index 6c4754eed..28511dbd6 100644 --- a/ebl/context.py +++ b/ebl/context.py @@ -29,7 +29,7 @@ from ebl.fragmentarium.infrastructure.mongo_findspot_repository import ( MongoFindspotRepository, ) -from ebl.dossier.application.dossier_repository import DossierRepository +from ebl.dossiers.application.dossier_repository import DossierRepository @attr.s(auto_attribs=True, frozen=True) diff --git a/ebl/dossier/application/dossier_repository.py b/ebl/dossiers/application/dossier_repository.py similarity index 83% rename from ebl/dossier/application/dossier_repository.py rename to ebl/dossiers/application/dossier_repository.py index 5d3f32f6c..3bebcb852 100644 --- a/ebl/dossier/application/dossier_repository.py +++ b/ebl/dossiers/application/dossier_repository.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod -from ebl.dossier.domain.dossier_record import ( +from ebl.dossiers.domain.dossier_record import ( DossierRecord, ) diff --git a/ebl/dossier/domain/dossier_record.py b/ebl/dossiers/domain/dossier_record.py similarity index 74% rename from ebl/dossier/domain/dossier_record.py rename to ebl/dossiers/domain/dossier_record.py index 517d1d58b..5b422a6fc 100644 --- a/ebl/dossier/domain/dossier_record.py +++ b/ebl/dossiers/domain/dossier_record.py @@ -1,5 +1,5 @@ import attr -from typing import Sequence, Optional, FrozenSet +from typing import Sequence, Optional from ebl.common.domain.provenance import Provenance from ebl.fragmentarium.domain.fragment import Script @@ -8,12 +8,12 @@ @attr.s(frozen=True, auto_attribs=True) class DossierRecord: - name: str + _id: str description: Optional[str] = None is_approximate_date: bool = False year_range_from: Optional[int] = None year_range_to: Optional[int] = None - related_kings: Optional[Sequence[float]] = [] + related_kings: Sequence[float] = [] provenance: Optional[Provenance] = None script: Optional[Script] = None - references: FrozenSet[ReferenceType] = frozenset() + references: Sequence[ReferenceType] = [] diff --git a/ebl/dossier/infrastructure/mongo_dossier_repository.py b/ebl/dossiers/infrastructure/mongo_dossier_repository.py similarity index 75% rename from ebl/dossier/infrastructure/mongo_dossier_repository.py rename to ebl/dossiers/infrastructure/mongo_dossier_repository.py index 47f262941..b6531be19 100644 --- a/ebl/dossier/infrastructure/mongo_dossier_repository.py +++ b/ebl/dossiers/infrastructure/mongo_dossier_repository.py @@ -1,10 +1,10 @@ from marshmallow import Schema, fields, post_load, EXCLUDE from pymongo.database import Database from ebl.mongo_collection import MongoCollection -from ebl.dossier.domain.dossier_record import ( +from ebl.dossiers.domain.dossier_record import ( DossierRecord, ) -from ebl.dossier.application.dossier_repository import DossierRepository +from ebl.dossiers.application.dossier_repository import DossierRepository from ebl.common.domain.provenance import Provenance from ebl.fragmentarium.application.fragment_schema import ScriptSchema from ebl.schemas import NameEnumField @@ -23,7 +23,7 @@ class DossierRecordSchema(Schema): class Meta: unknown = EXCLUDE - name = fields.String(required=True, unique=True) + _id = fields.String(required=True, unique=True) description = fields.String(load_default=None) is_approximate_date = fields.Boolean( data_key="isApproximateDate", load_default=False @@ -43,19 +43,7 @@ class Meta: @post_load def make_record(self, data, **kwargs): - return ( - DossierRecord( - data["name"], - data["description"], - data["is_approximate_date"], - data["year_range_from"], - data["year_range_to"], - data["related_kings"], - data["provenance"], - data["script"], - frozenset(data["references"]), - ), - ) + return DossierRecord(**data) class MongoDossierRepository(DossierRepository): @@ -67,9 +55,6 @@ def fetch(self, name: str) -> DossierRecord: record_data = self._dossier.find_one(query) if not record_data: raise ValueError(f"No dossier record found for the name: {name}") - - print("!!!!!!!!!!", record_data) - input() return DossierRecordSchema().load(record_data) def create(self, dossier_record: DossierRecord) -> str: diff --git a/ebl/dossier/web/bootstrap.py b/ebl/dossiers/web/bootstrap.py similarity index 68% rename from ebl/dossier/web/bootstrap.py rename to ebl/dossiers/web/bootstrap.py index 301d07571..b843ca2ac 100644 --- a/ebl/dossier/web/bootstrap.py +++ b/ebl/dossiers/web/bootstrap.py @@ -1,11 +1,11 @@ import falcon from ebl.context import Context -from ebl.dossier.web.dossier_records import ( +from ebl.dossiers.web.dossier_records import ( DossierResource, ) def create_afo_register_routes(api: falcon.App, context: Context): dossier_resourse = DossierResource(context.dossier_repository) - api.add_route("/dossier", dossier_resourse) + api.add_route("/dossiers", dossier_resourse) diff --git a/ebl/dossier/web/dossier_records.py b/ebl/dossiers/web/dossier_records.py similarity index 72% rename from ebl/dossier/web/dossier_records.py rename to ebl/dossiers/web/dossier_records.py index 69fcaa3c8..c3a71a39f 100644 --- a/ebl/dossier/web/dossier_records.py +++ b/ebl/dossiers/web/dossier_records.py @@ -1,8 +1,8 @@ from falcon import Request, Response from ebl.errors import NotFoundError -from ebl.dossier.application.dossier_repository import DossierRepository -from ebl.dossier.infrastructure.mongo_dossier_repository import ( +from ebl.dossiers.application.dossier_repository import DossierRepository +from ebl.dossiers.infrastructure.mongo_dossier_repository import ( DossierRecordSchema, ) @@ -16,6 +16,6 @@ def on_get(self, req: Request, resp: Response) -> None: response = self._dossierRepository.fetch(req.params) except ValueError as error: raise NotFoundError( - f"No dossier entries matching {str(req.params)} found." + f"No dossier records matching {str(req.params)} found." ) from error resp.media = DossierRecordSchema().dump(response, many=True) diff --git a/ebl/tests/conftest.py b/ebl/tests/conftest.py index e29e30436..af5d5dde6 100644 --- a/ebl/tests/conftest.py +++ b/ebl/tests/conftest.py @@ -84,7 +84,7 @@ from ebl.afo_register.infrastructure.mongo_afo_register_repository import ( MongoAfoRegisterRepository, ) -from ebl.dossier.infrastructure.mongo_dossier_repository import ( +from ebl.dossiers.infrastructure.mongo_dossier_repository import ( MongoDossierRepository, ) from ebl.users.domain.user import Guest, User diff --git a/ebl/tests/dossier/test_dossier.py b/ebl/tests/dossier/test_dossier.py index 6012c38d2..64d4cee6b 100644 --- a/ebl/tests/dossier/test_dossier.py +++ b/ebl/tests/dossier/test_dossier.py @@ -8,11 +8,6 @@ AfoRegisterRecordSchema, AfoRegisterRecordSuggestionSchema, ) -from ebl.tests.factories.afo_register import ( - AfoRegisterRecordFactory, - AfoRegisterRecordSuggestionFactory, -) - @pytest.fixture def afo_register_record(): @@ -57,15 +52,6 @@ def test_afo_register_record_to_dict(afo_register_record: AfoRegisterRecord) -> } -def test_afo_register_record_suggestion_to_dict( - afo_register_record_suggestion: AfoRegisterRecordSuggestion, -) -> None: - assert AfoRegisterRecordSuggestionSchema().dump(afo_register_record_suggestion) == { - "text": afo_register_record_suggestion.text, - "textNumbers": afo_register_record_suggestion.text_numbers, - } - - def test_afo_register_record_from_dict(afo_register_record: AfoRegisterRecord) -> None: serialized_data = AfoRegisterRecordSchema().dump(afo_register_record) deserialized_object = AfoRegisterRecordSchema().load(serialized_data) @@ -80,17 +66,4 @@ def test_afo_register_record_from_dict(afo_register_record: AfoRegisterRecord) - deserialized_object.discussed_by_notes == afo_register_record.discussed_by_notes ) - -def test_afo_register_record_suggestion_from_dict( - afo_register_record_suggestion: AfoRegisterRecordSuggestion, -) -> None: - serialized_data = AfoRegisterRecordSuggestionSchema().dump( - afo_register_record_suggestion - ) - deserialized_object = AfoRegisterRecordSuggestionSchema().load(serialized_data) - - assert deserialized_object.text == afo_register_record_suggestion.text - assert ( - deserialized_object.text_numbers == afo_register_record_suggestion.text_numbers - ) """ diff --git a/ebl/tests/dossier/test_dossier_repository.py b/ebl/tests/dossier/test_dossier_repository.py index 6d17eb150..6f1bc952f 100644 --- a/ebl/tests/dossier/test_dossier_repository.py +++ b/ebl/tests/dossier/test_dossier_repository.py @@ -1,5 +1,5 @@ from ebl.tests.factories.dossier import DossierRecordFactory -from ebl.dossier.application.dossier_repository import DossierRepository +from ebl.dossiers.application.dossier_repository import DossierRepository def test_fetch(dossier_repository: DossierRepository): diff --git a/ebl/tests/dossier/test_dossier_route.py b/ebl/tests/dossier/test_dossier_route.py index c00c2e340..0ec79acd0 100644 --- a/ebl/tests/dossier/test_dossier_route.py +++ b/ebl/tests/dossier/test_dossier_route.py @@ -39,42 +39,4 @@ def test_search_afo_register_record_route( assert get_result.status == falcon.HTTP_OK assert get_result.json == [AfoRegisterRecordSchema().dump(afo_register_record)] - - -def test_search_by_texts_and_numbers_route( - afo_register_repository: AfoRegisterRepository, client -) -> None: - record1 = AfoRegisterRecordFactory.build(text="Text1", text_number="1") - record2 = AfoRegisterRecordFactory.build(text="Text2", text_number="2") - record3 = AfoRegisterRecordFactory.build(text="Text3", text_number="3") - afo_register_repository.create(record1) - afo_register_repository.create(record2) - afo_register_repository.create(record3) - get_result = client.simulate_post( - "/afo-register/texts-numbers", body=json.dumps(["Text1 1", "Text3 3"]) - ) - expected_results = [ - AfoRegisterRecordSchema().dump(record) for record in [record1, record3] - ] - - assert get_result.status == falcon.HTTP_OK - assert get_result.json == expected_results - - -def test_search_afo_register_suggestions_route( - afo_register_record, afo_register_repository: AfoRegisterRepository, client -) -> None: - afo_register_repository.create(afo_register_record) - get_result = client.simulate_get( - "/afo-register/suggestions", - params={"text_query": afo_register_record.text[:-2]}, - ) - afo_register_record_suggestion = AfoRegisterRecordSuggestionFactory.build( - text=afo_register_record.text, text_numbers=[afo_register_record.text_number] - ) - - assert get_result.status == falcon.HTTP_OK - assert get_result.json == [ - AfoRegisterRecordSuggestionSchema().dump(afo_register_record_suggestion) - ] """ diff --git a/ebl/tests/factories/dossier.py b/ebl/tests/factories/dossier.py index 0ad9cfb67..45615afee 100644 --- a/ebl/tests/factories/dossier.py +++ b/ebl/tests/factories/dossier.py @@ -1,13 +1,12 @@ import factory from random import randint -from ebl.dossier.domain.dossier_record import ( +from ebl.dossiers.domain.dossier_record import ( DossierRecord, ) -from ebl.tests.factories.bibliography import ReferenceFactory -from ebl.tests.factories.collections import TupleFactory 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 class DossierRecordFactory(factory.Factory): @@ -28,6 +27,6 @@ class Meta: ) provenance = factory.fuzzy.FuzzyChoice(set(Provenance) - {Provenance.STANDARD_TEXT}) script = factory.SubFactory(ScriptFactory) - references = factory.List( - [factory.SubFactory(ReferenceFactory, with_document=True)], TupleFactory + references = factory.LazyAttribute( + lambda _: list({list(ReferenceType)[i] for i in range(randint(1, 6))}) )