Skip to content

Commit

Permalink
feat: add searchText query to post list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
osoken committed Aug 17, 2024
1 parent f0d4cd3 commit 2f13c99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def get_posts(
created_at_end: Union[None, TwitterTimestamp, str] = Query(default=None),
offset: int = Query(default=0, ge=0), # 確保 offset 是非負的
limit: int = Query(default=100, gt=0, le=1000), # 確保 limit 在合理範圍內
search_text: Union[None, str] = Query(default=None),
) -> PostListResponse:
posts = None

Expand All @@ -116,6 +117,9 @@ def get_posts(
posts = list(storage.get_posts_by_created_at_start(start=ensure_twitter_timestamp(created_at_start)))
elif created_at_end is not None:
posts = list(storage.get_posts_by_created_at_end(end=ensure_twitter_timestamp(created_at_end)))
elif search_text is not None and len(search_text) > 0:
print(search_text)
posts = list(storage.search_posts_by_text(search_text))
else:
posts = list(storage.get_posts())

Expand Down
21 changes: 18 additions & 3 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def post_samples(post_factory: PostFactory, x_user_samples: List[XUser]) -> Gene
post_id="2234567890123456781",
x_user_id="1234567890123456781",
x_user=x_user_samples[0],
text="text11",
text="""\
新しいプロジェクトがついに公開されました!詳細はこちら👉
https://t.co/xxxxxxxxxxx/ #プロジェクト #新発売 #Tech""",
media_details=None,
created_at=1152921600000,
like_count=10,
Expand All @@ -177,7 +180,10 @@ def post_samples(post_factory: PostFactory, x_user_samples: List[XUser]) -> Gene
post_id="2234567890123456791",
x_user_id="1234567890123456781",
x_user=x_user_samples[0],
text="text12",
text="""\
このブログ記事、めちゃくちゃ参考になった!🔥 チェックしてみて!
https://t.co/yyyyyyyyyyy/ #学び #自己啓発""",
media_details=None,
created_at=1153921700000,
like_count=10,
Expand All @@ -188,7 +194,8 @@ def post_samples(post_factory: PostFactory, x_user_samples: List[XUser]) -> Gene
post_id="2234567890123456801",
x_user_id="1234567890123456782",
x_user=x_user_samples[1],
text="text21",
text="""\
次の休暇はここに決めた!🌴🏖️ 見てみて~ https://t.co/xxxxxxxxxxx/ #旅行 #バケーション""",
media_details=None,
created_at=1154921800000,
like_count=10,
Expand Down Expand Up @@ -295,6 +302,14 @@ def _get_posts_by_created_at_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:
print(search_text in post.text, post.text)
if search_text in post.text:
yield post

mock.search_posts_by_text.side_effect = _search_posts_by_text

yield mock


Expand Down
7 changes: 7 additions & 0 deletions api/tests/routers/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def test_posts_get_timestamp_out_of_range(client: TestClient, post_samples: List
assert response.status_code == 422


def test_posts_search_by_text(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts/?searchText=https%3A%2F%2Ft.co%2Fxxxxxxxxxxx%2F")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[i].model_dump_json()) for i in (0, 2)]}


def test_notes_get(client: TestClient, note_samples: List[Note]) -> None:
response = client.get("/api/v1/data/notes")
assert response.status_code == 200
Expand Down

0 comments on commit 2f13c99

Please sign in to comment.