Skip to content

Commit

Permalink
feat: add Url search for list posts route
Browse files Browse the repository at this point in the history
  • Loading branch information
osoken committed Oct 6, 2024
1 parent 17f50a9 commit 00771ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def get_posts(
offset: int = Query(default=0, ge=0),
limit: int = Query(default=100, gt=0, le=1000),
search_text: Union[None, str] = Query(default=None),
search_url: Union[None, HttpUrl] = Query(default=None),
) -> PostListResponse:
if created_at_from is not None and isinstance(created_at_from, str):
created_at_from = ensure_twitter_timestamp(created_at_from)
Expand All @@ -113,6 +114,7 @@ def get_posts(
start=created_at_from,
end=created_at_to,
search_text=search_text,
search_url=search_url,
offset=offset,
limit=limit,
)
Expand All @@ -123,6 +125,7 @@ def get_posts(
start=created_at_from,
end=created_at_to,
search_text=search_text,
search_url=search_url,
)

for post in posts:
Expand Down
14 changes: 13 additions & 1 deletion api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
from polyfactory import Use
from polyfactory.factories.pydantic_factory import ModelFactory
from polyfactory.pytest_plugin import register_fixture
from pydantic import HttpUrl
from pytest import fixture

from birdxplorer_common.exceptions import UserEnrollmentNotFoundError
from birdxplorer_common.models import (
LanguageIdentifier,
Link,
LinkId,
Note,
NoteId,
ParticipantId,
Expand Down Expand Up @@ -263,6 +265,7 @@ def mock_storage(
topic_samples: List[Topic],
post_samples: List[Post],
note_samples: List[Note],
link_samples: List[Link],
) -> Generator[MagicMock, None, None]:
mock = MagicMock(spec=Storage)

Expand Down Expand Up @@ -311,11 +314,17 @@ def _get_posts(
start: Union[TwitterTimestamp, None] = None,
end: Union[TwitterTimestamp, None] = None,
search_text: Union[str, None] = None,
search_url: Union[HttpUrl, None] = None,
offset: Union[int, None] = None,
limit: Union[int, None] = None,
) -> Generator[Post, None, None]:
gen_count = 0
actual_gen_count = 0
url_id: LinkId | None = None
if search_url is not None:
url_candidates = [link.link_id for link in link_samples if link.url == search_url]
if len(url_candidates) > 0:
url_id = url_candidates[0]
for idx, post in enumerate(post_samples):
if limit is not None and actual_gen_count >= limit:
break
Expand All @@ -331,6 +340,8 @@ def _get_posts(
continue
if search_text is not None and search_text not in post.text:
continue
if search_url is not None and url_id not in [link.link_id for link in post.links]:
continue
gen_count += 1
if offset is not None and gen_count <= offset:
continue
Expand All @@ -345,8 +356,9 @@ def _get_number_of_posts(
start: Union[TwitterTimestamp, None] = None,
end: Union[TwitterTimestamp, None] = None,
search_text: Union[str, None] = None,
search_url: Union[str, None] = None,
) -> int:
return len(list(_get_posts(post_ids, note_ids, start, end, search_text)))
return len(list(_get_posts(post_ids, note_ids, start, end, search_text, search_url)))

mock.get_number_of_posts.side_effect = _get_number_of_posts

Expand Down

0 comments on commit 00771ef

Please sign in to comment.