From 1f5f8bde77a87471ee15431b6f4ae09a2db7e11b Mon Sep 17 00:00:00 2001 From: farchaab Date: Wed, 24 Apr 2024 16:54:50 +0200 Subject: [PATCH] added unit tests --- .github/workflows/unit-tests.yml | 60 ++++++++++++++++++++++++++++++++ tests/test_assembly_finder.py | 55 +++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 .github/workflows/unit-tests.yml create mode 100644 tests/test_assembly_finder.py diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..419a1a0 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,60 @@ +name: Tests + +on: + push: + branches: ["main"] + paths: + - ".github/workflows/unit-tests.yaml" + - "tests/**" + - "assembly_finder/**" + - "setup.py" + pull_request: + branches: ["main"] + paths: + - ".github/workflows/unit-tests.yaml" + - "tests/**" + - "assembly_finder/**" + - "setup.py" + +permissions: + contents: read + +jobs: + tests: + name: "Python ${{ matrix.python-version }} on ${{ matrix.os }}" + runs-on: ${{ matrix.os }} + + defaults: + run: + shell: bash -el {0} + + strategy: + fail-fast: false + matrix: + os: [macos-latest, ubuntu-latest] + python-version: ["3.10", "3.11", "3.12"] + + steps: + - uses: "actions/checkout@v3" + with: + fetch-depth: 0 + + - uses: conda-incubator/setup-miniconda@v3 + with: + auto-update-conda: true + miniforge-version: "latest" + miniforge-variant: Mambaforge + use-mamba: true + mamba-version: "*" + channels: conda-forge,bioconda,defaults + channel-priority: strict + activate-environment: assembly_finder + python-version: ${{ matrix.python-version }} + auto-activate-base: false + + - name: "Test and generate coverage report on ${{ matrix.os }} for Python ${{ matrix.python-version }}" + run: | + python -m pip install --upgrade pip + python -m pip install pytest coverage + python -m pip install . + coverage run -m pytest diff --git a/tests/test_assembly_finder.py b/tests/test_assembly_finder.py new file mode 100644 index 0000000..2f23cf1 --- /dev/null +++ b/tests/test_assembly_finder.py @@ -0,0 +1,55 @@ +import subprocess +from pathlib import Path +import pytest +import shutil +import os + + +@pytest.fixture(scope="session") +def tmp_dir(tmpdir_factory): + return tmpdir_factory.mktemp("tmp") + + +test_data_path = Path("assembly_finder/test_data") +outdir = Path("test_out") + + +def remove_directory(dir_path): + if os.path.exists(dir_path): + shutil.rmtree(dir_path) + + +def exec_command(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE): + """executes shell command and returns stdout if completes exit code 0 + Parameters + ---------- + cmnd : str + shell command to be executed + stdout, stderr : streams + Default value (PIPE) intercepts process output, setting to None + blocks this.""" + + proc = subprocess.Popen(cmnd, shell=True, stdout=stdout, stderr=stderr) + out, err = proc.communicate() + if proc.returncode != 0: + raise RuntimeError(f"FAILED: {cmnd}\n{err}") + return out.decode("utf8") if out is not None else None + + +def test_cli(): + exec_command("assembly_finder -h") + exec_command("assembly_finder -v") + + +def test_taxons(): + """download genomes from taxons""" + exec_command(f"assembly_finder -i {test_data_path}/taxons.tsv --output {outdir}") + remove_directory(outdir) + + +def test_accessions(): + """download genomes from accessions""" + exec_command( + f"assembly_finder -i {test_data_path}/accessions.txt --accession --output {outdir}" + ) + remove_directory(outdir)