github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/cookbook/multiple_output_pardo_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 multiple_output_pardo example.""" 19 20 # pytype: skip-file 21 22 import logging 23 import re 24 import unittest 25 import uuid 26 27 import pytest 28 29 from apache_beam.examples.cookbook import multiple_output_pardo 30 from apache_beam.testing.test_pipeline import TestPipeline 31 from apache_beam.testing.test_utils import create_file 32 from apache_beam.testing.test_utils import read_files_from_pattern 33 34 35 class MultipleOutputParDo(unittest.TestCase): 36 37 SAMPLE_TEXT = 'A whole new world\nA new fantastic point of view' 38 EXPECTED_SHORT_WORDS = [('A', 2), ('new', 2), ('of', 1)] 39 EXPECTED_WORDS = [('whole', 1), ('world', 1), ('fantastic', 1), ('point', 1), 40 ('view', 1)] 41 42 def get_wordcount_results(self, result_path): 43 results = [] 44 lines = read_files_from_pattern(result_path).splitlines() 45 for line in lines: 46 match = re.search(r'([A-Za-z]+): ([0-9]+)', line) 47 if match is not None: 48 results.append((match.group(1), int(match.group(2)))) 49 return results 50 51 @pytest.mark.examples_postcommit 52 @pytest.mark.sickbay_flink 53 def test_multiple_output_pardo(self): 54 test_pipeline = TestPipeline(is_integration_test=True) 55 56 # Setup the files with expected content. 57 temp_location = test_pipeline.get_option('temp_location') 58 input_folder = '/'.join([temp_location, str(uuid.uuid4())]) 59 input = create_file('/'.join([input_folder, 'input.txt']), self.SAMPLE_TEXT) 60 result_prefix = '/'.join([temp_location, str(uuid.uuid4()), 'result']) 61 62 extra_opts = {'input': input, 'output': result_prefix} 63 multiple_output_pardo.run( 64 test_pipeline.get_full_options_as_args(**extra_opts), 65 save_main_session=False) 66 67 expected_char_count = len(''.join(self.SAMPLE_TEXT.split('\n'))) 68 contents = read_files_from_pattern(result_prefix + '-chars*') 69 self.assertEqual(expected_char_count, int(contents)) 70 71 short_words = self.get_wordcount_results(result_prefix + '-short-words*') 72 self.assertEqual(sorted(short_words), sorted(self.EXPECTED_SHORT_WORDS)) 73 74 words = self.get_wordcount_results(result_prefix + '-words*') 75 self.assertEqual(sorted(words), sorted(self.EXPECTED_WORDS)) 76 77 78 if __name__ == '__main__': 79 logging.getLogger().setLevel(logging.INFO) 80 unittest.main()