]> jfr.im git - z_archive/twitter.git/blob - tests/test_sanity.py
Add a test for API v1 unsupported. Other tests use v1.1 instead of v1.
[z_archive/twitter.git] / tests / test_sanity.py
1 # encoding: utf8
2
3 from random import choice
4 import time
5
6 from twitter import Twitter, NoAuth, OAuth, read_token_file
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 twitter1 = Twitter(domain='api.twitter.com',
14 auth=oauth,
15 api_version='1')
16
17 twitter11 = Twitter(domain='api.twitter.com',
18 auth=oauth,
19 api_version='1.1')
20
21 twitter_na = Twitter(domain='api.twitter.com', auth=noauth, api_version='1.1')
22
23 AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24
25
26 def get_random_str():
27 return ''.join(choice(AZaz) for _ in range(10))
28
29
30 def test_v1_not_supported():
31 # Arrange
32 random_tweet = "Random tweet that shouldn't be posted " + get_random_str()
33
34 # Act
35 try:
36 twitter1.statuses.update(status=random_tweet)
37 time.sleep(2)
38 error = False
39 except:
40 error = True
41
42 # Assert
43 assert error
44
45
46 def test_API_set_tweet():
47 random_tweet = "A random tweet " + get_random_str()
48 twitter11.statuses.update(status=random_tweet)
49 time.sleep(2)
50 recent = twitter11.statuses.user_timeline()
51 assert recent
52 assert isinstance(recent.rate_limit_remaining, int)
53 assert isinstance(recent.rate_limit_reset, int)
54 assert random_tweet == recent[0]['text']
55
56
57 def test_API_set_unicode_tweet():
58 random_tweet = u"A random tweet with unicode üøπ" + get_random_str()
59 twitter11.statuses.update(status=random_tweet)
60
61 recent = twitter11.statuses.user_timeline()
62 assert recent
63 assert random_tweet == recent[0]['text']
64
65
66 def test_search():
67 t_search = Twitter(domain='search.twitter.com')
68 results = t_search.search(q='foo')
69 assert results
70
71
72 def test_get_trends():
73 # This is one method of inserting parameters, using named
74 # underscore params.
75 world_trends = twitter11.trends._woeid(_woeid=1)
76 assert world_trends
77
78
79 def test_get_trends_2():
80 # This is a nicer variation of the same call as above.
81 world_trends = twitter11.trends._(1)
82 assert world_trends
83
84
85 def test_get_trends_3():
86 # Of course they broke it all again in 1.1...
87 assert twitter11.trends.place(_id=1)
88
89 # End of file