forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·134 lines (100 loc) · 3.33 KB
/
test.py
File metadata and controls
executable file
·134 lines (100 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
"""tests for sampler.py"""
import os
import random
import re
import string
from subprocess import getstatusoutput
from Bio import SeqIO
from Bio.SeqUtils import GC
from numpy import mean
from itertools import chain
from shutil import rmtree
prg = './sampler.py'
n1k = './n1k.fa'
n10k = './n10k.fa'
n100k = './n100k.fa'
n1m = './n1m.fa'
# --------------------------------------------------
def random_string():
"""generate a random string"""
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
# --------------------------------------------------
def test_exists():
"""usage"""
for file in [prg, n1k, n10k, n100k, n1m]:
assert os.path.isfile(file)
# --------------------------------------------------
def test_usage():
"""usage"""
for flag in ['-h', '--help']:
rv, out = getstatusoutput('{} {}'.format(prg, flag))
assert rv == 0
assert re.match("usage", out, re.IGNORECASE)
# --------------------------------------------------
def test_bad_file():
"""die on bad file"""
bad = random_string()
rv, out = getstatusoutput(f'{prg} {bad}')
assert rv != 0
assert re.match('usage:', out, re.I)
assert re.search(f"No such file or directory: '{bad}'", out)
# --------------------------------------------------
def test_bad_pct():
"""die on bad pct"""
bad = random.randint(1, 10)
rv, out = getstatusoutput(f'{prg} -p {bad} {n1k}')
assert rv != 0
assert re.match('usage:', out, re.I)
assert re.search(f'--pct "{float(bad)}" must be between 0 and 1', out)
# --------------------------------------------------
def test_defaults():
"""runs on good input"""
out_dir = 'out'
try:
if os.path.isdir(out_dir):
rmtree(out_dir)
rv, out = getstatusoutput(f'{prg} -s 10 {n1k}')
assert rv == 0
expected = (' 1: n1k.fa\n'
'Wrote 108 sequences from 1 file to directory "out"')
assert out == expected
assert os.path.isdir(out_dir)
files = os.listdir(out_dir)
assert len(files) == 1
out_file = os.path.join(out_dir, 'n1k.fa')
assert os.path.isfile(out_file)
# correct number of seqs
seqs = list(SeqIO.parse(out_file, 'fasta'))
assert len(seqs) == 108
finally:
if os.path.isdir(out_dir):
rmtree(out_dir)
# --------------------------------------------------
def test_options():
"""runs on good input"""
out_dir = random_string()
try:
if os.path.isdir(out_dir):
rmtree(out_dir)
cmd = f'{prg} -s 4 -o {out_dir} -p .25 {n1k} {n10k} {n100k}'
print(cmd)
rv, out = getstatusoutput(cmd)
assert rv == 0
assert re.search('1: n1k.fa', out)
assert re.search('2: n10k.fa', out)
assert re.search('3: n100k.fa', out)
assert re.search(
f'Wrote 27,688 sequences from 3 files to directory "{out_dir}"',
out)
assert os.path.isdir(out_dir)
files = os.listdir(out_dir)
assert len(files) == 3
seqs_written = 0
for file in files:
seqs_written += len(
list(SeqIO.parse(os.path.join(out_dir, file), 'fasta')))
assert seqs_written == 27688
finally:
if os.path.isdir(out_dir):
rmtree(out_dir)