Skip to content

Commit

Permalink
feat: twitter langchain gha linter (#56)
Browse files Browse the repository at this point in the history
* first pass implementing twitter langchain gha linter

* formatting and refining the twitter langchain test suite
  • Loading branch information
stat authored Nov 25, 2024
1 parent 222f205 commit 8e2927f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,37 @@ jobs:

- name: Run linters
run: poetry run make lint

lint-twitter-langchain:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./twitter-langchain
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: latest
virtualenvs-create: true
virtualenvs-in-project: true

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: ./twitter-langchain/.venv
key: venv-twitter-langchain-${{ runner.os }}-${{ hashFiles('twitter-langchain/poetry.lock') }}

- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --with dev

- name: Run linters
run: poetry run make lint
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 8e2927f

Please sign in to comment.