1
1
from tornado .testing import AsyncTestCase
2
2
import tweetstream
3
3
import logging
4
+ import os
5
+ import time
4
6
5
- try :
6
- import test_settings
7
- except ImportError :
8
- test_settings = None
7
+ TEST_CONSUMER_KEY = os . environ . get ( "TWEETSTREAM_TEST_CONSUMER_KEY" )
8
+ TEST_CONSUMER_SECRET = os . environ . get ( "TWEETSTREAM_TEST_CONSUMER_SECRET" )
9
+ TEST_ACCESS_TOKEN = os . environ . get ( "TWEETSTREAM_TEST_ACCESS_TOKEN" )
10
+ TEST_ACCESS_SECRET = os . environ . get ( "TWEETSTREAM_TEST_ACCESS_TOKEN_SECRET" )
9
11
12
+ def test_real_config ():
13
+ configuration = {
14
+ "twitter_consumer_secret" : TEST_CONSUMER_SECRET ,
15
+ "twitter_consumer_key" : TEST_CONSUMER_KEY ,
16
+ "twitter_access_token_secret" : TEST_ACCESS_SECRET ,
17
+ "twitter_access_token" : TEST_ACCESS_TOKEN
18
+ }
19
+ if None in configuration .values ():
20
+ logging .debug ("Missing one or more test configuration values." )
21
+ return None
22
+ return configuration
10
23
11
24
class TestTweetStream (AsyncTestCase ):
12
25
13
- def setUp (self ):
14
- super (TestTweetStream , self ).setUp ()
15
- self .original_app_password = tweetstream .TWITTER_APP_PASSWORD
16
- tweetstream .TWITTER_APP_PASSWORD = "foobar"
17
-
18
- def tearDown (self ):
19
- super (TestTweetStream , self ).tearDown ()
20
- tweetstream .TWITTER_APP_PASSWORD = self .original_app_password
21
-
22
26
def test_twitter_stream (self ):
23
27
""" Test that the twitter stream is started with module defaults """
24
28
result = {}
25
29
def error_callback (error ):
26
30
result ["error" ] = error
31
+ print error
27
32
self .stop ()
28
- stream = tweetstream .TweetStream (ioloop = self .io_loop )
29
- stream .error_callback = error_callback
30
- stream .fetch ("foobar?whats=up" )
33
+
34
+ configuration = {
35
+ "twitter_consumer_secret" : "ABCDEF1234567890" ,
36
+ "twitter_consumer_key" : "0987654321ABCDEF" ,
37
+ "twitter_access_token_secret" : "1234567890ABCDEF" ,
38
+ "twitter_access_token" : "FEDCBA09123456789" ,
39
+ }
40
+ stream = tweetstream .TweetStream (configuration , ioloop = self .io_loop )
41
+ stream .set_error_callback (error_callback )
42
+ stream .fetch ("/1/statuses/sample.json" )
31
43
self .wait ()
32
44
self .assertTrue ("error" in result )
33
45
46
+ def test_twitter_stream_bad_configuration (self ):
47
+ """Test the configuration missing values."""
48
+ configuration = {
49
+ "twitter_consumer_secret" : "ABCDEF1234567890" ,
50
+ "twitter_consumer_key" : "0987654321ABCDEF" ,
51
+ "twitter_access_token_secret" : "1234567890ABCDEF" ,
52
+ "twitter_access_token" : "FEDCBA09123456789"
53
+ }
54
+ for key in configuration :
55
+ bad_config = configuration .copy ()
56
+ del bad_config [key ]
57
+ self .assertRaises (tweetstream .MissingConfiguration ,
58
+ lambda : tweetstream .TweetStream (bad_config ))
59
+
34
60
def test_twitter_stream_with_configuration (self ):
35
61
"""Test that the twitter stream supports instance configuration."""
36
62
configuration = {
37
- "twitter_app_username" : "newusername" ,
38
- "twitter_app_password" : "newpassword" ,
63
+ "twitter_consumer_secret" : "ABCDEF1234567890" ,
64
+ "twitter_consumer_key" : "0987654321ABCDEF" ,
65
+ "twitter_access_token_secret" : "1234567890ABCDEF" ,
66
+ "twitter_access_token" : "FEDCBA09123456789" ,
39
67
"twitter_stream_host" : "whatever.com" ,
40
68
"twitter_stream_port" : 556 ,
41
69
"twitter_stream_scheme" : "http"
@@ -44,48 +72,42 @@ def test_twitter_stream_with_configuration(self):
44
72
configuration = configuration )
45
73
# this is evil, but until module stuff is removed and
46
74
# proper configuration is refactored it will have to do.
47
- self .assertEqual ("newusername" , stream ._twitter_app_user )
48
- self .assertEqual ("newpassword" , stream ._twitter_app_password )
49
- self .assertEqual ("whatever.com" , stream ._twitter_stream_host )
50
75
self .assertEqual (556 , stream ._twitter_stream_port )
51
76
self .assertEqual ("http" , stream ._twitter_stream_scheme )
52
77
53
78
54
79
class TestActualTwitterCalls (AsyncTestCase ):
55
80
""" Testing actual calls, assuming settings are loaded. """
56
81
57
- def setUp (self ):
58
- super (TestActualTwitterCalls , self ).setUp ()
59
- self .original_app_user = tweetstream .TWITTER_APP_USER
60
- self .original_app_password = tweetstream .TWITTER_APP_PASSWORD
61
- if test_settings :
62
- tweetstream .TWITTER_APP_USER = \
63
- test_settings .TWITTER_APP_USER
64
- tweetstream .TWITTER_APP_PASSWORD = \
65
- test_settings .TWITTER_APP_PASSWORD
66
-
67
- def tearDown (self ):
68
- super (TestActualTwitterCalls , self ).tearDown ()
69
- tweetstream .TWITTER_APP_USER = self .original_app_user
70
- tweetstream .TWITTER_APP_PASSWORD = self .original_app_password
71
-
72
82
def get_message (self , path , clean = False ):
73
83
""" Wraps the ioloop start much like self.fetch """
74
- stream = tweetstream .TweetStream (ioloop = self .io_loop , clean = clean )
84
+ def error_callback (error ):
85
+ self .io_loop .stop ()
86
+ self .fail (str (error ))
87
+
88
+ stream = tweetstream .TweetStream (
89
+ configuration = test_real_config (),
90
+ ioloop = self .io_loop , clean = clean )
91
+ stream .set_error_callback (error_callback )
75
92
result = {}
76
93
def callback (message ):
77
94
""" Save result """
78
- result ["message" ] = message
79
- self .stop ()
95
+ if message .get ("text" ):
96
+ result ["message" ] = message
97
+ self .stop ()
98
+ # otherwise, it's not a tweet we care about...
99
+
80
100
stream .fetch (path , callback = callback )
81
101
self .wait ()
82
102
# will block until a message comes in or timeout
103
+ # now waiting to keep from hammering the stream connections
104
+ time .sleep (5 )
83
105
return result ["message" ]
84
106
85
107
def test_message (self ):
86
108
""" Test that twitter connects. """
87
109
#... if only everyone used 2.7 ...
88
- if not test_settings :
110
+ if not test_real_config () :
89
111
logging .debug ("Skipping test." )
90
112
return
91
113
result = self .get_message ("/1/statuses/sample.json" )
@@ -94,7 +116,7 @@ def test_message(self):
94
116
95
117
def test_stripped_message (self ):
96
118
""" Test that twitter connects and retrieves simple message. """
97
- if not test_settings :
119
+ if not test_real_config () :
98
120
logging .debug ("Skipping test" )
99
121
return
100
122
result = self .get_message ("/1/statuses/sample.json" , clean = True )
@@ -103,3 +125,12 @@ def test_stripped_message(self):
103
125
self .assertTrue ("text" in result )
104
126
self .assertTrue (result ["type" ] == "tweet" )
105
127
128
+ def test_search_term (self ):
129
+ """Test the statuses with a search term."""
130
+ if not test_real_config ():
131
+ logging .debug ("Skipping test" )
132
+ return
133
+ result = self .get_message ("/1/statuses/filter.json?track=twitter" )
134
+ self .assertTrue ("user" in result )
135
+ self .assertTrue ("text" in result )
136
+
0 commit comments