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

     1  # -*- coding: utf-8 -*-
     2  #
     3  # Licensed to the Apache Software Foundation (ASF) under one or more
     4  # contributor license agreements.  See the NOTICE file distributed with
     5  # this work for additional information regarding copyright ownership.
     6  # The ASF licenses this file to You under the Apache License, Version 2.0
     7  # (the "License"); you may not use this file except in compliance with
     8  # the License.  You may obtain a copy of the License at
     9  #
    10  #    http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  # Unless required by applicable law or agreed to in writing, software
    13  # distributed under the License is distributed on an "AS IS" BASIS,
    14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  # See the License for the specific language governing permissions and
    16  # limitations under the License.
    17  #
    18  
    19  """Test for the wordcount example."""
    20  
    21  # pytype: skip-file
    22  
    23  # beam-playground:
    24  #   name: WordCountTest
    25  #   description: Unit-test for the WordCount example.
    26  #   multifile: false
    27  #   context_line: 44
    28  #   categories:
    29  #     - IO
    30  #     - Combiners
    31  #   complexity: MEDIUM
    32  #   tags:
    33  #     - count
    34  #     - test
    35  
    36  import collections
    37  import logging
    38  import re
    39  import unittest
    40  import uuid
    41  
    42  import pytest
    43  
    44  from apache_beam.examples import wordcount
    45  from apache_beam.testing.test_pipeline import TestPipeline
    46  from apache_beam.testing.test_utils import create_file
    47  from apache_beam.testing.test_utils import read_files_from_pattern
    48  
    49  
    50  @pytest.mark.examples_postcommit
    51  class WordCountTest(unittest.TestCase):
    52  
    53    SAMPLE_TEXT = (
    54        u'a b c a b a\nacento gráfico\nJuly 30, 2018\n\n aa bb cc aa bb aa')
    55  
    56    def test_basics(self):
    57      test_pipeline = TestPipeline(is_integration_test=True)
    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      extra_opts = {'input': input, 'output': '%s.result' % temp_path}
    63      expected_words = collections.defaultdict(int)
    64      for word in re.findall(r'[\w\']+', self.SAMPLE_TEXT, re.UNICODE):
    65        expected_words[word] += 1
    66      wordcount.run(
    67          test_pipeline.get_full_options_as_args(**extra_opts),
    68          save_main_session=False)
    69      # Parse result file and compare.
    70      results = []
    71      lines = read_files_from_pattern(temp_path + '.result*').splitlines()
    72      for line in lines:
    73        match = re.search(r'(\S+): ([0-9]+)', line)
    74        if match is not None:
    75          results.append((match.group(1), int(match.group(2))))
    76      self.assertEqual(sorted(results), sorted(expected_words.items()))
    77  
    78  
    79  if __name__ == '__main__':
    80    logging.getLogger().setLevel(logging.INFO)
    81    unittest.main()