]> jfr.im git - z_archive/twitter.git/blame - tests/test_sanity.py
add tests for update_with_media api function
[z_archive/twitter.git] / tests / test_sanity.py
CommitLineData
f0603331
MV
1# encoding: utf-8
2from __future__ import unicode_literals
e541e268 3
9999744f 4import os
e541e268 5from random import choice
c77b5e4b 6import time
ed23f46c
MV
7import pickle
8import json
e541e268 9
32a4811d 10from twitter import Twitter, NoAuth, OAuth, read_token_file, TwitterHTTPError
ed23f46c 11from twitter.api import TwitterDictResponse, TwitterListResponse
e541e268
MV
12from twitter.cmdline import CONSUMER_KEY, CONSUMER_SECRET
13
14noauth = NoAuth()
15oauth = OAuth(*read_token_file('tests/oauth_creds')
2cf5509c 16 + (CONSUMER_KEY, CONSUMER_SECRET))
17
920528cd
MV
18twitter11 = Twitter(domain='api.twitter.com',
19 auth=oauth,
20 api_version='1.1')
e541e268 21
1ff50236 22twitter11_na = Twitter(domain='api.twitter.com',
23 auth=noauth,
24 api_version='1.1')
e541e268
MV
25
26AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
27
2cf5509c 28
e541e268
MV
29def get_random_str():
30 return ''.join(choice(AZaz) for _ in range(10))
31
32
e541e268
MV
33def test_API_set_tweet():
34 random_tweet = "A random tweet " + get_random_str()
2cf5509c 35 twitter11.statuses.update(status=random_tweet)
f0603331 36 time.sleep(5)
4a803760 37 recent = twitter11.statuses.home_timeline()
e541e268 38 assert recent
c77b5e4b
SK
39 assert isinstance(recent.rate_limit_remaining, int)
40 assert isinstance(recent.rate_limit_reset, int)
f0603331
MV
41 texts = [tweet['text'] for tweet in recent]
42 assert random_tweet in texts
e541e268
MV
43
44
066c34e0 45def test_API_set_unicode_tweet():
f0603331 46 random_tweet = "A random tweet with unicode üøπ" + get_random_str()
2cf5509c 47 twitter11.statuses.update(status=random_tweet)
f0603331 48 time.sleep(5)
4a803760 49 recent = twitter11.statuses.home_timeline()
066c34e0 50 assert recent
f0603331
MV
51 texts = [tweet['text'] for tweet in recent]
52 assert random_tweet in texts
066c34e0 53
54
9999744f
R
55def clean_link(text):
56 pos = text.find(" http://t.co")
57 if pos != -1:
58 return text[:pos]
59 return text
60
61__location__ = os.path.realpath(
62 os.path.join(os.getcwd(), os.path.dirname(__file__)))
63
64def test_API_set_unicode_twitpic(base64=False):
65 random_tweet = "A random twitpic from %s with unicode üøπ" % \
66 ("base64" if base64 else "file") + get_random_str()
67 if base64:
68 img = b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94JFhMBAJv5kaUAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAA4UlEQVQoz7WSIZLGIAxG6c5OFZjianBcIOfgPkju1DsEBWfAUEcNGGpY8Xe7dDoVFRvHfO8NJGRorZE39UVe1nd/WNfVObcsi3OOEAIASikAmOf5D2q/FWPUWgshKKWfiFIqhNBaxxhPjPQ05/z+Bs557xw9hBC89ymlu5BS8t6HEC5NW2sR8alRRLTWXoRSSinlSejT12M9BAAAgCeoTw9BSimlfBIu6WdYtVZEVErdaaUUItZaL/9wOsaY83YAMMb0dGtt6Jdv3/ec87ZtOWdCCGNsmibG2DiOJzP8+7b+AAOmsiPxyHWCAAAAAElFTkSuQmCC"
69 else:
70 with open(os.path.join(__location__, "test.png"), "rb") as f:
71 img = f.read()
72 params = {"status": random_tweet, "media[]": img}
73 if base64:
74 params["_base64"] = True
75 twitter11.statuses.update_with_media(**params)
76 time.sleep(5)
77 recent = twitter11.statuses.home_timeline()
78 assert recent
79 texts = [clean_link(tweet['text']) for tweet in recent]
80 assert random_tweet in texts
81
82def test_API_set_unicode_twitpic_base64():
83 test_API_set_unicode_twitpic(base64=True)
84
85
652c5402 86def test_search():
fcf08b18 87 # In 1.1, search works on api.twitter.com not search.twitter.com
88 # and requires authorisation
ed074882 89 results = twitter11.search.tweets(q='foo')
652c5402 90 assert results
e748eed8 91
92
93def test_get_trends():
94 # This is one method of inserting parameters, using named
95 # underscore params.
c6e1e349 96 world_trends = twitter11.trends.available(_woeid=1)
e748eed8 97 assert world_trends
98
99
100def test_get_trends_2():
101 # This is a nicer variation of the same call as above.
2cf5509c 102 world_trends = twitter11.trends._(1)
e748eed8 103 assert world_trends
920528cd
MV
104
105
106def test_get_trends_3():
107 # Of course they broke it all again in 1.1...
108 assert twitter11.trends.place(_id=1)
109
ed23f46c 110
32a4811d
HN
111def test_TwitterHTTPError_raised_for_invalid_oauth():
112 test_passed = False
113 try:
114 twitter11_na.statuses.mentions_timeline()
115 except TwitterHTTPError:
116 # this is the error we are looking for :)
117 test_passed = True
118 assert test_passed
ed23f46c
MV
119
120
121def test_picklability():
122 res = TwitterDictResponse({'a': 'b'})
123 p = pickle.dumps(res)
124 res2 = pickle.loads(p)
125 assert res == res2
126 assert res2['a'] == 'b'
127
128 res = TwitterListResponse([1, 2, 3])
129 p = pickle.dumps(res)
130 res2 = pickle.loads(p)
131 assert res == res2
132 assert res2[2] == 3
133
134
135def test_jsonifability():
136 res = TwitterDictResponse({'a': 'b'})
137 p = json.dumps(res)
138 res2 = json.loads(p)
139 assert res == res2
140 assert res2['a'] == 'b'
141
142 res = TwitterListResponse([1, 2, 3])
143 p = json.dumps(res)
144 res2 = json.loads(p)
145 assert res == res2
146 assert res2[2] == 3