X-Git-Url: https://code.communitydata.science/mediawiki_dump_tools.git/blobdiff_plain/c84844cfb58f343e6632f1a6c57f702bd96c616d..f147e1d899ea269dad715aea393153683353e946:/test/Wikiq_Unit_Test.py diff --git a/test/Wikiq_Unit_Test.py b/test/Wikiq_Unit_Test.py index 9011d79..159fd10 100644 --- a/test/Wikiq_Unit_Test.py +++ b/test/Wikiq_Unit_Test.py @@ -282,6 +282,119 @@ class Test_Stdout(unittest.TestCase): test = pd.read_table(StringIO(outs)) baseline = pd.read_table(baseline_file) assert_frame_equal(test,baseline) - + +class Test_Regex(unittest.TestCase): + + def setUp(self): + self.wiki = 'regextest' + self.wikiq_out_name = self.wiki + '.tsv' + self.infile = "{0}.xml.bz2".format(self.wiki) + + self.input_dir = "dumps" + self.input_file = os.path.join(".", self.input_dir,self.infile) + + if not os.path.exists("test_output"): + os.mkdir("test_output") + + self.test_output_dir = os.path.join(".", "test_output") + self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name) + # we have two base calls, one for checking inputs and the other for checking outputs + self.base_call = "../wikiq {0}" + self.base_call_outs = "../wikiq {0} -o {1}" + + self.baseline_output_dir = "baseline_output" + + # sample inputs for checking that bad inputs get terminated / test_regex_inputs + self.bad_input1 = "-RP '\\b\\d+\\b'" #label is missing + self.bad_input2 = "-RP 'NPO V' -RP THE -RPl testlabel" #number of reg and number of labels do not match + self.bad_input3 = "-CP '(Tamil|Li)' -RPl testlabel" #cp but rp label + self.bad_input4 = "-CPl testlabel" #regex is missing + self.bad_input5 = "-RP '\\b\\w{3}\\b' -RPl threeletters -CP '\\b\\w{3}\\b'" + + self.bad_inputs_list = [self.bad_input1,self.bad_input2,self.bad_input3,self.bad_input4,self.bad_input5] + + # sample inputs for checking the outcomes of good inputs / test_basic_regex + self.good_input1 = "-RP '\\b\\d{3}\\b' -RPl threedigits" + self.good_input2 = "-RP 'TestCase' -RP 'page' -RPl testcases -RPl page_word" + self.good_input3 = "-CP 'Chevalier' -CPl chev_com -RP 'welcome to Wikipedia' -RPl wiki_welcome -CP 'Warning' -CPl warning" + self.good_input4 = "-CP 'WP:EVADE' -CPl wp_evade" + + self.good_inputs_list = [self.good_input1,self.good_input2,self.good_input3, self.good_input4] + + # and with capture group(s) / test_capturegroup_regex + self.cap_input1 = "-RP 'Li Chevalier' -RPl li_cheval -CP '(?P\\b[a-zA-Z]{3}\\b)|(?P\\b\\d+\\b)|(?P\\bcat\\b)' -CPl three" + self.cap_input2 = "-CP '(?P\\bTestCaseA\\b)|(?P\\bTestCaseB\\b)|(?P\\bTestCaseC\\b)|(?P\\bTestCaseD\\b)' -CPl testcase -RP '(?Pnpov|NPOV)|(?Pneutral point of view)' -RPl npov" + + self.cap_inputs_list = [self.cap_input1,self.cap_input2] + + def test_regex_inputs(self): + for input in self.bad_inputs_list: + call = self.base_call.format(self.input_file) + call = call + " --stdout " + input + print(call) + proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) + stdout,stderr = proc.communicate() + #print(proc.returncode) + + # we want to check that the bad inputs were caught and sys.exit is stopping the code + print(stderr.decode("utf-8")) + self.assertNotEqual(proc.returncode,0) + + def test_basic_regex(self): + i = 1 + for input in self.good_inputs_list: + + test_filename = "basic_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i)) + #print(test_filename) + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) + + call = self.base_call_outs.format(self.input_file, self.test_output_dir) + call = call + " " + input + print(call) + + proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) + proc.wait() + + copyfile(self.call_output, test_file) + f = open(self.call_output, 'w') + f.close() + + # don't have a baseline file to compare a test to?? + # baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + i += 1 + + #TODO + #a proper assert statement still needs to go here, but I checked out the generated files for now and it functions + + + def test_capturegroup_regex(self): + i = 1 + for input in self.cap_inputs_list: + test_filename = "capturegroup_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i)) + print(test_filename) + test_file = os.path.join(self.test_output_dir, test_filename) + if os.path.exists(test_file): + os.remove(test_file) + + call = self.base_call_outs.format(self.input_file, self.test_output_dir) + call = call + " " + input + print(call) + + proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) + proc.wait() + + copyfile(self.call_output, test_file) + f = open(self.call_output, 'w') + f.close() + + # don't have a baseline file to compare a test to?? + # baseline_file = os.path.join(".", self.baseline_output_dir, test_filename) + i += 1 + + #TODO + #a proper assert statement still needs to go here, but I checked out the generated files for now and it functions + if __name__ == '__main__': unittest.main()