From f0d4cd3ba9ef369459c439e660520f6ebd00abea Mon Sep 17 00:00:00 2001 From: osoken Date: Sat, 17 Aug 2024 15:51:54 +0900 Subject: [PATCH] feat: add search_posts_by_text method --- common/birdxplorer_common/storage.py | 5 +++++ common/tests/conftest.py | 13 ++++++++++--- common/tests/test_storage.py | 12 ++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/common/birdxplorer_common/storage.py b/common/birdxplorer_common/storage.py index 5e6c30b..4f940e9 100644 --- a/common/birdxplorer_common/storage.py +++ b/common/birdxplorer_common/storage.py @@ -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) diff --git a/common/tests/conftest.py b/common/tests/conftest.py index 71ab36d..5af7934 100644 --- a/common/tests/conftest.py +++ b/common/tests/conftest.py @@ -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, @@ -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, @@ -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, diff --git a/common/tests/test_storage.py b/common/tests/test_storage.py index 63bf5bb..d6a66d5 100644 --- a/common/tests/test_storage.py +++ b/common/tests/test_storage.py @@ -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],