]> code.communitydata.science - mediawiki_dump_tools.git/blobdiff - test/Wikiq_Unit_Test.py
validate tests and add asserts and baselines for regex tests.
[mediawiki_dump_tools.git] / test / Wikiq_Unit_Test.py
index 159fd1088af1f3fbfeb9d10fb0810a099dd6ccc5..14d38f12c2521d1f858ec9fc782e8ac5ad5ca23b 100644 (file)
@@ -305,27 +305,33 @@ class Test_Regex(unittest.TestCase):
         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]
+        self.bad_inputs_list = [
+            #label is missing            
+            "-RP '\\b\\d+\\b'", 
+            #number of reg and number of labels do not match 
+            "-RP 'NPO V' -RP THE -RPl testlabel",
+            #cp but rp label
+            "-CP '(Tamil|Li)' -RPl testlabel",
+            #regex is missing
+            "-CPl testlabel",
+            "-RP '\\b\\w{3}\\b' -RPl threeletters -CP '\\b\\w{3}\\b'"
+        ]
 
         # 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 = [
+            "-RP '\\b\\d{3}\\b' -RPl threedigits",
+            "-RP 'TestCase' -RP 'page' -RPl testcases -RPl page_word",
+            "-CP 'Chevalier' -CPl chev_com -RP 'welcome to Wikipedia' -RPl wiki_welcome -CP 'Warning' -CPl warning",
+            "-CP 'WP:EVADE' -CPl wp_evade"         
+        ]
 
-        self.good_inputs_list = [self.good_input1,self.good_input2,self.good_input3, self.good_input4]
+        
+        self.cap_inputs_list = [
+            "-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",
+            "-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"
+        ]
 
-        # and with capture group(s) / test_capturegroup_regex
-        self.cap_input1 = "-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"
-        self.cap_input2 = "-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"
 
-        self.cap_inputs_list = [self.cap_input1,self.cap_input2]
 
     def test_regex_inputs(self):
         for input in self.bad_inputs_list:
@@ -341,8 +347,7 @@ class Test_Regex(unittest.TestCase):
             self.assertNotEqual(proc.returncode,0)
 
     def test_basic_regex(self):
-        i = 1
-        for input in self.good_inputs_list:
+        for i, input in enumerate(self.good_inputs_list):
 
             test_filename = "basic_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))
             #print(test_filename)
@@ -356,22 +361,18 @@ class Test_Regex(unittest.TestCase):
 
             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
+            test = pd.read_table(test_file)
+            
+            baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+            baseline = pd.read_table(baseline_file)
+            assert_frame_equal(test, baseline)
+            print(i)
 
 
     def test_capturegroup_regex(self):
-        i = 1
-        for input in self.cap_inputs_list:
+        for i, input in enumerate(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)
@@ -386,15 +387,13 @@ class Test_Regex(unittest.TestCase):
             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
+            test = pd.read_table(test_file)
+            
+            baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+            baseline = pd.read_table(baseline_file)
+            assert_frame_equal(test, baseline)
 
-            #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()

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