Skip to content

Commit

Permalink
refactor: migrate TweetId to NoteId
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichan044 committed Aug 5, 2024
1 parent fd5c1e9 commit 1bfc983
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 22 deletions.
3 changes: 1 addition & 2 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
PostId,
Topic,
TopicId,
TweetId,
TwitterTimestamp,
UserEnrollment,
)
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
PostId,
Topic,
TopicId,
TweetId,
TwitterTimestamp,
UserEnrollment,
XUser,
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 3 additions & 6 deletions common/birdxplorer_common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ class NotesValidationDifficulty(str, Enum):
empty = ""


class TweetId(UpToNineteenDigitsDecimalString): ...
class PostId(UpToNineteenDigitsDecimalString): ...


class NoteData(BaseModel):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -650,9 +650,6 @@ class XUser(BaseModel):
following_count: NonNegativeInt


class PostId(UpToNineteenDigitsDecimalString): ...


MediaDetails: TypeAlias = List[HttpUrl] | None


Expand Down
15 changes: 7 additions & 8 deletions common/birdxplorer_common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from .models import (
TopicId,
TopicLabel,
TweetId,
TwitterTimestamp,
UserEnrollment,
UserId,
Expand All @@ -39,7 +38,7 @@ class Base(DeclarativeBase):
TopicLabel: JSON,
NoteId: String,
ParticipantId: String,
TweetId: String,
PostId: String,
LanguageIdentifier: String,
TwitterTimestamp: DECIMAL,
SummaryString: String,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions common/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
PostId,
Topic,
TopicId,
TweetId,
TwitterTimestamp,
)
from birdxplorer_common.storage import NoteRecord, PostRecord, Storage, TopicRecord
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit 1bfc983

Please sign in to comment.