Skip to content

Commit

Permalink
feat(app): use storage to list topics
Browse files Browse the repository at this point in the history
  • Loading branch information
osoken committed Feb 25, 2024
1 parent a98e847 commit a2d1134
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 1 addition & 7 deletions birdxplorer/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion birdxplorer/storage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from .models import ParticipantId, UserEnrollment
from typing import Generator

from .models import ParticipantId, Topic, UserEnrollment
from .settings import GlobalSettings


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()
14 changes: 11 additions & 3 deletions tests/routers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit a2d1134

Please sign in to comment.