]> jfr.im git - z_archive/twitter.git/blame - tests/test_sanity.py
Merge pull request #217 from sixohsix/pickled
[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')
14 + (CONSUMER_KEY, CONSUMER_SECRET))
15
920528cd
MV
16twitter11 = Twitter(domain='api.twitter.com',
17 auth=oauth,
18 api_version='1.1')
32a4811d 19twitter11_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1.1')
e541e268
MV
20
21
22AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23
24def get_random_str():
25 return ''.join(choice(AZaz) for _ in range(10))
26
27
e541e268
MV
28def test_API_set_tweet():
29 random_tweet = "A random tweet " + get_random_str()
4a803760 30 twitter11.statuses.update(status=random_tweet)
c77b5e4b 31 time.sleep(2)
4a803760 32 recent = twitter11.statuses.home_timeline()
e541e268 33 assert recent
c77b5e4b
SK
34 assert isinstance(recent.rate_limit_remaining, int)
35 assert isinstance(recent.rate_limit_reset, int)
e541e268
MV
36 assert random_tweet == recent[0]['text']
37
38
066c34e0 39def test_API_set_unicode_tweet():
920528cd 40 random_tweet = u"A random tweet with unicode üøπ" + get_random_str()
4a803760 41 twitter11.statuses.update(status=random_tweet)
066c34e0 42
4a803760 43 recent = twitter11.statuses.home_timeline()
066c34e0 44 assert recent
45 assert random_tweet == recent[0]['text']
46
47
652c5402 48def test_search():
697d6b54 49 results = twitter11.search.tweets(q='foo')
652c5402 50 assert results
e748eed8 51
52
920528cd
MV
53def test_get_trends_3():
54 # Of course they broke it all again in 1.1...
55 assert twitter11.trends.place(_id=1)
56
ed23f46c 57
32a4811d
HN
58def test_TwitterHTTPError_raised_for_invalid_oauth():
59 test_passed = False
60 try:
61 twitter11_na.statuses.mentions_timeline()
62 except TwitterHTTPError:
63 # this is the error we are looking for :)
64 test_passed = True
65 assert test_passed
ed23f46c
MV
66
67
68def test_picklability():
69 res = TwitterDictResponse({'a': 'b'})
70 p = pickle.dumps(res)
71 res2 = pickle.loads(p)
72 assert res == res2
73 assert res2['a'] == 'b'
74
75 res = TwitterListResponse([1, 2, 3])
76 p = pickle.dumps(res)
77 res2 = pickle.loads(p)
78 assert res == res2
79 assert res2[2] == 3
80
81
82def test_jsonifability():
83 res = TwitterDictResponse({'a': 'b'})
84 p = json.dumps(res)
85 res2 = json.loads(p)
86 assert res == res2
87 assert res2['a'] == 'b'
88
89 res = TwitterListResponse([1, 2, 3])
90 p = json.dumps(res)
91 res2 = json.loads(p)
92 assert res == res2
93 assert res2[2] == 3