-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathtest_pipeline_test.py
More file actions
131 lines (109 loc) · 4.52 KB
/
test_pipeline_test.py
File metadata and controls
131 lines (109 loc) · 4.52 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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
"""Unit test for the TestPipeline class"""
# pytype: skip-file
import logging
import unittest
import mock
from hamcrest.core.assert_that import assert_that as hc_assert_that
from hamcrest.core.base_matcher import BaseMatcher
from apache_beam.internal import pickler
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.testing.test_pipeline import TestPipeline
# A simple matcher that is ued for testing extra options appending.
class SimpleMatcher(BaseMatcher):
def _matches(self, item):
return True
class TestPipelineTest(unittest.TestCase):
TEST_CASE = {
'options': ['--test-pipeline-options', '--job=mockJob --male --age=1'],
'expected_list': ['--job=mockJob', '--male', '--age=1'],
'expected_dict': {
'job': 'mockJob', 'male': True, 'age': 1
}
}
# Used for testing pipeline option creation.
class TestParsingOptions(PipelineOptions):
@classmethod
def _add_argparse_args(cls, parser):
parser.add_argument('--job', action='store', help='mock job')
parser.add_argument('--male', action='store_true', help='mock gender')
parser.add_argument('--age', action='store', type=int, help='mock age')
def test_option_args_parsing(self):
test_pipeline = TestPipeline(argv=self.TEST_CASE['options'])
self.assertListEqual(
sorted(test_pipeline.get_full_options_as_args()),
sorted(self.TEST_CASE['expected_list']))
def test_empty_option_args_parsing(self):
test_pipeline = TestPipeline()
self.assertListEqual([], test_pipeline.get_full_options_as_args())
def test_create_test_pipeline_options(self):
test_pipeline = TestPipeline(argv=self.TEST_CASE['options'])
test_options = PipelineOptions(test_pipeline.get_full_options_as_args())
self.assertLessEqual(
self.TEST_CASE['expected_dict'].items(),
test_options.get_all_options().items())
EXTRA_OPT_CASES = [{
'options': {
'name': 'Mark'
}, 'expected': ['--name=Mark']
}, {
'options': {
'student': True
}, 'expected': ['--student']
}, {
'options': {
'student': False
}, 'expected': []
},
{
'options': {
'name': 'Mark', 'student': True
},
'expected': ['--name=Mark', '--student']
}]
def test_append_extra_options(self):
test_pipeline = TestPipeline()
for case in self.EXTRA_OPT_CASES:
opt_list = test_pipeline.get_full_options_as_args(**case['options'])
self.assertListEqual(sorted(opt_list), sorted(case['expected']))
def test_append_verifier_in_extra_opt(self):
extra_opt = {'matcher': SimpleMatcher()}
opt_list = TestPipeline().get_full_options_as_args(**extra_opt)
_, value = opt_list[0].split('=', 1)
matcher = pickler.loads(value)
self.assertTrue(isinstance(matcher, BaseMatcher))
hc_assert_that(None, matcher)
def test_get_option(self):
name, value = ('job', 'mockJob')
test_pipeline = TestPipeline()
test_pipeline.options_list = ['--%s=%s' % (name, value)]
self.assertEqual(test_pipeline.get_option(name), value)
def test_skip_IT(self):
with TestPipeline(is_integration_test=True) as _:
# Note that this will never be reached since it should be skipped above.
pass
self.fail()
@mock.patch('apache_beam.testing.test_pipeline.Pipeline.run', autospec=True)
def test_not_use_test_runner_api(self, mock_run):
with TestPipeline(argv=['--not-use-test-runner-api'],
blocking=False) as test_pipeline:
pass
mock_run.assert_called_once_with(test_pipeline, test_runner_api=False)
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
unittest.main()