From 6227e09cdc9b745bbde7995a9c6954d7126e26c3 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Thu, 21 Nov 2024 12:04:02 -0800 Subject: [PATCH 1/2] first pass implementing twitter langchain gha linter --- .github/workflows/lint.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 456385ec4..d0430770c 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 From 18f3120646905ca9cfc4f974d5243b9676e920ab Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Mon, 25 Nov 2024 00:24:15 -0800 Subject: [PATCH 2/2] formatting and refining the twitter langchain test suite --- .../tests/actions/test_account_details.py | 7 +++++-- .../tests/actions/test_account_mentions.py | 10 ++++++++-- twitter-langchain/tests/actions/test_post_tweet.py | 11 +++++++---- .../tests/actions/test_post_tweet_reply.py | 14 ++++++++++---- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/twitter-langchain/tests/actions/test_account_details.py b/twitter-langchain/tests/actions/test_account_details.py index e590179b1..d1715583c 100644 --- a/twitter-langchain/tests/actions/test_account_details.py +++ b/twitter-langchain/tests/actions/test_account_details.py @@ -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": { @@ -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") @@ -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() diff --git a/twitter-langchain/tests/actions/test_account_mentions.py b/twitter-langchain/tests/actions/test_account_mentions.py index a07f59244..7e3098348 100644 --- a/twitter-langchain/tests/actions/test_account_mentions.py +++ b/twitter-langchain/tests/actions/test_account_mentions.py @@ -10,6 +10,7 @@ MOCK_ACCOUNT_ID = "1234" + def test_account_mentions_input_model_valid(): """Test that AccountMentionsInput accepts valid parameters.""" input_model = AccountMentionsInput( @@ -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": [ @@ -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") @@ -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) diff --git a/twitter-langchain/tests/actions/test_post_tweet.py b/twitter-langchain/tests/actions/test_post_tweet.py index 25df5c8db..8bb62169b 100644 --- a/twitter-langchain/tests/actions/test_post_tweet.py +++ b/twitter-langchain/tests/actions/test_post_tweet.py @@ -10,6 +10,7 @@ MOCK_TWEET = "hello, world!" + def test_post_tweet_input_model_valid(): """Test that PostTweetInput accepts valid parameters.""" input_model = PostTweetInput( @@ -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": { @@ -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") @@ -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) diff --git a/twitter-langchain/tests/actions/test_post_tweet_reply.py b/twitter-langchain/tests/actions/test_post_tweet_reply.py index 59e9d03f5..54c842b30 100644 --- a/twitter-langchain/tests/actions/test_post_tweet_reply.py +++ b/twitter-langchain/tests/actions/test_post_tweet_reply.py @@ -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( @@ -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": { @@ -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") @@ -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, + )