More tests, coverage, and excluding lines from being covered
There are some lines that will never be hit in tests, excluding those from being covered!
This commit is contained in:
parent
00c0bd91a6
commit
acdf73a04e
6 changed files with 45 additions and 10 deletions
|
|
@ -11,6 +11,8 @@ class TwythonAuthTestCase(unittest.TestCase):
|
||||||
self.bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET')
|
self.bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET')
|
||||||
|
|
||||||
self.oauth2_api = Twython(app_key, app_secret, oauth_version=2)
|
self.oauth2_api = Twython(app_key, app_secret, oauth_version=2)
|
||||||
|
self.oauth2_bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET',
|
||||||
|
oauth_version=2)
|
||||||
|
|
||||||
def test_get_authentication_tokens(self):
|
def test_get_authentication_tokens(self):
|
||||||
"""Test getting authentication tokens works"""
|
"""Test getting authentication tokens works"""
|
||||||
|
|
@ -44,6 +46,11 @@ class TwythonAuthTestCase(unittest.TestCase):
|
||||||
"""Test obtaining an Application Only OAuth 2 access token succeeds"""
|
"""Test obtaining an Application Only OAuth 2 access token succeeds"""
|
||||||
self.oauth2_api.obtain_access_token()
|
self.oauth2_api.obtain_access_token()
|
||||||
|
|
||||||
|
def test_obtain_access_token_bad_tokens(self):
|
||||||
|
"""Test obtaining an Application Only OAuth 2 access token using bad app tokens fails"""
|
||||||
|
self.assertRaises(TwythonAuthError,
|
||||||
|
self.oauth2_bad_api.obtain_access_token)
|
||||||
|
|
||||||
def test_obtain_access_token_raises_error_when_oauth1(self):
|
def test_obtain_access_token_raises_error_when_oauth1(self):
|
||||||
"""Test when API is set for OAuth 1, obtain_access_token raises a
|
"""Test when API is set for OAuth 1, obtain_access_token raises a
|
||||||
TwythonError"""
|
TwythonError"""
|
||||||
|
|
|
||||||
|
|
@ -84,10 +84,28 @@ class TwythonAPITestCase(unittest.TestCase):
|
||||||
self.api.encode('Twython is awesome!')
|
self.api.encode('Twython is awesome!')
|
||||||
|
|
||||||
def test_html_for_tweet(self):
|
def test_html_for_tweet(self):
|
||||||
"""Test HTML for Twitter returns what we want"""
|
"""Test HTML for Tweet returns what we want"""
|
||||||
tweet_text = self.api.html_for_tweet(test_tweet_object)
|
tweet_text = self.api.html_for_tweet(test_tweet_object)
|
||||||
self.assertEqual(test_tweet_html, tweet_text)
|
self.assertEqual(test_tweet_html, tweet_text)
|
||||||
|
|
||||||
|
def test_html_for_tweet_expanded_url(self):
|
||||||
|
"""Test using expanded url in HTML for Tweet displays full urls"""
|
||||||
|
tweet_text = self.api.html_for_tweet(test_tweet_object,
|
||||||
|
use_expanded_url=True)
|
||||||
|
# Make sure full url is in HTML
|
||||||
|
self.assertTrue('http://google.com' in tweet_text)
|
||||||
|
|
||||||
|
def test_html_for_tweet_short_url(self):
|
||||||
|
"""Test using expanded url in HTML for Tweet displays full urls"""
|
||||||
|
tweet_text = self.api.html_for_tweet(test_tweet_object, False)
|
||||||
|
# Make sure HTML doesn't contain the display OR exapanded url
|
||||||
|
self.assertTrue(not 'http://google.com' in tweet_text)
|
||||||
|
self.assertTrue(not 'google.com' in tweet_text)
|
||||||
|
|
||||||
|
def test_raise_error_on_bad_ssl_cert(self):
|
||||||
|
"""Test TwythonError is raised by a RequestException when an actual HTTP happens"""
|
||||||
|
self.assertRaises(TwythonError, self.api.get, 'https://example.com')
|
||||||
|
|
||||||
# Timelines
|
# Timelines
|
||||||
def test_get_mentions_timeline(self):
|
def test_get_mentions_timeline(self):
|
||||||
"""Test returning mentions timeline for authenticated user succeeds"""
|
"""Test returning mentions timeline for authenticated user succeeds"""
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,16 @@ class TwythonStreamTestCase(unittest.TestCase):
|
||||||
self.api = MyStreamer(app_key, app_secret,
|
self.api = MyStreamer(app_key, app_secret,
|
||||||
oauth_token, oauth_token_secret)
|
oauth_token, oauth_token_secret)
|
||||||
|
|
||||||
|
client_args = {
|
||||||
|
'headers': {
|
||||||
|
'User-Agent': '__twython__ Stream Test'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# Initialize with header for coverage checking for User-Agent
|
||||||
|
self.api_with_header = MyStreamer(app_key, app_secret,
|
||||||
|
oauth_token, oauth_token_secret,
|
||||||
|
client_args=client_args)
|
||||||
|
|
||||||
def test_stream_status_filter(self):
|
def test_stream_status_filter(self):
|
||||||
self.api.statuses.filter(track='twitter')
|
self.api.statuses.filter(track='twitter')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -169,13 +169,13 @@ class Twython(EndpointsMixin, object):
|
||||||
if errors and isinstance(errors, list):
|
if errors and isinstance(errors, list):
|
||||||
error_message = errors[0]['message']
|
error_message = errors[0]['message']
|
||||||
else:
|
else:
|
||||||
error_message = errors
|
error_message = errors # pragma: no cover
|
||||||
self._last_call['api_error'] = error_message
|
self._last_call['api_error'] = error_message
|
||||||
|
|
||||||
ExceptionType = TwythonError
|
ExceptionType = TwythonError
|
||||||
if response.status_code == 429:
|
if response.status_code == 429:
|
||||||
# Twitter API 1.1, always return 429 when rate limit is exceeded
|
# Twitter API 1.1, always return 429 when rate limit is exceeded
|
||||||
ExceptionType = TwythonRateLimitError
|
ExceptionType = TwythonRateLimitError # pragma: no cover
|
||||||
elif response.status_code == 401 or 'Bad Authentication data' in error_message:
|
elif response.status_code == 401 or 'Bad Authentication data' in error_message:
|
||||||
# Twitter API 1.1, returns a 401 Unauthorized or
|
# Twitter API 1.1, returns a 401 Unauthorized or
|
||||||
# a 400 "Bad Authentication data" for invalid/expired app keys/user tokens
|
# a 400 "Bad Authentication data" for invalid/expired app keys/user tokens
|
||||||
|
|
@ -186,7 +186,7 @@ class Twython(EndpointsMixin, object):
|
||||||
retry_after=response.headers.get('retry-after'))
|
retry_after=response.headers.get('retry-after'))
|
||||||
|
|
||||||
# if we have a json error here, then it's not an official Twitter API error
|
# if we have a json error here, then it's not an official Twitter API error
|
||||||
if json_error and not response.status_code in (200, 201, 202):
|
if json_error and not response.status_code in (200, 201, 202): # pragma: no cover
|
||||||
raise TwythonError('Response was not valid JSON, unable to decode.')
|
raise TwythonError('Response was not valid JSON, unable to decode.')
|
||||||
|
|
||||||
return content
|
return content
|
||||||
|
|
@ -303,7 +303,7 @@ class Twython(EndpointsMixin, object):
|
||||||
if not authorized_tokens:
|
if not authorized_tokens:
|
||||||
raise TwythonError('Unable to decode authorized tokens.')
|
raise TwythonError('Unable to decode authorized tokens.')
|
||||||
|
|
||||||
return authorized_tokens
|
return authorized_tokens # pragma: no cover
|
||||||
|
|
||||||
def obtain_access_token(self):
|
def obtain_access_token(self):
|
||||||
"""Returns an OAuth 2 access token to make OAuth 2 authenticated read-only calls.
|
"""Returns an OAuth 2 access token to make OAuth 2 authenticated read-only calls.
|
||||||
|
|
@ -387,7 +387,7 @@ class Twython(EndpointsMixin, object):
|
||||||
try:
|
try:
|
||||||
if not 'since_id' in params:
|
if not 'since_id' in params:
|
||||||
params['since_id'] = (int(content['statuses'][0]['id_str']) + 1)
|
params['since_id'] = (int(content['statuses'][0]['id_str']) + 1)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError): # pragma: no cover
|
||||||
raise TwythonError('Unable to generate next page of search results, `page` is not a number.')
|
raise TwythonError('Unable to generate next page of search results, `page` is not a number.')
|
||||||
|
|
||||||
for tweet in self.search_gen(search_query, **params):
|
for tweet in self.search_gen(search_query, **params):
|
||||||
|
|
|
||||||
|
|
@ -748,7 +748,7 @@ class EndpointsMixin(object):
|
||||||
return self.get('trends/closest', params=params)
|
return self.get('trends/closest', params=params)
|
||||||
|
|
||||||
# Spam Reporting
|
# Spam Reporting
|
||||||
def report_spam(self, **params):
|
def report_spam(self, **params): # pragma: no cover
|
||||||
"""Report the specified user as a spam account to Twitter.
|
"""Report the specified user as a spam account to Twitter.
|
||||||
|
|
||||||
Docs: https://dev.twitter.com/docs/api/1.1/post/users/report_spam
|
Docs: https://dev.twitter.com/docs/api/1.1/post/users/report_spam
|
||||||
|
|
@ -757,7 +757,7 @@ class EndpointsMixin(object):
|
||||||
return self.post('users/report_spam', params=params)
|
return self.post('users/report_spam', params=params)
|
||||||
|
|
||||||
# OAuth
|
# OAuth
|
||||||
def invalidate_token(self, **params):
|
def invalidate_token(self, **params): # pragma: no cover
|
||||||
"""Allows a registered application to revoke an issued OAuth 2 Bearer
|
"""Allows a registered application to revoke an issued OAuth 2 Bearer
|
||||||
Token by presenting its client credentials.
|
Token by presenting its client credentials.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -132,13 +132,13 @@ class TwythonStreamer(object):
|
||||||
if is_py3:
|
if is_py3:
|
||||||
line = line.decode('utf-8')
|
line = line.decode('utf-8')
|
||||||
data = json.loads(line)
|
data = json.loads(line)
|
||||||
if self.on_success(data):
|
if self.on_success(data): # pragma: no cover
|
||||||
for message_type in self.handlers:
|
for message_type in self.handlers:
|
||||||
if message_type in data:
|
if message_type in data:
|
||||||
handler = getattr(self, 'on_' + message_type, None)
|
handler = getattr(self, 'on_' + message_type, None)
|
||||||
if handler and callable(handler) and not handler(data.get(message_type)):
|
if handler and callable(handler) and not handler(data.get(message_type)):
|
||||||
break
|
break
|
||||||
except ValueError:
|
except ValueError: # pragma: no cover
|
||||||
self.on_error(response.status_code, 'Unable to decode response, not valid JSON.')
|
self.on_error(response.status_code, 'Unable to decode response, not valid JSON.')
|
||||||
|
|
||||||
response.close()
|
response.close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue