Commit 4da98dee authored by Joshua Roesslein's avatar Joshua Roesslein
Browse files

Create a common test case class.

- Sets up authentication.
- Handles recording or replaying HTTP traffic.
parent afe19c28
import os import os
from unittest import TestCase
from httreplay import start_replay, stop_replay
from httreplay.utils import filter_headers_key
from tweepy.auth import OAuthHandler from tweepy.auth import OAuthHandler
from tweepy.api import API
username = os.environ.get('TWITTER_USERNAME', '') username = os.environ.get('TWITTER_USERNAME', '')
oauth_consumer_key = os.environ.get('CONSUMER_KEY', '') oauth_consumer_key = os.environ.get('CONSUMER_KEY', '')
oauth_consumer_secret = os.environ.get('CONSUMER_SECRET', '') oauth_consumer_secret = os.environ.get('CONSUMER_SECRET', '')
oauth_token = os.environ.get('ACCESS_KEY', '') oauth_token = os.environ.get('ACCESS_KEY', '')
oauth_token_secret = os.environ.get('ACCESS_SECRET', '') oauth_token_secret = os.environ.get('ACCESS_SECRET', '')
use_replay = os.environ.get('USE_REPLAY', False)
class TweepyTestCase(TestCase):
def setUp(self):
self.auth = create_auth()
self.api = API(self.auth)
self.api.retry_count = 2
self.api.retry_delay = 5
if use_replay:
start_replay('tests/record.json',
headers_key=filter_headers_key(['Authorization']))
def tearDown(self):
if use_replay:
stop_replay()
def create_auth(): def create_auth():
auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret) auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
......
This diff is collapsed.
...@@ -5,15 +5,11 @@ import os ...@@ -5,15 +5,11 @@ import os
from nose import SkipTest from nose import SkipTest
from tweepy import (API, OAuthHandler, Friendship, Cursor, from tweepy import Friendship, MemoryCache, FileCache
MemoryCache, FileCache) from config import TweepyTestCase, username, use_replay
from config import *
from httreplay import start_replay, stop_replay
from httreplay.utils import filter_headers_key
import os
test_tweet_id = '266367358078169089' test_tweet_id = '266367358078169089'
tweet_text = 'testing 1000'
"""Unit tests""" """Unit tests"""
...@@ -30,19 +26,7 @@ class TweepyErrorTests(unittest.TestCase): ...@@ -30,19 +26,7 @@ class TweepyErrorTests(unittest.TestCase):
self.assertEqual(e.reason, e2.reason) self.assertEqual(e.reason, e2.reason)
self.assertEqual(e.response, e2.response) self.assertEqual(e.response, e2.response)
class TweepyAPITests(unittest.TestCase): class TweepyAPITests(TweepyTestCase):
def setUp(self):
if os.environ['TRAVIS_SECURE_ENV_VARS'] == 'false':
start_replay('records/test.json', headers_key=filter_headers_key(['Authorization']))
auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
auth.set_access_token(oauth_token, oauth_token_secret)
self.api = API(auth)
self.api.retry_count = 2
self.api.retry_delay = 5
def tearDown(self):
stop_replay()
# TODO: Actually have some sort of better assertion # TODO: Actually have some sort of better assertion
def testgetoembed(self): def testgetoembed(self):
...@@ -75,7 +59,7 @@ class TweepyAPITests(unittest.TestCase): ...@@ -75,7 +59,7 @@ class TweepyAPITests(unittest.TestCase):
def testupdateanddestroystatus(self): def testupdateanddestroystatus(self):
# test update # test update
text = 'testing %i' % random.randint(0, 1000) text = tweet_text if use_replay else 'testing %i' % random.randint(0, 1000)
update = self.api.update_status(status=text) update = self.api.update_status(status=text)
self.assertEqual(update.text, text) self.assertEqual(update.text, text)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment