]>
Commit | Line | Data |
---|---|---|
066c34e0 | 1 | # encoding: utf8 |
e541e268 MV |
2 | |
3 | from random import choice | |
c77b5e4b | 4 | import time |
ed23f46c MV |
5 | import pickle |
6 | import json | |
e541e268 | 7 | |
32a4811d | 8 | from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError |
ed23f46c | 9 | from twitter.api import TwitterDictResponse, TwitterListResponse |
e541e268 MV |
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 | ||
920528cd MV |
16 | twitter11 = Twitter(domain='api.twitter.com', |
17 | auth=oauth, | |
18 | api_version='1.1') | |
32a4811d | 19 | twitter11_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1.1') |
e541e268 MV |
20 | |
21 | ||
22 | AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
23 | ||
24 | def get_random_str(): | |
25 | return ''.join(choice(AZaz) for _ in range(10)) | |
26 | ||
27 | ||
e541e268 MV |
28 | def 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 | 39 | def 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 | 48 | def test_search(): |
697d6b54 | 49 | results = twitter11.search.tweets(q='foo') |
652c5402 | 50 | assert results |
e748eed8 | 51 | |
52 | ||
920528cd MV |
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 | ||
ed23f46c | 57 | |
32a4811d HN |
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 | |
ed23f46c MV |
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 |