<s> import unittest <EOL> import random <EOL> import shutil <EOL> from time import sleep <EOL> import os <EOL> from ast import literal_eval <EOL> from nose import SkipTest <EOL> from tweepy import Friendship , MemoryCache , FileCache , API <EOL> from tweepy . parsers import Parser <EOL> from . config import TweepyTestCase , username , use_replay , tape <EOL> test_tweet_id = '<STR_LIT>' <EOL> tweet_text = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> class TweepyErrorTests ( unittest . TestCase ) : <EOL> def testpickle ( self ) : <EOL> """<STR_LIT>""" <EOL> import pickle <EOL> from tweepy . error import TweepError <EOL> e = TweepError ( '<STR_LIT>' , { '<STR_LIT:status>' : <NUM_LIT:200> } ) <EOL> e2 = pickle . loads ( pickle . dumps ( e ) ) <EOL> self . assertEqual ( e . reason , e2 . reason ) <EOL> self . assertEqual ( e . response , e2 . response ) <EOL> class TweepyAPITests ( TweepyTestCase ) : <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testapierror ( self ) : <EOL> from tweepy . error import TweepError <EOL> with self . assertRaises ( TweepError ) as cm : <EOL> self . api . direct_messages ( ) <EOL> reason , = literal_eval ( cm . exception . reason ) <EOL> self . assertEqual ( reason [ '<STR_LIT:message>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( reason [ '<STR_LIT:code>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( cm . exception . api_code , <NUM_LIT> ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testgetoembed ( self ) : <EOL> data = self . api . get_oembed ( test_tweet_id ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testparserargumenthastobeaparserinstance ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( TypeError , API , self . auth , parser = Parser ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testhometimeline ( self ) : <EOL> self . api . home_timeline ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testusertimeline ( self ) : <EOL> self . api . user_timeline ( ) <EOL> self . api . user_timeline ( '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testmentionstimeline ( self ) : <EOL> self . api . mentions_timeline ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testretweetsofme ( self ) : <EOL> self . api . retweets_of_me ( ) <EOL> def testretweet ( self ) : <EOL> raise SkipTest ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testretweets ( self ) : <EOL> self . api . retweets ( test_tweet_id ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testretweeters ( self ) : <EOL> self . api . retweeters ( test_tweet_id ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testgetstatus ( self ) : <EOL> self . api . get_status ( id = test_tweet_id ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testupdateanddestroystatus ( self ) : <EOL> text = tweet_text if use_replay else '<STR_LIT>' % random . randint ( <NUM_LIT:0> , <NUM_LIT:1000> ) <EOL> update = self . api . update_status ( status = text ) <EOL> self . assertEqual ( update . text , text ) <EOL> deleted = self . api . destroy_status ( id = update . id ) <EOL> self . assertEqual ( deleted . id , update . id ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testupdateanddestroystatuswithoutkwarg ( self ) : <EOL> text = tweet_text if use_replay else '<STR_LIT>' % random . randint ( <NUM_LIT:0> , <NUM_LIT:1000> ) <EOL> update = self . api . update_status ( text ) <EOL> self . assertEqual ( update . text , text ) <EOL> deleted = self . api . destroy_status ( id = update . id ) <EOL> self . assertEqual ( deleted . id , update . id ) <EOL> @ tape . use_cassette ( '<STR_LIT>' , serializer = '<STR_LIT>' ) <EOL> def testupdatestatuswithmedia ( self ) : <EOL> update = self . api . update_with_media ( '<STR_LIT>' , status = tweet_text ) <EOL> self . assertIn ( tweet_text + '<STR_LIT>' , update . text ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testgetuser ( self ) : <EOL> u = self . api . get_user ( '<STR_LIT>' ) <EOL> self . assertEqual ( u . screen_name , '<STR_LIT>' ) <EOL> u = self . api . get_user ( <NUM_LIT> ) <EOL> self . assertEqual ( u . screen_name , '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testlookupusers ( self ) : <EOL> def check ( users ) : <EOL> self . assertEqual ( len ( users ) , <NUM_LIT:2> ) <EOL> check ( self . api . lookup_users ( user_ids = [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> check ( self . api . lookup_users ( screen_names = [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsearchusers ( self ) : <EOL> self . api . search_users ( '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsuggestedcategories ( self ) : <EOL> self . api . suggested_categories ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsuggestedusers ( self ) : <EOL> categories = self . api . suggested_categories ( ) <EOL> if len ( categories ) != <NUM_LIT:0> : <EOL> self . api . suggested_users ( categories [ <NUM_LIT:0> ] . slug ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsuggesteduserstweets ( self ) : <EOL> categories = self . api . suggested_categories ( ) <EOL> if len ( categories ) != <NUM_LIT:0> : <EOL> self . api . suggested_users_tweets ( categories [ <NUM_LIT:0> ] . slug ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testme ( self ) : <EOL> me = self . api . me ( ) <EOL> self . assertEqual ( me . screen_name , username ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testdirectmessages ( self ) : <EOL> self . api . direct_messages ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsentdirectmessages ( self ) : <EOL> self . api . sent_direct_messages ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsendanddestroydirectmessage ( self ) : <EOL> sent_dm = self . api . send_direct_message ( username , text = '<STR_LIT>' ) <EOL> self . assertEqual ( sent_dm . text , '<STR_LIT>' ) <EOL> self . assertEqual ( sent_dm . sender . screen_name , username ) <EOL> self . assertEqual ( sent_dm . recipient . screen_name , username ) <EOL> destroyed_dm = self . api . destroy_direct_message ( sent_dm . id ) <EOL> self . assertEqual ( destroyed_dm . text , sent_dm . text ) <EOL> self . assertEqual ( destroyed_dm . id , sent_dm . id ) <EOL> self . assertEqual ( destroyed_dm . sender . screen_name , username ) <EOL> self . assertEqual ( destroyed_dm . recipient . screen_name , username ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testcreatedestroyfriendship ( self ) : <EOL> enemy = self . api . destroy_friendship ( '<STR_LIT>' ) <EOL> self . assertEqual ( enemy . screen_name , '<STR_LIT>' ) <EOL> sleep ( <NUM_LIT:5> ) <EOL> friend = self . api . create_friendship ( '<STR_LIT>' ) <EOL> self . assertEqual ( friend . screen_name , '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testshowfriendship ( self ) : <EOL> source , target = self . api . show_friendship ( target_screen_name = '<STR_LIT>' ) <EOL> self . assert_ ( isinstance ( source , Friendship ) ) <EOL> self . assert_ ( isinstance ( target , Friendship ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testfriendsids ( self ) : <EOL> self . api . friends_ids ( username ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testfollowersids ( self ) : <EOL> self . api . followers_ids ( username ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testfriends ( self ) : <EOL> self . api . friends ( username ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testfollowers ( self ) : <EOL> self . api . followers ( username ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testverifycredentials ( self ) : <EOL> self . assertNotEqual ( self . api . verify_credentials ( ) , False ) <EOL> me = self . api . verify_credentials ( include_entities = True ) <EOL> self . assertTrue ( me . status . entities ) <EOL> me = self . api . verify_credentials ( skip_status = True ) <EOL> self . assertFalse ( hasattr ( me , '<STR_LIT:status>' ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testratelimitstatus ( self ) : <EOL> self . api . rate_limit_status ( ) <EOL> """<STR_LIT>""" <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testupdateprofilecolors ( self ) : <EOL> original = self . api . me ( ) <EOL> updated = self . api . update_profile_colors ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . api . update_profile_colors ( <EOL> original . profile_background_color , <EOL> original . profile_text_color , <EOL> original . profile_link_color , <EOL> original . profile_sidebar_fill_color , <EOL> original . profile_sidebar_border_color <EOL> ) <EOL> self . assertEqual ( updated . profile_background_color , '<STR_LIT>' ) <EOL> self . assertEqual ( updated . profile_text_color , '<STR_LIT>' ) <EOL> self . assertEqual ( updated . profile_link_color , '<STR_LIT>' ) <EOL> self . assertEqual ( updated . profile_sidebar_fill_color , '<STR_LIT>' ) <EOL> self . assertEqual ( updated . profile_sidebar_border_color , '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> @ tape . use_cassette ( '<STR_LIT>' , serializer = '<STR_LIT>' ) <EOL> def testupdateprofilebannerimage ( self ) : <EOL> self . api . update_profile_banner ( '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testupdateprofile ( self ) : <EOL> original = self . api . me ( ) <EOL> profile = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:location>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> } <EOL> updated = self . api . update_profile ( ** profile ) <EOL> self . api . update_profile ( <EOL> name = original . name , url = original . url , <EOL> location = original . location , description = original . description <EOL> ) <EOL> for k , v in profile . items ( ) : <EOL> if k == '<STR_LIT:email>' : continue <EOL> self . assertEqual ( getattr ( updated , k ) , v ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testfavorites ( self ) : <EOL> self . api . favorites ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testcreatedestroyfavorite ( self ) : <EOL> self . api . create_favorite ( <NUM_LIT> ) <EOL> self . api . destroy_favorite ( <NUM_LIT> ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testcreatedestroyblock ( self ) : <EOL> self . api . create_block ( '<STR_LIT>' ) <EOL> self . api . destroy_block ( '<STR_LIT>' ) <EOL> self . api . create_friendship ( '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testblocks ( self ) : <EOL> self . api . blocks ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testblocksids ( self ) : <EOL> self . api . blocks_ids ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testlistsall ( self ) : <EOL> self . api . lists_all ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testlistsmemberships ( self ) : <EOL> self . api . lists_memberships ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testlistssubscriptions ( self ) : <EOL> self . api . lists_subscriptions ( ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testlisttimeline ( self ) : <EOL> self . api . list_timeline ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testgetlist ( self ) : <EOL> self . api . get_list ( owner_screen_name = '<STR_LIT>' , slug = '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testaddremovelistmember ( self ) : <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : username , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def assert_list ( l ) : <EOL> self . assertEqual ( l . name , params [ '<STR_LIT>' ] ) <EOL> assert_list ( self . api . add_list_member ( ** params ) ) <EOL> sleep ( <NUM_LIT:3> ) <EOL> assert_list ( self . api . remove_list_member ( ** params ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testaddremovelistmembers ( self ) : <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : username , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> def assert_list ( l ) : <EOL> self . assertEqual ( l . name , params [ '<STR_LIT>' ] ) <EOL> assert_list ( self . api . add_list_members ( ** params ) ) <EOL> assert_list ( self . api . remove_list_members ( ** params ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testlistmembers ( self ) : <EOL> self . api . list_members ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testshowlistmember ( self ) : <EOL> self . assertTrue ( self . api . show_list_member ( owner_screen_name = '<STR_LIT>' , slug = '<STR_LIT>' , screen_name = '<STR_LIT>' ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsubscribeunsubscribelist ( self ) : <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . api . subscribe_list ( ** params ) <EOL> self . api . unsubscribe_list ( ** params ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testlistsubscribers ( self ) : <EOL> self . api . list_subscribers ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testshowlistsubscriber ( self ) : <EOL> self . assertTrue ( self . api . show_list_subscriber ( '<STR_LIT>' , '<STR_LIT:test>' , '<STR_LIT>' ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsavedsearches ( self ) : <EOL> s = self . api . create_saved_search ( '<STR_LIT:test>' ) <EOL> self . api . saved_searches ( ) <EOL> self . assertEqual ( self . api . get_saved_search ( s . id ) . query , '<STR_LIT:test>' ) <EOL> self . api . destroy_saved_search ( s . id ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsearch ( self ) : <EOL> self . api . search ( '<STR_LIT>' ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testgeoapis ( self ) : <EOL> def place_name_in_list ( place_name , place_list ) : <EOL> """<STR_LIT>""" <EOL> return any ( [ x . full_name . lower ( ) == place_name . lower ( ) for x in place_list ] ) <EOL> twitter_hq = self . api . geo_similar_places ( lat = '<STR_LIT>' , <EOL> long = '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) <EOL> self . assertEqual ( twitter_hq [ <NUM_LIT:0> ] . id , '<STR_LIT>' ) <EOL> self . assertEqual ( self . api . geo_id ( id = '<STR_LIT>' ) . full_name , '<STR_LIT>' ) <EOL> self . assertTrue ( place_name_in_list ( '<STR_LIT>' , <EOL> self . api . reverse_geocode ( lat = <NUM_LIT> , long = - <NUM_LIT> ) ) ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testsupportedlanguages ( self ) : <EOL> languages = self . api . supported_languages ( ) <EOL> expected_dict = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:code>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : "<STR_LIT>" <EOL> } <EOL> self . assertTrue ( expected_dict in languages ) <EOL> @ tape . use_cassette ( '<STR_LIT>' ) <EOL> def testcachedresult ( self ) : <EOL> self . api . cache = MemoryCache ( ) <EOL> self . api . home_timeline ( ) <EOL> self . assertFalse ( self . api . cached_result ) <EOL> self . api . home_timeline ( ) <EOL> self . assertTrue ( self . api . cached_result ) <EOL> class TweepyCacheTests ( unittest . TestCase ) : <EOL> timeout = <NUM_LIT> <EOL> memcache_servers = [ '<STR_LIT>' ] <EOL> def _run_tests ( self , do_cleanup = True ) : <EOL> self . cache . store ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( self . cache . get ( '<STR_LIT>' ) , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> sleep ( self . timeout ) <EOL> self . assertEqual ( self . cache . get ( '<STR_LIT>' ) , None , <EOL> '<STR_LIT>' ) <EOL> if do_cleanup : <EOL> self . cache . store ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sleep ( self . timeout ) <EOL> self . cache . cleanup ( ) <EOL> self . assertEqual ( self . cache . count ( ) , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:20> ) : <EOL> self . cache . store ( '<STR_LIT>' % i , '<STR_LIT>' ) <EOL> self . assertEqual ( self . cache . count ( ) , <NUM_LIT:20> , '<STR_LIT>' ) <EOL> self . cache . flush ( ) <EOL> self . assertEqual ( self . cache . count ( ) , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> def testmemorycache ( self ) : <EOL> self . cache = MemoryCache ( timeout = self . timeout ) <EOL> self . _run_tests ( ) <EOL> def testfilecache ( self ) : <EOL> os . mkdir ( '<STR_LIT>' ) <EOL> try : <EOL> self . cache = FileCache ( '<STR_LIT>' , self . timeout ) <EOL> self . _run_tests ( ) <EOL> self . cache . flush ( ) <EOL> finally : <EOL> if os . path . exists ( '<STR_LIT>' ) : <EOL> shutil . rmtree ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
