]> jfr.im git - z_archive/twitter.git/blob - tests/test_sanity.py
6e5f0c492a7880e4f13ff06e8fe5e502011e9bb6
[z_archive/twitter.git] / tests / test_sanity.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from random import choice
5 import time
6 import pickle
7 import json
8
9 from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError
10 from twitter.api import TwitterDictResponse, TwitterListResponse
11 from twitter.cmdline import CONSUMER_KEY, CONSUMER_SECRET
12
13 noauth = NoAuth()
14 oauth = OAuth(*read_token_file('tests/oauth_creds')
15 + (CONSUMER_KEY, CONSUMER_SECRET))
16
17 twitter11 = Twitter(domain='api.twitter.com',
18 auth=oauth,
19 api_version='1.1')
20
21 twitter11_na = Twitter(domain='api.twitter.com',
22 auth=noauth,
23 api_version='1.1')
24
25 AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26
27
28 def get_random_str():
29 return ''.join(choice(AZaz) for _ in range(10))
30
31
32 def test_API_set_tweet():
33 random_tweet = "A random tweet " + get_random_str()
34 twitter11.statuses.update(status=random_tweet)
35 time.sleep(5)
36 recent = twitter11.statuses.home_timeline()
37 assert recent
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
42
43
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)
47 time.sleep(5)
48 recent = twitter11.statuses.home_timeline()
49 assert recent
50 texts = [tweet['text'] for tweet in recent]
51 assert random_tweet in texts
52
53
54 def test_search():
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')
58 assert results
59
60
61 def test_get_trends():
62 # This is one method of inserting parameters, using named
63 # underscore params.
64 world_trends = twitter11.trends.available(_woeid=1)
65 assert world_trends
66
67
68 def test_get_trends_2():
69 # This is a nicer variation of the same call as above.
70 world_trends = twitter11.trends._(1)
71 assert world_trends
72
73
74 def test_get_trends_3():
75 # Of course they broke it all again in 1.1...
76 assert twitter11.trends.place(_id=1)
77
78
79 def 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
87
88
89 def 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
103 def 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