From 7f6e642fa667ff65d3aab690e0f355060abe63e5 Mon Sep 17 00:00:00 2001
From: Amandine PERRIN <amandine.perrin@pasteur.fr>
Date: Sat, 17 Oct 2020 11:55:42 +0200
Subject: [PATCH] finish functional tests for prepare module

---
 .gitlab-ci.yml                              |  1 -
 PanACoTA/subcommands/prepare.py             |  2 +-
 PanACoTA/utils.py                           |  2 +-
 PanACoTA/utils_argparse.py                  |  2 +-
 test/test_functional/test_annote.py         |  4 +-
 test/test_functional/test_corepers.py       |  3 +-
 test/test_functional/test_prepare-parser.py |  8 +-
 test/test_functional/test_prepare.py        | 84 +++++++++++++++++++++
 test/test_unit/test_utils-argparse.py       | 36 +++++++++
 test/test_unit/test_utils-logging.py        | 33 ++++++--
 test/test_unit/test_utils.py                |  4 +-
 11 files changed, 158 insertions(+), 21 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index fe551a83..88daf349 100755
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -113,7 +113,6 @@ func-test-ubuntu:
     # - py.test test/test_functional/test_align* -vx
     - py.test test/test_functional/test_align-parser.py -vx
     - py.test test/test_functional/test_tree* -vx
-    - py.test test/test_functional/test_tree-parser.py -vx
     - py.test test/test_functional/test_annote-parser.py -vx
     - py.test test/test_functional/test_annote.py -xv
     - mv .coverage .coverage-functional
diff --git a/PanACoTA/subcommands/prepare.py b/PanACoTA/subcommands/prepare.py
index 135628d5..ebe85ea6 100644
--- a/PanACoTA/subcommands/prepare.py
+++ b/PanACoTA/subcommands/prepare.py
@@ -245,7 +245,7 @@ def main(cmd, NCBI_species, NCBI_taxid, levels, outdir, tmp_dir, threads, no_ref
     else:
         logger.warning('You asked to run only mash steps.')
         if not os.path.exists(info_file):  # info-file missing -> error and exit
-            logger.error(f"Your info file {info_file} does not exist. Please Provide the  "
+            logger.error(f"Your info file {info_file} does not exist. Please provide the  "
                           "right name/path, or remove the '--mash-only option to rerun "
                           "quality control.")
             sys.exit(1)
diff --git a/PanACoTA/utils.py b/PanACoTA/utils.py
index 09043506..9828d464 100755
--- a/PanACoTA/utils.py
+++ b/PanACoTA/utils.py
@@ -824,7 +824,7 @@ def read_genomes_info(list_file, name, date=None, logger=None):
     if len(genomes) > 0:
         logger.info(("Found {} genomes in total").format(len(genomes)))
     else:
-        logger.error(f"no genome listed in {list_file} were found.")
+        logger.error(f"No genome listed in {list_file} was found.")
         sys.exit(1)
     return genomes
 
diff --git a/PanACoTA/utils_argparse.py b/PanACoTA/utils_argparse.py
index 9f4d1e4b..f553a571 100644
--- a/PanACoTA/utils_argparse.py
+++ b/PanACoTA/utils_argparse.py
@@ -120,6 +120,6 @@ def mash_dist(param):
         msg = f"error: mash distance: invalid float value: '{param}'"
         raise argparse.ArgumentTypeError(msg)
     if param < 0 or param > 1:
-        msg = f"error: mash_distance must be between 0 and 1: invalid value: '{param}'"
+        msg = f"error: mash distance must be between 0 and 1: invalid value: '{param}'"
         raise argparse.ArgumentTypeError(msg)
     return param
diff --git a/test/test_functional/test_annote.py b/test/test_functional/test_annote.py
index 950a6179..e4c3ca1c 100755
--- a/test/test_functional/test_annote.py
+++ b/test/test_functional/test_annote.py
@@ -582,8 +582,8 @@ def test_main_novalid_genome_frominfo(capsys):
                    prodigal_only=True, small=True)
     out, err = capsys.readouterr()
     # Check logs
-    assert ("no genome listed in test/data/annotate/test_files/lstinfo-no-genome.lst "
-            "were found.") in err
+    assert ("No genome listed in test/data/annotate/test_files/lstinfo-no-genome.lst "
+            "was found.") in err
 
     # Check output folders not created
     protdir = os.path.join(GENEPATH, "Proteins")
diff --git a/test/test_functional/test_corepers.py b/test/test_functional/test_corepers.py
index d4e778ca..9d07b799 100755
--- a/test/test_functional/test_corepers.py
+++ b/test/test_functional/test_corepers.py
@@ -34,7 +34,8 @@ def setup_teardown_module():
     - remove all log files
     - remove directory with generated results
     """
-    os.mkdir(GENEPATH)
+    if not os.path.isdir(GENEPATH):
+        os.mkdir(GENEPATH)
     shutil.copyfile(OPAN, UPAN)
     print("setup")
 
diff --git a/test/test_functional/test_prepare-parser.py b/test/test_functional/test_prepare-parser.py
index 30d7a4fb..b0571710 100644
--- a/test/test_functional/test_prepare-parser.py
+++ b/test/test_functional/test_prepare-parser.py
@@ -164,12 +164,12 @@ def test_max_mash_dist(capsys):
     with pytest.raises(SystemExit):
         prepare.parse(parser, "--max_dist 1.5".split())
     _, err = capsys.readouterr()
-    assert "error: mash_distance must be between 0 and 1: invalid value: '1.5'" in err
+    assert "error: mash distance must be between 0 and 1: invalid value: '1.5'" in err
     # < 0
     with pytest.raises(SystemExit):
         prepare.parse(parser, "--max_dist -0.5".split())
     _, err = capsys.readouterr()
-    assert "error: mash_distance must be between 0 and 1: invalid value: '-0.5'" in err
+    assert "error: mash distance must be between 0 and 1: invalid value: '-0.5'" in err
 
 
 def test_min_mash_dist(capsys):
@@ -191,12 +191,12 @@ def test_min_mash_dist(capsys):
     with pytest.raises(SystemExit):
         prepare.parse(parser, "--min_dist 1.5".split())
     _, err = capsys.readouterr()
-    assert "error: mash_distance must be between 0 and 1: invalid value: '1.5'" in err
+    assert "error: mash distance must be between 0 and 1: invalid value: '1.5'" in err
     # < 0
     with pytest.raises(SystemExit):
         prepare.parse(parser, "--min_dist -0.5".split())
     _, err = capsys.readouterr()
-    assert "error: mash_distance must be between 0 and 1: invalid value: '-0.5'" in err
+    assert "error: mash distance must be between 0 and 1: invalid value: '-0.5'" in err
 
 
 def test_min_sup_max(capsys):
diff --git a/test/test_functional/test_prepare.py b/test/test_functional/test_prepare.py
index f60f3d72..4a84da72 100644
--- a/test/test_functional/test_prepare.py
+++ b/test/test_functional/test_prepare.py
@@ -485,3 +485,87 @@ def test_only_mash(capsys):
     assert tutil.compare_order_content(out_lst, exp_lst)
 
 
+def test_only_mash_empty_lstinfo(capsys):
+    """
+    Running only mash step giving an empty lstinfo file -> error, no genome found
+    """
+    NCBI_species = ""
+    NCBI_taxid = ""
+    levels = ""
+    outdir = GENEPATH
+    tmp_dir = ""
+    threads = 1
+    no_refseq = False
+    db_dir = ""
+    only_mash = True
+    # Create empty lstinfo file
+    info_file = os.path.join(GENEPATH, "LSTINFO-empty.lst")
+    open(info_file, "w").close()
+    l90 = 100
+    nbcont = 999
+    cutn = 5
+    min_dist = 1e-4
+    max_dist = 0.06
+    verbose = 1
+    quiet = False
+    with pytest.raises(SystemExit):
+        prepare.main("cmd", NCBI_species, NCBI_taxid, levels, outdir, tmp_dir, threads, no_refseq,
+                     db_dir, only_mash, info_file, l90, nbcont, cutn, min_dist, max_dist,
+                     verbose, quiet)
+    out, err = capsys.readouterr()
+    assert ("You asked to run only mash steps") in err
+    assert ("You want to run only mash steps. Getting information from "
+            "test/data/prepare/generated_by_func-tests/LSTINFO-empty.lst") in out
+    assert ("No genome listed in test/data/prepare/generated_by_func-tests/LSTINFO-empty.lst "
+            "was found.") in err
+
+    # Check output files
+    assert len(os.listdir(os.path.join(outdir, "tmp_files"))) == 0
+    # Check logfiles are here
+    log_files = glob.glob(os.path.join(outdir, "*log*"))
+    assert len(log_files) == 3
+    # Check lstinfo file is still here and still empty
+    assert os.stat(info_file).st_size == 0
+
+
+def test_only_mash_no_lstinfo(capsys):
+    """
+    Running only mash step giving an info file which does not exist -> error missing infofile
+    """
+    NCBI_species = ""
+    NCBI_taxid = ""
+    levels = ""
+    outdir = GENEPATH
+    tmp_dir = ""
+    threads = 1
+    no_refseq = False
+    db_dir = ""
+    only_mash = True
+    # Create empty lstinfo file
+    info_file = "info_file.lst"
+    l90 = 100
+    nbcont = 999
+    cutn = 5
+    min_dist = 1e-4
+    max_dist = 0.06
+    verbose = 1
+    quiet = False
+    with pytest.raises(SystemExit):
+        prepare.main("cmd", NCBI_species, NCBI_taxid, levels, outdir, tmp_dir, threads, no_refseq,
+                     db_dir, only_mash, info_file, l90, nbcont, cutn, min_dist, max_dist,
+                     verbose, quiet)
+    out, err = capsys.readouterr()
+    assert ("You asked to run only mash steps") in err
+    assert ("Your info file info_file.lst does not exist. Please provide the  "
+            "right name/path, or remove the '--mash-only option to rerun "
+            "quality control.") in err
+
+    # Check output files
+    assert len(os.listdir(os.path.join(outdir, "tmp_files"))) == 0
+    # Check logfiles are here
+    log_files = glob.glob(os.path.join(outdir, "*log*"))
+    assert len(log_files) == 3
+    # Check that outdir contains only 4 elements: 3 logs + tmp_files repo
+    files = os.listdir(outdir)
+    files = [f for f in files if "fuse" not in f]
+    assert len(files) == 4
diff --git a/test/test_unit/test_utils-argparse.py b/test/test_unit/test_utils-argparse.py
index 61d3efde..fb2a35cc 100644
--- a/test/test_unit/test_utils-argparse.py
+++ b/test/test_unit/test_utils-argparse.py
@@ -97,3 +97,39 @@ def test_thread_num():
     assert ("Please provide a positive number of threads (or 0 for all threads): "
             "Invalid value: -1") in str(err.value)
     assert autils.thread_num(0) == nb_cpu
+
+
+def test_positive_int():
+    """
+    Test checking that given argument is a positive integer
+    """
+    assert autils.positive_int("1") == 1
+    with pytest.raises(argparse.ArgumentTypeError) as err:
+        a = autils.positive_int("1.1")
+    assert ("argument --cutn: invalid int value: '1.1'") in str(err.value)
+    with pytest.raises(argparse.ArgumentTypeError) as err:
+        a = autils.positive_int("-1")
+    assert ("error: argument --cutn must be a positive integer: "
+            "invalid int value: '-1'") in str(err.value)
+
+
+def test_mash_dist():
+    """
+    Test checking that given value is ok for a mash distance
+    """
+    assert autils.mash_dist("0.05") == 0.05
+    assert autils.mash_dist("1e-4") == 0.0001
+    with pytest.raises(argparse.ArgumentTypeError) as err:
+        a = autils.mash_dist("1.1.1")
+    assert ("error: mash distance: invalid float value: '1.1.1'") in str(err.value)
+    with pytest.raises(argparse.ArgumentTypeError) as err:
+        a = autils.mash_dist("one")
+    assert ("error: mash distance: invalid float value: 'one'") in str(err.value)
+    with pytest.raises(argparse.ArgumentTypeError) as err:
+        a = autils.mash_dist("1.000001")
+    assert ("error: mash distance must be between 0 and 1: "
+            "invalid value: '1.000001'") in str(err.value)
+    with pytest.raises(argparse.ArgumentTypeError) as err:
+        a = autils.mash_dist("-1e-4")
+    assert ("error: mash distance must be between 0 and 1: "
+            "invalid value: '-0.0001'") in str(err.value)
diff --git a/test/test_unit/test_utils-logging.py b/test/test_unit/test_utils-logging.py
index 0f093f93..8c59c708 100755
--- a/test/test_unit/test_utils-logging.py
+++ b/test/test_unit/test_utils-logging.py
@@ -20,11 +20,16 @@ def setup_teardown_module():
     """
     # Init logger to level detail (15)
     # utils.init_logger(LOGFILE_BASE, logging.DEBUG, 'test_utils', verbose=1)
-    os.mkdir(GENEPATH)
+    if os.path.isdir(GENEPATH):
+        content = os.listdir(GENEPATH)
+        for f in content:
+            assert f.startswith(".fuse")
+    else:
+        os.mkdir(GENEPATH)
     print("setup")
 
     yield
-    shutil.rmtree(GENEPATH)
+    shutil.rmtree(GENEPATH, ignore_errors=True)
     print("teardown")
 
 
@@ -288,7 +293,9 @@ def test_logger_info_verbose1(capsys):
     assert "info warning" in err
     assert "info error" in err
     assert "info critical" in err
-    assert len(os.listdir(GENEPATH)) == 2
+    files = os.listdir(GENEPATH)
+    files = [f for f in files if "fuse" not in f]
+    assert len(files) == 2
     with open(logfile + ".log", "r") as logf:
         assert logf.readline().endswith(" :: INFO :: info info\n")
         assert logf.readline().endswith(" :: WARNING :: info warning\n")
@@ -322,7 +329,9 @@ def test_logger_info_verbose2(capsys):
     assert "info warning" in err
     assert "info error" in err
     assert "info critical" in err
-    assert len(os.listdir(GENEPATH)) == 2
+    files = os.listdir(GENEPATH)
+    files = [f for f in files if "fuse" not in f]
+    assert len(files) == 2
     with open(logfile + ".log", "r") as logf:
         assert logf.readline().endswith(" :: INFO :: info info\n")
         assert logf.readline().endswith(" :: WARNING :: info warning\n")
@@ -354,7 +363,9 @@ def test_logger_warning(capsys):
     assert "info info" in out
     assert "info error" in err
     assert "info critical" in err
-    assert len(os.listdir(GENEPATH)) == 2
+    files = os.listdir(GENEPATH)
+    files = [f for f in files if "fuse" not in f]
+    assert len(files) == 2
     with open(logfile + ".log", "r") as logf:
         assert logf.readline().endswith(" :: INFO :: info info\n")
         assert logf.readline().endswith(" :: WARNING :: info warning\n")
@@ -387,7 +398,9 @@ def test_logger_warning_verbose1(capsys):
     assert "info error" in err
     assert "info warning" in err
     assert "info critical" in err
-    assert len(os.listdir(GENEPATH)) == 2
+    files = os.listdir(GENEPATH)
+    files = [f for f in files if "fuse" not in f]
+    assert len(files) == 2
     with open(logfile + ".log", "r") as logf:
         assert logf.readline().endswith(" :: INFO :: info info\n")
         assert logf.readline().endswith(" :: WARNING :: info warning\n")
@@ -421,7 +434,9 @@ def test_logger_warning_verbose2(capsys):
     assert "info error" in err
     assert "info warning" in err
     assert "info critical" in err
-    assert len(os.listdir(GENEPATH)) == 2
+    files = os.listdir(GENEPATH)
+    files = [f for f in files if "fuse" not in f]
+    assert len(files) == 2
     with open(logfile + ".log", "r") as logf:
         assert logf.readline().endswith(" :: INFO :: info info\n")
         assert logf.readline().endswith(" :: WARNING :: info warning\n")
@@ -454,7 +469,9 @@ def test_logger_critical(capsys):
     assert "info info" in out
     assert "info error" in err
     assert "info critical" in err
-    assert len(os.listdir(GENEPATH)) == 2
+    files = os.listdir(GENEPATH)
+    files = [f for f in files if "fuse" not in f]
+    assert len(files) == 2
     with open(logfile + ".log", "r") as logf:
         assert logf.readline().endswith(" :: INFO :: info info\n")
         assert logf.readline().endswith(" :: WARNING :: info warning\n")
diff --git a/test/test_unit/test_utils.py b/test/test_unit/test_utils.py
index 83917d1a..cd653d4e 100755
--- a/test/test_unit/test_utils.py
+++ b/test/test_unit/test_utils.py
@@ -660,8 +660,8 @@ def test_read_genomes_info_no_genomes(caplog):
         utils.read_genomes_info(lstinfo_file, name)
     assert ("Reading given information on your genomes in "
             "test/data/annotate/test_files/lstinfo-no-genome.lst") in caplog.text
-    assert ("no genome listed in test/data/annotate/test_files/lstinfo-no-genome.lst "
-            "were found.") in caplog.text
+    assert ("No genome listed in test/data/annotate/test_files/lstinfo-no-genome.lst "
+            "was found.") in caplog.text
 
 
 def test_read_genomes_info_ok(caplog):
-- 
GitLab