<s> import re <EOL> import sys <EOL> import unittest <EOL> import warnings <EOL> import twitter <EOL> import responses <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , category = DeprecationWarning ) <EOL> DEF_URL_RE = re . compile ( r'<STR_LIT>' ) <EOL> class ErrNull ( object ) : <EOL> """<STR_LIT>""" <EOL> def write ( self , data ) : <EOL> pass <EOL> class RateLimitTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . api = twitter . Api ( <EOL> consumer_key = '<STR_LIT:test>' , <EOL> consumer_secret = '<STR_LIT:test>' , <EOL> access_token_key = '<STR_LIT:test>' , <EOL> access_token_secret = '<STR_LIT:test>' , <EOL> sleep_on_rate_limit = False ) <EOL> self . base_url = '<STR_LIT>' <EOL> self . _stderr = sys . stderr <EOL> sys . stderr = ErrNull ( ) <EOL> def tearDown ( self ) : <EOL> sys . stderr = self . _stderr <EOL> pass <EOL> @ responses . activate <EOL> def testInitializeRateLimit ( self ) : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> resp_data = f . read ( ) <EOL> url = '<STR_LIT>' % self . api . base_url <EOL> responses . add ( <EOL> responses . GET , <EOL> url , <EOL> body = resp_data , <EOL> match_querystring = True , <EOL> status = <NUM_LIT:200> ) <EOL> self . api . InitializeRateLimit ( ) <EOL> self . assertTrue ( self . api . rate_limit ) <EOL> self . rate_limit = None <EOL> self . api . sleep_on_rate_limit = True <EOL> self . api . InitializeRateLimit ( ) <EOL> self . assertTrue ( self . api . rate_limit ) <EOL> self . assertTrue ( self . api . sleep_on_rate_limit ) <EOL> responses . add ( responses . GET , url = DEF_URL_RE , body = b'<STR_LIT:{}>' , status = <NUM_LIT:200> ) <EOL> try : <EOL> self . api . GetStatus ( status_id = <NUM_LIT> ) <EOL> self . api . GetUser ( screen_name = '<STR_LIT:test>' ) <EOL> except Exception as e : <EOL> self . fail ( e ) <EOL> @ responses . activate <EOL> def testCheckRateLimit ( self ) : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> resp_data = f . read ( ) <EOL> url = '<STR_LIT>' % self . api . base_url <EOL> responses . add ( <EOL> responses . GET , <EOL> url , <EOL> body = resp_data , <EOL> match_querystring = True , <EOL> status = <NUM_LIT:200> ) <EOL> rt = self . api . CheckRateLimit ( '<STR_LIT>' ) <EOL> self . assertEqual ( rt . limit , <NUM_LIT:15> ) <EOL> self . assertEqual ( rt . remaining , <NUM_LIT:15> ) <EOL> self . assertEqual ( rt . reset , <NUM_LIT> ) <EOL> class RateLimitMethodsTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ responses . activate <EOL> def setUp ( self ) : <EOL> self . api = twitter . Api ( <EOL> consumer_key = '<STR_LIT:test>' , <EOL> consumer_secret = '<STR_LIT:test>' , <EOL> access_token_key = '<STR_LIT:test>' , <EOL> access_token_secret = '<STR_LIT:test>' , <EOL> sleep_on_rate_limit = False ) <EOL> self . base_url = '<STR_LIT>' <EOL> self . _stderr = sys . stderr <EOL> sys . stderr = ErrNull ( ) <EOL> with open ( '<STR_LIT>' ) as f : <EOL> resp_data = f . read ( ) <EOL> url = '<STR_LIT>' % self . api . base_url <EOL> responses . add ( <EOL> responses . GET , <EOL> url , <EOL> body = resp_data , <EOL> match_querystring = True , <EOL> status = <NUM_LIT:200> ) <EOL> self . api . InitializeRateLimit ( ) <EOL> self . assertTrue ( self . api . rate_limit ) <EOL> def tearDown ( self ) : <EOL> sys . stderr = self . _stderr <EOL> pass <EOL> def testGetRateLimit ( self ) : <EOL> lim = self . api . rate_limit . get_limit ( '<STR_LIT>' ) <EOL> self . assertEqual ( lim . limit , <NUM_LIT> ) <EOL> self . assertEqual ( lim . remaining , <NUM_LIT> ) <EOL> self . assertEqual ( lim . reset , <NUM_LIT> ) <EOL> def testNonStandardEndpointRateLimit ( self ) : <EOL> lim = self . api . rate_limit . get_limit ( '<STR_LIT>' ) <EOL> self . assertEqual ( lim . limit , <NUM_LIT> ) <EOL> lim = self . api . rate_limit . get_limit ( '<STR_LIT>' ) <EOL> self . assertEqual ( lim . limit , <NUM_LIT:15> ) <EOL> lim = self . api . rate_limit . get_limit ( '<STR_LIT>' ) <EOL> self . assertEqual ( lim . limit , <NUM_LIT> ) <EOL> def testSetRateLimit ( self ) : <EOL> previous_limit = self . api . rate_limit . get_limit ( '<STR_LIT>' ) <EOL> self . api . rate_limit . set_limit ( <EOL> url = '<STR_LIT>' , <EOL> limit = previous_limit . limit , <EOL> remaining = previous_limit . remaining - <NUM_LIT:1> , <EOL> reset = previous_limit . reset ) <EOL> new_limit = self . api . rate_limit . get_limit ( '<STR_LIT>' ) <EOL> self . assertEqual ( new_limit . remaining , previous_limit . remaining - <NUM_LIT:1> ) <EOL> def testFamilyNotFound ( self ) : <EOL> limit = self . api . rate_limit . get_limit ( '<STR_LIT>' ) <EOL> self . assertEqual ( limit . limit , <NUM_LIT:15> ) <EOL> self . assertEqual ( limit . remaining , <NUM_LIT:15> ) <EOL> self . assertEqual ( limit . reset , <NUM_LIT:0> ) <EOL> def testSetUnknownRateLimit ( self ) : <EOL> self . api . rate_limit . set_limit ( <EOL> url = '<STR_LIT>' , <EOL> limit = <NUM_LIT:15> , <EOL> remaining = <NUM_LIT> , <EOL> reset = <NUM_LIT:100> ) <EOL> limit = self . api . rate_limit . get_limit ( <EOL> url = '<STR_LIT>' ) <EOL> self . assertEqual ( limit . remaining , <NUM_LIT> ) </s>
