generated from jonn-smith/python_cli_template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests to be more independent (#165)
* converting tests to use pathlib.Path instead of os converting stream tests to use CliRunner with input=fh * removed slow fixtures, added intermediate files for filter tweaked some test code to look more consistent * use isolated_filesystem to not keep test output files * created a new fixture for test_correct and made the piped version of the test work
- Loading branch information
1 parent
11a0723
commit 07865eb
Showing
14 changed files
with
189 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,34 @@ | ||
import pytest | ||
import os | ||
import sys | ||
import subprocess | ||
import tempfile | ||
import pathlib | ||
|
||
from click.testing import CliRunner | ||
|
||
from longbow.__main__ import main_entry as longbow | ||
|
||
from ..utils import cat_file_to_pipe | ||
TEST_DATA_FOLDER = pathlib.Path(__file__).parent.parent / "test_data" | ||
TEST_PARAMS = [ | ||
TEST_DATA_FOLDER / "annotate" / "mas15v2_expected.bam", | ||
TEST_DATA_FOLDER / "annotate" / "mas10v2_expected.bam", | ||
] | ||
|
||
TEST_DATA_FOLDER = path = os.path.abspath( | ||
__file__ + os.path.sep + "../../" + os.path.sep + "test_data" | ||
) + os.path.sep | ||
|
||
|
||
@pytest.fixture(scope="module", params=[ | ||
(TEST_DATA_FOLDER + "mas15_test_input.bam", "mas_15_sc_10x5p_single_none"), | ||
(TEST_DATA_FOLDER + "mas10_test_input.bam", "mas_10_sc_10x5p_single_none"), | ||
]) | ||
def annotated_bam_file_from_pipeline(request): | ||
input_bam, model_name = request.param | ||
|
||
with tempfile.NamedTemporaryFile(delete=True) as annotate_bam: | ||
|
||
runner = CliRunner() | ||
|
||
result_annotate = runner.invoke(longbow, ["annotate", "-m", model_name, "-f", "-o", annotate_bam.name, input_bam]) | ||
assert result_annotate.exit_code == 0 | ||
|
||
# Yield file here so that when we return, we get to clean up automatically | ||
yield annotate_bam.name | ||
|
||
|
||
def test_demultiplex_from_file(tmpdir, annotated_bam_file_from_pipeline): | ||
args = ["demultiplex", "-d", "YN", "-o", "demux", annotated_bam_file_from_pipeline] | ||
@pytest.mark.parametrize("input_bam", TEST_PARAMS) | ||
def test_demultiplex_from_file(tmpdir, input_bam): | ||
args = ["demultiplex", "-d", "YN", "-o", "demux", str(input_bam)] | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(longbow, args) | ||
with runner.isolated_filesystem(): | ||
result = runner.invoke(longbow, args) | ||
|
||
assert result.exit_code == 0 | ||
|
||
|
||
@pytest.mark.skip(reason="this test is broken and I don't know why") | ||
def test_demultiplex_from_pipe(tmpdir, annotated_bam_file_from_pipeline): | ||
proc = subprocess.Popen( | ||
[ sys.executable, "-m", "longbow", "demultiplex", "-d", "YN", "-o", "demux", annotated_bam_file_from_pipeline ], | ||
stdin=subprocess.PIPE | ||
) | ||
@pytest.mark.parametrize("input_bam", TEST_PARAMS) | ||
def test_demultiplex_from_pipe(tmpdir, input_bam): | ||
args = ["demultiplex", "-d", "YN", "-o" "demux"] | ||
|
||
cat_file_to_pipe(annotated_bam_file_from_pipeline, proc) | ||
runner = CliRunner() | ||
with runner.isolated_filesystem(), open(input_bam, "rb") as fh: | ||
result = runner.invoke(longbow, args, input=fh) | ||
|
||
assert proc.returncode == 0 | ||
assert result.exit_code == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,37 @@ | ||
import pytest | ||
import os | ||
import sys | ||
import subprocess | ||
import tempfile | ||
import pathlib | ||
|
||
from click.testing import CliRunner | ||
|
||
from longbow.__main__ import main_entry as longbow | ||
|
||
from ..utils import cat_file_to_pipe | ||
|
||
TEST_DATA_FOLDER = path = os.path.abspath( | ||
__file__ + os.path.sep + "../../" + os.path.sep + "test_data" | ||
) + os.path.sep | ||
TEST_DATA_FOLDER = pathlib.Path(__file__).parent.parent / "test_data" | ||
TEST_PARAMS = [ | ||
TEST_DATA_FOLDER / "segment" / "mas_15_sc_10x5p_single_none.expected.bam", | ||
TEST_DATA_FOLDER / "segment" / "mas_10_sc_10x5p_single_none.expected.bam", | ||
] | ||
|
||
|
||
@pytest.fixture(scope="module", params=[ | ||
(TEST_DATA_FOLDER + "mas15_test_input.bam", "mas_15_sc_10x5p_single_none"), | ||
(TEST_DATA_FOLDER + "mas10_test_input.bam", "mas_10_sc_10x5p_single_none"), | ||
]) | ||
def segmented_bam_file_from_pipeline(request): | ||
input_bam, model_name = request.param | ||
|
||
with tempfile.NamedTemporaryFile(delete=True) as annotate_bam, \ | ||
tempfile.NamedTemporaryFile(delete=True) as filter_bam, \ | ||
tempfile.NamedTemporaryFile(delete=True) as segment_bam: | ||
|
||
runner = CliRunner() | ||
|
||
result_annotate = runner.invoke(longbow, ["annotate", "-t", 1, "-m", model_name, "-f", "-o", annotate_bam.name, input_bam]) | ||
assert result_annotate.exit_code == 0 | ||
|
||
result_filter = runner.invoke(longbow, ["filter", "-m", model_name, "-f", "-o", filter_bam.name, annotate_bam.name]) | ||
assert result_filter.exit_code == 0 | ||
|
||
result_segment = runner.invoke(longbow, ["segment", "-t", 1, "-m", model_name, "-f", "-o", segment_bam.name, filter_bam.name]) | ||
assert result_segment.exit_code == 0 | ||
|
||
# Yield file here so that when we return, we get to clean up automatically | ||
yield segment_bam.name | ||
|
||
|
||
def test_extract_from_file(tmpdir, segmented_bam_file_from_pipeline): | ||
@pytest.mark.parametrize("input_bam", TEST_PARAMS) | ||
def test_extract_from_file(tmpdir, input_bam): | ||
actual_file = tmpdir.join("extract_actual_out.bam") | ||
args = ["extract", "-f", "-o", actual_file, segmented_bam_file_from_pipeline] | ||
args = ["extract", "-f", "-o", actual_file, str(input_bam)] | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(longbow, args) | ||
|
||
assert result.exit_code == 0 | ||
|
||
|
||
def test_extract_from_pipe(tmpdir, segmented_bam_file_from_pipeline): | ||
@pytest.mark.parametrize("input_bam", TEST_PARAMS) | ||
def test_extract_from_pipe(tmpdir, input_bam): | ||
actual_file = tmpdir.join("extract_actual_out.pipe.bam") | ||
|
||
args = ["extract", "-f", "-o", actual_file] | ||
|
||
runner = CliRunner() | ||
with open(segmented_bam_file_from_pipeline, "rb") as fh: | ||
with open(input_bam, "rb") as fh: | ||
result = runner.invoke(longbow, args, input=fh) | ||
|
||
assert result.exit_code == 0 |
Oops, something went wrong.