Skip to content

Commit

Permalink
feat: add search_posts_by_text method
Browse files Browse the repository at this point in the history
  • Loading branch information
osoken committed Aug 17, 2024
1 parent 9049850 commit f0d4cd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
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

0 comments on commit f0d4cd3

Please sign in to comment.