-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pytest scripts and requirements.txt
- Loading branch information
1 parent
4cd214c
commit 2252ee3
Showing
5 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a single version of Python | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python | ||
|
||
name: Python application | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python 3.10 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.10" | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Test with pytest | ||
run: | | ||
py -m pytest py_test.py | ||
env: | ||
TESTING: ${{ secrets.TESTING || 'true' }} | ||
BEARER_TOKEN: ${{ secrets.BEARER_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.env | ||
__pycache__/ | ||
output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import pytest | ||
from unittest.mock import Mock, patch, mock_open | ||
from stream_listener import fetch_tweets, get_user_id | ||
import datetime | ||
import re | ||
import unittest | ||
from freezegun import freeze_time | ||
|
||
author_name = 'LotsoFandom' | ||
author_id = '1742141344787857408' | ||
tweet_id = '1777555856705667262' | ||
|
||
# Test for get_user_id function | ||
def test_get_user_id(): | ||
with patch('stream_listener.client') as mocked_client: | ||
mocked_client.get_user.return_value = Mock(data=Mock(id=author_id)) | ||
user_id = get_user_id(author_name) | ||
assert user_id == author_id | ||
mocked_client.get_user.assert_called_once_with(username='LotsoFandom') | ||
|
||
# Test for fetch_tweets function | ||
@freeze_time("2023-04-23 01:01:01") | ||
def test_fetch_tweets(): | ||
with patch('stream_listener.client') as mocked_client, \ | ||
patch('builtins.open', mock_open(), create=True) as mocked_file: # Note the create=True for broader patching | ||
# Setup the mock return value for tweets | ||
mocked_client.get_users_tweets.return_value = Mock( | ||
data=[Mock(id=tweet_id, author_id=author_id, created_at='2023-01-01', text='Sample Tweet')] | ||
) | ||
|
||
seen_tweets = set() | ||
|
||
# Action | ||
fetch_tweets(author_id, seen_tweets, 1) | ||
|
||
# Assert | ||
assert tweet_id in seen_tweets | ||
mocked_client.get_users_tweets.assert_called_once_with( | ||
id=author_id, | ||
max_results=1, | ||
exclude='replies', | ||
tweet_fields=["created_at", "text", "author_id"] | ||
) | ||
|
||
# Assert that the file was attempted to be opened with the correct parameters | ||
filename = f"output/{author_id}_{tweet_id}_20230423010101.md" | ||
mocked_file.assert_called_once_with(filename, 'w', encoding='utf-8') | ||
|
||
# Check the content written to the file | ||
mocked_file().write.assert_called_once_with( | ||
"## Tweet by 1742141344787857408\n\n" | ||
"**Tweet ID:** 1777555856705667262\n" | ||
"**Created at:** 2023-01-01\n" | ||
"**Text:**\n\n" | ||
"Sample Tweet\n" | ||
) | ||
|
||
# Test for handling too many tweets requested | ||
def test_fetch_tweets_too_many(): | ||
with patch('stream_listener.client') as mocked_client: | ||
fetch_tweets(author_id, set(), 150) | ||
mocked_client.get_users_tweets.assert_called_with( | ||
id=author_id, | ||
max_results=100, | ||
exclude='replies', | ||
tweet_fields=["created_at", "text", "author_id"] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tweepy | ||
python-dotenv | ||
pytest | ||
freezegun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters