]> code.communitydata.science - mediawiki_dump_tools.git/blobdiff - test/Wikiq_Unit_Test.py
migrate to mwxml. This completes the migration away from python-mediawiki-utilities...
[mediawiki_dump_tools.git] / test / Wikiq_Unit_Test.py
index b32d6a9f9b391779fbd78296df4d24dbcd2e8565..e43ee7defd31a2fcaa2eca23241d844354fb246e 100644 (file)
@@ -1,9 +1,10 @@
 import unittest
 import os
-import sys
 import subprocess
 from shutil import copyfile
-import pdb
+import pandas as pd
+from pandas.util.testing import assert_frame_equal
+from io import StringIO
 
 # with / without pwr DONE
 # with / without url encode DONE
@@ -32,21 +33,23 @@ class Test_Wikipedia(unittest.TestCase):
         self.baseline_output_dir = "baseline_output"
 
     def test_WP_url_encode(self):
+        test_filename =  "url-encode_" + self.wikiq_out_name
+        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.format(self.input_file, self.test_output_dir)
         call = call + " --url-encode"
         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
         proc.wait()
-        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)
+        copyfile(self.call_output, test_file)
+        baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
 
-        test_lines.close()
-        baseline_lines.close()
+        # as a test let's make sure that we get equal data frames
+        test = pd.read_table(test_file)
+        baseline = pd.read_table(baseline_file)
+        assert_frame_equal(test,baseline)
 
 
 class Test_Basic(unittest.TestCase):
@@ -68,77 +71,102 @@ class Test_Basic(unittest.TestCase):
 
     def test_noargs(self):
 
+        test_filename =  "noargs_" + self.wikiq_out_name
+        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.format(self.input_file, self.test_output_dir)
         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
         proc.wait()
-        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)
+        copyfile(self.call_output, 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)
+        baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+
+        test = pd.read_table(test_file)
+        baseline = pd.read_table(baseline_file)
+        assert_frame_equal(test,baseline)
 
-        test_lines.close()
-        baseline_lines.close()
 
     def test_collapse_user(self):
+        test_filename =  "collapse-user_" + self.wikiq_out_name
+        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.format(self.input_file, self.test_output_dir)
         call = call + " --collapse-user"
 
         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
         proc.wait()
 
-        test_file =  "collapse-user_" + self.wikiq_out_name
-        copyfile(self.call_output, os.path.join(self.test_output_dir, test_file))
+        copyfile(self.call_output, test_file)
 
-        baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
+        baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+        test = pd.read_table(test_file)
+        baseline = pd.read_table(baseline_file)
+        assert_frame_equal(test,baseline)
+
+    def test_pwr_legacy(self):
+        test_filename =  "persistence_legacy_" + self.wikiq_out_name
+        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.format(self.input_file, self.test_output_dir)
+        call = call + " --persistence-legacy"
+        proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
+        proc.wait()
+
+
+        copyfile(self.call_output, 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)
+        baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
 
-        test_lines.close()
-        baseline_lines.close()
+        test = pd.read_table(test_file)
+        baseline = pd.read_table(baseline_file)
+        assert_frame_equal(test,baseline)
 
     def test_pwr(self):
+        test_filename =  "persistence_" + self.wikiq_out_name
+        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.format(self.input_file, self.test_output_dir)
         call = call + " --persistence"
         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
         proc.wait()
 
-        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)
+        copyfile(self.call_output, 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)
+        baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+
+        test = pd.read_table(test_file)
+        baseline = pd.read_table(baseline_file)
+        assert_frame_equal(test,baseline)
 
-        test_lines.close()
-        baseline_lines.close()
 
     def test_url_encode(self):
+        test_filename =  "url-encode_" + self.wikiq_out_name
+
+        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.format(self.input_file, self.test_output_dir)
         call = call + " --url-encode"
         proc = subprocess.Popen(call,stdout=subprocess.PIPE,shell=True)
         proc.wait()
-        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)
+        copyfile(self.call_output, test_file)
+        baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+        test = pd.read_table(test_file)
+        baseline = pd.read_table(baseline_file)
+        assert_frame_equal(test,baseline)
 
-        test_lines.close()
-        baseline_lines.close()
 
 class Test_Malformed(unittest.TestCase):
 
@@ -182,28 +210,14 @@ class Test_Stdout(unittest.TestCase):
 
         call = self.base_call.format(self.input_file)
         proc = subprocess.run(call,stdout=subprocess.PIPE,shell=True)
-        outs = proc.stdout.decode('utf-8')
-        
+        outs = proc.stdout.decode("utf8")
+
         test_file = "noargs_" + self.wikiq_out_name
         baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
-
-        test_lines = outs.splitlines(True)
-        baseline_lines = open(baseline_file)
-        for test, baseline in zip(test_lines, baseline_lines):
-            self.assertEqual(test,baseline)
-
-        # 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()
-
+        print(baseline_file)
+        test = pd.read_table(StringIO(outs))
+        baseline = pd.read_table(baseline_file)
+        assert_frame_equal(test,baseline)
         
 if __name__ == '__main__':
     unittest.main()

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