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