github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/runners/runner_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 tests for the PipelineRunner and DirectRunner classes.
    19  
    20  Note that PipelineRunner and DirectRunner functionality is tested in all
    21  the other unit tests. In this file we choose to test only aspects related to
    22  caching and clearing values that are not tested elsewhere.
    23  """
    24  
    25  # pytype: skip-file
    26  
    27  import unittest
    28  
    29  import apache_beam as beam
    30  from apache_beam.metrics.metric import Metrics
    31  from apache_beam.runners import DirectRunner
    32  from apache_beam.runners import create_runner
    33  
    34  
    35  class RunnerTest(unittest.TestCase):
    36    default_properties = [
    37        '--dataflow_endpoint=ignored',
    38        '--job_name=test-job',
    39        '--project=test-project',
    40        '--staging_location=ignored',
    41        '--temp_location=/dev/null',
    42        '--no_auth'
    43    ]
    44  
    45    def test_create_runner(self):
    46      self.assertTrue(isinstance(create_runner('DirectRunner'), DirectRunner))
    47      self.assertRaises(ValueError, create_runner, 'xyz')
    48  
    49    def test_create_runner_shorthand(self):
    50      self.assertTrue(isinstance(create_runner('DiReCtRuNnEr'), DirectRunner))
    51      self.assertTrue(isinstance(create_runner('directrunner'), DirectRunner))
    52      self.assertTrue(isinstance(create_runner('direct'), DirectRunner))
    53      self.assertTrue(isinstance(create_runner('DiReCt'), DirectRunner))
    54      self.assertTrue(isinstance(create_runner('Direct'), DirectRunner))
    55  
    56    def test_run_api(self):
    57      my_metric = Metrics.counter('namespace', 'my_metric')
    58      runner = DirectRunner()
    59      result = runner.run(
    60          beam.Create([1, 10, 100]) | beam.Map(lambda x: my_metric.inc(x)))
    61      result.wait_until_finish()
    62      # Use counters to assert the pipeline actually ran.
    63      my_metric_value = result.metrics().query()['counters'][0].committed
    64      self.assertEqual(my_metric_value, 111)
    65  
    66    def test_run_api_with_callable(self):
    67      my_metric = Metrics.counter('namespace', 'my_metric')
    68  
    69      def fn(start):
    70        return (
    71            start
    72            | beam.Create([1, 10, 100])
    73            | beam.Map(lambda x: my_metric.inc(x)))
    74  
    75      runner = DirectRunner()
    76      result = runner.run(fn)
    77      result.wait_until_finish()
    78      # Use counters to assert the pipeline actually ran.
    79      my_metric_value = result.metrics().query()['counters'][0].committed
    80      self.assertEqual(my_metric_value, 111)
    81  
    82  
    83  if __name__ == '__main__':
    84    unittest.main()