Skip to content

Commit

Permalink
Fix(tests) Improve panel pytest (#2682)(patch)
Browse files Browse the repository at this point in the history
### Added

- New test for the `panel` function
  • Loading branch information
Vince-janv authored Nov 24, 2023
1 parent 2e193e9 commit fe99adb
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 29 deletions.
48 changes: 47 additions & 1 deletion tests/cli/workflow/mip/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from cg.store.api.find_business_data import FindBusinessDataHandler
from cg.store.api.status import StatusHandler
from cg.store.models import Case
from tests.store.conftest import case_obj
from tests.store_helpers import StoreHelpers


Expand Down Expand Up @@ -192,3 +191,50 @@ def setup_mocks(

mocker.patch.object(FindBusinessDataHandler, "are_all_flow_cells_on_disk")
FindBusinessDataHandler.are_all_flow_cells_on_disk.return_value = True


@pytest.fixture
def scout_panel_output() -> str:
return """##genome_build=37
##gene_panel=OMIM-AUTO,version=22.0,updated_at=2023-10-17,display_name=OMIM-AUTO
##gene_panel=PANELAPP-GREEN,version=6.0,updated_at=2023-10-19,display_name=PanelApp Green Genes
##contig=1
##contig=2
##contig=3
##contig=4
##contig=5
##contig=6
##contig=7
##contig=8
##contig=9
##contig=10
##contig=X
##contig=Y
##contig=MT
#chromosome gene_start gene_stop hgnc_id hgnc_symbol
1 568915 569121 44571 MTATP8P1
1 860260 879955 28706 SAMD11
2 162164549 162268228 16889 PSMD14
2 162272605 162282381 11590 TBR1
3 120315156 120321347 7699 NDUFB4
3 120347020 120401418 4892 HGD
4 185570767 185616117 26575 PRIMPOL
4 185676749 185747972 3569 ACSL1
5 473425 524447 11073 SLC9A3
5 892758 919472 12307 TRIP13
6 18224099 18265054 2768 DEK
6 19837617 19840915 5363 ID4
7 44256749 44374176 1461 CAMK2B
7 44646171 44748665 8124 OGDH
8 22993101 23021543 11907 TNFRSF10D
8 23047965 23082639 11904 TNFRSF10A
9 35099888 35103154 14559 STOML2
9 35161999 35405335 12566 UNC13B
10 23481256 23483181 23734 PTF1A
10 25305587 25315593 26160 THNSL1
X 154719776 154899605 18308 TMLHE
X 155110956 155173433 11486 VAMP7
Y 6778727 6959724 18502 TBL1Y
Y 14813160 14972764 12633 USP9Y
MT 577 647 7481 MT-TF
MT 648 1601 7470 MT-RNR1"""
17 changes: 12 additions & 5 deletions tests/cli/workflow/mip/test_cli_mip_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

import mock
from _pytest.logging import LogCaptureFixture
from click.testing import CliRunner
from pytest_mock import MockFixture
Expand Down Expand Up @@ -83,7 +84,7 @@ def test_spring_decompression_needed_and_start_failed(
assert result.exit_code == 0

# THEN it should be announced that spring decompression is needed but fail to start
assert f"Decompression failed to start for" in caplog.text
assert "Decompression failed to start for" in caplog.text


def test_spring_decompression_needed_and_cant_start(
Expand Down Expand Up @@ -120,7 +121,7 @@ def test_spring_decompression_needed_and_cant_start(
assert result.exit_code == 0

# THEN it should be announced that spring decompression is needed but fail to start
assert f"Decompression can not be started for" in caplog.text
assert "Decompression can not be started for" in caplog.text


def test_decompression_cant_start_and_is_running(
Expand Down Expand Up @@ -157,7 +158,7 @@ def test_decompression_cant_start_and_is_running(
assert result.exit_code == 0

# THEN it should be announced that spring decompression is needed but fail to start
assert f"Decompression is running for" in caplog.text
assert "Decompression is running for" in caplog.text


def test_case_needs_to_be_stored(
Expand Down Expand Up @@ -188,8 +189,14 @@ def test_case_needs_to_be_stored(
mocker=mocker,
)

# WHEN MIP analysis is started
result = cli_runner.invoke(start, [case.internal_id, "--dry-run"], obj=mip_dna_context)
# GIVEN that a panel is returned
with mock.patch.object(
mip_dna_context.scout_api,
"export_panels",
return_value=["OMIM-AUTO"],
):
# WHEN MIP analysis is started
result = cli_runner.invoke(start, [case.internal_id, "--dry-run"], obj=mip_dna_context)

# THEN command should run without errors
assert result.exit_code == 0
Expand Down
35 changes: 28 additions & 7 deletions tests/cli/workflow/mip/test_cli_mip_dna_panel.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
""" Test the CLI for mip-dna panel"""
from pathlib import Path

import mock
from click.testing import CliRunner

from cg.cli.workflow.mip_dna.base import panel
from cg.io.txt import read_txt
from cg.meta.workflow.mip import MipAnalysisAPI
from cg.models.cg_config import CGConfig
from tests.conftest import create_process_response


def test_panel_file_is_written(
case_id: str, cli_runner: CliRunner, mip_dna_context: CGConfig, scout_panel_output: str
):
analysis_api: MipAnalysisAPI = mip_dna_context.meta_apis["analysis_api"]

# GIVEN a case

def test_cg_workflow_mip_dna_panel(cli_runner, case_id, mip_dna_context):
"""Test print the MIP dna panel command to console"""
# GIVEN that the scout command writes the panel to stdout
with mock.patch(
"cg.utils.commands.subprocess.run",
return_value=create_process_response(std_out=scout_panel_output),
):
# WHEN creating a panel file
cli_runner.invoke(panel, [case_id], obj=mip_dna_context)

# GIVEN a cli function
panel_file = Path(analysis_api.root, case_id, "gene_panels.bed")

# WHEN we run a case in dry run mode
result = cli_runner.invoke(panel, [case_id], obj=mip_dna_context)
# THEN the file should exist
assert panel_file.exists()

# THEN the command should be printed
assert result.exit_code == 0
# THEN the file should contain the output from scout
file_content: str = read_txt(file_path=panel_file, read_to_string=True)
assert file_content == scout_panel_output
15 changes: 14 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from copy import deepcopy
from datetime import MAXYEAR, datetime, timedelta
from pathlib import Path
from subprocess import CompletedProcess
from typing import Any, Generator, Union

import pytest
Expand Down Expand Up @@ -2400,7 +2401,7 @@ def context_config(
"tower_pipeline": "taxprofiler",
},
"scout": {
"binary_path": "echo",
"binary_path": "bin/scout",
"config_path": "scout-stage.yaml",
},
"statina": {
Expand Down Expand Up @@ -3283,6 +3284,18 @@ def flow_cell_encryption_api(
return flow_cell_encryption_api


def create_process_response(
return_code: int = 0, args: str = "", std_out: str = "", std_err: str = ""
) -> CompletedProcess:
"""Returns a CompletedProcess object with default parameters."""
return CompletedProcess(
args=args,
returncode=return_code,
stderr=std_err.encode("utf-8"),
stdout=std_out.encode("utf-8"),
)


# Downsample
@pytest.fixture()
def store_with_case_and_sample_with_reads(
Expand Down
12 changes: 0 additions & 12 deletions tests/meta/backup/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fnmatch
from pathlib import Path
from subprocess import CompletedProcess
from typing import Callable

import pytest
Expand Down Expand Up @@ -73,14 +72,3 @@ def archived_flow_cell() -> Path:
def archived_key() -> Path:
"""Path of archived key"""
return Path("/path/to/archived/encryption_key.key.gpg")


def create_process_response(
return_code: int, args: str = "", std_out: str = "", std_err: str = ""
) -> CompletedProcess:
return CompletedProcess(
args=args,
returncode=return_code,
stderr=std_err.encode("utf-8"),
stdout=std_out.encode("utf-8"),
)
2 changes: 1 addition & 1 deletion tests/meta/backup/test_meta_pdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from cg.models.cg_config import CGConfig
from cg.store import Store
from cg.store.models import Flowcell
from tests.meta.backup.conftest import create_process_response
from tests.conftest import create_process_response
from tests.store_helpers import StoreHelpers


Expand Down
4 changes: 2 additions & 2 deletions tests/store/api/find/test_find_business_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
Application,
ApplicationLimitations,
ApplicationVersion,
Customer,
Case,
CaseSample,
Customer,
Flowcell,
Invoice,
Pool,
Expand Down Expand Up @@ -286,10 +286,10 @@ def test_are_all_flow_cells_on_disk_when_requested(
case_id: str,
helpers: StoreHelpers,
sample: Sample,
request,
):
"""Test check if all flow cells for samples on a case is on disk when requested."""
caplog.set_level(logging.DEBUG)

# GIVEN a store with two flow cell
helpers.add_flow_cell(
store=base_store,
Expand Down

0 comments on commit fe99adb

Please sign in to comment.