-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathpipeline_common_test.py
300 lines (247 loc) · 13.1 KB
/
pipeline_common_test.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
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright 2018 Google Inc. All Rights Reserved.
#
# 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.
"""Tests for pipeline_common script."""
import collections
import unittest
from apache_beam.io import filesystem
from apache_beam.io.filesystems import FileSystems
from apache_beam.testing import test_pipeline
from apache_beam.testing.util import assert_that
import mock
from gcp_variant_transforms import pipeline_common
from gcp_variant_transforms.pipeline_common import PipelineModes
from gcp_variant_transforms.testing import asserts
from gcp_variant_transforms.testing import temp_dir
from gcp_variant_transforms.testing import testdata_util
class PipelineCommonWithPatternTest(unittest.TestCase):
"""Tests cases for the `pipeline_common` script with pattern input."""
def _create_mock_args(self, **args):
return collections.namedtuple('MockArgs', args.keys())(*args.values())
def _get_pipeline_mode(self, args):
all_patterns = pipeline_common._get_all_patterns(args.input_pattern,
args.input_file)
return pipeline_common.get_pipeline_mode(all_patterns,
args.optimize_for_large_inputs)
def test_validation_failure_for_invalid_input_pattern(self):
with self.assertRaisesRegexp(
ValueError, 'Input pattern .* did not match any files.'):
pipeline_common._get_all_patterns(
input_pattern='nonexistent_file.vcf', input_file=None)
def test_get_mode_optimize_set(self):
args = self._create_mock_args(
input_pattern='**', input_file=None, optimize_for_large_inputs=True)
match_result = collections.namedtuple('MatchResult', ['metadata_list'])
match = match_result([None for _ in range(100)])
with mock.patch.object(FileSystems, 'match', return_value=[match]):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE)
def test_get_mode_small(self):
args = self._create_mock_args(
input_pattern='*', input_file=None, optimize_for_large_inputs=False)
match_result = collections.namedtuple('MatchResult', ['metadata_list'])
match = match_result([None for _ in range(100)])
with mock.patch.object(FileSystems, 'match', return_value=[match]):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.SMALL)
def test_get_mode_medium(self):
args = self._create_mock_args(
input_pattern='*', input_file=None, optimize_for_large_inputs=False)
match_result = collections.namedtuple('MatchResult', ['metadata_list'])
match = match_result(range(101))
with mock.patch.object(FileSystems, 'match', return_value=[match]):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.MEDIUM)
match = match_result(range(50000))
with mock.patch.object(FileSystems, 'match', return_value=[match]):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.MEDIUM)
def test_get_mode_large(self):
args = self._create_mock_args(
input_pattern='test', input_file=None, optimize_for_large_inputs=False)
match_result = collections.namedtuple('MatchResult', ['metadata_list'])
match = match_result(range(50001))
with mock.patch.object(FileSystems, 'match', return_value=[match]):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE)
def test_fail_on_invalid_flags(self):
# Start with valid flags, without setup.py.
pipeline_args = ['--project',
'gcp-variant-transforms-test',
'--staging_location',
'gs://integration_test_runs/staging']
pipeline_common._raise_error_on_invalid_flags(pipeline_args, None)
# Add Dataflow runner (requires --setup_file).
pipeline_args.extend(['--runner', 'DataflowRunner'])
with self.assertRaisesRegexp(ValueError, 'setup_file'):
pipeline_common._raise_error_on_invalid_flags(pipeline_args, None)
# Add setup.py (required for Variant Transforms run). This is now valid.
pipeline_args.extend(['--setup_file', 'setup.py'])
pipeline_common._raise_error_on_invalid_flags(pipeline_args, None)
with self.assertRaisesRegexp(ValueError, '--temp_location is required*'):
pipeline_common._raise_error_on_invalid_flags(pipeline_args, 'output')
pipeline_args.extend(['--temp_location', 'wrong_gcs'])
with self.assertRaisesRegexp(ValueError, '--temp_location must be valid*'):
pipeline_common._raise_error_on_invalid_flags(pipeline_args, 'output')
pipeline_args = pipeline_args[:-1] + ['gs://valid_bucket/temp']
pipeline_common._raise_error_on_invalid_flags(pipeline_args, 'output')
# Add an unknown flag.
pipeline_args.extend(['--unknown_flag', 'somevalue'])
with self.assertRaisesRegexp(ValueError, 'Unrecognized.*unknown_flag'):
pipeline_common._raise_error_on_invalid_flags(pipeline_args, 'output')
def test_get_compression_type(self):
vcf_metadata_list = [filesystem.FileMetadata(path, size) for
(path, size) in [('gs://1.vcf', 100), ('2.vcf', 100)]]
with mock.patch.object(
FileSystems, 'match',
return_value=[filesystem.MatchResult('vcf', vcf_metadata_list)]):
self.assertEqual(pipeline_common.get_compression_type(['vcf']),
filesystem.CompressionTypes.AUTO)
gzip_metadata_list = [
filesystem.FileMetadata(path, size) for
(path, size) in [('gs://1.vcf.gz', 100), ('2.vcf.gz', 100)]]
with mock.patch.object(
FileSystems, 'match',
return_value=[filesystem.MatchResult('gzip', gzip_metadata_list)]):
self.assertEqual(pipeline_common.get_compression_type('gzip'),
filesystem.CompressionTypes.GZIP)
mixed_metadata_list = [
filesystem.FileMetadata(path, size) for
(path, size) in [('gs://1.vcf.gz', 100), ('2.vcf', 100)]]
with mock.patch.object(
FileSystems, 'match',
return_value=[filesystem.MatchResult('mixed', mixed_metadata_list)]):
with self.assertRaises(ValueError):
pipeline_common.get_compression_type('mixed')
def test_get_splittable_bgzf(self):
non_gs_metadata_list = [filesystem.FileMetadata(path, size) for
(path, size) in [('1.vcf', 100),
('2.vcf', 100)]]
with mock.patch.object(
FileSystems, 'match',
return_value=[filesystem.MatchResult('non_gs', non_gs_metadata_list)]):
self.assertEqual(pipeline_common._get_splittable_bgzf(['non_gs']),
[])
gs_metadata_list = [filesystem.FileMetadata(path, size) for
(path, size) in [('gs://1.vcf.bgz', 100),
('gs://2.vcf.bgz', 100)]]
with mock.patch.object(
FileSystems, 'match',
return_value=[filesystem.MatchResult('gs', gs_metadata_list)]):
with mock.patch.object(FileSystems, 'exists', return_value=True):
self.assertEqual(
pipeline_common._get_splittable_bgzf(['index file exists']),
['gs://1.vcf.bgz', 'gs://2.vcf.bgz'])
with mock.patch.object(FileSystems, 'exists', return_value=False):
self.assertEqual(
pipeline_common._get_splittable_bgzf(['no index file']),
[])
class PipelineCommonWithFileTest(unittest.TestCase):
"""Tests cases for the `pipeline_common` script with file input."""
SAMPLE_LINES = ['./gcp_variant_transforms/testing/data/vcf/valid-4.0.vcf\n',
'./gcp_variant_transforms/testing/data/vcf/valid-4.0.vcf\n',
'./gcp_variant_transforms/testing/data/vcf/valid-4.0.vcf\n']
def _create_mock_args(self, **args):
return collections.namedtuple('MockArgs', args.keys())(*args.values())
def _get_pipeline_mode(self, args):
all_patterns = pipeline_common._get_all_patterns(args.input_pattern,
args.input_file)
return pipeline_common.get_pipeline_mode(all_patterns,
args.optimize_for_large_inputs)
def test_get_mode_optimize_set(self):
with temp_dir.TempDir() as tempdir:
filename = tempdir.create_temp_file(lines=self.SAMPLE_LINES)
args = self._create_mock_args(
input_pattern=None,
input_file=filename,
optimize_for_large_inputs=True)
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE)
def test_get_mode_small_still_large(self):
with temp_dir.TempDir() as tempdir:
filename = tempdir.create_temp_file(lines=self.SAMPLE_LINES)
args = self._create_mock_args(
input_pattern=None,
input_file=filename,
optimize_for_large_inputs=False)
match_result = collections.namedtuple('MatchResult', ['metadata_list'])
match = match_result([None for _ in range(100)])
with mock.patch.object(FileSystems, 'match', return_value=[match]):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE)
def test_get_mode_large(self):
with temp_dir.TempDir() as tempdir:
filename = tempdir.create_temp_file(lines=self.SAMPLE_LINES)
args = self._create_mock_args(
input_pattern=None,
input_file=filename,
optimize_for_large_inputs=False)
match_result = collections.namedtuple('MatchResult', ['metadata_list'])
match = match_result(range(50001))
with mock.patch.object(FileSystems, 'match', return_value=[match]):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE)
matches = [match_result(range(25000)),
match_result(range(25000)),
match_result(range(1))]
with mock.patch.object(FileSystems, 'match', return_value=matches):
self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE)
def test_validation_failure_for_invalid_input_file(self):
with self.assertRaisesRegexp(ValueError, 'Input file .* doesn\'t exist'):
pipeline_common._get_all_patterns(
input_pattern=None, input_file='nonexistent_file.vcf')
def test_validation_failure_for_empty_input_file(self):
with temp_dir.TempDir() as tempdir:
filename = tempdir.create_temp_file(lines=[])
with self.assertRaisesRegexp(ValueError, 'Input file .* is empty.'):
pipeline_common._get_all_patterns(
input_pattern=None, input_file=filename)
def test_validation_failure_for_wrong_pattern_in_input_file(self):
lines = ['./gcp_variant_transforms/testing/data/vcf/valid-4.0.vcf\n',
'non_existent.vcf\n',
'./gcp_variant_transforms/testing/data/vcf/valid-4.0.vcf\n']
with temp_dir.TempDir() as tempdir:
filename = tempdir.create_temp_file(lines=lines)
with self.assertRaisesRegexp(
ValueError, 'Input pattern .* from .* did not match any files.'):
pipeline_common._get_all_patterns(
input_pattern=None, input_file=filename)
class CommonPipelineTest(unittest.TestCase):
def test_read_variants(self):
pipeline = test_pipeline.TestPipeline()
all_patterns = [testdata_util.get_full_file_path('valid-4.0.vcf')]
variants = pipeline_common.read_variants(pipeline,
all_patterns,
PipelineModes.SMALL,
False)
assert_that(variants, asserts.count_equals_to(5))
pipeline.run()
def test_read_variants_large_mode(self):
pipeline = test_pipeline.TestPipeline()
all_patterns = [testdata_util.get_full_file_path('valid-4.0.vcf')]
variants = pipeline_common.read_variants(pipeline,
all_patterns,
PipelineModes.LARGE,
False)
assert_that(variants, asserts.count_equals_to(5))
pipeline.run()
def test_read_variants_pysam(self):
pipeline = test_pipeline.TestPipeline()
all_patterns = [testdata_util.get_full_file_path('valid-4.0.vcf')]
variants = pipeline_common.read_variants(pipeline,
all_patterns,
PipelineModes.SMALL,
False)
assert_that(variants, asserts.count_equals_to(5))
pipeline.run()
def test_read_variants_large_mode_pysam(self):
pipeline = test_pipeline.TestPipeline()
all_patterns = [testdata_util.get_full_file_path('valid-4.0.vcf')]
variants = pipeline_common.read_variants(pipeline,
all_patterns,
PipelineModes.LARGE,
False)
assert_that(variants, asserts.count_equals_to(5))
pipeline.run()