From a2d11349670e0360197dcb9b15b08075d0d83bcb Mon Sep 17 00:00:00 2001 From: osoken Date: Sun, 25 Feb 2024 10:36:13 +0900 Subject: [PATCH] feat(app): use storage to list topics --- birdxplorer/routers/data.py | 8 +------- birdxplorer/storage.py | 7 ++++++- tests/routers/conftest.py | 14 +++++++++++--- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/birdxplorer/routers/data.py b/birdxplorer/routers/data.py index 6cd76f5..a3faad2 100644 --- a/birdxplorer/routers/data.py +++ b/birdxplorer/routers/data.py @@ -22,12 +22,6 @@ def get_user_enrollment_by_participant_id(participant_id: ParticipantId) -> User @router.get("/topics", response_model=TopicListResponse) def get_topics() -> TopicListResponse: - return TopicListResponse( - data=[ - Topic(topic_id=1, label={"en": "topic1", "ja": "トピック1"}, reference_count=12341), - Topic(topic_id=2, label={"en": "topic2", "ja": "トピック2"}, reference_count=1232312342), - Topic(topic_id=3, label={"en": "topic3", "ja": "トピック3"}, reference_count=3), - ] - ) + return TopicListResponse(data=list(storage.get_topics())) return router diff --git a/birdxplorer/storage.py b/birdxplorer/storage.py index faad21d..e7fa7c9 100644 --- a/birdxplorer/storage.py +++ b/birdxplorer/storage.py @@ -1,4 +1,6 @@ -from .models import ParticipantId, UserEnrollment +from typing import Generator + +from .models import ParticipantId, Topic, UserEnrollment from .settings import GlobalSettings @@ -6,6 +8,9 @@ class Storage: def get_user_enrollment_by_participant_id(self, participant_id: ParticipantId) -> UserEnrollment: raise NotImplementedError + def get_topics(self) -> Generator[Topic, None, None]: + raise NotImplementedError + def gen_storage(settings: GlobalSettings) -> Storage: return Storage() diff --git a/tests/routers/conftest.py b/tests/routers/conftest.py index d5e15d3..1aff30a 100644 --- a/tests/routers/conftest.py +++ b/tests/routers/conftest.py @@ -48,16 +48,24 @@ def user_enrollment_samples( @fixture -def mock_storage(user_enrollment_samples: List[UserEnrollment]) -> Generator[MagicMock, None, None]: +def mock_storage( + user_enrollment_samples: List[UserEnrollment], topic_samples: List[Topic] +) -> Generator[MagicMock, None, None]: mock = MagicMock(spec=Storage) - def _(participant_id: ParticipantId) -> UserEnrollment: + def _get_user_enrollment_by_participant_id(participant_id: ParticipantId) -> UserEnrollment: x = {d.participant_id: d for d in user_enrollment_samples}.get(participant_id) if x is None: raise UserEnrollmentNotFoundError(participant_id=participant_id) return x - mock.get_user_enrollment_by_participant_id.side_effect = _ + mock.get_user_enrollment_by_participant_id.side_effect = _get_user_enrollment_by_participant_id + + def _get_topics() -> Generator[Topic, None, None]: + yield from topic_samples + + mock.get_topics.side_effect = _get_topics + yield mock