]> jfr.im git - z_archive/twitter.git/blame - tests/test_sanity.py
Add a test for method_for_uri()
[z_archive/twitter.git] / tests / test_sanity.py
CommitLineData
f0603331
MV
1# encoding: utf-8
2from __future__ import unicode_literals
e541e268
MV
3
4from random import choice
c77b5e4b 5import time
ed23f46c
MV
6import pickle
7import json
e541e268 8
32a4811d 9from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError
1e95ec45 10from twitter.api import TwitterDictResponse, TwitterListResponse, POST_ACTIONS, method_for_uri
e541e268
MV
11from twitter.cmdline import CONSUMER_KEY, CONSUMER_SECRET
12
13noauth = NoAuth()
14oauth = OAuth(*read_token_file('tests/oauth_creds')
2cf5509c 15 + (CONSUMER_KEY, CONSUMER_SECRET))
16
920528cd
MV
17twitter11 = Twitter(domain='api.twitter.com',
18 auth=oauth,
19 api_version='1.1')
e541e268 20
1ff50236 21twitter11_na = Twitter(domain='api.twitter.com',
22 auth=noauth,
23 api_version='1.1')
e541e268
MV
24
25AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26
2cf5509c 27
e541e268
MV
28def get_random_str():
29 return ''.join(choice(AZaz) for _ in range(10))
30
31
e541e268
MV
32def test_API_set_tweet():
33 random_tweet = "A random tweet " + get_random_str()
2cf5509c 34 twitter11.statuses.update(status=random_tweet)
f0603331 35 time.sleep(5)
4a803760 36 recent = twitter11.statuses.home_timeline()
e541e268 37 assert recent
c77b5e4b
SK
38 assert isinstance(recent.rate_limit_remaining, int)
39 assert isinstance(recent.rate_limit_reset, int)
f0603331
MV
40 texts = [tweet['text'] for tweet in recent]
41 assert random_tweet in texts
e541e268
MV
42
43
066c34e0 44def test_API_set_unicode_tweet():
f0603331 45 random_tweet = "A random tweet with unicode üøπ" + get_random_str()
2cf5509c 46 twitter11.statuses.update(status=random_tweet)
f0603331 47 time.sleep(5)
4a803760 48 recent = twitter11.statuses.home_timeline()
066c34e0 49 assert recent
f0603331
MV
50 texts = [tweet['text'] for tweet in recent]
51 assert random_tweet in texts
066c34e0 52
53
652c5402 54def test_search():
fcf08b18 55 # In 1.1, search works on api.twitter.com not search.twitter.com
56 # and requires authorisation
ed074882 57 results = twitter11.search.tweets(q='foo')
652c5402 58 assert results
e748eed8 59
60
61def test_get_trends():
62 # This is one method of inserting parameters, using named
63 # underscore params.
c6e1e349 64 world_trends = twitter11.trends.available(_woeid=1)
e748eed8 65 assert world_trends
66
67
68def test_get_trends_2():
69 # This is a nicer variation of the same call as above.
2cf5509c 70 world_trends = twitter11.trends._(1)
e748eed8 71 assert world_trends
920528cd
MV
72
73
74def test_get_trends_3():
75 # Of course they broke it all again in 1.1...
76 assert twitter11.trends.place(_id=1)
77
ed23f46c 78
32a4811d
HN
79def test_TwitterHTTPError_raised_for_invalid_oauth():
80 test_passed = False
81 try:
82 twitter11_na.statuses.mentions_timeline()
83 except TwitterHTTPError:
84 # this is the error we are looking for :)
85 test_passed = True
86 assert test_passed
ed23f46c
MV
87
88
89def test_picklability():
90 res = TwitterDictResponse({'a': 'b'})
91 p = pickle.dumps(res)
92 res2 = pickle.loads(p)
93 assert res == res2
94 assert res2['a'] == 'b'
95
96 res = TwitterListResponse([1, 2, 3])
97 p = pickle.dumps(res)
98 res2 = pickle.loads(p)
99 assert res == res2
100 assert res2[2] == 3
101
102
103def test_jsonifability():
104 res = TwitterDictResponse({'a': 'b'})
105 p = json.dumps(res)
106 res2 = json.loads(p)
107 assert res == res2
108 assert res2['a'] == 'b'
109
110 res = TwitterListResponse([1, 2, 3])
111 p = json.dumps(res)
112 res2 = json.loads(p)
113 assert res == res2
114 assert res2[2] == 3
1e95ec45
BB
115
116
117def 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'