|
| 1 | +# test_count_coverage.py |
| 2 | +# ---------------------- |
| 3 | +# Unit tests for count_coverage.py |
| 4 | + |
| 5 | +import io |
| 6 | +import pytest |
| 7 | +from count_coverage import count_coverage |
| 8 | + |
| 9 | +def test_count_coverage_basic(capsys): |
| 10 | + """ |
| 11 | + Test that count_coverage correctly counts samples with nonzero coverage. |
| 12 | + """ |
| 13 | + # Simulated input (tab-separated) |
| 14 | + input_data = io.StringIO( |
| 15 | + "chrM\t1\t0\t88\t65\n" |
| 16 | + "chrM\t2\t55\t77\t8\n" |
| 17 | + "chrM\t3\t58\t64\t0\n" |
| 18 | + ) |
| 19 | + |
| 20 | + # Run the function |
| 21 | + count_coverage(input_data) |
| 22 | + |
| 23 | + # Capture printed output |
| 24 | + captured = capsys.readouterr() |
| 25 | + |
| 26 | + # Expected output: zero-based, half-open intervals |
| 27 | + expected_output = ( |
| 28 | + "chrM\t0\t1\t2\n" |
| 29 | + "chrM\t1\t2\t3\n" |
| 30 | + "chrM\t2\t3\t2\n" |
| 31 | + ) |
| 32 | + |
| 33 | + assert captured.out == expected_output |
| 34 | + |
| 35 | +def test_count_coverage_noninteger(capsys): |
| 36 | + """ |
| 37 | + Test that non-integer coverage values cause the script to exit with an error. |
| 38 | + """ |
| 39 | + input_data = io.StringIO("chr1\t1\t10\tnan\t5\n") |
| 40 | + with pytest.raises(SystemExit): |
| 41 | + count_coverage(input_data) |
| 42 | + captured = capsys.readouterr() |
| 43 | + assert "non-integer value" in captured.err.lower() |
| 44 | + |
| 45 | +def test_count_coverage_too_few_fields(capsys): |
| 46 | + """ |
| 47 | + Test that lines with fewer than 2 fields cause the script to exit with an error. |
| 48 | + """ |
| 49 | + input_data = io.StringIO("chr1\n") |
| 50 | + with pytest.raises(SystemExit): |
| 51 | + count_coverage(input_data) |
| 52 | + captured = capsys.readouterr() |
| 53 | + assert "fewer than 2 fields" in captured.err.lower() |
| 54 | + |
| 55 | +def test_count_coverage_zero_only(capsys): |
| 56 | + """ |
| 57 | + Test that positions with zero coverage across all samples are skipped. |
| 58 | + """ |
| 59 | + input_data = io.StringIO("chr2\t1\t0\t0\t0\n") |
| 60 | + count_coverage(input_data) |
| 61 | + captured = capsys.readouterr() |
| 62 | + assert captured.out == "" |
| 63 | + |
0 commit comments