github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/cookbook/coders_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 coders example.""" 19 20 # pytype: skip-file 21 22 import logging 23 import unittest 24 25 import apache_beam as beam 26 from apache_beam.examples.cookbook import coders 27 from apache_beam.testing.test_pipeline import TestPipeline 28 from apache_beam.testing.util import assert_that 29 from apache_beam.testing.util import equal_to 30 31 32 class CodersTest(unittest.TestCase): 33 34 SAMPLE_RECORDS = [{ 35 'host': ['Germany', 1], 'guest': ['Italy', 0] 36 }, { 37 'host': ['Germany', 1], 'guest': ['Brasil', 3] 38 }, { 39 'host': ['Brasil', 1], 'guest': ['Italy', 0] 40 }] 41 42 EXPECTED_RESULT = [('Italy', 0), ('Brasil', 6), ('Germany', 3)] 43 44 def test_compute_points(self): 45 with TestPipeline() as p: 46 records = p | 'create' >> beam.Create(self.SAMPLE_RECORDS) 47 result = ( 48 records 49 | 'points' >> beam.FlatMap(coders.compute_points) 50 | beam.CombinePerKey(sum)) 51 assert_that(result, equal_to(self.EXPECTED_RESULT)) 52 53 54 if __name__ == '__main__': 55 logging.getLogger().setLevel(logging.INFO) 56 unittest.main()