Standardize calling for wikiq in tests

This way failures show the output of stderr/etc.

Also create path constant strings for use in tests to avoid repetition
and make changes easier.

Signed-off-by: Will Beason <willbeason@gmail.com>
This commit is contained in:
Will Beason 2025-05-27 14:27:49 -05:00
parent ebc57864f2
commit 4d3900b541

View File

@ -13,6 +13,27 @@ from typing import Final
TEST_DIR: Final[str] = os.path.dirname(os.path.realpath(__file__))
WIKIQ: Final[str] = os.path.join(os.path.dirname(TEST_DIR), "wikiq")
TEST_OUTPUT_DIR: Final[str] = os.path.join(TEST_DIR, "test_output")
BASELINE_OUTPUT_DIR: Final[str] = os.path.join(TEST_DIR, "baseline_output")
def setup():
tracemalloc.start()
# Perform directory check and reset here as this is a one-time setup step as opposed to per-test setup.
if not os.path.exists(TEST_OUTPUT_DIR):
os.mkdir(TEST_OUTPUT_DIR)
else:
# Avoid subsequent calls to tests interfering with each other.
# Otherwise, a test may erroneously pass if the program has no output
# but a previous run output what was expected.
for f in os.listdir(TEST_OUTPUT_DIR):
os.remove(os.path.join(TEST_OUTPUT_DIR, f))
setup()
def call_wikiq(*args: str):
call = ' '.join([WIKIQ, *args])
print(call)
subprocess.check_output(call, stderr=subprocess.PIPE, shell=True)
# with / without pwr DONE
# with / without url encode DONE
@ -26,16 +47,14 @@ TEST_OUTPUT_DIR: Final[str] = os.path.join(TEST_DIR, "test_output")
class Test_Wikipedia(unittest.TestCase):
def setUp(self):
self.wiki = 'ikwiki-20180301-pages-meta-history'
self.wikiq_out_name = self.wiki + ".tsv"
wiki = 'ikwiki-20180301-pages-meta-history'
self.wikiq_out_name = wiki + ".tsv"
self.call_output = os.path.join(TEST_OUTPUT_DIR, self.wikiq_out_name)
self.infile = "{0}.xml.bz2".format(self.wiki)
infile = "{0}.xml.bz2".format(wiki)
self.base_call = WIKIQ + " {0} -o {1}"
self.input_dir = os.path.join(TEST_DIR, "dumps")
self.input_file = os.path.join(TEST_DIR, self.input_dir, self.infile)
self.baseline_output_dir = os.path.join(TEST_DIR, "baseline_output")
input_dir = os.path.join(TEST_DIR, "dumps")
self.input_file = os.path.join(TEST_DIR, input_dir, infile)
def test_WP_url_encode(self):
test_filename = "url-encode_" + self.wikiq_out_name
@ -43,17 +62,13 @@ class Test_Wikipedia(unittest.TestCase):
if os.path.exists(test_file):
os.remove(test_file)
call = self.base_call.format(self.input_file, TEST_OUTPUT_DIR)
call = call + " --url-encode"
print(call)
try:
subprocess.check_output(call, stderr=subprocess.PIPE, shell=True)
call_wikiq(self.input_file, "-o", TEST_OUTPUT_DIR, "--url-encode")
except subprocess.CalledProcessError as exc:
print(exc.stderr.decode("utf8"))
self.fail()
self.fail(exc.stderr.decode("utf8"))
copyfile(self.call_output, test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
# as a test let's make sure that we get equal data frames
test = pd.read_table(test_file)
@ -67,15 +82,14 @@ class Test_Wikipedia(unittest.TestCase):
if os.path.exists(test_file):
os.remove(test_file)
call = self.base_call.format(self.input_file, TEST_OUTPUT_DIR)
call = call + " -n 0 -n 1"
print(call)
with subprocess.Popen(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) as proc:
proc.wait()
self.assertEqual(proc.returncode, 0)
try:
call_wikiq(self.input_file, "-o", TEST_OUTPUT_DIR,
"-n 0", "-n 1")
except subprocess.CalledProcessError as exc:
self.fail(exc.stderr.decode("utf8"))
copyfile(self.call_output, test_file)
baseline_file = os.path.join(os.path.abspath("."), self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
# as a test let's make sure that we get equal data frames
test = pd.read_table(test_file)
@ -91,15 +105,14 @@ class Test_Wikipedia(unittest.TestCase):
if os.path.exists(test_file):
os.remove(test_file)
call = self.base_call.format(self.input_file, TEST_OUTPUT_DIR)
call = call + " -n 0 -n 1 -rr 1"
print(call)
with subprocess.Popen(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) as proc:
proc.wait()
self.assertEqual(proc.returncode, 0)
try:
call_wikiq(self.input_file, "-o", TEST_OUTPUT_DIR,
"-n 0", "-n 1", "-rr 1")
except subprocess.CalledProcessError as exc:
self.fail(exc.stderr.decode("utf8"))
copyfile(self.call_output, test_file)
baseline_file = os.path.join(os.path.abspath("."), self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
# as a test let's make sure that we get equal data frames
test = pd.read_table(test_file)
@ -120,7 +133,6 @@ class Test_Basic(unittest.TestCase):
self.base_call = WIKIQ + " {0} -o {1}"
self.input_dir = os.path.join(TEST_DIR, "dumps")
self.input_file = os.path.join(TEST_DIR, self.input_dir, self.infile)
self.baseline_output_dir = os.path.join(TEST_DIR, "baseline_output")
def test_noargs(self):
@ -137,7 +149,7 @@ class Test_Basic(unittest.TestCase):
copyfile(self.call_output, test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
test = pd.read_table(test_file)
baseline = pd.read_table(baseline_file)
@ -158,7 +170,7 @@ class Test_Basic(unittest.TestCase):
copyfile(self.call_output, test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
test = pd.read_table(test_file)
baseline = pd.read_table(baseline_file)
assert_frame_equal(test, baseline, check_like=True)
@ -178,7 +190,7 @@ class Test_Basic(unittest.TestCase):
copyfile(self.call_output, test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
test = pd.read_table(test_file)
baseline = pd.read_table(baseline_file)
@ -199,7 +211,7 @@ class Test_Basic(unittest.TestCase):
copyfile(self.call_output, test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
test = pd.read_table(test_file)
baseline = pd.read_table(baseline_file)
@ -220,7 +232,7 @@ class Test_Basic(unittest.TestCase):
copyfile(self.call_output, test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
test = pd.read_table(test_file)
baseline = pd.read_table(baseline_file)
@ -242,7 +254,7 @@ class Test_Basic(unittest.TestCase):
self.assertEqual(proc.returncode, 0)
copyfile(self.call_output, test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
test = pd.read_table(test_file)
baseline = pd.read_table(baseline_file)
@ -282,7 +294,6 @@ class Test_Stdout(unittest.TestCase):
self.base_call = WIKIQ + " {0} --stdout"
self.input_dir = os.path.join(TEST_DIR, "dumps")
self.input_file = os.path.join(TEST_DIR, self.input_dir, self.infile)
self.baseline_output_dir = os.path.join(TEST_DIR, "baseline_output")
def test_noargs(self):
call = self.base_call.format(self.input_file)
@ -291,7 +302,7 @@ class Test_Stdout(unittest.TestCase):
outs = proc.stdout.decode("utf8")
test_file = "noargs_" + self.wikiq_out_name
baseline_file = os.path.join(".", self.baseline_output_dir, test_file)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_file)
print(baseline_file)
test = pd.read_table(StringIO(outs))
baseline = pd.read_table(baseline_file)
@ -312,8 +323,6 @@ class Test_Regex(unittest.TestCase):
self.base_call = WIKIQ + " {0}"
self.base_call_outs = WIKIQ + " {0} -o {1}"
self.baseline_output_dir = os.path.join(TEST_DIR, "baseline_output")
# sample arguments for checking that bad arguments get terminated / test_regex_arguments
self.bad_arguments_list = [
# label is missing
@ -372,7 +381,7 @@ class Test_Regex(unittest.TestCase):
test = pd.read_table(test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
baseline = pd.read_table(baseline_file)
assert_frame_equal(test, baseline, check_like=True)
print(i)
@ -397,22 +406,10 @@ class Test_Regex(unittest.TestCase):
test = pd.read_table(test_file)
baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
baseline_file = os.path.join(BASELINE_OUTPUT_DIR, test_filename)
baseline = pd.read_table(baseline_file)
assert_frame_equal(test, baseline, check_like=True)
if __name__ == '__main__':
tracemalloc.start()
# Perform directory check and reset here as this is a one-time setup step as opposed to per-test setup.
if not os.path.exists(TEST_OUTPUT_DIR):
os.mkdir(TEST_OUTPUT_DIR)
else:
# Avoid subsequent calls to tests interfering with each other.
# Otherwise, a test may erroneously pass if the program has no output
# but a previous run output what was expected.
for f in os.listdir(TEST_OUTPUT_DIR):
os.remove(os.path.join(TEST_OUTPUT_DIR, f))
unittest.main()