From 1bfc9834453c2ce81a6d8d5e15d14aca8d99fc80 Mon Sep 17 00:00:00 2001 From: sushi-chaaaan Date: Mon, 5 Aug 2024 16:15:03 +0900 Subject: [PATCH] refactor: migrate TweetId to NoteId --- api/birdxplorer_api/routers/data.py | 3 +-- api/tests/conftest.py | 3 +-- common/birdxplorer_common/models.py | 9 +++------ common/birdxplorer_common/storage.py | 15 +++++++-------- common/tests/test_storage.py | 7 +++---- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/api/birdxplorer_api/routers/data.py b/api/birdxplorer_api/routers/data.py index 2398df8..352ff32 100644 --- a/api/birdxplorer_api/routers/data.py +++ b/api/birdxplorer_api/routers/data.py @@ -14,7 +14,6 @@ PostId, Topic, TopicId, - TweetId, TwitterTimestamp, UserEnrollment, ) @@ -73,7 +72,7 @@ def get_notes( created_at_from: Union[None, TwitterTimestamp] = Query(default=None), created_at_to: Union[None, TwitterTimestamp] = Query(default=None), topic_ids: Union[List[TopicId], None] = Query(default=None), - post_ids: Union[List[TweetId], None] = Query(default=None), + post_ids: Union[List[PostId], None] = Query(default=None), language: Union[LanguageIdentifier, None] = Query(default=None), ) -> NoteListResponse: return NoteListResponse( diff --git a/api/tests/conftest.py b/api/tests/conftest.py index 4fcc8e0..ac586a7 100644 --- a/api/tests/conftest.py +++ b/api/tests/conftest.py @@ -21,7 +21,6 @@ PostId, Topic, TopicId, - TweetId, TwitterTimestamp, UserEnrollment, XUser, @@ -227,7 +226,7 @@ def _get_notes( created_at_from: Union[None, TwitterTimestamp] = None, created_at_to: Union[None, TwitterTimestamp] = None, topic_ids: Union[List[TopicId], None] = None, - post_ids: Union[List[TweetId], None] = None, + post_ids: Union[List[PostId], None] = None, language: Union[LanguageIdentifier, None] = None, ) -> Generator[Note, None, None]: for note in note_samples: diff --git a/common/birdxplorer_common/models.py b/common/birdxplorer_common/models.py index 5d19baa..3712d35 100644 --- a/common/birdxplorer_common/models.py +++ b/common/birdxplorer_common/models.py @@ -565,7 +565,7 @@ class NotesValidationDifficulty(str, Enum): empty = "" -class TweetId(UpToNineteenDigitsDecimalString): ... +class PostId(UpToNineteenDigitsDecimalString): ... class NoteData(BaseModel): @@ -576,7 +576,7 @@ class NoteData(BaseModel): note_id: NoteId note_author_participant_id: ParticipantId created_at_millis: TwitterTimestamp - tweet_id: TweetId + tweet_id: PostId believable: NotesBelievable misleading_other: BinaryBool misleading_factual_error: BinaryBool @@ -629,7 +629,7 @@ class SummaryString(NonEmptyTrimmedString): ... class Note(BaseModel): note_id: NoteId - post_id: TweetId + post_id: PostId language: LanguageIdentifier topics: List[Topic] summary: SummaryString @@ -650,9 +650,6 @@ class XUser(BaseModel): following_count: NonNegativeInt -class PostId(UpToNineteenDigitsDecimalString): ... - - MediaDetails: TypeAlias = List[HttpUrl] | None diff --git a/common/birdxplorer_common/storage.py b/common/birdxplorer_common/storage.py index cd07b7a..0bff610 100644 --- a/common/birdxplorer_common/storage.py +++ b/common/birdxplorer_common/storage.py @@ -16,7 +16,6 @@ from .models import ( TopicId, TopicLabel, - TweetId, TwitterTimestamp, UserEnrollment, UserId, @@ -39,7 +38,7 @@ class Base(DeclarativeBase): TopicLabel: JSON, NoteId: String, ParticipantId: String, - TweetId: String, + PostId: String, LanguageIdentifier: String, TwitterTimestamp: DECIMAL, SummaryString: String, @@ -65,7 +64,7 @@ class NoteRecord(Base): __tablename__ = "notes" note_id: Mapped[NoteId] = mapped_column(primary_key=True) - post_id: Mapped[TweetId] = mapped_column(nullable=False) + post_id: Mapped[PostId] = mapped_column(nullable=False) topics: Mapped[List[NoteTopicAssociation]] = relationship() language: Mapped[LanguageIdentifier] = mapped_column(nullable=False) summary: Mapped[SummaryString] = mapped_column(nullable=False) @@ -92,7 +91,7 @@ class XUserRecord(Base): class PostRecord(Base): __tablename__ = "posts" - post_id: Mapped[TweetId] = mapped_column(primary_key=True) + post_id: Mapped[PostId] = mapped_column(primary_key=True) user_id: Mapped[UserId] = mapped_column(ForeignKey("x_users.user_id"), nullable=False) user: Mapped[XUserRecord] = relationship() text: Mapped[SummaryString] = mapped_column(nullable=False) @@ -109,7 +108,7 @@ class RowNoteRecord(Base): note_id: Mapped[NoteId] = mapped_column(primary_key=True) note_author_participant_id: Mapped[ParticipantId] = mapped_column(nullable=False) created_at_millis: Mapped[TwitterTimestamp] = mapped_column(nullable=False) - tweet_id: Mapped[TweetId] = mapped_column(nullable=False) + tweet_id: Mapped[PostId] = mapped_column(nullable=False) believable: Mapped[BinaryBool] = mapped_column(nullable=False) misleading_other: Mapped[BinaryBool] = mapped_column(nullable=False) misleading_factual_error: Mapped[BinaryBool] = mapped_column(nullable=False) @@ -129,14 +128,14 @@ class RowNoteRecord(Base): harmful: Mapped[NotesHarmful] = mapped_column(nullable=False) validation_difficulty: Mapped[SummaryString] = mapped_column(nullable=False) summary: Mapped[SummaryString] = mapped_column(nullable=False) - row_post_id: Mapped[TweetId] = mapped_column(ForeignKey("row_posts.post_id"), nullable=True) + row_post_id: Mapped[PostId] = mapped_column(ForeignKey("row_posts.post_id"), nullable=True) row_post: Mapped["RowPostRecord"] = relationship("RowPostRecord", back_populates="row_notes") class RowPostRecord(Base): __tablename__ = "row_posts" - post_id: Mapped[TweetId] = mapped_column(primary_key=True) + post_id: Mapped[PostId] = mapped_column(primary_key=True) author_id: Mapped[UserId] = mapped_column(ForeignKey("row_users.user_id"), nullable=False) text: Mapped[SummaryString] = mapped_column(nullable=False) media_type: Mapped[String] = mapped_column(nullable=True) @@ -224,7 +223,7 @@ def get_notes( created_at_from: Union[None, TwitterTimestamp] = None, created_at_to: Union[None, TwitterTimestamp] = None, topic_ids: Union[List[TopicId], None] = None, - post_ids: Union[List[TweetId], None] = None, + post_ids: Union[List[PostId], None] = None, language: Union[LanguageIdentifier, None] = None, ) -> Generator[NoteModel, None, None]: with Session(self.engine) as sess: diff --git a/common/tests/test_storage.py b/common/tests/test_storage.py index 3b74975..ff898e9 100644 --- a/common/tests/test_storage.py +++ b/common/tests/test_storage.py @@ -10,7 +10,6 @@ PostId, Topic, TopicId, - TweetId, TwitterTimestamp, ) from birdxplorer_common.storage import NoteRecord, PostRecord, Storage, TopicRecord @@ -208,8 +207,8 @@ def test_get_notes_by_post_ids( ) -> None: storage = Storage(engine=engine_for_test) post_ids = [ - TweetId.from_str("2234567890123456781"), - TweetId.from_str("2234567890123456782"), + PostId.from_str("2234567890123456781"), + PostId.from_str("2234567890123456782"), ] expected = [note for note in note_samples if note.post_id in post_ids] actual = list(storage.get_notes(post_ids=post_ids)) @@ -222,7 +221,7 @@ def test_get_notes_by_post_ids_empty( note_records_sample: List[NoteRecord], ) -> None: storage = Storage(engine=engine_for_test) - post_ids: List[TweetId] = [] + post_ids: List[PostId] = [] expected: List[Note] = [] actual = list(storage.get_notes(post_ids=post_ids)) assert expected == actual