2 from __future__
import unicode_literals
5 from random
import choice
10 from twitter
import Twitter
, NoAuth
, OAuth
, read_token_file
, TwitterHTTPError
11 from twitter
.api
import TwitterDictResponse
, TwitterListResponse
12 from twitter
.cmdline
import CONSUMER_KEY
, CONSUMER_SECRET
15 oauth
= OAuth(*read_token_file('tests/oauth_creds')
16 + (CONSUMER_KEY
, CONSUMER_SECRET
))
18 twitter11
= Twitter(domain
='api.twitter.com',
22 twitter11_na
= Twitter(domain
='api.twitter.com',
26 AZaz
= "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30 return ''.join(choice(AZaz
) for _
in range(10))
33 def test_API_set_tweet():
34 random_tweet
= "A random tweet " + get_random_str()
35 twitter11
.statuses
.update(status
=random_tweet
)
37 recent
= twitter11
.statuses
.home_timeline()
39 assert isinstance(recent
.rate_limit_remaining
, int)
40 assert isinstance(recent
.rate_limit_reset
, int)
41 texts
= [tweet
['text'] for tweet
in recent
]
42 assert random_tweet
in texts
45 def test_API_set_unicode_tweet():
46 random_tweet
= "A random tweet with unicode üøπ" + get_random_str()
47 twitter11
.statuses
.update(status
=random_tweet
)
49 recent
= twitter11
.statuses
.home_timeline()
51 texts
= [tweet
['text'] for tweet
in recent
]
52 assert random_tweet
in texts
56 pos
= text
.find(" http://t.co")
61 __location__
= os
.path
.realpath(
62 os
.path
.join(os
.getcwd(), os
.path
.dirname(__file__
)))
64 def test_API_set_unicode_twitpic(base64
=False):
65 random_tweet
= "A random twitpic from %s with unicode üøπ" % \
66 ("base64" if base64
else "file") + get_random_str()
68 img
= b
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94JFhMBAJv5kaUAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAA4UlEQVQoz7WSIZLGIAxG6c5OFZjianBcIOfgPkju1DsEBWfAUEcNGGpY8Xe7dDoVFRvHfO8NJGRorZE39UVe1nd/WNfVObcsi3OOEAIASikAmOf5D2q/FWPUWgshKKWfiFIqhNBaxxhPjPQ05/z+Bs557xw9hBC89ymlu5BS8t6HEC5NW2sR8alRRLTWXoRSSinlSejT12M9BAAAgCeoTw9BSimlfBIu6WdYtVZEVErdaaUUItZaL/9wOsaY83YAMMb0dGtt6Jdv3/ec87ZtOWdCCGNsmibG2DiOJzP8+7b+AAOmsiPxyHWCAAAAAElFTkSuQmCC"
70 with open(os
.path
.join(__location__
, "test.png"), "rb") as f
:
72 params
= {"status": random_tweet, "media[]": img}
74 params
["_base64"] = True
75 twitter11
.statuses
.update_with_media(**params
)
77 recent
= twitter11
.statuses
.home_timeline()
79 texts
= [clean_link(tweet
['text']) for tweet
in recent
]
80 assert random_tweet
in texts
82 def test_API_set_unicode_twitpic_base64():
83 test_API_set_unicode_twitpic(base64
=True)
87 # In 1.1, search works on api.twitter.com not search.twitter.com
88 # and requires authorisation
89 results
= twitter11
.search
.tweets(q
='foo')
93 def test_get_trends():
94 # This is one method of inserting parameters, using named
96 world_trends
= twitter11
.trends
.available(_woeid
=1)
100 def test_get_trends_2():
101 # This is a nicer variation of the same call as above.
102 world_trends
= twitter11
.trends
._(1)
106 def test_get_trends_3():
107 # Of course they broke it all again in 1.1...
108 assert twitter11
.trends
.place(_id
=1)
111 def test_TwitterHTTPError_raised_for_invalid_oauth():
114 twitter11_na
.statuses
.mentions_timeline()
115 except TwitterHTTPError
:
116 # this is the error we are looking for :)
121 def test_picklability():
122 res
= TwitterDictResponse({'a': 'b'}
)
123 p
= pickle
.dumps(res
)
124 res2
= pickle
.loads(p
)
126 assert res2
['a'] == 'b'
128 res
= TwitterListResponse([1, 2, 3])
129 p
= pickle
.dumps(res
)
130 res2
= pickle
.loads(p
)
135 def test_jsonifability():
136 res
= TwitterDictResponse({'a': 'b'}
)
140 assert res2
['a'] == 'b'
142 res
= TwitterListResponse([1, 2, 3])