Skip to content

Commit

Permalink
add tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sann5 committed May 14, 2024
1 parent 5ba4eec commit 83193c6
Show file tree
Hide file tree
Showing 24 changed files with 2,554 additions and 13 deletions.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion q2_moshpit/busco/tests/test_busco.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from unittest.mock import patch, ANY, call, Mock, MagicMock
from qiime2.plugin.testing import TestPluginBase
from q2_types.per_sample_sequences._format import MultiMAGSequencesDirFmt
from q2_moshpit.busco.types import BuscoDatabaseDirFmt


class MockContext(Mock):
Expand All @@ -33,6 +34,10 @@ def setUp(self):
path=self.get_data_path('mags'),
mode="r",
)
self.busco_db = BuscoDatabaseDirFmt(
path=self.get_data_path("busco_db"),
mode="r"
)

def _prepare_summaries(self):
for s in ['1', '2']:
Expand Down Expand Up @@ -110,7 +115,7 @@ def test_busco_helper(self, mock_len, mock_run):
)

@patch("q2_moshpit.busco.busco._busco_helper")
def test_evaluate_busco(self, mock_helper):
def test_evaluate_busco_online(self, mock_helper):
_evaluate_busco(
bins=self.mags, mode="some_mode", lineage_dataset="bacteria_odb10"
)
Expand All @@ -121,6 +126,24 @@ def test_evaluate_busco(self, mock_helper):
'--limit', '3']
)

@patch("q2_moshpit.busco.busco._busco_helper")
def test_evaluate_busco_offline(self, mock_helper):
_evaluate_busco(
bins=self.mags,
busco_db=self.busco_db,
mode="some_mode",
lineage_dataset="lineage_1"
)
mock_helper.assert_called_with(
self.mags,
[
'--mode', 'some_mode', '--lineage_dataset', 'lineage_1',
'--cpu', '1', '--contig_break', '10', '--evalue', '0.001',
'--limit', '3', '--offline', "--download_path",
f"{str(self.busco_db)}/busco_downloads"
]
)

@patch(
"q2_moshpit.busco.busco._draw_detailed_plots",
return_value={"fake1": {"plot": "spec"}}
Expand Down
69 changes: 66 additions & 3 deletions q2_moshpit/busco/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
# ----------------------------------------------------------------------------
import json
import tempfile

import pandas as pd
from qiime2.plugin.testing import TestPluginBase

from q2_moshpit.busco.utils import (
_parse_busco_params, _collect_summaries, _parse_df_columns,
_partition_dataframe, _get_feature_table, _calculate_summary_stats,
_get_mag_lengths,
_get_mag_lengths, _validate_lineage_dataset_input
)
from q2_types.per_sample_sequences._format import MultiMAGSequencesDirFmt
from q2_moshpit.busco.types import BuscoDatabaseDirFmt


class TestBUSCOUtils(TestPluginBase):
Expand Down Expand Up @@ -218,3 +217,67 @@ def test_get_mag_lengths(self):
}, name="length"
)
pd.testing.assert_series_equal(obs, exp)

def test_validate_lineage_dataset_input_valid(self):
# Give path to valid database
p = self.get_data_path("busco_db")
busco_db = BuscoDatabaseDirFmt(path=p, mode="r")
_validate_lineage_dataset_input(
lineage_dataset="lineage_1",
auto_lineage=False,
auto_lineage_euk=False,
auto_lineage_prok=False,
busco_db=busco_db,
kwargs={}
)

def test_validate_lineage_dataset_input_invalid(self):
# Give path to valid database
p = self.get_data_path("busco_db")
busco_db = BuscoDatabaseDirFmt(path=p, mode="r")

with self.assertRaisesRegex(
ValueError,
"is not present in input database."
):
# Run busco
_validate_lineage_dataset_input(
lineage_dataset="lineage2",
auto_lineage=False,
auto_lineage_euk=False,
auto_lineage_prok=False,
busco_db=busco_db,
kwargs={}
)

def test_validate_lineage_dataset_input_warning(self):
# Give path to valid database
p = self.get_data_path("busco_db")
busco_db = BuscoDatabaseDirFmt(path=p, mode="r")
kwargs = {
"auto_lineage": True,
"auto_lineage_euk": False,
"auto_lineage_prok": False
}
with self.assertWarnsRegex(
Warning,
"`--p-auto-lineage` flags will be ignored."
):
# Run busco
_validate_lineage_dataset_input(
lineage_dataset="lineage_1",
auto_lineage=True,
auto_lineage_euk=False,
auto_lineage_prok=False,
busco_db=busco_db,
kwargs=kwargs
)

self.assertDictEqual(
kwargs,
{
"auto_lineage": False,
"auto_lineage_euk": False,
"auto_lineage_prok": False
}
)
19 changes: 10 additions & 9 deletions q2_moshpit/busco/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import List
import skbio.io
from q2_types.per_sample_sequences import MultiMAGSequencesDirFmt
from q2_moshpit.busco.types import BuscoDatabaseDirFmt

arguments_with_hyphens = {
"auto_lineage": "auto-lineage",
Expand All @@ -25,18 +26,18 @@


def _validate_lineage_dataset_input(
lineage_dataset,
auto_lineage,
auto_lineage_euk,
auto_lineage_prok,
busco_db,
lineage_dataset: str,
auto_lineage: bool,
auto_lineage_euk: bool,
auto_lineage_prok: bool,
busco_db: BuscoDatabaseDirFmt,
kwargs
) -> None:
# When lineage_dataset is specified all other lineage flags are ignored
if any([auto_lineage, auto_lineage_euk, auto_lineage_prok]):
warnings.warn(
f"`--p-lineage-dataset` was specified as {lineage_dataset}. "
"--p-auto-lineage flags will be ignored."
f"`--p-lineage-dataset` was specified as '{lineage_dataset}'. "
"`--p-auto-lineage` flags will be ignored."
)
kwargs["auto_lineage"] = False
kwargs["auto_lineage_euk"] = False
Expand All @@ -51,8 +52,8 @@ def _validate_lineage_dataset_input(
os.path.join(str(busco_db), "busco_downloads/lineages/")
)
raise ValueError(
f"The specified --p-lineage-dataset {lineage_dataset} "
"is not present in input database (--i-busco-db). \n"
f"The specified lineage_dataset ({lineage_dataset}) "
"is not present in input database. \n"
"Printing lineage datasets present in input database: \n"
f"{present_lineages}"
)
Expand Down

0 comments on commit 83193c6

Please sign in to comment.