github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/dataframe/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  import collections
    24  import logging
    25  import re
    26  import unittest
    27  import uuid
    28  
    29  import pytest
    30  
    31  from apache_beam.examples.dataframe import wordcount
    32  from apache_beam.testing.test_pipeline import TestPipeline
    33  from apache_beam.testing.test_utils import create_file
    34  from apache_beam.testing.test_utils import read_files_from_pattern
    35  
    36  
    37  class WordCountTest(unittest.TestCase):
    38  
    39    SAMPLE_TEXT = """
    40    a
    41    a b
    42    a b c
    43    loooooonger words
    44    """
    45  
    46    @pytest.mark.examples_postcommit
    47    def test_basics(self):
    48      test_pipeline = TestPipeline(is_integration_test=True)
    49      # Setup the files with expected content.
    50      temp_location = test_pipeline.get_option('temp_location')
    51      temp_path = '/'.join([temp_location, str(uuid.uuid4())])
    52      input = create_file('/'.join([temp_path, 'input.txt']), self.SAMPLE_TEXT)
    53      expected_words = collections.defaultdict(int)
    54      for word in re.findall(r'[\w]+', self.SAMPLE_TEXT):
    55        expected_words[word] += 1
    56      extra_opts = {'input': input, 'output': '%s.result' % temp_path}
    57      wordcount.run(test_pipeline.get_full_options_as_args(**extra_opts))
    58      # Parse result file and compare.
    59      results = []
    60      lines = read_files_from_pattern(temp_path + '.result*').splitlines()
    61      for line in lines:
    62        match = re.search(r'(\S+),([0-9]+)', line)
    63        if match is not None:
    64          results.append((match.group(1), int(match.group(2))))
    65        elif line.strip():
    66          self.assertEqual(line.strip(), 'word,count')
    67      self.assertEqual(sorted(results), sorted(expected_words.items()))
    68  
    69  
    70  if __name__ == '__main__':
    71    logging.getLogger().setLevel(logging.INFO)
    72    unittest.main()