]>
Commit | Line | Data |
---|---|---|
f0603331 MV |
1 | # encoding: utf-8 |
2 | from __future__ import unicode_literals | |
e541e268 MV |
3 | |
4 | from random import choice | |
c77b5e4b | 5 | import time |
ed23f46c MV |
6 | import pickle |
7 | import json | |
e541e268 | 8 | |
32a4811d | 9 | from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError |
ed23f46c | 10 | from twitter.api import TwitterDictResponse, TwitterListResponse |
e541e268 MV |
11 | from twitter.cmdline import CONSUMER_KEY, CONSUMER_SECRET |
12 | ||
13 | noauth = NoAuth() | |
14 | oauth = OAuth(*read_token_file('tests/oauth_creds') | |
2cf5509c | 15 | + (CONSUMER_KEY, CONSUMER_SECRET)) |
16 | ||
920528cd MV |
17 | twitter11 = Twitter(domain='api.twitter.com', |
18 | auth=oauth, | |
19 | api_version='1.1') | |
e541e268 | 20 | |
1ff50236 | 21 | twitter11_na = Twitter(domain='api.twitter.com', |
22 | auth=noauth, | |
23 | api_version='1.1') | |
e541e268 MV |
24 | |
25 | AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
26 | ||
2cf5509c | 27 | |
e541e268 MV |
28 | def get_random_str(): |
29 | return ''.join(choice(AZaz) for _ in range(10)) | |
30 | ||
31 | ||
e541e268 MV |
32 | def test_API_set_tweet(): |
33 | random_tweet = "A random tweet " + get_random_str() | |
2cf5509c | 34 | twitter11.statuses.update(status=random_tweet) |
f0603331 | 35 | time.sleep(5) |
4a803760 | 36 | recent = twitter11.statuses.home_timeline() |
e541e268 | 37 | assert recent |
c77b5e4b SK |
38 | assert isinstance(recent.rate_limit_remaining, int) |
39 | assert isinstance(recent.rate_limit_reset, int) | |
f0603331 MV |
40 | texts = [tweet['text'] for tweet in recent] |
41 | assert random_tweet in texts | |
e541e268 MV |
42 | |
43 | ||
066c34e0 | 44 | def test_API_set_unicode_tweet(): |
f0603331 | 45 | random_tweet = "A random tweet with unicode üøπ" + get_random_str() |
2cf5509c | 46 | twitter11.statuses.update(status=random_tweet) |
f0603331 | 47 | time.sleep(5) |
4a803760 | 48 | recent = twitter11.statuses.home_timeline() |
066c34e0 | 49 | assert recent |
f0603331 MV |
50 | texts = [tweet['text'] for tweet in recent] |
51 | assert random_tweet in texts | |
066c34e0 | 52 | |
53 | ||
652c5402 | 54 | def test_search(): |
fcf08b18 | 55 | # In 1.1, search works on api.twitter.com not search.twitter.com |
56 | # and requires authorisation | |
ed074882 | 57 | results = twitter11.search.tweets(q='foo') |
652c5402 | 58 | assert results |
e748eed8 | 59 | |
60 | ||
61 | def test_get_trends(): | |
62 | # This is one method of inserting parameters, using named | |
63 | # underscore params. | |
c6e1e349 | 64 | world_trends = twitter11.trends.available(_woeid=1) |
e748eed8 | 65 | assert world_trends |
66 | ||
67 | ||
68 | def test_get_trends_2(): | |
69 | # This is a nicer variation of the same call as above. | |
2cf5509c | 70 | world_trends = twitter11.trends._(1) |
e748eed8 | 71 | assert world_trends |
920528cd MV |
72 | |
73 | ||
74 | def test_get_trends_3(): | |
75 | # Of course they broke it all again in 1.1... | |
76 | assert twitter11.trends.place(_id=1) | |
77 | ||
ed23f46c | 78 | |
32a4811d HN |
79 | def test_TwitterHTTPError_raised_for_invalid_oauth(): |
80 | test_passed = False | |
81 | try: | |
82 | twitter11_na.statuses.mentions_timeline() | |
83 | except TwitterHTTPError: | |
84 | # this is the error we are looking for :) | |
85 | test_passed = True | |
86 | assert test_passed | |
ed23f46c MV |
87 | |
88 | ||
89 | def test_picklability(): | |
90 | res = TwitterDictResponse({'a': 'b'}) | |
91 | p = pickle.dumps(res) | |
92 | res2 = pickle.loads(p) | |
93 | assert res == res2 | |
94 | assert res2['a'] == 'b' | |
95 | ||
96 | res = TwitterListResponse([1, 2, 3]) | |
97 | p = pickle.dumps(res) | |
98 | res2 = pickle.loads(p) | |
99 | assert res == res2 | |
100 | assert res2[2] == 3 | |
101 | ||
102 | ||
103 | def test_jsonifability(): | |
104 | res = TwitterDictResponse({'a': 'b'}) | |
105 | p = json.dumps(res) | |
106 | res2 = json.loads(p) | |
107 | assert res == res2 | |
108 | assert res2['a'] == 'b' | |
109 | ||
110 | res = TwitterListResponse([1, 2, 3]) | |
111 | p = json.dumps(res) | |
112 | res2 = json.loads(p) | |
113 | assert res == res2 | |
114 | assert res2[2] == 3 |