github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/wordcount_minimal_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  """Test for the minimal wordcount example."""
    19  
    20  # pytype: skip-file
    21  
    22  # beam-playground:
    23  #   name: WordCountMinimalTest
    24  #   description: Unit-test for the minimal WordCount example.
    25  #   multifile: false
    26  #   context_line: 43
    27  #   categories:
    28  #     - IO
    29  #     - Combiners
    30  #   complexity: MEDIUM
    31  #   tags:
    32  #     - count
    33  #     - test
    34  
    35  import collections
    36  import logging
    37  import re
    38  import unittest
    39  import uuid
    40  
    41  import pytest
    42  
    43  from apache_beam.examples import wordcount_minimal
    44  from apache_beam.testing.test_pipeline import TestPipeline
    45  from apache_beam.testing.test_utils import create_file
    46  from apache_beam.testing.test_utils import read_files_from_pattern
    47  
    48  
    49  @pytest.mark.examples_postcommit
    50  class WordCountMinimalTest(unittest.TestCase):
    51    """Unit test for wordcount_minimal example with direct runner."""
    52  
    53    SAMPLE_TEXT = 'a b c a b a\n aa bb cc aa bb aa'
    54  
    55    def test_basics(self):
    56      test_pipeline = TestPipeline(is_integration_test=True)
    57  
    58      # Setup the files with expected content.
    59      temp_location = test_pipeline.get_option('temp_location')
    60      temp_path = '/'.join([temp_location, str(uuid.uuid4())])
    61      input = create_file('/'.join([temp_path, 'input.txt']), self.SAMPLE_TEXT)
    62  
    63      extra_opts = {'input': input, 'output': '%s.result' % temp_path}
    64      expected_words = collections.defaultdict(int)
    65      for word in re.findall(r'\w+', self.SAMPLE_TEXT):
    66        expected_words[word] += 1
    67      wordcount_minimal.main(
    68          test_pipeline.get_full_options_as_args(**extra_opts),
    69          save_main_session=False)
    70  
    71      # Parse result file and compare.
    72      results = []
    73      lines = read_files_from_pattern(temp_path + '.result*').splitlines()
    74      for line in lines:
    75        match = re.search(r'([a-z]+): ([0-9]+)', line)
    76        if match is not None:
    77          results.append((match.group(1), int(match.group(2))))
    78      self.assertEqual(sorted(results), sorted(expected_words.items()))
    79  
    80  
    81  if __name__ == '__main__':
    82    logging.getLogger().setLevel(logging.INFO)
    83    unittest.main()