]>
Commit | Line | Data |
---|---|---|
1 | # encoding: utf8 | |
2 | ||
3 | from random import choice | |
4 | import time | |
5 | ||
6 | from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError | |
7 | from twitter.cmdline import CONSUMER_KEY, CONSUMER_SECRET | |
8 | ||
9 | noauth = NoAuth() | |
10 | oauth = OAuth(*read_token_file('tests/oauth_creds') | |
11 | + (CONSUMER_KEY, CONSUMER_SECRET)) | |
12 | ||
13 | twitter = Twitter(domain='api.twitter.com', | |
14 | auth=oauth, | |
15 | api_version='1') | |
16 | twitter11 = Twitter(domain='api.twitter.com', | |
17 | auth=oauth, | |
18 | api_version='1.1') | |
19 | twitter_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1') | |
20 | twitter11_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1.1') | |
21 | ||
22 | ||
23 | AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
24 | ||
25 | def get_random_str(): | |
26 | return ''.join(choice(AZaz) for _ in range(10)) | |
27 | ||
28 | ||
29 | def test_API_set_tweet(): | |
30 | random_tweet = "A random tweet " + get_random_str() | |
31 | twitter.statuses.update(status=random_tweet) | |
32 | time.sleep(2) | |
33 | recent = twitter.statuses.user_timeline() | |
34 | assert recent | |
35 | assert isinstance(recent.rate_limit_remaining, int) | |
36 | assert isinstance(recent.rate_limit_reset, int) | |
37 | assert random_tweet == recent[0]['text'] | |
38 | ||
39 | ||
40 | def test_API_set_unicode_tweet(): | |
41 | random_tweet = u"A random tweet with unicode üøπ" + get_random_str() | |
42 | twitter.statuses.update(status=random_tweet) | |
43 | ||
44 | recent = twitter.statuses.user_timeline() | |
45 | assert recent | |
46 | assert random_tweet == recent[0]['text'] | |
47 | ||
48 | ||
49 | def test_search(): | |
50 | t_search = Twitter(domain='search.twitter.com') | |
51 | results = t_search.search(q='foo') | |
52 | assert results | |
53 | ||
54 | ||
55 | def test_get_trends(): | |
56 | # This is one method of inserting parameters, using named | |
57 | # underscore params. | |
58 | world_trends = twitter.trends._woeid(_woeid=1) | |
59 | assert world_trends | |
60 | ||
61 | ||
62 | def test_get_trends_2(): | |
63 | # This is a nicer variation of the same call as above. | |
64 | world_trends = twitter.trends._(1) | |
65 | assert world_trends | |
66 | ||
67 | ||
68 | def test_get_trends_3(): | |
69 | # Of course they broke it all again in 1.1... | |
70 | assert twitter11.trends.place(_id=1) | |
71 | ||
72 | def test_TwitterHTTPError_raised_for_invalid_oauth(): | |
73 | test_passed = False | |
74 | try: | |
75 | twitter11_na.statuses.mentions_timeline() | |
76 | except TwitterHTTPError: | |
77 | # this is the error we are looking for :) | |
78 | test_passed = True | |
79 | assert test_passed | |
80 |