From 6ce39f992140051c52b15fb4920f43c0c8d5da7a Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Tue, 11 Jun 2013 14:41:46 -0400 Subject: [PATCH] Moving tests to their own folder --- .travis.yml | 2 +- tests/__init__.py | 0 tests/config.py | 18 +++++ tests/test_auth.py | 41 ++++++++++++ test_twython.py => tests/test_core.py | 96 ++------------------------- tests/test_streaming.py | 49 ++++++++++++++ 6 files changed, 115 insertions(+), 91 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/config.py create mode 100644 tests/test_auth.py rename test_twython.py => tests/test_core.py (84%) create mode 100644 tests/test_streaming.py diff --git a/.travis.yml b/.travis.yml index 9464162..ef1eb14 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ env: - TEST_TWEET_ID=332992304010899457 - TEST_LIST_ID=574 install: pip install -r requirements.txt -script: nosetests -v --logging-filter="twython" --with-cov --cov twython test_twython.py --cov-report term-missing +script: nosetests -v --logging-filter="twython" --with-cov --cov twython tests/ --cov-report term-missing notifications: email: false branches: diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config.py b/tests/config.py new file mode 100644 index 0000000..30d832f --- /dev/null +++ b/tests/config.py @@ -0,0 +1,18 @@ +import os + +app_key = os.environ.get('APP_KEY') +app_secret = os.environ.get('APP_SECRET') +oauth_token = os.environ.get('OAUTH_TOKEN') +oauth_token_secret = os.environ.get('OAUTH_TOKEN_SECRET') + +screen_name = os.environ.get('SCREEN_NAME', '__twython__') + +# Protected Account you ARE following and they ARE following you +protected_twitter_1 = os.environ.get('PROTECTED_TWITTER_1', 'TwythonSecure1') + +# Protected Account you ARE NOT following +protected_twitter_2 = os.environ.get('PROTECTED_TWITTER_2', 'TwythonSecure2') + +# Test Ids +test_tweet_id = os.environ.get('TEST_TWEET_ID', '318577428610031617') +test_list_id = os.environ.get('TEST_LIST_ID', '574') # 574 is @twitter/team diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 0000000..4ec2506 --- /dev/null +++ b/tests/test_auth.py @@ -0,0 +1,41 @@ +from twython import Twython, TwythonError, TwythonAuthError + +from .config import app_key, app_secret, screen_name + +import unittest + + +class TwythonAuthTestCase(unittest.TestCase): + def setUp(self): + self.api = Twython(app_key, app_secret) + self.bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET') + + self.oauth2_api = Twython(app_key, app_secret, oauth_version=2) + + def test_get_authentication_tokens(self): + """Test getting authentication tokens works""" + self.api.get_authentication_tokens(callback_url='http://google.com/', + force_login=True, + screen_name=screen_name) + + def test_get_authentication_tokens_bad_tokens(self): + """Test getting authentication tokens with bad tokens + raises TwythonAuthError""" + self.assertRaises(TwythonAuthError, self.bad_api.get_authentication_tokens, + callback_url='http://google.com/') + + def test_get_authorized_tokens_bad_tokens(self): + """Test getting final tokens fails with wrong tokens""" + self.assertRaises(TwythonError, self.bad_api.get_authorized_tokens, + 'BAD_OAUTH_VERIFIER') + + def test_get_authentication_tokens_raises_error_when_oauth2(self): + """Test when API is set for OAuth 2, get_authentication_tokens raises + a TwythonError""" + self.assertRaises(TwythonError, self.oauth2_api.get_authentication_tokens) + + def test_get_authorization_tokens_raises_error_when_oauth2(self): + """Test when API is set for OAuth 2, get_authentication_tokens raises + a TwythonError""" + self.assertRaises(TwythonError, self.oauth2_api.get_authorized_tokens, + 'BAD_OAUTH_VERIFIER') diff --git a/test_twython.py b/tests/test_core.py similarity index 84% rename from test_twython.py rename to tests/test_core.py index 9a44b75..d49f2ed 100644 --- a/test_twython.py +++ b/tests/test_core.py @@ -1,52 +1,14 @@ -from twython import( - Twython, TwythonStreamer, TwythonError, - TwythonAuthError, TwythonStreamError +from twython import Twython, TwythonError, TwythonAuthError + +from .config import ( + app_key, app_secret, oauth_token, oauth_token_secret, + protected_twitter_1, protected_twitter_2, screen_name, + test_tweet_id, test_list_id ) -import os import time import unittest -app_key = os.environ.get('APP_KEY') -app_secret = os.environ.get('APP_SECRET') -oauth_token = os.environ.get('OAUTH_TOKEN') -oauth_token_secret = os.environ.get('OAUTH_TOKEN_SECRET') - -screen_name = os.environ.get('SCREEN_NAME', '__twython__') - -# Protected Account you ARE following and they ARE following you -protected_twitter_1 = os.environ.get('PROTECTED_TWITTER_1', 'TwythonSecure1') - -# Protected Account you ARE NOT following -protected_twitter_2 = os.environ.get('PROTECTED_TWITTER_2', 'TwythonSecure2') - -# Test Ids -test_tweet_id = os.environ.get('TEST_TWEET_ID', '318577428610031617') -test_list_id = os.environ.get('TEST_LIST_ID', '574') # 574 is @twitter/team - - -class TwythonAuthTestCase(unittest.TestCase): - def setUp(self): - self.api = Twython(app_key, app_secret) - self.bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET') - - def test_get_authentication_tokens(self): - """Test getting authentication tokens works""" - self.api.get_authentication_tokens(callback_url='http://google.com/', - force_login=True, - screen_name=screen_name) - - def test_get_authentication_tokens_bad_tokens(self): - """Test getting authentication tokens with bad tokens - raises TwythonAuthError""" - self.assertRaises(TwythonAuthError, self.bad_api.get_authentication_tokens, - callback_url='http://google.com/') - - def test_get_authorized_tokens_bad_tokens(self): - """Test getting final tokens fails with wrong tokens""" - self.assertRaises(TwythonError, self.bad_api.get_authorized_tokens, - 'BAD_OAUTH_VERIFIER') - class TwythonAPITestCase(unittest.TestCase): def setUp(self): @@ -467,49 +429,3 @@ class TwythonAPITestCase(unittest.TestCase): """Test getting the locations that Twitter has trending topic information for, closest to a specified location succeeds""" self.api.get_closest_trends(lat='37', long='-122') - - -class TwythonStreamTestCase(unittest.TestCase): - def setUp(self): - class MyStreamer(TwythonStreamer): - def on_success(self, data): - self.disconnect() - - def on_error(self, status_code, data): - raise TwythonStreamError(data) - - def on_delete(self, data): - return - - def on_limit(self, data): - return - - def on_disconnect(self, data): - return - - def on_timeout(self, data): - return - - self.api = MyStreamer(app_key, app_secret, - oauth_token, oauth_token_secret) - - def test_stream_status_filter(self): - self.api.statuses.filter(track='twitter') - - def test_stream_status_sample(self): - self.api.statuses.sample() - - def test_stream_status_firehose(self): - self.assertRaises(TwythonStreamError, self.api.statuses.firehose, - track='twitter') - - def test_stream_site(self): - self.assertRaises(TwythonStreamError, self.api.site, - follow='twitter') - - def test_stream_user(self): - self.api.user(track='twitter') - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_streaming.py b/tests/test_streaming.py new file mode 100644 index 0000000..c6b83bc --- /dev/null +++ b/tests/test_streaming.py @@ -0,0 +1,49 @@ +from twython import TwythonStreamer, TwythonStreamError + +from .config import ( + app_key, app_secret, oauth_token, oauth_token_secret +) + +import unittest + + +class TwythonStreamTestCase(unittest.TestCase): + def setUp(self): + class MyStreamer(TwythonStreamer): + def on_success(self, data): + self.disconnect() + + def on_error(self, status_code, data): + raise TwythonStreamError(data) + + def on_delete(self, data): + return + + def on_limit(self, data): + return + + def on_disconnect(self, data): + return + + def on_timeout(self, data): + return + + self.api = MyStreamer(app_key, app_secret, + oauth_token, oauth_token_secret) + + def test_stream_status_filter(self): + self.api.statuses.filter(track='twitter') + + def test_stream_status_sample(self): + self.api.statuses.sample() + + def test_stream_status_firehose(self): + self.assertRaises(TwythonStreamError, self.api.statuses.firehose, + track='twitter') + + def test_stream_site(self): + self.assertRaises(TwythonStreamError, self.api.site, + follow='twitter') + + def test_stream_user(self): + self.api.user(track='twitter')