]> code.communitydata.science - mediawiki_dump_tools.git/blob - test/Wikiq_Unit_Test.py
write regex captures to parquet arrays.
[mediawiki_dump_tools.git] / test / Wikiq_Unit_Test.py
1 import unittest
2 import os
3 import subprocess
4 from shutil import copyfile
5 import pandas as pd
6 from pandas.testing import assert_frame_equal
7 from io import StringIO
8
9 # with / without pwr DONE
10 # with / without url encode DONE
11 # with / without collapse user DONE
12 # with output to sdtout DONE
13 # note that the persistence radius is 7 by default
14 # reading various file formats including
15 #        7z, gz, bz2, xml  DONE
16 # wikia and wikipedia data DONE
17 # malformed xmls DONE
18
19 class Test_Wikipedia(unittest.TestCase):
20     def setUp(self):
21         if not os.path.exists("test_output"):
22             os.mkdir("test_output")
23
24         self.wiki = 'ikwiki-20180301-pages-meta-history'
25         self.wikiq_out_name =  self.wiki + ".tsv"
26         self.test_output_dir = os.path.join(".", "test_output")
27         self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
28
29         self.infile = "{0}.xml.bz2".format(self.wiki)    
30         self.base_call = "../wikiq {0} -o {1}"
31         self.input_dir = "dumps"
32         self.input_file = os.path.join(".", self.input_dir,self.infile)
33         self.baseline_output_dir = "baseline_output"
34
35     def test_WP_url_encode(self):
36         test_filename =  "url-encode_" + self.wikiq_out_name
37         test_file = os.path.join(self.test_output_dir, test_filename)
38         if os.path.exists(test_file):
39             os.remove(test_file)
40         
41         call = self.base_call.format(self.input_file, self.test_output_dir)
42         call = call + " --url-encode"
43         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
44         proc.wait()
45
46         copyfile(self.call_output, test_file)
47         baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
48
49         # as a test let's make sure that we get equal data frames
50         test = pd.read_table(test_file)
51         baseline = pd.read_table(baseline_file)
52         assert_frame_equal(test,baseline, check_like=True)
53
54     def test_WP_namespaces(self):
55         print(os.path.abspath('.'))
56         test_filename =  "namespaces_" + self.wikiq_out_name
57         test_file = os.path.join(self.test_output_dir, test_filename)
58         if os.path.exists(test_file):
59             os.remove(test_file)
60         
61         call = self.base_call.format(self.input_file, self.test_output_dir)
62         call = call + " -n 0 -n 1"
63         print(call)
64         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
65         proc.wait()
66         copyfile(self.call_output, test_file)
67         baseline_file = os.path.join(os.path.abspath("."), self.baseline_output_dir, test_filename)
68
69         # as a test let's make sure that we get equal data frames
70         test = pd.read_table(test_file)
71         num_wrong_ns = sum(~ test.namespace.isin({0,1}))
72         self.assertEqual(num_wrong_ns, 0)
73         baseline = pd.read_table(baseline_file)
74         assert_frame_equal(test,baseline, check_like=True)
75
76     def test_WP_revert_radius(self):
77         print(os.path.abspath('.'))
78         test_filename =  "revert_radius_" + self.wikiq_out_name
79         test_file = os.path.join(self.test_output_dir, test_filename)
80         if os.path.exists(test_file):
81             os.remove(test_file)
82         
83         call = self.base_call.format(self.input_file, self.test_output_dir)
84         call = call + " -n 0 -n 1 -rr 1"
85         print(call)
86         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
87         proc.wait()
88         copyfile(self.call_output, test_file)
89         baseline_file = os.path.join(os.path.abspath("."), self.baseline_output_dir, test_filename)
90
91         # as a test let's make sure that we get equal data frames
92         test = pd.read_table(test_file)
93         num_wrong_ns = sum(~ test.namespace.isin({0,1}))
94         self.assertEqual(num_wrong_ns, 0)
95         baseline = pd.read_table(baseline_file)
96         assert_frame_equal(test,baseline, check_like=True)
97
98
99
100 class Test_Basic(unittest.TestCase):
101
102     def setUp(self):
103         if not os.path.exists("test_output"):
104             os.mkdir("test_output")
105
106         self.wiki = 'sailormoon'
107         self.wikiq_out_name =  self.wiki + ".tsv"
108         self.test_output_dir = os.path.join(".", "test_output")
109         self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
110
111         self.infile = "{0}.xml.7z".format(self.wiki)
112         self.base_call = "../wikiq {0} -o {1}"
113         self.input_dir = "dumps"
114         self.input_file = os.path.join(".", self.input_dir,self.infile)
115         self.baseline_output_dir = "baseline_output"
116
117     def test_noargs(self):
118
119         test_filename =  "noargs_" + self.wikiq_out_name
120         test_file = os.path.join(self.test_output_dir, test_filename)
121         if os.path.exists(test_file):
122             os.remove(test_file)
123         
124         call = self.base_call.format(self.input_file, self.test_output_dir)
125         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
126         proc.wait()
127
128         copyfile(self.call_output, test_file)
129
130         baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
131
132         test = pd.read_table(test_file)
133         baseline = pd.read_table(baseline_file)
134         assert_frame_equal(test,baseline, check_like=True)
135
136
137     def test_collapse_user(self):
138         test_filename =  "collapse-user_" + self.wikiq_out_name
139         test_file = os.path.join(self.test_output_dir, test_filename)
140         if os.path.exists(test_file):
141             os.remove(test_file)
142         
143         call = self.base_call.format(self.input_file, self.test_output_dir)
144         call = call + " --collapse-user"
145
146         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
147         proc.wait()
148
149         copyfile(self.call_output, test_file)
150
151         baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
152         test = pd.read_table(test_file)
153         baseline = pd.read_table(baseline_file)
154         assert_frame_equal(test,baseline, check_like=True)
155
156     def test_pwr_segment(self):
157         test_filename =  "persistence_segment_" + self.wikiq_out_name
158         test_file = os.path.join(self.test_output_dir, test_filename)
159         if os.path.exists(test_file):
160             os.remove(test_file)
161         
162         call = self.base_call.format(self.input_file, self.test_output_dir)
163         call = call + " --persistence segment"
164         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
165         proc.wait()
166
167
168         copyfile(self.call_output, test_file)
169
170         baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
171
172         test = pd.read_table(test_file)
173         baseline = pd.read_table(baseline_file)
174         assert_frame_equal(test,baseline, check_like=True)
175
176     def test_pwr_legacy(self):
177         test_filename =  "persistence_legacy_" + self.wikiq_out_name
178         test_file = os.path.join(self.test_output_dir, test_filename)
179         if os.path.exists(test_file):
180             os.remove(test_file)
181         
182         call = self.base_call.format(self.input_file, self.test_output_dir)
183         call = call + " --persistence legacy"
184         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
185         proc.wait()
186
187
188         copyfile(self.call_output, test_file)
189
190         baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
191
192         test = pd.read_table(test_file)
193         baseline = pd.read_table(baseline_file)
194         assert_frame_equal(test,baseline, check_like=True)
195
196     def test_pwr(self):
197         test_filename =  "persistence_" + self.wikiq_out_name
198         test_file = os.path.join(self.test_output_dir, test_filename)
199         if os.path.exists(test_file): 
200            os.remove(test_file)
201         
202         call = self.base_call.format(self.input_file, self.test_output_dir)
203         call = call + " --persistence"
204         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
205         proc.wait()
206
207
208         copyfile(self.call_output, test_file)
209
210         baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
211
212         test = pd.read_table(test_file)
213         baseline = pd.read_table(baseline_file)
214
215         test = test.reindex(columns=sorted(test.columns))
216         assert_frame_equal(test,baseline, check_like=True)
217
218
219     def test_url_encode(self):
220         test_filename =  "url-encode_" + self.wikiq_out_name
221
222         test_file = os.path.join(self.test_output_dir, test_filename)
223         if os.path.exists(test_file):
224             os.remove(test_file)
225         
226         call = self.base_call.format(self.input_file, self.test_output_dir)
227         call = call + " --url-encode"
228         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
229         proc.wait()
230
231         copyfile(self.call_output, test_file)
232         baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
233         test = pd.read_table(test_file)
234         baseline = pd.read_table(baseline_file)
235
236         test = test.reindex(columns=sorted(test.columns))
237         assert_frame_equal(test,baseline, check_like=True)
238
239
240 class Test_Malformed(unittest.TestCase):
241     def setUp(self):
242         if not os.path.exists("test_output"):
243             os.mkdir("test_output")
244
245         self.wiki = 'twinpeaks'
246         self.wikiq_out_name =  self.wiki + ".tsv"
247         self.test_output_dir = os.path.join(".", "test_output")
248         self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) 
249
250         self.infile = "{0}.xml.7z".format(self.wiki)
251         self.base_call = "../wikiq {0} -o {1}"
252         self.input_dir = "dumps"
253         self.input_file = os.path.join(".", self.input_dir,self.infile)
254
255
256     def test_malformed_noargs(self):
257
258         call = self.base_call.format(self.input_file, self.test_output_dir)
259         proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
260         proc.wait()
261         outs, errs = proc.communicate()
262         errlines = str(errs).split("\\n")
263         self.assertEqual(errlines[-2],'xml.etree.ElementTree.ParseError: no element found: line 1369, column 0')
264
265
266 class Test_Redirect_Ikwiki(unittest.TestCase):
267     def setUp(self):
268         self.wiki = 'ikwiki-20180301-pages-meta-history'
269         wiki_siteinfo = "ikwiki-20191101-siteinfo-namespaces.json.gz"
270         self.input_dir = "dumps"
271         self.wiki_siteinfo = os.path.join(".", self.input_dir, wiki_siteinfo)
272         self.wikiq_out_name =  self.wiki + ".tsv"
273         self.infile = "{0}.xml.bz2".format(self.wiki)    
274
275         self.base_call = "../wikiq {0} --siteinfo {1} -o {2}"
276         self.input_dir = "dumps"
277         self.input_file = os.path.join(".", self.input_dir, self.infile)
278         self.baseline_output_dir = "baseline_output"
279
280         self.test_output_dir = os.path.join(".", "test_output")
281         self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
282
283         self.infile = "{0}.xml.bz2".format(self.wiki)    
284
285     def test_redirect(self):
286
287         call = self.base_call.format(self.input_file, self.wiki_siteinfo, self.test_output_dir)
288         print(call)
289         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
290         proc.wait()
291
292         test_file = os.path.join("redirect_" + self.wikiq_out_name)
293         copyfile(self.call_output, test_file)
294         baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
295
296         # as a test let's make sure that we get equal data frames
297         test = pd.read_table(os.path.join(self.test_output_dir, test_file))
298
299         baseline = pd.read_table(baseline_file)
300         assert_frame_equal(test,baseline, check_like=True)
301
302
303
304 class Test_Redirect_Wikia(unittest.TestCase):
305     def setUp(self):
306         self.wiki = 'pokemonfandomcom_fr-20200215-history'
307         wiki_siteinfo = "pokemonfandomcom_fr-20200215-history.xml.7z"
308         self.input_dir = "dumps"
309         self.wiki_siteinfo = os.path.join(".", self.input_dir, wiki_siteinfo)
310         self.wikiq_out_name =  self.wiki + ".tsv"
311         self.infile = "{0}.xml.7z".format(self.wiki)    
312
313         self.base_call = "../wikiq {0} --siteinfo {1} -o {2}"
314         self.input_dir = "dumps"
315         self.input_file = os.path.join(".", self.input_dir, self.infile)
316         self.baseline_output_dir = "baseline_output"
317
318         self.test_output_dir = os.path.join(".", "test_output")
319         self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
320
321         self.infile = "{0}.xml.7z".format(self.wiki)    
322
323     def test_redirect(self):
324
325         call = self.base_call.format(self.input_file, self.wiki_siteinfo, self.test_output_dir)
326         print(call)
327         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
328         proc.wait()
329
330         test_file = os.path.join("redirect_" + self.wikiq_out_name)
331         copyfile(self.call_output, test_file)
332         baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
333
334         # as a test let's make sure that we get equal data frames
335         test = pd.read_table(os.path.join(self.test_output_dir, test_file))
336         baseline = pd.read_table(baseline_file)
337         assert_frame_equal(test,baseline, check_like=True)
338
339
340     
341 class Test_Stdout(unittest.TestCase):
342
343     def setUp(self):
344         self.wiki = 'sailormoon'
345         self.wikiq_out_name =  self.wiki + ".tsv"
346
347         self.infile = "{0}.xml.7z".format(self.wiki)
348         self.base_call = "../wikiq {0} --stdout"
349         self.input_dir = "dumps"
350         self.input_file = os.path.join(".", self.input_dir,self.infile)
351         self.baseline_output_dir = "baseline_output"
352
353     def test_noargs(self):
354
355         call = self.base_call.format(self.input_file)
356         print(call)
357         proc = subprocess.run(call,stdout=subprocess.PIPE,shell=True)
358         outs = proc.stdout.decode("utf8")
359
360         test_file = "noargs_" + self.wikiq_out_name
361         baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
362         print(baseline_file)
363         s = StringIO(outs)
364         test = pd.read_table(s)
365         baseline = pd.read_table(baseline_file)
366         assert_frame_equal(test,baseline, check_like=True)
367         
368
369 class Test_Regex(unittest.TestCase):
370
371     def setUp(self):
372         self.wiki = 'regextest'
373         self.wikiq_out_name = self.wiki + '.tsv'
374         self.infile = "{0}.xml.bz2".format(self.wiki)
375
376         self.input_dir = "dumps"
377         self.input_file = os.path.join(".", self.input_dir,self.infile)
378
379         if not os.path.exists("test_output"):
380             os.mkdir("test_output")
381
382         self.test_output_dir = os.path.join(".", "test_output")
383         self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
384         # we have two base calls, one for checking inputs and the other for checking outputs
385         self.base_call = "../wikiq {0}"
386         self.base_call_outs = "../wikiq {0} -o {1}"
387
388         self.baseline_output_dir = "baseline_output"
389
390         # sample inputs for checking that bad inputs get terminated / test_regex_inputs
391         self.bad_inputs_list = [
392             #label is missing            
393             "-RP '\\b\\d+\\b'", 
394             #number of reg and number of labels do not match 
395             "-RP 'NPO V' -RP THE -RPl testlabel",
396             #cp but rp label
397             "-CP '(Tamil|Li)' -RPl testlabel",
398             #regex is missing
399             "-CPl testlabel",
400             "-RP '\\b\\w{3}\\b' -RPl threeletters -CP '\\b\\w{3}\\b'"
401         ]
402
403         # sample inputs for checking the outcomes of good inputs / test_basic_regex
404         self.good_inputs_list = [
405             "-RP '\\b\\d{3}\\b' -RPl threedigits",
406             "-RP 'TestCase' -RP 'page' -RPl testcases -RPl page_word",
407             "-CP 'Chevalier' -CPl chev_com -RP 'welcome to Wikipedia' -RPl wiki_welcome -CP 'Warning' -CPl warning",
408             "-CP 'WP:EVADE' -CPl wp_evade"         
409         ]
410
411         
412         self.cap_inputs_list = [
413             "-RP 'Li Chevalier' -RPl li_cheval -CP '(?P<letter>\\b[a-zA-Z]{3}\\b)|(?P<number>\\b\\d+\\b)|(?P<cat>\\bcat\\b)' -CPl three",
414             "-CP '(?P<a>\\bTestCaseA\\b)|(?P<b>\\bTestCaseB\\b)|(?P<c>\\bTestCaseC\\b)|(?P<d>\\bTestCaseD\\b)' -CPl testcase -RP '(?P<npov>npov|NPOV)|(?P<neutral>neutral point of view)' -RPl npov"
415         ]
416
417
418
419     def test_regex_inputs(self):
420         for inpt in self.bad_inputs_list:
421             call = self.base_call.format(self.input_file)
422             call = call + " --stdout " + inpt
423             print(call)
424             proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
425             stdout,stderr = proc.communicate()
426             #print(proc.returncode)
427             
428             # we want to check that the bad inputs were caught and sys.exit is stopping the code
429             print(stderr.decode("utf-8"))
430             self.assertNotEqual(proc.returncode,0)
431
432     def test_basic_regex(self):
433         for i, inpt in enumerate(self.good_inputs_list):
434
435             test_filename = "basic_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))
436             #print(test_filename)
437             test_file = os.path.join(self.test_output_dir, test_filename)
438             if os.path.exists(test_file):
439                 os.remove(test_file)
440
441             call = self.base_call_outs.format(self.input_file, self.test_output_dir)
442             call = call + " " + inpt
443             print(call)
444
445             proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
446             proc.wait()
447             copyfile(self.call_output, test_file)
448
449             test = pd.read_table(test_file)
450             
451             baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
452             baseline = pd.read_table(baseline_file)
453             assert_frame_equal(test, baseline, check_like=True)
454             print(i)
455
456
457     def test_capturegroup_regex(self):
458         for i, inpt in enumerate(self.cap_inputs_list):
459             test_filename = "capturegroup_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))
460             print(test_filename)
461             test_file = os.path.join(self.test_output_dir, test_filename)
462             if os.path.exists(test_file):
463                 os.remove(test_file)
464
465             call = self.base_call_outs.format(self.input_file, self.test_output_dir)
466             call = call + " " + inpt
467             print(call)
468
469             proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
470             proc.wait()
471
472             copyfile(self.call_output, test_file)
473             
474             test = pd.read_table(test_file)
475             
476             baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
477             baseline = pd.read_table(baseline_file)
478             assert_frame_equal(test, baseline, check_like=True)
479
480
481 if __name__ == '__main__':
482     unittest.main()

Community Data Science Collective || Want to submit a patch?