Skip to content

Commit

Permalink
Add one more unit test for amplicon_utils_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mberacochea authored and chrisAta committed Feb 22, 2024
1 parent d4ad22e commit 4945548
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
*.egg-info/
__pycache__/
build/
4 changes: 2 additions & 2 deletions mgnify_pipelines_toolkit/analysis/amplicon/amplicon_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/analysis/amplicon/amplicon_utils_tests.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4945548

Please sign in to comment.