Exemplo n.º 1
0
 def setUp(self):
     super(TestTweepyErrorCache, self).setUp()
     self.monkey = MonkeyPatch()
Exemplo n.º 2
0
class TestTweepyErrorCache(PyMockTestCase):

    def setUp(self):
        super(TestTweepyErrorCache, self).setUp()
        self.monkey = MonkeyPatch()

    def tearDown(self):
        super(TestTweepyErrorCache, self).tearDown()
        self.monkey.undo()

    def test_ratelimit_key(self):
        #APIMethod is hidden => makes it difficult to test, or I don't know enough about python, please educate ([email protected])
        #I'm duplicating the RateLimit test to test auth and noauth case of _ratelimit_key
        pass

    class HTTPResponse(object):
        status = 400
        def read(self):
            return {'error':'errror'}

    class HTTPConnection(httplib.HTTPConnection):
        def request(self, method, url, headers=None, body=None):
            pass
        def getresponse(self):
            return TestTweepyErrorCache.HTTPResponse()

    def test_RateLimited(self):

        #monkey http connection -> stubs
        self.monkey.setattr(httplib, 'HTTPConnection', TestTweepyErrorCache.HTTPConnection)

        #errorCache mock
        errorCacheMock = mock()

        #first call, get nothing
        self.expectAndReturn(errorCacheMock.get('tweepy.errorcache:unauthenticated'), None)
        #second call cache
        errorCacheMock.set('tweepy.errorcache:unauthenticated', 3600, time=3600)
        #third call, second request, get something
        self.expectAndReturn(errorCacheMock.get('tweepy.errorcache:unauthenticated'), 3600)

        self.replay()

        api = API(errorCache=errorCacheMock)
        #first call
        self.assertRaises(tweepy.error.TweepError, api.user_timeline, 'test')
        #second
        self.assertRaises(tweepy.error.CachedRateLimit, api.user_timeline, 'test')

        self.verify()

    def test_RateLimited_OAuth(self):

        #monkey http connection -> stubs
        self.monkey.setattr(httplib, 'HTTPConnection', TestTweepyErrorCache.HTTPConnection)

        #errorCache mock
        errorCacheMock = mock()

        #first call, get nothing
        self.expectAndReturn(errorCacheMock.get('tweepy.errorcache:at'), None)
        #second call cache
        errorCacheMock.set('tweepy.errorcache:at', 3600, time=3600)
        #third call, second request, get something
        self.expectAndReturn(errorCacheMock.get('tweepy.errorcache:at'), 3600)

        self.replay()

        auth = OAuthHandler('ck', 'cks')
        auth.set_access_token('at', 'ats')
        api = API(auth_handler=auth, errorCache=errorCacheMock)
        #first call
        self.assertRaises(tweepy.error.TweepError, api.user_timeline, 'test')
        #second
        self.assertRaises(tweepy.error.CachedRateLimit, api.user_timeline, 'test')

        self.verify()

    def test_RateLimited_BasicAuth(self):

        #monkey http connection -> stubs
        self.monkey.setattr(httplib, 'HTTPConnection', TestTweepyErrorCache.HTTPConnection)

        #errorCache mock
        errorCacheMock = mock()

        #first call, get nothing
        self.expectAndReturn(errorCacheMock.get('tweepy.errorcache:username'), None)
        #second call cache
        errorCacheMock.set('tweepy.errorcache:username', 3600, time=3600)
        #third call, second request, get something
        self.expectAndReturn(errorCacheMock.get('tweepy.errorcache:username'), 3600)

        self.replay()

        auth = BasicAuthHandler('username', 'password')
        api = API(auth_handler=auth, errorCache=errorCacheMock)
        #first call
        self.assertRaises(tweepy.error.TweepError, api.user_timeline, 'test')
        #second
        self.assertRaises(tweepy.error.CachedRateLimit, api.user_timeline, 'test')

        self.verify()