-import unittest
-import os
-import sys
-from shutil import copyfile
-
-# with / without pwr DONE
-# with / without url encode DONE
-# with / without collapse user DONE
-# with output to sdtout
-# note that the persistence radius is 7 by default
-# reading various file formats including
-# 7z, gz, bz2, xml
-# wikia and wikipedia data
-# malformed xmls
-
-class Test_Basic(unittest.TestCase):
-
- def setUp(self):
- if not os.path.exists("test_output"):
- os.mkdir("test_output")
-
- self.wiki = 'sailormoon'
- self.wikiq_out_name = self.wiki + ".tsv"
- self.test_output_dir = os.path.join(".", "test_output")
- self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
-
- self.infile = "{0}.xml.7z".format(self.wiki)
- self.base_call = "../wikiq {0} -o {1}"
- self.input_dir = "dumps"
- self.input_file = os.path.join(".", self.input_dir,self.infile)
- self.baseline_output_dir = "baseline_output"
-
- def test_noargs(self):
-
- call = self.base_call.format(self.input_file, self.test_output_dir)
- os.system(call)
-
- test_file = "noargs_" + self.wikiq_out_name
- copyfile(self.call_output, os.path.join(self.test_output_dir, test_file))
-
- baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
-
- test_lines = open(os.path.join(self.test_output_dir,test_file))
- baseline_lines = open(baseline_file)
- for test, baseline in zip(test_lines, baseline_lines):
- self.assertEqual(test,baseline)
-
- test_lines.close()
- baseline_lines.close()
-
- def test_collapse_user(self):
- call = self.base_call.format(self.input_file, self.test_output_dir)
- os.system(call)
- call = call + " --collapse-user"
-
- os.system(call)
-
- test_file = "collapse-user_" + self.wikiq_out_name
- copyfile(self.call_output, os.path.join(self.test_output_dir, test_file))
-
- baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
-
- test_lines = open(os.path.join(self.test_output_dir,test_file))
- baseline_lines = open(baseline_file)
- for test, baseline in zip(test_lines, baseline_lines):
- self.assertEqual(test,baseline)
-
- test_lines.close()
- baseline_lines.close()
-
- def test_pwr(self):
- call = self.base_call.format(self.input_file, self.test_output_dir)
- call = call + " --persistence"
- os.system(call)
-
- test_file = "persistence_" + self.wikiq_out_name
- copyfile(self.call_output, os.path.join(self.test_output_dir, test_file))
-
- baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
-
- test_lines = open(os.path.join(self.test_output_dir,test_file))
- baseline_lines = open(baseline_file)
- for test, baseline in zip(test_lines, baseline_lines):
- self.assertEqual(test,baseline)
-
- test_lines.close()
- baseline_lines.close()
-
- def test_url_encode(self):
- call = self.base_call.format(self.input_file, self.test_output_dir)
- call = call + " --url-encode"
- os.system(call)
- test_file = "url-encode_" + self.wikiq_out_name
- copyfile(self.call_output, os.path.join(self.test_output_dir, test_file))
- baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
-
- test_lines = open(os.path.join(self.test_output_dir,test_file))
- baseline_lines = open(baseline_file)
- for test, baseline in zip(test_lines, baseline_lines):
- self.assertEqual(test,baseline)
-
- test_lines.close()
- baseline_lines.close()
-
-
-
-if __name__ == '__main__':
- unittest.main()
+import unittest\r
+import os\r
+import subprocess\r
+from shutil import copyfile\r
+import pandas as pd\r
+from pandas.util.testing import assert_frame_equal\r
+from io import StringIO\r
+\r
+# with / without pwr DONE\r
+# with / without url encode DONE\r
+# with / without collapse user DONE\r
+# with output to sdtout DONE\r
+# note that the persistence radius is 7 by default\r
+# reading various file formats including\r
+# 7z, gz, bz2, xml DONE\r
+# wikia and wikipedia data DONE\r
+# malformed xmls DONE\r
+\r
+class Test_Wikipedia(unittest.TestCase):\r
+ def setUp(self):\r
+ if not os.path.exists("test_output"):\r
+ os.mkdir("test_output")\r
+\r
+ self.wiki = 'ikwiki-20180301-pages-meta-history'\r
+ self.wikiq_out_name = self.wiki + ".tsv"\r
+ self.test_output_dir = os.path.join(".", "test_output")\r
+ self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)\r
+\r
+ self.infile = "{0}.xml.bz2".format(self.wiki) \r
+ self.base_call = "../wikiq {0} -o {1}"\r
+ self.input_dir = "dumps"\r
+ self.input_file = os.path.join(".", self.input_dir,self.infile)\r
+ self.baseline_output_dir = "baseline_output"\r
+\r
+ def test_WP_url_encode(self):\r
+ test_filename = "url-encode_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " --url-encode"\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+ copyfile(self.call_output, test_file)\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+\r
+ # as a test let's make sure that we get equal data frames\r
+ test = pd.read_table(test_file)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+ def test_WP_namespaces(self):\r
+ print(os.path.abspath('.'))\r
+ test_filename = "namespaces_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " -n 0 -n 1"\r
+ print(call)\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+ copyfile(self.call_output, test_file)\r
+ baseline_file = os.path.join(os.path.abspath("."), self.baseline_output_dir, test_filename)\r
+\r
+ # as a test let's make sure that we get equal data frames\r
+ test = pd.read_table(test_file)\r
+ num_wrong_ns = sum(~ test.namespace.isin({0,1}))\r
+ self.assertEqual(num_wrong_ns, 0)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+ def test_WP_revert_radius(self):\r
+ print(os.path.abspath('.'))\r
+ test_filename = "revert_radius_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " -n 0 -n 1 -rr 1"\r
+ print(call)\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+ copyfile(self.call_output, test_file)\r
+ baseline_file = os.path.join(os.path.abspath("."), self.baseline_output_dir, test_filename)\r
+\r
+ # as a test let's make sure that we get equal data frames\r
+ test = pd.read_table(test_file)\r
+ num_wrong_ns = sum(~ test.namespace.isin({0,1}))\r
+ self.assertEqual(num_wrong_ns, 0)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+\r
+\r
+class Test_Basic(unittest.TestCase):\r
+\r
+ def setUp(self):\r
+ if not os.path.exists("test_output"):\r
+ os.mkdir("test_output")\r
+\r
+ self.wiki = 'sailormoon'\r
+ self.wikiq_out_name = self.wiki + ".tsv"\r
+ self.test_output_dir = os.path.join(".", "test_output")\r
+ self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)\r
+\r
+ self.infile = "{0}.xml.7z".format(self.wiki)\r
+ self.base_call = "../wikiq {0} -o {1}"\r
+ self.input_dir = "dumps"\r
+ self.input_file = os.path.join(".", self.input_dir,self.infile)\r
+ self.baseline_output_dir = "baseline_output"\r
+\r
+ def test_noargs(self):\r
+\r
+ test_filename = "noargs_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+ copyfile(self.call_output, test_file)\r
+\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+\r
+ test = pd.read_table(test_file)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+\r
+ def test_collapse_user(self):\r
+ test_filename = "collapse-user_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " --collapse-user"\r
+\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+ copyfile(self.call_output, test_file)\r
+\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+ test = pd.read_table(test_file)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+ def test_pwr_segment(self):\r
+ test_filename = "persistence_segment_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " --persistence segment"\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+\r
+ copyfile(self.call_output, test_file)\r
+\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+\r
+ test = pd.read_table(test_file)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+ def test_pwr_legacy(self):\r
+ test_filename = "persistence_legacy_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " --persistence legacy"\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+\r
+ copyfile(self.call_output, test_file)\r
+\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+\r
+ test = pd.read_table(test_file)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+ def test_pwr(self):\r
+ test_filename = "persistence_" + self.wikiq_out_name\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file): \r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " --persistence"\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+\r
+ copyfile(self.call_output, test_file)\r
+\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+\r
+ test = pd.read_table(test_file)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+\r
+ def test_url_encode(self):\r
+ test_filename = "url-encode_" + self.wikiq_out_name\r
+\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+ \r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ call = call + " --url-encode"\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+ copyfile(self.call_output, test_file)\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+ test = pd.read_table(test_file)\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+\r
+class Test_Malformed(unittest.TestCase):\r
+ def setUp(self):\r
+ if not os.path.exists("test_output"):\r
+ os.mkdir("test_output")\r
+\r
+ self.wiki = 'twinpeaks'\r
+ self.wikiq_out_name = self.wiki + ".tsv"\r
+ self.test_output_dir = os.path.join(".", "test_output")\r
+ self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)\r
+\r
+ self.infile = "{0}.xml.7z".format(self.wiki)\r
+ self.base_call = "../wikiq {0} -o {1}"\r
+ self.input_dir = "dumps"\r
+ self.input_file = os.path.join(".", self.input_dir,self.infile)\r
+\r
+\r
+ def test_malformed_noargs(self):\r
+\r
+ call = self.base_call.format(self.input_file, self.test_output_dir)\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)\r
+ proc.wait()\r
+ outs, errs = proc.communicate()\r
+ errlines = str(errs).split("\\n")\r
+ self.assertEqual(errlines[-2],'xml.etree.ElementTree.ParseError: no element found: line 1369, column 0')\r
+\r
+class Test_Stdout(unittest.TestCase):\r
+\r
+ def setUp(self):\r
+ self.wiki = 'sailormoon'\r
+ self.wikiq_out_name = self.wiki + ".tsv"\r
+\r
+ self.infile = "{0}.xml.7z".format(self.wiki)\r
+ self.base_call = "../wikiq {0} --stdout"\r
+ self.input_dir = "dumps"\r
+ self.input_file = os.path.join(".", self.input_dir,self.infile)\r
+ self.baseline_output_dir = "baseline_output"\r
+\r
+ def test_noargs(self):\r
+\r
+ call = self.base_call.format(self.input_file)\r
+ proc = subprocess.run(call,stdout=subprocess.PIPE,shell=True)\r
+ outs = proc.stdout.decode("utf8")\r
+\r
+ test_file = "noargs_" + self.wikiq_out_name\r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_file)\r
+ print(baseline_file)\r
+ test = pd.read_table(StringIO(outs))\r
+ baseline = pd.read_table(baseline_file)\r
+ assert_frame_equal(test,baseline)\r
+\r
+class Test_Regex(unittest.TestCase):\r
+\r
+ def setUp(self):\r
+ self.wiki = 'emptytext'\r
+ self.wikiq_out_name = self.wiki + '.tsv'\r
+ self.infile = "{0}.xml.bz2".format(self.wiki)\r
+\r
+ self.input_dir = "dumps"\r
+ self.input_file = os.path.join(".", self.input_dir,self.infile)\r
+\r
+ if not os.path.exists("test_output"):\r
+ os.mkdir("test_output")\r
+\r
+ self.test_output_dir = os.path.join(".", "test_output")\r
+ self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)\r
+ # we have two base calls, one for checking inputs and the other for checking outputs\r
+ self.base_call = "../wikiq {0}"\r
+ self.base_call_outs = "../wikiq {0} -o {1}"\r
+\r
+ self.baseline_output_dir = "baseline_output"\r
+\r
+ # sample inputs for checking that bad inputs get terminated / test_regex_inputs\r
+ self.bad_inputs_list = [\r
+ #label is missing \r
+ "-RP '\\b\\d+\\b'", \r
+ #number of reg and number of labels do not match \r
+ "-RP 'NPO V' -RP THE -RPl testlabel",\r
+ #cp but rp label\r
+ "-CP '(Tamil|Li)' -RPl testlabel",\r
+ #regex is missing\r
+ "-CPl testlabel",\r
+ "-RP '\\b\\w{3}\\b' -RPl threeletters -CP '\\b\\w{3}\\b'"\r
+ ]\r
+\r
+ # sample inputs for checking the outcomes of good inputs / test_basic_regex\r
+ self.good_inputs_list = [\r
+ "-RP '\\b\\d{3}\\b' -RPl threedigits",\r
+ #"-RP 'TestCase' -RP 'page' -RPl testcases -RPl page_word",\r
+ "-RP '(\\b[a-zA-Z]{3}\\b)' -RPl 3LETTERS -RP '(\\b(1[\d+])|(2[\d+])\\b)' -RPl NUMBERS",\r
+ "-CP 'Chevalier' -CPl chev_com -RP 'welcome to Wikipedia' -RPl wiki_welcome -CP 'Warning' -CPl warning",\r
+ "-CP 'WP:EVADE' -CPl wp_evade" \r
+ ]\r
+\r
+ \r
+ self.cap_inputs_list = [\r
+ "-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",\r
+ "-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"\r
+ ]\r
+\r
+\r
+\r
+ def test_regex_inputs(self):\r
+ for input in self.bad_inputs_list:\r
+ call = self.base_call.format(self.input_file)\r
+ call = call + " --stdout " + input\r
+ #print(call)\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)\r
+ stdout,stderr = proc.communicate()\r
+ #print(proc.returncode)\r
+ \r
+ # we want to check that the bad inputs were caught and sys.exit is stopping the code\r
+ #print(stderr.decode("utf-8"))\r
+ self.assertNotEqual(proc.returncode,0)\r
+\r
+ def test_basic_regex(self):\r
+ for i, input in enumerate(self.good_inputs_list):\r
+\r
+ test_filename = "basic_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))\r
+ #print(test_filename)\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+\r
+ call = self.base_call_outs.format(self.input_file, self.test_output_dir)\r
+ call = call + " " + input\r
+ #print(call)\r
+\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+ copyfile(self.call_output, test_file)\r
+\r
+ test = pd.read_table(test_file)\r
+ \r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+ baseline = pd.read_table(baseline_file)\r
+ #assert_frame_equal(test, baseline)\r
+ #print(i)\r
+\r
+\r
+ def test_capturegroup_regex(self):\r
+ for i, input in enumerate(self.cap_inputs_list):\r
+ test_filename = "capturegroup_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))\r
+ #print(test_filename)\r
+ test_file = os.path.join(self.test_output_dir, test_filename)\r
+ if os.path.exists(test_file):\r
+ os.remove(test_file)\r
+\r
+ call = self.base_call_outs.format(self.input_file, self.test_output_dir)\r
+ call = call + " " + input\r
+ #print(call)\r
+\r
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)\r
+ proc.wait()\r
+\r
+ copyfile(self.call_output, test_file)\r
+ \r
+ test = pd.read_table(test_file)\r
+ \r
+ baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)\r
+ baseline = pd.read_table(baseline_file)\r
+ #assert_frame_equal(test, baseline)\r
+\r
+\r
+if __name__ == '__main__':\r
+ unittest.main()\r