Skip to content

Commit

Permalink
test: fix implementation for getting posts from storage
Browse files Browse the repository at this point in the history
  • Loading branch information
osoken committed Aug 18, 2024
1 parent ea859de commit b91fb8b
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,58 +254,64 @@ def _get_notes(
mock.get_topics.side_effect = _get_topics
mock.get_notes.side_effect = _get_notes

def _get_posts_impl(
post_ids: Union[List[PostId], None] = None,
note_ids: Union[List[NoteId], None] = None,
start: Union[TwitterTimestamp, None] = None,
end: Union[TwitterTimestamp, None] = None,
search_text: Union[str, None] = None,
) -> Generator[Post, None, None]:
for post in post_samples:
if post_ids is not None and post.post_id not in post_ids:
continue
if note_ids is not None and not any(
note.note_id in note_ids and note.post_id == post.post_id for note in note_samples
):
continue
if start is not None and post.created_at < start:
continue
if end is not None and post.created_at >= end:
continue
if search_text is not None and search_text not in post.text:
continue
yield post

def _get_posts() -> Generator[Post, None, None]:
yield from post_samples
yield from _get_posts_impl()

mock.get_posts.side_effect = _get_posts

def _get_posts_by_ids(post_ids: List[PostId]) -> Generator[Post, None, None]:
for i in post_ids:
for post in post_samples:
if post.post_id == i:
yield post
break
yield from _get_posts_impl(post_ids=post_ids)

mock.get_posts_by_ids.side_effect = _get_posts_by_ids

def _get_posts_by_note_ids(note_ids: List[NoteId]) -> Generator[Post, None, None]:
for post in post_samples:
for note in note_samples:
if note.note_id in note_ids and post.post_id == note.post_id:
yield post
break
yield from _get_posts_impl(note_ids=note_ids)

mock.get_posts_by_note_ids.side_effect = _get_posts_by_note_ids

def _get_posts_by_created_at_range(start: TwitterTimestamp, end: TwitterTimestamp) -> Generator[Post, None, None]:
for post in post_samples:
if start <= post.created_at < end:
yield post
yield from _get_posts_impl(start=start, end=end)

mock.get_posts_by_created_at_range.side_effect = _get_posts_by_created_at_range

def _get_posts_by_created_at_start(
start: TwitterTimestamp,
) -> Generator[Post, None, None]:
for post in post_samples:
if start <= post.created_at:
yield post
yield from _get_posts_impl(start=start)

mock.get_posts_by_created_at_start.side_effect = _get_posts_by_created_at_start

def _get_posts_by_created_at_end(
end: TwitterTimestamp,
) -> Generator[Post, None, None]:
for post in post_samples:
if post.created_at < end:
yield post
yield from _get_posts_impl(end=end)

mock.get_posts_by_created_at_end.side_effect = _get_posts_by_created_at_end

def _search_posts_by_text(search_text: str) -> Generator[Post, None, None]:
for post in post_samples:
if search_text in post.text:
yield post
yield from _get_posts_impl(search_text=search_text)

mock.search_posts_by_text.side_effect = _search_posts_by_text

Expand Down

0 comments on commit b91fb8b

Please sign in to comment.