From 49455485131537e9f5fb9d553160b3ee58ac1c26 Mon Sep 17 00:00:00 2001 From: Martin Beracochea Date: Wed, 21 Feb 2024 21:40:20 +0000 Subject: [PATCH] Add one more unit test for amplicon_utils_tests --- .gitignore | 1 + .../analysis/amplicon/amplicon_utils.py | 4 +- .../analysis/amplicon/amplicon_utils_tests.py | 68 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/unit/analysis/amplicon/amplicon_utils_tests.py diff --git a/.gitignore b/.gitignore index b4aea5a..a86b0c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dist/ *.egg-info/ __pycache__/ +build/ \ No newline at end of file diff --git a/mgnify_pipelines_toolkit/analysis/amplicon/amplicon_utils.py b/mgnify_pipelines_toolkit/analysis/amplicon/amplicon_utils.py index 2054bbc..155182e 100644 --- a/mgnify_pipelines_toolkit/analysis/amplicon/amplicon_utils.py +++ b/mgnify_pipelines_toolkit/analysis/amplicon/amplicon_utils.py @@ -70,10 +70,10 @@ def get_read_count(read_path, type='fastq'): grep_proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = grep_proc.communicate() - read_count = stdout.strip() + read_count = stdout.strip() if stdout is not None else "" if not read_count.isdigit(): - logging.error("Read count is not a digit, something wrong.") + logging.error(f"Read count is not a digit, something is wrong. stdout: '{stdout}', stderr: '{stderr}'") exit(1) read_count = int(read_count) diff --git a/tests/unit/analysis/amplicon/amplicon_utils_tests.py b/tests/unit/analysis/amplicon/amplicon_utils_tests.py new file mode 100644 index 0000000..7cfb745 --- /dev/null +++ b/tests/unit/analysis/amplicon/amplicon_utils_tests.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright 2024 EMBL - European Bioinformatics Institute +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from unittest.mock import patch + +import pytest + +from mgnify_pipelines_toolkit.analysis.amplicon.amplicon_utils import get_read_count + + +@pytest.mark.parametrize( + "read_count_output, expected_result", + [ + ("123\n", 123), + ("5435", 5435), + ("1000000000000", 1000000000000), + ("999999999999999999999999999999", 999999999999999999999999999999), + ("\n999999999999999999999999999999", 999999999999999999999999999999), + ("\n999999999999999999999999999999\n", 999999999999999999999999999999), + (" 999999999999999999999999999999 ", 999999999999999999999999999999), + ], +) +@patch("mgnify_pipelines_toolkit.analysis.amplicon.amplicon_utils.subprocess.Popen") +def test_get_read_count(mock_popen, read_count_output, expected_result): + mock_process = mock_popen.return_value + mock_process.communicate.return_value = (read_count_output, expected_result) + + print(expected_result) + + read_count = get_read_count("/path/to/read_file") + + assert read_count == expected_result + + +@pytest.mark.parametrize( + "read_count_output", + [ + "9999.9999", + "abc\n", + "", + None, + "8765\t888", + "888 888", + ], +) +@patch("mgnify_pipelines_toolkit.analysis.amplicon.amplicon_utils.subprocess.Popen") +def test_get_read_count_error(mock_popen, read_count_output): + mock_process = mock_popen.return_value + mock_process.communicate.return_value = (read_count_output, None) + # Simulating non-digit output + with pytest.raises(SystemExit) as exc_info: + get_read_count("/path/to/read_file") + + assert exc_info.type == SystemExit + assert exc_info.value.code == 1