github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/testing/test_pipeline_test.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  
    18  """Unit test for the TestPipeline class"""
    19  
    20  # pytype: skip-file
    21  
    22  import logging
    23  import unittest
    24  
    25  import mock
    26  from hamcrest.core.assert_that import assert_that as hc_assert_that
    27  from hamcrest.core.base_matcher import BaseMatcher
    28  
    29  from apache_beam.internal import pickler
    30  from apache_beam.options.pipeline_options import PipelineOptions
    31  from apache_beam.testing.test_pipeline import TestPipeline
    32  
    33  
    34  # A simple matcher that is ued for testing extra options appending.
    35  class SimpleMatcher(BaseMatcher):
    36    def _matches(self, item):
    37      return True
    38  
    39  
    40  class TestPipelineTest(unittest.TestCase):
    41  
    42    TEST_CASE = {
    43        'options': ['--test-pipeline-options', '--job=mockJob --male --age=1'],
    44        'expected_list': ['--job=mockJob', '--male', '--age=1'],
    45        'expected_dict': {
    46            'job': 'mockJob', 'male': True, 'age': 1
    47        }
    48    }
    49  
    50    # Used for testing pipeline option creation.
    51    class TestParsingOptions(PipelineOptions):
    52      @classmethod
    53      def _add_argparse_args(cls, parser):
    54        parser.add_argument('--job', action='store', help='mock job')
    55        parser.add_argument('--male', action='store_true', help='mock gender')
    56        parser.add_argument('--age', action='store', type=int, help='mock age')
    57  
    58    def test_option_args_parsing(self):
    59      test_pipeline = TestPipeline(argv=self.TEST_CASE['options'])
    60      self.assertListEqual(
    61          sorted(test_pipeline.get_full_options_as_args()),
    62          sorted(self.TEST_CASE['expected_list']))
    63  
    64    def test_empty_option_args_parsing(self):
    65      test_pipeline = TestPipeline()
    66      self.assertListEqual([], test_pipeline.get_full_options_as_args())
    67  
    68    def test_create_test_pipeline_options(self):
    69      test_pipeline = TestPipeline(argv=self.TEST_CASE['options'])
    70      test_options = PipelineOptions(test_pipeline.get_full_options_as_args())
    71      self.assertDictContainsSubset(
    72          self.TEST_CASE['expected_dict'], test_options.get_all_options())
    73  
    74    EXTRA_OPT_CASES = [{
    75        'options': {
    76            'name': 'Mark'
    77        }, 'expected': ['--name=Mark']
    78    }, {
    79        'options': {
    80            'student': True
    81        }, 'expected': ['--student']
    82    }, {
    83        'options': {
    84            'student': False
    85        }, 'expected': []
    86    },
    87                       {
    88                           'options': {
    89                               'name': 'Mark', 'student': True
    90                           },
    91                           'expected': ['--name=Mark', '--student']
    92                       }]
    93  
    94    def test_append_extra_options(self):
    95      test_pipeline = TestPipeline()
    96      for case in self.EXTRA_OPT_CASES:
    97        opt_list = test_pipeline.get_full_options_as_args(**case['options'])
    98        self.assertListEqual(sorted(opt_list), sorted(case['expected']))
    99  
   100    def test_append_verifier_in_extra_opt(self):
   101      extra_opt = {'matcher': SimpleMatcher()}
   102      opt_list = TestPipeline().get_full_options_as_args(**extra_opt)
   103      _, value = opt_list[0].split('=', 1)
   104      matcher = pickler.loads(value)
   105      self.assertTrue(isinstance(matcher, BaseMatcher))
   106      hc_assert_that(None, matcher)
   107  
   108    def test_get_option(self):
   109      name, value = ('job', 'mockJob')
   110      test_pipeline = TestPipeline()
   111      test_pipeline.options_list = ['--%s=%s' % (name, value)]
   112      self.assertEqual(test_pipeline.get_option(name), value)
   113  
   114    def test_skip_IT(self):
   115      with TestPipeline(is_integration_test=True) as _:
   116        # Note that this will never be reached since it should be skipped above.
   117        pass
   118      self.fail()
   119  
   120    @mock.patch('apache_beam.testing.test_pipeline.Pipeline.run', autospec=True)
   121    def test_not_use_test_runner_api(self, mock_run):
   122      with TestPipeline(argv=['--not-use-test-runner-api'],
   123                        blocking=False) as test_pipeline:
   124        pass
   125      mock_run.assert_called_once_with(test_pipeline, test_runner_api=False)
   126  
   127  
   128  if __name__ == '__main__':
   129    logging.getLogger().setLevel(logging.INFO)
   130    unittest.main()