-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_extractor.py
230 lines (208 loc) · 9.92 KB
/
test_extractor.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
226
227
228
229
import unittest
import os, json, datetime, shutil
import extractor
import pdb
class TestTwitterExtractor(unittest.TestCase):
def setUp(self):
extractor.raw2date("test/test_tweet_lines.txt",
"test/tweets",
"test_tweet")
def test_retweets(self):
#pdb.set_trace() # for debugging
tweet_list = extractor.make_network("test/tweets",
retweets=True,
mentions=False)
retweeter_ids = []
retweet_times =[]
for tweet in tweet_list:
id_str = tweet[0]
timestamp = tweet[2]
retweeter_ids.append(id_str)
retweet_times.append(timestamp)
self.assertTrue("1" in retweeter_ids,
'id 1 should be in retweeters')
self.assertTrue("2" in retweeter_ids,
'id 2 should be in retweeters')
self.assertTrue("4" in retweeter_ids,
'id 4 should be in retweeters')
self.assertTrue("7" in retweeter_ids,
'id 7 should be in retweeters')
self.assertTrue("1599845613000" in retweet_times,
"Tweet with id 12 should be in retweets")
# the non-retweet has timestamp "1599838215000"
self.assertTrue("1599838215000" not in retweet_times,
"Tweet with id 1 should not be in retweets")
def test_retweet_dict(self):
# pdb.set_trace()
tweet_dict = extractor.make_network("test/tweets",
output="dictionary",
retweets=True,
mentions=False)
retweeter_ids = []
retweet_times =[]
for key in tweet_dict:
tweet = tweet_dict[key]
id_str = tweet['user']['id_str']
timestamp = tweet['timestamp_ms']
retweeter_ids.append(id_str)
retweet_times.append(timestamp)
self.assertTrue("1" in retweeter_ids,
'id 1 should be in retweeters')
self.assertTrue("2" in retweeter_ids,
'id 2 should be in retweeters')
self.assertTrue("4" in retweeter_ids,
'id 4 should be in retweeters')
self.assertTrue("7" in retweeter_ids,
'id 7 should be in retweeters')
self.assertTrue("1599845613000" in retweet_times,
"Tweet with id 12 should be in retweets")
# the non-retweet has timestamp "1599838215000"
self.assertTrue("1599838215000" not in retweet_times,
"Tweet with id 1 should not be in retweets")
def test_retweeters(self):
tweet_list = extractor.make_network("test/tweets",
retweets=True,
senders_rt=["1"],
mentions=False)
retweeter_ids = []
for tweet in tweet_list:
id_str = tweet[0]
retweeter_ids.append(id_str)
self.assertTrue("1" in retweeter_ids,
"Retweeter with id 1 should be included")
self.assertTrue("2" not in retweeter_ids,
"Retweeter with id 2 should not be included")
def test_retweeteds(self):
tweet_list = extractor.make_network("test/tweets",
retweets=True,
receivers_rt=["5"],
mentions=False)
retweeted_ids = []
for tweet in tweet_list:
id_str = tweet[1]
retweeted_ids.append(id_str)
self.assertTrue("5" in retweeted_ids,
"Retweeted with id 5 should be included")
self.assertTrue("6" not in retweeted_ids,
"Retweeted with id 6 should not be included")
def test_languages(self):
tweet_list = extractor.make_network("test/tweets",
retweets=True,
languages=["fi"],
mentions=False)
timestamps = []
for tweet in tweet_list:
time = tweet[2]
timestamps.append(time)
self.assertTrue("1599841875000" in timestamps,
"Tweet with id 10 should be included")
self.assertTrue("1599845613000" not in timestamps,
"Tweet with id 12 should not be included")
def test_keywords(self):
tweet_list = extractor.make_network("test/tweets",
retweets=True,
mentions=False,
keywords=[
"climate",
"ilmastonmuutos"
])
timestamps = []
for tweet in tweet_list:
time = tweet[2]
timestamps.append(time)
self.assertTrue("1599838275000" in timestamps,
"Tweet with id 2 should be included")
self.assertTrue("1599819010000" in timestamps,
"Tweet with id 6 should be included")
self.assertTrue("1599838275000" in timestamps,
"Tweet with id 8 should be included")
self.assertTrue("1599841875000" in timestamps,
"Tweet with id 10 should be included")
self.assertTrue("1599845613000" in timestamps,
"Tweet with id 12 should be included")
self.assertTrue("1599838876000" not in timestamps,
"Tweet with id 4 should not be included")
def test_dates(self):
tweet_list = extractor.make_network("test/tweets",
retweets=True,
mentions=False,
dates=[
"20190813",
"20200911"
])
timestamps = []
for tweet in tweet_list:
time = tweet[2]
timestamps.append(time)
self.assertTrue("1599838275000" in timestamps,
"Tweet with id 2 should be included")
self.assertTrue("1599819010000" in timestamps,
"Tweet with id 6 should be included")
self.assertTrue("1599838275000" in timestamps,
"Tweet with id 8 should be included")
self.assertTrue("1599841875000" in timestamps,
"Tweet with id 10 should be included")
self.assertTrue("1599845613000" in timestamps,
"Tweet with id 12 should be included")
self.assertTrue("1565804013000" in timestamps,
"Tweet with id 13 should be included")
self.assertTrue("1565544813000" not in timestamps,
"Tweet with id 15 should not be included")
def test_no_truncated_field(self):
tweet_list = extractor.make_network("test/tweets",
retweets=True,
mentions=False)
retweeter_ids = []
for tweet in tweet_list:
id_str = tweet[0]
retweeter_ids.append(id_str)
self.assertTrue("16" in retweeter_ids,
"Retweeter with id 16 should be included")
def test_no_truncated_with_keyword(self):
tweet_list = extractor.make_network("test/tweets",
retweets=True,
mentions=False,
keywords=["field"])
retweeter_ids = []
for tweet in tweet_list:
id_str = tweet[0]
retweeter_ids.append(id_str)
self.assertTrue("16" in retweeter_ids,
"Retweeter with id 16 should be included with keyword")
def test_no_truncated_with_keyword_oneline_file(self):
tweet_list = extractor.make_network("test/one_liners",
tweet_per_line=False,
retweets=True,
mentions=False,
keywords=["field"]
)
retweeter_ids = []
for tweet in tweet_list:
id_str = tweet[0]
retweeter_ids.append(id_str)
self.assertTrue("16" in retweeter_ids,
"Retweeter with id 16 should be included with keyword")
def test_union_of_retweeters(self):
tweet_list = extractor.make_network("test/one_liners",
tweet_per_line=False,
retweets=True,
mentions=False,
senders_rt=["2"],
receivers_rt=["2"],
union_rt=True
)
retweeter_ids = []
retweeted_ids = []
for tweet in tweet_list:
retweeter = tweet[0]
retweeted = tweet[1]
retweeter_ids.append(retweeter)
retweeted_ids.append(retweeted)
self.assertTrue("2" in retweeter_ids,
"Retweeter with id 2 should be included")
self.assertTrue("2" in retweeted_ids,
"Retweeted with id 2 should be included")
def tearDown(self):
shutil.rmtree("test/tweets")
if __name__ == '__main__':
unittest.main()