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