Exemple #1
0
def test_media_object(client: TwitterAPI.TwitterAPI) -> None:
    SAMPLE_TWEET: Dict = {
        "id": "1263145271946551300",
        "media_key": "13_1263145212760805376",
        "width": 1920,
        "height": 1080,
        "duration_ms": 46947,
        "view_count": 1845,
        "preview_image_url": "https://pbs.twimg.com/media/EYeX7akWsAIP1_1.jpg",
        "type": Media.Type.VIDEO,
    }

    MEDIA_FIELDS: List[Media.Field] = [
        Media.Field.HEIGHT,
        Media.Field.WIDTH,
        Media.Field.VIEW_COUNT,
        Media.Field.DURATION_MS,
        Media.Field.PREVIEW_IMAGE_URL,
    ]

    tweet: Tweet.Tweet = client.get_tweet(
        SAMPLE_TWEET["id"],
        expansions=[Tweet.Expantion.MEDIA_KEYS],
        media_fields=MEDIA_FIELDS,
    )

    assert tweet.medias, "media should exist."
    media: Media.Media = tweet.medias[0]

    assert media.media_key == SAMPLE_TWEET["media_key"], "media_key is wrong."
    assert media.width == SAMPLE_TWEET["width"], "width is wrong"
    assert media.height == SAMPLE_TWEET["height"], "height is wrong."
    assert media.duration_ms == SAMPLE_TWEET["duration_ms"], "duration_ms is wrong."
    assert media.view_count, "view_count should exist."
    assert media.type == SAMPLE_TWEET["type"], "type should be video."
Exemple #2
0
def test_user_with_additional_fields(client: TwitterAPI.TwitterAPI) -> None:

    # ref: https://twitter.com/TwitterDev
    SAMPLE_USER: Dict = {
        # default fields
        "id": "2244994945",
        "name": "Twitter Dev",
        "username": "******",
        # additional fields
        "created_at": "2013-12-14T04:35:55.000+00:00",
        "description": {
            "text": "The voice of the #TwitterDev team and your official source for updates, news, and events, related to the #TwitterAPI.",
        },
        "location": "127.0.0.1",
        "url": "https://t.co/3ZX3TNiZCY",
        "profile_image_url": "https://pbs.twimg.com/profile_images/1283786620521652229/lEODkLTh_normal.jpg",
        "protected": False,
        "pinned_tweet_id": "1293593516040269825",
        "verified": True,
    }

    SAMPLE_USER_FIELDS: List[User.Field] = [
        User.Field.CREATED_AT,
        User.Field.DESCRIPTION,
        User.Field.LOCATION,
        User.Field.PINNED_TWEET_ID,
        User.Field.PROFILE_IMAGE_URL,
        User.Field.PROTECTED,
        User.Field.URL,
        User.Field.VERIFIED,
    ]

    user: User.User = client.get_user_by_id(
        SAMPLE_USER["id"], user_fields=SAMPLE_USER_FIELDS
    )

    assert user.id == SAMPLE_USER["id"], "User ID is wrong."
    assert user.name == SAMPLE_USER["name"], "name field is wrong."
    assert user.username == SAMPLE_USER["username"], "username field is wrong."
    assert user.created_at == datetime.fromisoformat(
        SAMPLE_USER["created_at"]
    ), "created_at is wrong."
    assert user.description, "description should exit."
    assert (
        user.description.text == SAMPLE_USER["description"]["text"]
    ), "description field is wrong."
    assert user.location == SAMPLE_USER["location"], "location field is wrong."
    assert (
        user.pinned_tweet_id == SAMPLE_USER["pinned_tweet_id"]
    ), "pinned_tweet_id field is wrong."
    assert (
        user.profile_image_url == SAMPLE_USER["profile_image_url"]
    ), "profile_image_url field is wrong."
    assert user.protected == SAMPLE_USER["protected"], "protected field is wrong."
    assert user.url == SAMPLE_USER["url"], "url field is wrong."
    assert user.verified == SAMPLE_USER["verified"], "verified field is wrong."
Exemple #3
0
def test_minimum_tweet(client: TwitterAPI.TwitterAPI) -> None:

    SAMPLE_TWEET: Dict = {
        "id": "1212092628029698048",
        "text": "We believe the best future version of our API will come from building it with YOU. Here\u2019s to another great year with everyone who builds on the Twitter platform. We can\u2019t wait to continue working with you in the new year. https://t.co/yvxdK6aOo2",
    }

    tweet: Tweet.Tweet = client.get_tweet(SAMPLE_TWEET["id"])

    assert tweet.text == SAMPLE_TWEET["text"], "text field is wrong."
Exemple #4
0
def test_metrics_object(client: TwitterAPI.TwitterAPI) -> None:

    tweet: Tweet.Tweet = client.get_tweet(
        "1204084171334832128", tweet_fields=[Tweet.Field.PUBLIC_METRICS]
    )

    assert tweet.public_metrics, "public_metric should exist."
    assert tweet.public_metrics.retweet_count, "public_metric should exist."
    assert tweet.public_metrics.quote_count, "quote_count should exist."
    assert tweet.public_metrics.like_count, "like_count should exist."
    assert tweet.public_metrics.reply_count, "reply_count should exist."
Exemple #5
0
def test_public_metric_for_user(client: TwitterAPI.TwitterAPI) -> None:

    # ref: https://twitter.com/TwitterDev
    user: User.User = client.get_user_by_id(
        "2244994945", user_fields=[User.Field.PUBLIC_METRICS]
    )

    assert user.public_metrics, "public_metric should exist."
    assert user.public_metrics.followers_count, "followers_count should exist."
    assert user.public_metrics.following_count, "following_count should exist."
    assert user.public_metrics.tweet_count, "tweet_count should exist."
    assert user.public_metrics.listed_count, "listed_count should exist."
Exemple #6
0
def test_get_user_by_username(client: TwitterAPI.TwitterAPI) -> None:

    # ref: https://twitter.com/TwitterDev
    SAMPLE_USER: Dict = {
        "id": "2244994945",
        "name": "Twitter Dev",
        "username": "******",
    }

    user: User.User = client.get_user_by_username(SAMPLE_USER["username"])

    assert user.id == SAMPLE_USER["id"], "User ID is wrong."
    assert user.name == SAMPLE_USER["name"], "name field is wrong."
    assert user.username == SAMPLE_USER["username"], "username field is wrong."
Exemple #7
0
def test_poll_object(client: TwitterAPI.TwitterAPI) -> None:

    SAMPLE_TWEET: Dict = {
        "id": "1199786642791452673",
        "options": [
            {"label": "“C Sharp”", "votes": 795},
            {"label": "“C Hashtag”", "votes": 156},
        ],
        "status": Poll.Status.CLOSED,
        "duration_minutes": 1440,
        "end_datetime": datetime.fromisoformat("2019-11-28T20:26:41.000+00:00"),
    }

    POLL_FIELDS: List[Poll.Field] = [
        Poll.Field.DURATION_MINUTES,
        Poll.Field.END_DATETIME,
        Poll.Field.VOTING_STATUS,
    ]

    tweet: Tweet.Tweet = client.get_tweet(
        SAMPLE_TWEET["id"], [Tweet.Expantion.POLL_IDS], poll_fields=POLL_FIELDS
    )

    assert tweet, "Tweet should exist."
    assert tweet.polls, "Poll should exist."

    for poll in tweet.polls:
        assert poll.id, "Poll ID should exist."

        assert poll.options, "Options should exist."
        for idx, option in enumerate(poll.options):
            assert option.position == idx + 1, "position is wrong."
            assert (
                option.label == SAMPLE_TWEET["options"][idx]["label"]
            ), "label is wrong."
            assert (
                option.votes == SAMPLE_TWEET["options"][idx]["votes"]
            ), "votes is wrong."

        assert (
            poll.duration_minutes == SAMPLE_TWEET["duration_minutes"]
        ), "duration_minutes is wrong."
        assert poll.end_datetime == SAMPLE_TWEET["end_datetime"], "datetime is wrong."
        assert poll.voting_status == SAMPLE_TWEET["status"], "voting_status is wrong."
Exemple #8
0
def test_normal_tweet(client: TwitterAPI.TwitterAPI) -> None:

    SAMPLE_TWEET: Dict = {
        # default fields
        "id": "1212092628029698048",
        "text": "We believe the best future version of our API will come from building it with YOU. Here\u2019s to another great year with everyone who builds on the Twitter platform. We can\u2019t wait to continue working with you in the new year. https://t.co/yvxdK6aOo2",
        # additional fields
        "author_id": "2244994945",
        "created_at": "2019-12-31T19:26:16.000+00:00",
        "lang": "en",
        "possibly_sensitive": False,
        "source": "Twitter Web App",
    }

    SAMPLE_TWEET_FIELDS: List[Tweet.Field] = [
        Tweet.Field.AUTHOR_ID,
        Tweet.Field.CREATED_AT,
        Tweet.Field.LANG,
        Tweet.Field.POSSIBLY_SENSITIVE,
        Tweet.Field.SOURCE,
    ]

    tweet: Tweet.Tweet = client.get_tweet(
        SAMPLE_TWEET["id"], tweet_fields=SAMPLE_TWEET_FIELDS
    )

    assert SAMPLE_TWEET["id"] == tweet.id, "Tweet ID is wrong."
    assert SAMPLE_TWEET["text"] == tweet.text, "text is wrong."

    assert SAMPLE_TWEET["author_id"] == tweet.author_id, "User ID is wrong."
    assert SAMPLE_TWEET["source"] == tweet.source, "source is wrong."
    assert (
        SAMPLE_TWEET["possibly_sensitive"] == tweet.possibly_sensitive
    ), "possibly_sensitive is wrong."
    assert SAMPLE_TWEET["lang"] == tweet.lang, "lang is wrong."
    assert (
        datetime.fromisoformat(SAMPLE_TWEET["created_at"]) == tweet.created_at
    ), "created_at is wrong."
Exemple #9
0
def test_not_exist_tweet(client: TwitterAPI.TwitterAPI) -> None:
    with pytest.raises(Exception):
        client.get_tweet("")
Exemple #10
0
def test_entities_for_tweet(client: TwitterAPI.TwitterAPI) -> None:
    SAMPLE_TWEET: Dict = {
        # default fields
        "id": "1212092628029698048",
        "text": "We believe the best future version of our API will come from building it with YOU. Here\u2019s to another great year with everyone who builds on the Twitter platform. We can\u2019t wait to continue working with you in the new year. https://t.co/yvxdK6aOo2",
        "entities": {
            "annotations": [
                {
                    "start": 144,
                    "end": 150,
                    "probability": 0.626,
                    "type": "Product",
                    "normalized_text": "Twitter",
                }
            ],
            "urls": [
                {
                    "start": 222,
                    "end": 245,
                    "url": "https://t.co/yvxdK6aOo2",
                    "expanded_url": "https://twitter.com/LovesNandos/status/1211797914437259264/photo/1",
                    "display_url": "pic.twitter.com/yvxdK6aOo2",
                }
            ],
        },
    }

    tweet: Tweet.Tweet = client.get_tweet(
        SAMPLE_TWEET["id"], tweet_fields=[Tweet.Field.ENTITIES]
    )

    assert tweet.id == SAMPLE_TWEET["id"], "Tweet ID is wrong."
    assert tweet.entities, "entities should exist."

    entities: Dict = SAMPLE_TWEET["entities"]

    assert tweet.entities.annotations, "annotations should exist."
    SAMPLE_ANNOTATION: List[Dict] = entities["annotations"]
    for idx, annotation in enumerate(tweet.entities.annotations):
        assert (
            annotation.start == SAMPLE_ANNOTATION[idx]["start"]
        ), f"Annotation{idx}: start is wrong."
        assert (
            annotation.end == SAMPLE_ANNOTATION[idx]["end"]
        ), f"Annotation{idx}: end is wrong."
        assert (
            annotation.probability == SAMPLE_ANNOTATION[idx]["probability"]
        ), f"Annotation{idx}: probability is wrong."
        assert (
            annotation.type == SAMPLE_ANNOTATION[idx]["type"]
        ), f"Annotation{idx}: type is wrong."
        assert (
            annotation.normalized_text == SAMPLE_ANNOTATION[idx]["normalized_text"]
        ), f"Annotation{idx}: normalized_text is wrong."

    assert tweet.entities.urls, "urls should exist."
    SAMPLE_URLS: List[Dict] = entities["urls"]
    for idx, url in enumerate(tweet.entities.urls):
        assert url.start == SAMPLE_URLS[idx]["start"], f"URL[{idx}]: start is wrong."
        assert url.end == SAMPLE_URLS[idx]["end"], f"URL[{idx}]: end is wrong."
        assert url.url == SAMPLE_URLS[idx]["url"], f"URL[{idx}]: url is wrong."
        assert (
            url.expanded_url == SAMPLE_URLS[idx]["expanded_url"]
        ), f"URL[{idx}]: expanded_url is wrong."
        assert (
            url.display_url == SAMPLE_URLS[idx]["display_url"]
        ), f"URL[{idx}]: display_url is wrong."

    assert tweet.entities.cashtags is None, "cashtags should be None."
    assert tweet.entities.hashtags is None, "hashtags should be None."
    assert tweet.entities.mentions is None, "mentions should be None."
Exemple #11
0
def test_not_exist_user_by_username(client: TwitterAPI.TwitterAPI) -> None:

    with pytest.raises(Exception):
        client.get_user_by_username("")
Exemple #12
0
def test_entities_for_user(client: TwitterAPI.TwitterAPI) -> None:

    # ref: https://twitter.com/TwitterDev
    SAMPLE_USER: Dict = {
        # default fields
        "id": "2244994945",
        "name": "Twitter Dev",
        "username": "******",
        # additional fields
        "description": {
            "text": "The voice of the #TwitterDev team and your official source for updates, news, and events, related to the #TwitterAPI.",
            "hashtags": [
                {"start": 17, "end": 28, "tag": "TwitterDev"},
                {"start": 105, "end": 116, "tag": "TwitterAPI"},
            ],
        },
        "url": {
            "start": 0,
            "end": 23,
            "url": "https://t.co/3ZX3TNiZCY",
            "expanded_url": "https://developer.twitter.com/en/community",
            "display_url": "developer.twitter.com/en/community",
        },
    }

    user: User.User = client.get_user_by_id(
        SAMPLE_USER["id"], [User.Field.ENTITIES, User.Field.DESCRIPTION]
    )

    assert user.id == SAMPLE_USER["id"], "User ID is wrong."
    assert user.name == SAMPLE_USER["name"], "name field is wrong."
    assert user.username == SAMPLE_USER["username"], "username field is wrong."

    # description
    assert user.description, "User description should exist."
    assert (
        user.description.text == SAMPLE_USER["description"]["text"]
    ), "description's text is wrong."
    assert user.description.hashtags, "hashtags should exist."
    for idx, hashtag in enumerate(user.description.hashtags):
        assert (
            hashtag.start == SAMPLE_USER["description"]["hashtags"][idx]["start"]
        ), f"hashtags[{idx}]: start is wrong."
        assert (
            hashtag.end == SAMPLE_USER["description"]["hashtags"][idx]["end"]
        ), f"hashtags[{idx}]: end is wrong."
        assert (
            hashtag.tag == SAMPLE_USER["description"]["hashtags"][idx]["tag"]
        ), f"hashtags[{idx}]: tag is wrong."

    # url
    assert isinstance(user.url, Url), "url's type is wrong"

    assert user.url.url == SAMPLE_USER["url"]["url"], "url is wrong."
    assert user.url.start == SAMPLE_USER["url"]["start"], "url start is wrong."
    assert user.url.end == SAMPLE_USER["url"]["end"], "url end is wrong."
    assert (
        user.url.expanded_url == SAMPLE_USER["url"]["expanded_url"]
    ), "expanded_url is wrong."
    assert (
        user.url.display_url == SAMPLE_USER["url"]["display_url"]
    ), "display_url is wrong."