]> jfr.im git - z_archive/twitter.git/blame - tests/test_sanity.py
Merge remote-tracking branch 'upstream/master'
[z_archive/twitter.git] / tests / test_sanity.py
CommitLineData
066c34e0 1# encoding: utf8
e541e268
MV
2
3from random import choice
c77b5e4b 4import time
ed23f46c
MV
5import pickle
6import json
e541e268 7
32a4811d 8from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError
ed23f46c 9from twitter.api import TwitterDictResponse, TwitterListResponse
e541e268
MV
10from twitter.cmdline import CONSUMER_KEY, CONSUMER_SECRET
11
12noauth = NoAuth()
13oauth = OAuth(*read_token_file('tests/oauth_creds')
2cf5509c 14 + (CONSUMER_KEY, CONSUMER_SECRET))
15
920528cd
MV
16twitter11 = Twitter(domain='api.twitter.com',
17 auth=oauth,
18 api_version='1.1')
e541e268 19
1ff50236 20twitter11_na = Twitter(domain='api.twitter.com',
21 auth=noauth,
22 api_version='1.1')
e541e268
MV
23
24AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
25
2cf5509c 26
e541e268
MV
27def get_random_str():
28 return ''.join(choice(AZaz) for _ in range(10))
29
30
e541e268
MV
31def test_API_set_tweet():
32 random_tweet = "A random tweet " + get_random_str()
2cf5509c 33 twitter11.statuses.update(status=random_tweet)
c77b5e4b 34 time.sleep(2)
4a803760 35 recent = twitter11.statuses.home_timeline()
e541e268 36 assert recent
c77b5e4b
SK
37 assert isinstance(recent.rate_limit_remaining, int)
38 assert isinstance(recent.rate_limit_reset, int)
e541e268
MV
39 assert random_tweet == recent[0]['text']
40
41
066c34e0 42def test_API_set_unicode_tweet():
920528cd 43 random_tweet = u"A random tweet with unicode üøπ" + get_random_str()
2cf5509c 44 twitter11.statuses.update(status=random_tweet)
066c34e0 45
4a803760 46 recent = twitter11.statuses.home_timeline()
066c34e0 47 assert recent
48 assert random_tweet == recent[0]['text']
49
50
652c5402 51def test_search():
fcf08b18 52 # In 1.1, search works on api.twitter.com not search.twitter.com
53 # and requires authorisation
ed074882 54 results = twitter11.search.tweets(q='foo')
652c5402 55 assert results
e748eed8 56
57
58def test_get_trends():
59 # This is one method of inserting parameters, using named
60 # underscore params.
c6e1e349 61 world_trends = twitter11.trends.available(_woeid=1)
e748eed8 62 assert world_trends
63
64
65def test_get_trends_2():
66 # This is a nicer variation of the same call as above.
2cf5509c 67 world_trends = twitter11.trends._(1)
e748eed8 68 assert world_trends
920528cd
MV
69
70
71def test_get_trends_3():
72 # Of course they broke it all again in 1.1...
73 assert twitter11.trends.place(_id=1)
74
ed23f46c 75
32a4811d
HN
76def 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
ed23f46c
MV
84
85
86def 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
100def 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
1ff50236 112
2cf5509c 113# End of file