]>
jfr.im git - z_archive/twitter.git/blob - tests/test_sanity.py
2 from __future__
import unicode_literals
4 from random
import choice
9 from twitter
import Twitter
, NoAuth
, OAuth
, read_token_file
, TwitterHTTPError
10 from twitter
.api
import TwitterDictResponse
, TwitterListResponse
, POST_ACTIONS
, method_for_uri
11 from twitter
.cmdline
import CONSUMER_KEY
, CONSUMER_SECRET
14 oauth
= OAuth(*read_token_file('tests/oauth_creds')
15 + (CONSUMER_KEY
, CONSUMER_SECRET
))
17 twitter11
= Twitter(domain
='api.twitter.com',
21 twitter11_na
= Twitter(domain
='api.twitter.com',
25 AZaz
= "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
29 return ''.join(choice(AZaz
) for _
in range(10))
32 def test_API_set_tweet():
33 random_tweet
= "A random tweet " + get_random_str()
34 twitter11
.statuses
.update(status
=random_tweet
)
36 recent
= twitter11
.statuses
.home_timeline()
38 assert isinstance(recent
.rate_limit_remaining
, int)
39 assert isinstance(recent
.rate_limit_reset
, int)
40 texts
= [tweet
['text'] for tweet
in recent
]
41 assert random_tweet
in texts
44 def test_API_set_unicode_tweet():
45 random_tweet
= "A random tweet with unicode üøπ" + get_random_str()
46 twitter11
.statuses
.update(status
=random_tweet
)
48 recent
= twitter11
.statuses
.home_timeline()
50 texts
= [tweet
['text'] for tweet
in recent
]
51 assert random_tweet
in texts
55 # In 1.1, search works on api.twitter.com not search.twitter.com
56 # and requires authorisation
57 results
= twitter11
.search
.tweets(q
='foo')
61 def test_get_trends():
62 # This is one method of inserting parameters, using named
64 world_trends
= twitter11
.trends
.available(_woeid
=1)
68 def test_get_trends_2():
69 # This is a nicer variation of the same call as above.
70 world_trends
= twitter11
.trends
._(1)
74 def test_get_trends_3():
75 # Of course they broke it all again in 1.1...
76 assert twitter11
.trends
.place(_id
=1)
79 def test_TwitterHTTPError_raised_for_invalid_oauth():
82 twitter11_na
.statuses
.mentions_timeline()
83 except TwitterHTTPError
:
84 # this is the error we are looking for :)
89 def test_picklability():
90 res
= TwitterDictResponse({'a': 'b'}
)
92 res2
= pickle
.loads(p
)
94 assert res2
['a'] == 'b'
96 res
= TwitterListResponse([1, 2, 3])
98 res2
= pickle
.loads(p
)
103 def test_jsonifability():
104 res
= TwitterDictResponse({'a': 'b'}
)
108 assert res2
['a'] == 'b'
110 res
= TwitterListResponse([1, 2, 3])
117 def test_method_for_uri():
118 for action
in POST_ACTIONS
:
119 assert method_for_uri(get_random_str() + '/' + action
) == 'POST'
120 assert method_for_uri('statuses/timeline') == 'GET'