Skip to content

Commit

Permalink
feat: add Link model
Browse files Browse the repository at this point in the history
  • Loading branch information
osoken committed Sep 27, 2024
1 parent 39288b3 commit 2c8db37
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
21 changes: 21 additions & 0 deletions common/birdxplorer_common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,26 @@ class XUser(BaseModel):
MediaDetails: TypeAlias = List[HttpUrl] | None


class LinkId(NonNegativeInt):
"""
>>> LinkId.from_int(1)
LinkId(1)
"""

pass


class Link(BaseModel):
"""
>>> Link.model_validate_json('{"linkId": 1, "canonicalUrl": "https://example.com", "shortUrl": "https://example.com/short"}')
Link(link_id=LinkId(1), canonical_url=Url('https://example.com/'), short_url=Url('https://example.com/short'))
""" # noqa: E501

link_id: LinkId
canonical_url: HttpUrl
short_url: HttpUrl


class Post(BaseModel):
post_id: PostId
link: Optional[HttpUrl] = None
Expand All @@ -688,6 +708,7 @@ class Post(BaseModel):
like_count: NonNegativeInt
repost_count: NonNegativeInt
impression_count: NonNegativeInt
links: List[Link] = []


class PaginationMeta(BaseModel):
Expand Down
39 changes: 37 additions & 2 deletions common/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sqlalchemy.sql import text

from birdxplorer_common.models import (
Link,
Note,
Post,
Topic,
Expand Down Expand Up @@ -99,6 +100,11 @@ class PostFactory(ModelFactory[Post]):
__model__ = Post


@register_fixture(name="link_factory")
class LinkFactory(ModelFactory[Link]):
__model__ = Link


@fixture
def user_enrollment_samples(
user_enrollment_factory: UserEnrollmentFactory,
Expand All @@ -117,6 +123,17 @@ def topic_samples(topic_factory: TopicFactory) -> Generator[List[Topic], None, N
yield topics


@fixture
def link_samples(link_factory: LinkFactory) -> Generator[List[Link], None, None]:
links = [
link_factory.build(link_id=0, canonical_url="https://t.co/xxxxxxxxxxx/", short_url="https://example.com/sh0"),
link_factory.build(link_id=1, canonical_url="https://t.co/yyyyyyyyyyy/", short_url="https://example.com/sh1"),
link_factory.build(link_id=2, canonical_url="https://t.co/zzzzzzzzzzz/", short_url="https://example.com/sh2"),
link_factory.build(link_id=3, canonical_url="https://t.co/wwwwwwwwwww/", short_url="https://example.com/sh3"),
]
yield links


@fixture
def note_samples(note_factory: NoteFactory, topic_samples: List[Topic]) -> Generator[List[Note], None, None]:
notes = [
Expand Down Expand Up @@ -201,7 +218,9 @@ def x_user_samples(x_user_factory: XUserFactory) -> Generator[List[XUser], None,


@fixture
def post_samples(post_factory: PostFactory, x_user_samples: List[XUser]) -> Generator[List[Post], None, None]:
def post_samples(
post_factory: PostFactory, x_user_samples: List[XUser], link_samples: List[Link]
) -> Generator[List[Post], None, None]:
posts = [
post_factory.build(
post_id="2234567890123456781",
Expand All @@ -217,6 +236,7 @@ def post_samples(post_factory: PostFactory, x_user_samples: List[XUser]) -> Gene
like_count=10,
repost_count=20,
impression_count=30,
links=[link_samples[0]],
),
post_factory.build(
post_id="2234567890123456791",
Expand All @@ -232,19 +252,34 @@ def post_samples(post_factory: PostFactory, x_user_samples: List[XUser]) -> Gene
like_count=10,
repost_count=20,
impression_count=30,
links=[link_samples[1]],
),
post_factory.build(
post_id="2234567890123456801",
link=None,
x_user_id="1234567890123456782",
x_user=x_user_samples[1],
text="""\
次の休暇はここに決めた!🌴🏖️ 見てみて~ https://t.co/xxxxxxxxxxx/ #旅行 #バケーション""",
次の休暇はここに決めた!🌴🏖️ 見てみて~ https://t.co/xxxxxxxxxxx/ https://t.co/wwwwwwwwwww/ #旅行 #バケーション""",
media_details=None,
created_at=1154921800000,
like_count=10,
repost_count=20,
impression_count=30,
links=[link_samples[0], link_samples[3]],
),
post_factory.build(
post_id="2234567890123456811",
link=None,
x_user_id="1234567890123456782",
x_user=x_user_samples[1],
text="https://t.co/zzzzzzzzzzz/ https://t.co/wwwwwwwwwww/",
media_details=None,
created_at=1154922900000,
like_count=10,
repost_count=20,
impression_count=30,
links=[link_samples[2], link_samples[3]],
),
]
yield posts
Expand Down
8 changes: 4 additions & 4 deletions common/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def test_get_topic_list(
@pytest.mark.parametrize(
["filter_args", "expected_indices"],
[
[dict(), [0, 1, 2]],
[dict(offset=1), [1, 2]],
[dict(), [0, 1, 2, 3]],
[dict(offset=1), [1, 2, 3]],
[dict(limit=1), [0]],
[dict(offset=1, limit=1), [1]],
[dict(post_ids=[PostId.from_str("2234567890123456781"), PostId.from_str("2234567890123456801")]), [0, 2]],
Expand Down Expand Up @@ -63,11 +63,11 @@ def test_get_post(
@pytest.mark.parametrize(
["filter_args", "expected_indices"],
[
[dict(), [0, 1, 2]],
[dict(), [0, 1, 2, 3]],
[dict(post_ids=[PostId.from_str("2234567890123456781"), PostId.from_str("2234567890123456801")]), [0, 2]],
[dict(post_ids=[]), []],
[dict(start=TwitterTimestamp.from_int(1153921700000), end=TwitterTimestamp.from_int(1153921800000)), [1]],
[dict(start=TwitterTimestamp.from_int(1153921700000)), [1, 2]],
[dict(start=TwitterTimestamp.from_int(1153921700000)), [1, 2, 3]],
[dict(end=TwitterTimestamp.from_int(1153921700000)), [0]],
[dict(search_text="https://t.co/xxxxxxxxxxx/"), [0, 2]],
[dict(note_ids=[NoteId.from_str("1234567890123456781")]), [0]],
Expand Down

0 comments on commit 2c8db37

Please sign in to comment.