-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_tweet_filter.py
executable file
·225 lines (170 loc) · 9.1 KB
/
test_tweet_filter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
"""
"""
# Standard Library modules
import unittest
# Local modules
from tweet_filter import *
class TestFilterValidJSON(unittest.TestCase):
def test_check_required_fields(self):
json_tweet = '{"id": 1, "id_str": "1", "text":"foo", "user": {"screen_name": "charman"}}'
json_tweet_no_screen_name = '{"id": 1, "id_str": "1", "text":"foo"}'
tweet_json_filter = TweetFilterValidJSON()
self.assertTrue(tweet_json_filter.filter(json_tweet))
self.assertFalse(tweet_json_filter.filter(json_tweet_no_screen_name))
class TestFilterNoURLs(unittest.TestCase):
def test_url_filtering(self):
json_tweet_http = '{"id": 1, "id_str": "1", "text":"http://twitter.com"}'
json_tweet_https = '{"id": 2, "id_str": "2", "text":"https://twitter.com"}'
json_tweet_clean = '{"id": 3, "id_str": "3", "text":"no urls here"}'
tweet_no_url_filter = TweetFilterNoURLs()
self.assertFalse(tweet_no_url_filter.filter(json_tweet_http))
self.assertFalse(tweet_no_url_filter.filter(json_tweet_https))
self.assertTrue(tweet_no_url_filter.filter(json_tweet_clean))
class TestFilterOneTweetPerScreenName(unittest.TestCase):
def test_screen_name_filtering(self):
json_tweet_1 = '{"id": 1, "id_str": "1", "user": {"screen_name":"charman"}}'
json_tweet_2 = '{"id": 2, "id_str": "2", "user": {"screen_name":"PHonyDoc"}}'
json_tweet_3 = '{"id": 3, "id_str": "3", "user": {"screen_name":"charman"}}'
json_tweet_4 = '{"id": 4, "id_str": "4", "user": {"screen_name":"PHonyDoc"}}'
screen_name_filter = TweetFilterOneTweetPerScreenName(TweetFilter)
self.assertTrue(screen_name_filter.filter(json_tweet_1))
self.assertTrue(screen_name_filter.filter(json_tweet_2))
self.assertFalse(screen_name_filter.filter(json_tweet_3))
self.assertFalse(screen_name_filter.filter(json_tweet_4))
class TestFilterReliablyEnglish(unittest.TestCase):
def test_english_filtering(self):
spanish_tweet = '{"id": 1, "id_str": "1", "text":"Muchas de las victimas fueron mostrados con vendajes aplicados a toda prisa"}'
english_tweet = '{"id": 2, "id_str": "2", "text":"The quick brown fox jumped over the lazy sleeping dog"}'
english_filter = TweetFilterReliablyEnglish()
self.assertFalse(english_filter.filter(spanish_tweet))
self.assertTrue(english_filter.filter(english_tweet))
class TestFilterTweetIDInSet(unittest.TestCase):
def test_add_tweet_functions(self):
json_tweet_1 = '{"id": 1, "id_str": "1"}'
json_tweet_2 = '{"id": 2, "id_str": "2"}'
json_tweet_3 = '{"id": 3, "id_str": "3"}'
json_tweet_4 = '{"id": 4, "id_str": "4"}'
json_tweet_5 = '{"id": 5, "id_str": "5"}'
tweet_id_filter = TweetFilterTweetIDInSet()
self.assertFalse(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweet(json_tweet_1)
self.assertTrue(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweets([json_tweet_2, json_tweet_3])
self.assertTrue(tweet_id_filter.filter(json_tweet_2))
self.assertTrue(tweet_id_filter.filter(json_tweet_3))
self.assertFalse(tweet_id_filter.filter(json_tweet_4))
tweet_id_filter.add_tweet_id(4)
self.assertTrue(tweet_id_filter.filter(json_tweet_4))
tweet_id_filter.add_tweet_ids([4,5])
self.assertTrue(tweet_id_filter.filter(json_tweet_5))
def test_tweet_id_as_ints(self):
json_tweet_1 = '{"id": 1, "id_str": "1"}'
tweet_id_filter = TweetFilterTweetIDInSet()
self.assertFalse(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweet_id(1)
self.assertTrue(tweet_id_filter.filter(json_tweet_1))
def test_tweet_id_as_unicode(self):
json_tweet_1 = '{"id": 1, "id_str": "1"}'
tweet_id_filter = TweetFilterTweetIDInSet()
self.assertFalse(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweet_id(u'1')
self.assertTrue(tweet_id_filter.filter(json_tweet_1))
class TestFilterTweetIDNotInSet(unittest.TestCase):
def test_add_tweet_functions(self):
json_tweet_1 = '{"id": 1, "id_str": "1"}'
json_tweet_2 = '{"id": 2, "id_str": "2"}'
json_tweet_3 = '{"id": 3, "id_str": "3"}'
json_tweet_4 = '{"id": 4, "id_str": "4"}'
json_tweet_5 = '{"id": 5, "id_str": "5"}'
tweet_id_filter = TweetFilterTweetIDNotInSet()
self.assertTrue(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweet(json_tweet_1)
self.assertFalse(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweets([json_tweet_2, json_tweet_3])
self.assertFalse(tweet_id_filter.filter(json_tweet_2))
self.assertFalse(tweet_id_filter.filter(json_tweet_3))
self.assertTrue(tweet_id_filter.filter(json_tweet_4))
tweet_id_filter.add_tweet_id(4)
self.assertFalse(tweet_id_filter.filter(json_tweet_4))
tweet_id_filter.add_tweet_ids([4,5])
self.assertFalse(tweet_id_filter.filter(json_tweet_5))
def test_tweet_id_as_ints(self):
json_tweet_1 = '{"id": 1, "id_str": "1"}'
tweet_id_filter = TweetFilterTweetIDNotInSet()
self.assertTrue(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweet_id(1)
self.assertFalse(tweet_id_filter.filter(json_tweet_1))
def test_tweet_id_as_unicode(self):
json_tweet_1 = '{"id": 1, "id_str": "1"}'
tweet_id_filter = TweetFilterTweetIDNotInSet()
self.assertTrue(tweet_id_filter.filter(json_tweet_1))
tweet_id_filter.add_tweet_id(u'1')
self.assertFalse(tweet_id_filter.filter(json_tweet_1))
class TestFilteredTweetReader(unittest.TestCase):
def test_add_filter_when_reader_crated(self):
filtered_reader = FilteredTweetReader([TweetFilterNotARetweet()])
filtered_reader.open("testdata/shears.txt")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 30)
filtered_reader.close()
def test_add_multiple_filters(self):
filtered_reader = FilteredTweetReader()
filtered_reader.add_filter(TweetFilterNotARetweet())
filtered_reader.add_filter(TweetFilterFieldMatchesRegEx('text', r'\bmy %s(s|es)?\b' % 'shears'))
filtered_reader.open("testdata/shears.txt")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 19)
filtered_reader.close()
def test_filter_field_matches_regex(self):
filtered_reader = FilteredTweetReader()
regex_filter = TweetFilterFieldMatchesRegEx('text', r'\bmy %s(s|es)?\b' % 'shears')
filtered_reader.add_filter(regex_filter)
filtered_reader.open("testdata/shears.txt")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 20)
filtered_reader.close()
def test_filter_not_a_retweet(self):
filtered_reader = FilteredTweetReader()
filtered_reader.add_filter(TweetFilterNotARetweet())
filtered_reader.open("testdata/retweet_x1")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 0)
filtered_reader.close()
filtered_reader.open("testdata/shears.txt")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 30)
filtered_reader.close()
def test_filter_valid_json(self):
# FilteredTweetReader() uses the valid JSON filter by default
filtered_reader = FilteredTweetReader()
filtered_reader.open("testdata/bad_json_tweets_x3")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 0)
filtered_reader.close()
def test_reader_with_no_filters(self):
filtered_reader = FilteredTweetReader()
filtered_reader.open("testdata/shears.txt")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 32)
filtered_reader.close()
def test_filter_raises_exception(self):
filtered_reader = FilteredTweetReader()
filtered_reader.add_filter(TweetFilterAlwaysRaiseException())
filtered_reader.open("testdata/shears.txt")
self.assertRaises(Exception, total_tweets_passed_through_filters, filtered_reader)
filtered_reader.close()
def test_filters_short_circuit_after_first_rejection(self):
filtered_reader = FilteredTweetReader()
# Filteres are applied in order, so TweetFilterAlwaysRaiseException should never be called
filtered_reader.add_filter(TweetFilterAlwaysReject())
filtered_reader.add_filter(TweetFilterAlwaysRaiseException())
filtered_reader.open("testdata/shears.txt")
self.assertEqual(total_tweets_passed_through_filters(filtered_reader), 0)
filtered_reader.close()
def total_tweets_passed_through_filters(filtered_reader):
tweets = []
for tweet in filtered_reader:
tweets.append(tweet)
return len(tweets)
class TweetFilterAlwaysRaiseException(TweetFilter):
def filter(self, json_tweet_string):
raise Exception
class TweetFilterAlwaysReject(TweetFilter):
def filter(self, json_tweet_string):
return False
if __name__ == '__main__':
unittest.main(buffer=True)