]>
Commit | Line | Data |
---|---|---|
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 | twitter11_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1.1') | |
20 | ||
21 | ||
22 | AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
23 | ||
24 | def get_random_str(): | |
25 | return ''.join(choice(AZaz) for _ in range(10)) | |
26 | ||
27 | ||
28 | def test_API_set_tweet(): | |
29 | random_tweet = "A random tweet " + get_random_str() | |
30 | twitter11.statuses.update(status=random_tweet) | |
31 | time.sleep(2) | |
32 | recent = twitter11.statuses.home_timeline() | |
33 | assert recent | |
34 | assert isinstance(recent.rate_limit_remaining, int) | |
35 | assert isinstance(recent.rate_limit_reset, int) | |
36 | assert random_tweet == recent[0]['text'] | |
37 | ||
38 | ||
39 | def test_API_set_unicode_tweet(): | |
40 | random_tweet = u"A random tweet with unicode üøπ" + get_random_str() | |
41 | twitter11.statuses.update(status=random_tweet) | |
42 | ||
43 | recent = twitter11.statuses.home_timeline() | |
44 | assert recent | |
45 | assert random_tweet == recent[0]['text'] | |
46 | ||
47 | ||
48 | def test_search(): | |
49 | results = twitter11.search.tweets(q='foo') | |
50 | assert results | |
51 | ||
52 | ||
53 | def test_get_trends_3(): | |
54 | # Of course they broke it all again in 1.1... | |
55 | assert twitter11.trends.place(_id=1) | |
56 | ||
57 | ||
58 | def 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 | |
66 | ||
67 | ||
68 | def 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 | ||
82 | def 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 |