Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rrwick committed Jul 1, 2021
1 parent bb005cb commit c966b06
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
source = polypolish
13 changes: 3 additions & 10 deletions polypolish/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .help_formatter import MyParser, MyHelpFormatter
from .log import log, bold, section_header, explanation
from .polish_targets import polish_target_sequences
from .misc import get_ascii_art, load_fasta
from .misc import get_ascii_art, load_fasta, check_python_version
from .version import __version__


Expand All @@ -31,7 +31,7 @@ def main():
alignments = load_alignments(args.sam1, args.sam2, args.max_errors)
new_lengths = polish_target_sequences(alignments, assembly_seqs, args.debug, args.min_depth,
args.min_fraction)
finished_message(start_time, new_lengths)
finished_message(start_time, new_lengths, args.debug)


def parse_args():
Expand Down Expand Up @@ -103,7 +103,7 @@ def starting_message(args):
else:
log(f' --debug {args.debug}')
log()
check_python()
check_python_version()
return datetime.datetime.now()


Expand Down Expand Up @@ -133,12 +133,5 @@ def load_assembly(assembly_filename):
return assembly_seqs


def check_python():
major, minor = sys.version_info.major, sys.version_info.minor
good_version = (major >= 3 and minor >= 6)
if not good_version:
sys.exit('Error: Polypolish requires Python 3.6 or later')


if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions polypolish/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,8 @@ def get_ascii_art():
bold_yellow(r" __/ || | ") + '\n' +
bold_yellow(r" |___/ |_| ") + '\n')
return ascii_art


def check_python_version():
if sys.version_info.major < 3 or sys.version_info.minor < 6:
sys.exit('\nError: Polypolish requires Python 3.6 or later')
145 changes: 145 additions & 0 deletions test/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
"""
This module contains some tests for Polypolish. To run them, execute `pytest` from the root
Polypolish directory.
Copyright 2021 Ryan Wick (rrwick@gmail.com)
https://github.com/rrwick/Polypolish
This file is part of Polypolish. Polypolish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version. Polypolish is distributed
in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details. You should have received a copy of the GNU General Public License along with Polypolish.
If not, see <http://www.gnu.org/licenses/>.
"""

import gzip
import pytest
import sys
import unittest.mock

import polypolish.misc


def test_get_compression_type_1():
assert polypolish.misc.get_compression_type('test/test_misc/test.txt') == 'plain'


def test_get_compression_type_2():
assert polypolish.misc.get_compression_type('test/test_misc/test.gz') == 'gz'


def test_get_compression_type_3():
with pytest.raises(SystemExit) as e:
polypolish.misc.get_compression_type('test/test_misc/test.bz2')
assert 'cannot use bzip2' in str(e.value)


def test_get_compression_type_4():
with pytest.raises(SystemExit) as e:
polypolish.misc.get_compression_type('test/test_misc/test.zip')
assert 'cannot use zip' in str(e.value)


def test_get_open_func_1():
assert polypolish.misc.get_open_func('test/test_misc/test.txt') == open


def test_get_open_func_2():
assert polypolish.misc.get_open_func('test/test_misc/test.gz') == gzip.open


def test_load_fasta_1():
seqs = polypolish.misc.load_fasta('test/test_misc/test.fasta')
assert len(seqs) == 2
assert seqs[0][0] == 'A'
assert seqs[0][1].startswith('TTGCCTGTAGTCGGGACC')
assert seqs[1][0] == 'B'
assert seqs[1][1].startswith('ATTCTCAGAATGGCGTAG')


def test_load_fasta_2():
seqs = polypolish.misc.load_fasta('test/test_misc/test.fasta', include_full_header=True)
assert len(seqs) == 2
assert seqs[0][0] == 'A'
assert seqs[0][1] == 'A info'
assert seqs[0][2].startswith('TTGCCTGTAGTCGGGACC')
assert seqs[1][0] == 'B'
assert seqs[1][1] == 'B stuff'
assert seqs[1][2].startswith('ATTCTCAGAATGGCGTAG')


def test_load_fasta_3():
seqs = polypolish.misc.load_fasta('test/test_misc/test.fasta.gz')
assert len(seqs) == 2
assert seqs[0][0] == 'A'
assert seqs[0][1].startswith('TTGCCTGTAGTCGGGACC')
assert seqs[1][0] == 'B'
assert seqs[1][1].startswith('ATTCTCAGAATGGCGTAG')


def test_load_fasta_4():
seqs = polypolish.misc.load_fasta('test/test_misc/bad_1.fasta')
assert len(seqs) == 2
assert seqs[0][0] == 'A'
assert seqs[0][1].startswith('TTGCCTGTAGTCGGGACC')
assert seqs[1][0] == 'B'
assert seqs[1][1].startswith('ATTCTCAGAATGGCGTAG')


def test_load_fasta_5():
seqs = polypolish.misc.load_fasta('test/test_misc/lowercase.fasta')
assert len(seqs) == 2
assert seqs[0][0] == 'A'
assert seqs[0][1].startswith('TTGCCTGTAGTCGGGACC')
assert seqs[1][0] == 'B'
assert seqs[1][1].startswith('ATTCTCAGAATGGCGTAG')


def test_reverse_complement_1():
assert polypolish.misc.reverse_complement('GGGGaaaaaaaatttatatat') == 'atatataaattttttttCCCC'


def test_reverse_complement_2():
assert polypolish.misc.reverse_complement('atatataaattttttttCCCC') == 'GGGGaaaaaaaatttatatat'


def test_reverse_complement_3():
assert polypolish.misc.reverse_complement('ACGT123') == 'NNNACGT'


def test_check_python_version_1():
with unittest.mock.patch.object(sys, 'version_info') as v_info:
v_info.major = 3
v_info.minor = 6
polypolish.misc.check_python_version()


def test_check_python_version_2():
with unittest.mock.patch.object(sys, 'version_info') as v_info:
v_info.major = 3
v_info.minor = 8
polypolish.misc.check_python_version()


def test_check_python_version_3():
with pytest.raises(SystemExit) as e:
with unittest.mock.patch.object(sys, 'version_info') as v_info:
v_info.major = 3
v_info.minor = 5
polypolish.misc.check_python_version()
assert 'requires Python 3.6 or later' in str(e.value)


def test_check_python_version_4():
with pytest.raises(SystemExit) as e:
with unittest.mock.patch.object(sys, 'version_info') as v_info:
v_info.major = 2
v_info.minor = 7
polypolish.misc.check_python_version()
assert 'requires Python 3.6 or later' in str(e.value)


def test_get_ascii_art():
assert "| | | |(_) | |" in polypolish.misc.get_ascii_art()
7 changes: 7 additions & 0 deletions test/test_misc/bad_1.fasta
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
>A
TTGCCTGTAGTCGGGACCCCGTGACTAGGAAAGCAATCAGCGACTAACAGGCGGAGACCGTCTATAGCGCACGGGGTGTAGTTGGCTATTACTGATCTCT



>B
ATTCTCAGAATGGCGTAGTATTCATATTTGTTCGTAGCCCGCCTCCGTACATGTTATTGTGCTCATCGGTGGCCTGCGCCGTGGGGAGTGCAAAACGTGG
9 changes: 9 additions & 0 deletions test/test_misc/bad_1.fastq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@A info
TTGCCTGTAGTCGGGACCCCGTGACTAGGAAAGCAATCAGCGACTAACAGGCGGAGACCGTCTATAGCGCACGGGGTGTAGTTGGCTATTACTGATCTCT
+
##$#%#%&++3*&&&-.72:789>;:<74362%&&(%()%$&$$&#(%*'&$%&$%*##$'/-&'&&'%%%'$%#"$#'#$$)##%((#%$('*'$'($'

@B stuff
ATTCTCAGAATGGCGTAGTATTCATATTTGTTCGTAGCCCGCCTCCGTACATGTTATTGTGCTCATCGGTGGCCTGCGCCGTGGGGAGTGCAAAACGTGG
+
:;@@AHD98/.5C*-CEC68BHJD/>:@<?>9CA=@??DEIF835<1.*+)<8++1--5?;62<B>9;%3))2/@=BC6651:65.?@>EFBBFNJ@BJK
9 changes: 9 additions & 0 deletions test/test_misc/bad_2.fastq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@A info
TTGCCTGTAGTCGGGACCCCGTGACTAGGAAAGCAATCAGCGACTAACAGGCGGAGACCGTCTATAGCGCACGGGGTGTAGTTGGCTATTACTGATCTCT
+
##$#%#%&++3*&&&-.72:789>;:<74362%&&(%()%$&$$&#(%*'&$%&$%*##$'/-&'&&'%%%'$%#"$#'#$$)##%((#%$('*'$'($'
EXTRA LINE
@B stuff
ATTCTCAGAATGGCGTAGTATTCATATTTGTTCGTAGCCCGCCTCCGTACATGTTATTGTGCTCATCGGTGGCCTGCGCCGTGGGGAGTGCAAAACGTGG
+
:;@@AHD98/.5C*-CEC68BHJD/>:@<?>9CA=@??DEIF835<1.*+)<8++1--5?;62<B>9;%3))2/@=BC6651:65.?@>EFBBFNJ@BJK
Empty file added test/test_misc/empty
Empty file.
4 changes: 4 additions & 0 deletions test/test_misc/lowercase.fasta
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
>A info
ttgcctgtagtcgggaccccgtgactaggaaagcaatcagcgactaacaggcggagaccgtctatagcgcacggggtgtagttggctattactgatctct
>B stuff
attctcagaatggcgtagtattcatatttgttcgtagcccgcctccgtacatgttattgtgctcatcggtggcctgcgccgtggggagtgcaaaacgtgg
1 change: 1 addition & 0 deletions test/test_misc/not_unicode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�vW�l
Binary file added test/test_misc/test.bz2
Binary file not shown.
4 changes: 4 additions & 0 deletions test/test_misc/test.fasta
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
>A info
TTGCCTGTAGTCGGGACCCCGTGACTAGGAAAGCAATCAGCGACTAACAGGCGGAGACCGTCTATAGCGCACGGGGTGTAGTTGGCTATTACTGATCTCT
>B stuff
ATTCTCAGAATGGCGTAGTATTCATATTTGTTCGTAGCCCGCCTCCGTACATGTTATTGTGCTCATCGGTGGCCTGCGCCGTGGGGAGTGCAAAACGTGG
Binary file added test/test_misc/test.fasta.gz
Binary file not shown.
8 changes: 8 additions & 0 deletions test/test_misc/test.fastq
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@A info
TTGCCTGTAGTCGGGACCCCGTGACTAGGAAAGCAATCAGCGACTAACAGGCGGAGACCGTCTATAGCGCACGGGGTGTAGTTGGCTATTACTGATCTCT
+
##$#%#%&++3*&&&-.72:789>;:<74362%&&(%()%$&$$&#(%*'&$%&$%*##$'/-&'&&'%%%'$%#"$#'#$$)##%((#%$('*'$'($'
@B stuff
ATTCTCAGAATGGCGTAGTATTCATATTTGTTCGTAGCCCGCCTCCGTACATGTTATTGTGCTCATCGGTGGCCTGCGCCGTGGGGAGTGCAAAACGTGG
+
:;@@AHD98/.5C*-CEC68BHJD/>:@<?>9CA=@??DEIF835<1.*+)<8++1--5?;62<B>9;%3))2/@=BC6651:65.?@>EFBBFNJ@BJK
Binary file added test/test_misc/test.fastq.gz
Binary file not shown.
Binary file added test/test_misc/test.gz
Binary file not shown.
1 change: 1 addition & 0 deletions test/test_misc/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a plain text file.
Binary file added test/test_misc/test.zip
Binary file not shown.

0 comments on commit c966b06

Please sign in to comment.