Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mlupei committed Dec 7, 2023
1 parent ed2578e commit acd913b
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions tests/test_standardize_output.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,93 @@
import unittest
import unittest
import os
import subprocess

def get_script_path():
script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
return os.path.join(script_dir, 'srcs', 'standardize_yacht_output.py')

def assert_file_exists(file_path):
assert os.path.exists(file_path)

def assert_file_not_exists(file_path):
assert not os.path.exists(file_path)

def create_outdir(outdir):
cmd = f'rm -rf {outdir}'
try:
subprocess.run(cmd, shell=True, check=True)
except:
pass
assert not os.path.exists(outdir)

def cleanup_outdir(outdir):
cmd = f'rm -rf {outdir}'
res = subprocess.run(cmd, shell=True, check=True)
assert res.returncode == 0

class TestScript(unittest.TestCase):
def test_everything_exists(self):
script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
script_dir = os.path.join(script_dir, 'srcs')
script_full_path = os.path.join(script_dir, 'standardize_yacht_output.py')
assert os.path.exists(script_full_path)
def setUp(self):
self.script_full_path = get_script_path()

def test_everything_exists(self):
yacht_output = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/result.xlsx')
assert os.path.exists(yacht_output)

genome_to_taxid = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/toy_genome_to_taxid.tsv')
assert os.path.exists(genome_to_taxid)

outdir = os.path.join(os.path.dirname(__file__), 'testdata')
assert os.path.exists(outdir)

cmd = f"python {script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"
assert_file_exists(self.script_full_path)
assert_file_exists(yacht_output)
assert_file_exists(genome_to_taxid)
assert_file_exists(outdir)

cmd = f"python {self.script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"
res = subprocess.run(cmd, shell=True, check=True)
assert res.returncode == 0
assert os.path.exists(os.path.join(outdir, 'cami_result.cami'))
assert_file_exists(os.path.join(outdir, 'cami_result.cami'))

def test_wrong_yacht_output(self):
script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
script_dir = os.path.join(script_dir, 'srcs')
script_full_path = os.path.join(script_dir, 'standardize_yacht_output.py')
assert os.path.exists(script_full_path)

yacht_output = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/result_nonexisting.xlsx')
assert not os.path.exists(yacht_output)

genome_to_taxid = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/toy_genome_to_taxid.tsv')
assert os.path.exists(genome_to_taxid)

outdir = os.path.join(os.path.dirname(__file__), 'testdata')
assert os.path.exists(outdir)

cmd = f"python {script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"

assert_file_exists(self.script_full_path)
assert_file_not_exists(yacht_output)
assert_file_exists(genome_to_taxid)
assert_file_exists(outdir)

cmd = f"python {self.script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"
with self.assertRaises(subprocess.CalledProcessError):
res = subprocess.run(cmd, shell=True, check=True)

def test_wrong_genome_to_taxid(self):
script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
script_dir = os.path.join(script_dir, 'srcs')
script_full_path = os.path.join(script_dir, 'standardize_yacht_output.py')
assert os.path.exists(script_full_path)

yacht_output = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/result.xlsx')
assert os.path.exists(yacht_output)

genome_to_taxid = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/toy_genome_to_taxid_nonexisting.tsv')
assert not os.path.exists(genome_to_taxid)

outdir = os.path.join(os.path.dirname(__file__), 'testdata')
assert os.path.exists(outdir)

cmd = f"python {script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"
assert_file_exists(self.script_full_path)
assert_file_exists(yacht_output)
assert_file_not_exists(genome_to_taxid)
assert_file_exists(outdir)

cmd = f"python {self.script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"
with self.assertRaises(subprocess.CalledProcessError):
res = subprocess.run(cmd, shell=True, check=True)

def test_wrong_outdir(self):
script_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
script_dir = os.path.join(script_dir, 'srcs')
script_full_path = os.path.join(script_dir, 'standardize_yacht_output.py')
assert os.path.exists(script_full_path)

yacht_output = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/result.xlsx')
assert os.path.exists(yacht_output)

genome_to_taxid = os.path.join(os.path.dirname(__file__), 'testdata/standardize_output_testdata/toy_genome_to_taxid.tsv')
assert os.path.exists(genome_to_taxid)

outdir = os.path.join(os.path.dirname(__file__), 'testdata_nonexisting')
cmd = 'rm -rf ' + outdir
try:
subprocess.run(cmd, shell=True, check=True)
except:
pass
assert not os.path.exists(outdir)

cmd = f"python {script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"
res = subprocess.run(cmd, shell=True, check=True)
assert res.returncode == 0
assert os.path.exists(outdir)

cmd = 'rm -rf ' + outdir
assert_file_exists(self.script_full_path)
assert_file_exists(yacht_output)
assert_file_exists(genome_to_taxid)
create_outdir(outdir)

cmd = f"python {self.script_full_path} --yacht_output {yacht_output} --sheet_name min_coverage0.2 --genome_to_taxid {genome_to_taxid} --outfile_prefix cami_result --outdir {outdir}"
res = subprocess.run(cmd, shell=True, check=True)
assert res.returncode == 0
assert_file_exists(outdir)

cleanup_outdir(outdir)

if __name__ == '__main__':
unittest.main()

0 comments on commit acd913b

Please sign in to comment.