Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/issue 95 add text search for post #97

Merged
merged 5 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def get_posts(
created_at_end: Union[None, TwitterTimestamp, str] = Query(default=None),
offset: int = Query(default=0, ge=0),
limit: int = Query(default=100, gt=0, le=1000),
search_text: Union[None, str] = Query(default=None),
) -> PostListResponse:
posts = None

Expand All @@ -118,6 +119,8 @@ 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:
posts = list(storage.search_posts_by_text(search_text))
else:
posts = list(storage.get_posts())

Expand Down
20 changes: 17 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,13 @@ 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:
if search_text in post.text:
yield post

mock.search_posts_by_text.side_effect = _search_posts_by_text

yield mock


Expand Down
10 changes: 10 additions & 0 deletions api/tests/routers/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ 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)],
"meta": {"next": None, "prev": None},
}


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
5 changes: 5 additions & 0 deletions common/birdxplorer_common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ def get_posts_by_note_ids(self, note_ids: List[NoteId]) -> Generator[PostModel,
for post_record in sess.execute(query).scalars().all():
yield self._post_record_to_model(post_record)

def search_posts_by_text(self, search_word: str) -> Generator[PostModel, None, None]:
with Session(self.engine) as sess:
for post_record in sess.query(PostRecord).filter(PostRecord.text.like(f"%{search_word}%")):
yield self._post_record_to_model(post_record)


def gen_storage(settings: GlobalSettings) -> Storage:
engine = create_engine(settings.storage_settings.sqlalchemy_database_url)
Expand Down
13 changes: 10 additions & 3 deletions common/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,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 @@ -218,7 +221,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 @@ -229,7 +235,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
12 changes: 12 additions & 0 deletions common/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ def test_get_posts_by_created_at_end(
assert expected == actual


def test_search_posts_by_text(
engine_for_test: Engine,
post_samples: List[Post],
post_records_sample: List[PostRecord],
) -> None:
storage = Storage(engine=engine_for_test)
search_word = "https://t.co/xxxxxxxxxxx/"
expected = [post_samples[i] for i in (0, 2)]
actual = list(storage.search_posts_by_text(search_word))
assert actual == expected


def test_get_notes_by_ids(
engine_for_test: Engine,
note_samples: List[Note],
Expand Down
Loading