Skip to content

Commit

Permalink
formatting and refining the twitter langchain test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
stat committed Nov 25, 2024
1 parent 6227e09 commit 18f3120
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
7 changes: 5 additions & 2 deletions twitter-langchain/tests/actions/test_account_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


def test_account_details_success(tweepy_factory):
"""Test successful retrievial of the authenticated Twitter (X) account."""
mock_client = tweepy_factory()
mock_client_result = {
"data": {
Expand All @@ -25,14 +26,15 @@ def test_account_details_success(tweepy_factory):
expected_result["data"]["url"] = f"https://x.com/{MOCK_USERNAME}"
expected_response = f"Successfully retrieved authenticated user account details:\n{dumps(expected_result)}"

with patch.object(mock_client, "get_me", return_value=mock_client_result) as mock_get_me:
with patch.object(mock_client, "get_me", return_value=mock_client_result) as mock_tweepy_get_me:
response = account_details(mock_client)

assert response == expected_response
mock_get_me.assert_called_once_with()
mock_tweepy_get_me.assert_called_once_with()


def test_account_details_failure(tweepy_factory):
"""Test failure when an API error occurs."""
mock_client = tweepy_factory()

expected_result = tweepy.errors.TweepyException("Tweepy Error")
Expand All @@ -42,3 +44,4 @@ def test_account_details_failure(tweepy_factory):
response = account_details(mock_client)

assert response == expected_response
mock_tweepy_get_me.assert_called_once_with()
10 changes: 8 additions & 2 deletions twitter-langchain/tests/actions/test_account_mentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

MOCK_ACCOUNT_ID = "1234"


def test_account_mentions_input_model_valid():
"""Test that AccountMentionsInput accepts valid parameters."""
input_model = AccountMentionsInput(
Expand All @@ -24,7 +25,9 @@ def test_account_mentions_input_model_missing_params():
with pytest.raises(ValueError):
AccountMentionsInput()


def test_account_mentions_success(tweepy_factory):
"""Test successful retrieval of the authenticated Twitter (X) account's mentions."""
mock_client = tweepy_factory()
mock_client_result = {
"data": [
Expand All @@ -43,13 +46,15 @@ def test_account_mentions_success(tweepy_factory):

expected_response = f"Successfully retrieved authenticated user account mentions:\n{dumps(mock_client_result)}"

with patch.object(mock_client, "get_users_mentions", return_value=mock_client_result) as mock_get_users_mentions:
with patch.object(mock_client, "get_users_mentions", return_value=mock_client_result) as mock_tweepy_get_users_mentions:
response = account_mentions(mock_client, MOCK_ACCOUNT_ID)

assert response == expected_response
mock_get_users_mentions.assert_called_once_with(MOCK_ACCOUNT_ID)
mock_tweepy_get_users_mentions.assert_called_once_with(MOCK_ACCOUNT_ID)


def test_account_mentions_failure(tweepy_factory):
"""Test failure when an API error occurs."""
mock_client = tweepy_factory()

expected_result = tweepy.errors.TweepyException("Tweepy Error")
Expand All @@ -58,3 +63,4 @@ def test_account_mentions_failure(tweepy_factory):
with patch.object(mock_client, "get_users_mentions", side_effect=expected_result) as mock_get_users_mentions:
response = account_mentions(mock_client, MOCK_ACCOUNT_ID)
assert response == expected_response
mock_get_users_mentions.assert_called_once_with(MOCK_ACCOUNT_ID)
11 changes: 7 additions & 4 deletions twitter-langchain/tests/actions/test_post_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

MOCK_TWEET = "hello, world!"


def test_post_tweet_input_model_valid():
"""Test that PostTweetInput accepts valid parameters."""
input_model = PostTweetInput(
Expand All @@ -24,7 +25,9 @@ def test_post_tweet_input_model_missing_params():
with pytest.raises(ValueError):
PostTweetInput()


def test_post_tweet_success(tweepy_factory):
"""Test successful tweet post to the authenticated Twitter (X) account."""
mock_client = tweepy_factory()
mock_client_result = {
"data": {
Expand All @@ -36,14 +39,15 @@ def test_post_tweet_success(tweepy_factory):

expected_response = f"Successfully posted to Twitter:\n{dumps(mock_client_result)}"

with patch.object(mock_client, "create_tweet", return_value=mock_client_result) as mock_create_tweet:
with patch.object(mock_client, "create_tweet", return_value=mock_client_result) as mock_tweepy_create_tweet:
response = post_tweet(mock_client, MOCK_TWEET)

assert response == expected_response
mock_create_tweet.assert_called_once_with(text=MOCK_TWEET)
mock_tweepy_create_tweet.assert_called_once_with(text=MOCK_TWEET)


def test_post_tweet_failure(tweepy_factory):
"""Test failure when an API error occurs."""
mock_client = tweepy_factory()

expected_result = tweepy.errors.TweepyException("Tweepy Error")
Expand All @@ -52,5 +56,4 @@ def test_post_tweet_failure(tweepy_factory):
with patch.object(mock_client, "create_tweet", side_effect=expected_result) as mock_tweepy_create_tweet:
response = post_tweet(mock_client, MOCK_TWEET)
assert response == expected_response


mock_tweepy_create_tweet.assert_called_once_with(text=MOCK_TWEET)
14 changes: 10 additions & 4 deletions twitter-langchain/tests/actions/test_post_tweet_reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
MOCK_TWEET_ID = "1234"
MOCK_TWEET_REPLY = "So good to be here!"


def test_post_tweet_input_model_valid():
"""Test that PostTweetReplyInput accepts valid parameters."""
input_model = PostTweetReplyInput(
Expand All @@ -27,7 +28,9 @@ def test_post_tweet_input_model_missing_params():
with pytest.raises(ValueError):
PostTweetReplyInput()


def test_post_tweet_success(tweepy_factory):
"""Test successful reply to a Twitter (X) post."""
mock_client = tweepy_factory()
mock_client_result = {
"data": {
Expand All @@ -39,17 +42,18 @@ def test_post_tweet_success(tweepy_factory):

expected_response = f"Successfully posted reply to Twitter:\n{dumps(mock_client_result)}"

with patch.object(mock_client, "create_tweet", return_value=mock_client_result) as mock_create_tweet:
with patch.object(mock_client, "create_tweet", return_value=mock_client_result) as mock_tweepy_create_tweet:
response = post_tweet_reply(mock_client, MOCK_TWEET_ID, MOCK_TWEET_REPLY)

assert response == expected_response
mock_create_tweet.assert_called_once_with(
mock_tweepy_create_tweet.assert_called_once_with(
in_reply_to_tweet_id=MOCK_TWEET_ID,
text=MOCK_TWEET_REPLY,
)


def test_post_tweet_failure(tweepy_factory):
"""Test failure when an API error occurs."""
mock_client = tweepy_factory()

expected_result = tweepy.errors.TweepyException("Tweepy Error")
Expand All @@ -58,5 +62,7 @@ def test_post_tweet_failure(tweepy_factory):
with patch.object(mock_client, "create_tweet", side_effect=expected_result) as mock_tweepy_create_tweet:
response = post_tweet_reply(mock_client, MOCK_TWEET_ID, MOCK_TWEET_REPLY)
assert response == expected_response


mock_tweepy_create_tweet.assert_called_once_with(
in_reply_to_tweet_id=MOCK_TWEET_ID,
text=MOCK_TWEET_REPLY,
)

0 comments on commit 18f3120

Please sign in to comment.