Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for the utils available. #10

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/__init__.py
Empty file.
Empty file added tests/utils/__init__.py
Empty file.
Empty file added tests/utils/gsheet/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions tests/utils/gsheet/test_gsheet_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unittest import TestCase
from test.support import EnvironmentVarGuard

from utils.gsheet.helper import GoogleSheetHelper
class TestGsheetHelper(TestCase):
def setUp(self):
self.env = EnvironmentVarGuard()
self.env.clear()

def test_google_credentials_required(self):
"""
Test to ensure that GOOGLE_CREDENTIALS are always set in the environment variables
while creating a GoogleSheetHelper.
"""
with self.assertRaises(ValueError):
GoogleSheetHelper()

def test_google_credentials_provided_has_required_values(self):
"""
Test to ensure the GOOGLE_CREDENTIALS provided has required values e.g
sheet_name
"""
self.env.set('GOOGLE_CREDENTIALS', "{}")
with self.assertRaises(ValueError):
GoogleSheetHelper()
Empty file added tests/utils/twitter/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions tests/utils/twitter/test_stream_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from unittest import TestCase
from test.support import EnvironmentVarGuard

from utils.twitter.stream_listener import Listener


class TestTweeterStream(TestCase):
def setUp(self):
self.env = EnvironmentVarGuard()
self.env.clear()

def test_keys_required_for_twitter_authorization_are_provided(self):
"""
Test to ensure that keys required for twitter authoization i.e
CLIENT_KEY, CLIENT_SECRET, ACCESS_TOKEN and ACCESS_SECRET
are always set in the environment variables
while creating a StreamListener.
"""
with self.assertRaises(ValueError):
Listener()
10 changes: 8 additions & 2 deletions utils/gsheet/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ def __init__(self) -> None:
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive'
]
credentials = os.getenv('GOOGLE_CREDENTIALS')
if not credentials:
raise ValueError("GOOGLE_CREDENTIALS must be set as an environment variable.")
google_credentials = json.loads(
eval(os.getenv('GOOGLE_CREDENTIALS')), strict=False)
credentials, strict=False)
sheet_name = google_credentials.get('sheet_name')
if not sheet_name:
raise ValueError("Sheet name has not been set in the GOOGLE_CREDENTIALS environment variable.")
self.__credentials = ServiceAccountCredentials.from_json_keyfile_dict(
google_credentials, scopes=self.__scope)
self.__client = gspread.authorize(self.__credentials)
self.__sheet_name = google_credentials['sheet_name']
self.__sheet_name = sheet_name
self.__sheet = self.__client.open(self.__sheet_name).sheet1

def open_sheet(self) -> Optional[dict]:
Expand Down
17 changes: 15 additions & 2 deletions utils/twitter/stream_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@ class Listener(StreamListener):

def __init__(self):
super(Listener, self).__init__()
self.__auth = OAuthHandler(os.getenv('CLIENT_KEY'), os.getenv('CLIENT_SECRET')) # type: OAuthHandler
self.__auth.set_access_token(os.getenv('ACCESS_TOKEN'), os.getenv('ACCESS_SECRET'))
client_key = os.getenv('CLIENT_KEY')
client_secret = os.getenv('CLIENT_SECRET')
access_token = os.getenv('ACCESS_TOKEN')
access_secret = os.getenv('ACCESS_SECRET')

if not client_key:
raise ValueError("CLIENT_KEY not set as an environment variable.")
if not client_secret:
raise ValueError("CLIENT_SECRET not set as an environment variable.")
if not access_token:
raise ValueError("ACCESS_TOKEN not set as an environment variable.")
if not access_secret:
raise ValueError("ACCESS_SECRET not set as an environment variable.")
self.__auth = OAuthHandler(client_key, client_secret) # type: OAuthHandler
self.__auth.set_access_token(access_token, access_secret)
self.__stream = [] # type: list

@property
Expand Down