From 2f13c99ee682845079cc4c3836e2235762e41d90 Mon Sep 17 00:00:00 2001 From: osoken Date: Sat, 17 Aug 2024 16:14:19 +0900 Subject: [PATCH] feat: add searchText query to post list endpoint --- api/birdxplorer_api/routers/data.py | 4 ++++ api/tests/conftest.py | 21 ++++++++++++++++++--- api/tests/routers/test_data.py | 7 +++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/api/birdxplorer_api/routers/data.py b/api/birdxplorer_api/routers/data.py index a0a060b..52413dd 100644 --- a/api/birdxplorer_api/routers/data.py +++ b/api/birdxplorer_api/routers/data.py @@ -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 @@ -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()) diff --git a/api/tests/conftest.py b/api/tests/conftest.py index c6b5bee..f68fbe8 100644 --- a/api/tests/conftest.py +++ b/api/tests/conftest.py @@ -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, @@ -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, @@ -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, @@ -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 diff --git a/api/tests/routers/test_data.py b/api/tests/routers/test_data.py index 82833d3..75a93d1 100644 --- a/api/tests/routers/test_data.py +++ b/api/tests/routers/test_data.py @@ -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