/
test_subcommands.py
288 lines (245 loc) · 10.7 KB
/
test_subcommands.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import contextlib
import unittest
import tempfile
import shutil
import copy
import os
import os.path
from taxtastic import refpkg
from taxtastic.subcommands import update, create, strip, rollback, rollforward, taxtable, check, add_to_taxtable
import config
from config import OutputRedirectMixin
class TestUpdate(OutputRedirectMixin, unittest.TestCase):
def setUp(self):
super(TestUpdate, self).setUp()
class _Args(object):
refpkg = None
changes = []
metadata = False
stats_type = None
frequency_type = None
self.args = _Args()
def test_action(self):
with config.tempdir() as scratch:
pkg_path = os.path.join(scratch, 'test.refpkg')
r = refpkg.Refpkg(pkg_path, create=True)
test_file = config.data_path('bv_refdata.csv')
self.args.refpkg = pkg_path
self.args.changes = ['meep='+test_file, 'hilda='+test_file]
update.action(self.args)
r._sync_from_disk()
self.assertEqual(r.contents['files']['meep'], 'bv_refdata.csv')
# Second file should have been assigned a non-clashing name
h = r.contents['files']['hilda']
self.assertNotEqual(h, 'bv_refdata.csv')
self.assertTrue(h.startswith('bv_refdata'))
self.assertTrue(h.endswith('.csv'))
self.assertTrue(os.path.exists(r.resource_path('hilda')))
def test_metadata_action(self):
with config.tempdir() as scratch:
pkg_path = os.path.join(scratch, 'test.refpkg')
r = refpkg.Refpkg(pkg_path, create=True)
self.args.changes = ['meep=boris', 'hilda=vrrp']
self.args.metadata = True
self.args.refpkg = pkg_path
update.action(self.args)
r._sync_from_disk()
self.assertEqual(r.metadata('meep'), 'boris')
self.assertEqual(r.metadata('hilda'), 'vrrp')
def test_update_stats_action(self):
with config.tempdir() as scratch:
pkg_path = os.path.join(scratch, 'test.refpkg')
r = refpkg.Refpkg(pkg_path, create=True)
args = self.args
stats_path = os.path.join(config.datadir, 'phyml_aa_stats.txt')
args.refpkg = pkg_path
args.changes = ['tree_stats=' + stats_path]
args.frequency_type = 'empirical'
update.action(args)
r._sync_from_disk()
self.assertIn('tree_stats', r.contents['files'])
self.assertIn('phylo_model', r.contents['files'])
self.assertTrue(r.contents['files']['phylo_model'].endswith('.json'))
class TestCreate(OutputRedirectMixin, unittest.TestCase):
def setUp(self):
super(TestCreate, self).setUp()
class _Args(object):
clobber = True
locus = 'Nowhere'
description = 'A description'
author = 'Boris the Mad Baboon'
package_version = '0.3'
tree_stats = None
stats_type = None
aln_fasta = None
aln_sto = None
phylo_model = None
seq_info = None
mask = None
profile = None
readme = None
tree = None
taxonomy = None
reroot = False
rppr = 'rppr'
frequency_type = None
def __init__(self, scratch):
self.package_name = os.path.join(scratch, 'test.refpkg')
self._Args = _Args
def test_create(self):
with config.tempdir() as scratch:
args = self._Args(scratch)
create.action(args)
r = refpkg.Refpkg(args.package_name, create=False)
self.assertEqual(r.metadata('locus'), 'Nowhere')
self.assertEqual(r.metadata('description'), 'A description')
self.assertEqual(r.metadata('author'), 'Boris the Mad Baboon')
self.assertEqual(r.metadata('package_version'), '0.3')
self.assertEqual(r.metadata('format_version'), '1.1')
self.assertEqual(r.contents['rollback'], None)
args2 = self._Args(scratch)
args2.package_name = os.path.join(scratch, 'test.refpkg')
args2.clobber = True
self.assertEqual(0, create.action(args2))
def _test_create_phylo_model(self, stats_path, stats_type=None,
frequency_type=None):
with config.tempdir() as scratch:
args = self._Args(scratch)
args.tree_stats = stats_path
args.stats_type = stats_type
args.frequency_type = frequency_type
create.action(args)
r = refpkg.Refpkg(args.package_name, create=False)
self.assertIn('phylo_model', r.contents['files'])
def test_create_phyml_aa(self):
stats_path = os.path.join(config.datadir, 'phyml_aa_stats.txt')
self._test_create_phylo_model(stats_path, frequency_type='model')
self._test_create_phylo_model(stats_path, 'PhyML', frequency_type='model')
self._test_create_phylo_model(stats_path, 'PhyML', frequency_type='empirical')
self.assertRaises(ValueError, self._test_create_phylo_model,
stats_path, 'FastTree', frequency_type='empirical')
self.assertRaises(ValueError, self._test_create_phylo_model,
stats_path, 'garli', frequency_type='empirical')
class TestStrip(OutputRedirectMixin, unittest.TestCase):
def test_strip(self):
with config.tempdir() as scratch:
rpkg = os.path.join(scratch, 'tostrip.refpkg')
shutil.copytree(config.data_path('lactobacillus2-0.2.refpkg'), rpkg)
r = refpkg.Refpkg(rpkg, create=False)
r.update_metadata('boris', 'hilda')
r.update_metadata('meep', 'natasha')
class _Args(object):
refpkg = rpkg
strip.action(_Args())
r._sync_from_disk()
self.assertEqual(r.contents['rollback'], None)
self.assertEqual(r.contents['rollforward'], None)
class TestRollback(OutputRedirectMixin, unittest.TestCase):
maxDiff = None
def test_rollback(self):
with config.tempdir() as scratch:
rpkg = os.path.join(scratch, 'tostrip.refpkg')
shutil.copytree(config.data_path('lactobacillus2-0.2.refpkg'), rpkg)
r = refpkg.Refpkg(rpkg, create=False)
original_contents = copy.deepcopy(r.contents)
r.update_metadata('boris', 'hilda')
r.update_metadata('meep', 'natasha')
updated_contents = copy.deepcopy(r.contents)
class _Args(object):
refpkg = rpkg
def __init__(self, n):
self.n = n
self.assertEqual(rollback.action(_Args(3)), 1)
r._sync_from_disk()
self.assertEqual(r.contents, updated_contents)
self.assertEqual(rollback.action(_Args(2)), 0)
r._sync_from_disk()
self.assertEqual(r.contents['metadata'], original_contents['metadata'])
self.assertEqual(r.contents['rollback'], None)
self.assertNotEqual(r.contents['rollforward'], None)
class TestRollforward(OutputRedirectMixin, unittest.TestCase):
maxDiff = None
def test_rollforward(self):
with config.tempdir() as scratch:
rpkg = os.path.join(scratch, 'tostrip.refpkg')
shutil.copytree(config.data_path('lactobacillus2-0.2.refpkg'), rpkg)
r = refpkg.Refpkg(rpkg, create=False)
original_contents = copy.deepcopy(r.contents)
r.update_metadata('boris', 'hilda')
r.update_metadata('meep', 'natasha')
updated_contents = copy.deepcopy(r.contents)
r.rollback()
r.rollback()
class _Args(object):
refpkg = rpkg
def __init__(self, n):
self.n = n
self.assertEqual(rollforward.action(_Args(3)), 1)
r._sync_from_disk()
self.assertEqual(r.contents['metadata'], original_contents['metadata'])
self.assertEqual(rollforward.action(_Args(2)), 0)
r._sync_from_disk()
self.assertEqual(r.contents['metadata'], updated_contents['metadata'])
self.assertEqual(r.contents['rollforward'], None)
self.assertNotEqual(r.contents['rollback'], None)
@contextlib.contextmanager
def scratch_file(unlink=True):
"""Create a temporary file and return its name.
At the start of the with block a secure, temporary file is created
and its name returned. At the end of the with block it is
deleted.
"""
try:
(tmp_fd, tmp_name) = tempfile.mkstemp(text=True)
os.close(tmp_fd)
yield tmp_name
except ValueError, v:
raise v
else:
if unlink:
os.unlink(tmp_name)
class TestTaxtable(OutputRedirectMixin, unittest.TestCase):
maxDiff = None
def test_invalid_taxid(self):
with scratch_file() as out:
with open(out, 'w') as h:
class _Args(object):
database_file = config.ncbi_master_db
taxids = 'horace,hilda'
taxnames = None
seq_info = None
verbosity = 0
out_file = h
self.assertEqual(taxtable.action(_Args()), 1)
def test_seqinfo(self):
with tempfile.TemporaryFile() as tf, \
open(config.data_path('simple_seqinfo.csv')) as ifp:
class _Args(object):
database_file = config.ncbi_master_db
taxids = None
taxnames = None
seq_info = ifp
out_file = tf
verbosity = 0
self.assertEqual(taxtable.action(_Args()), 0)
# No output check at present
self.assertTrue(tf.tell() > 0)
class TestAddToTaxtable(OutputRedirectMixin, unittest.TestCase):
maxDiff = None
def test_seqinfo(self):
with tempfile.TemporaryFile() as tf, \
open(config.data_path('minimal_taxonomy.csv')) as taxtable_fp, \
open(config.data_path('minimal_add_taxonomy.csv')) as extra_nodes_fp:
class _Args(object):
extra_nodes_csv = extra_nodes_fp
taxtable = taxtable_fp
out_file = tf
verbosity = 0
self.assertFalse(add_to_taxtable.action(_Args()))
# No output check at present
self.assertTrue(tf.tell() > 0)
class TestCheck(OutputRedirectMixin, unittest.TestCase):
def test_runs(self):
class _Args(object):
refpkg = config.data_path('lactobacillus2-0.2.refpkg')
self.assertEqual(check.action(_Args()), 0)