github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/complete/game/leader_board_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 leader_board 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.complete.game import leader_board 27 from apache_beam.options.pipeline_options import PipelineOptions 28 from apache_beam.testing.test_pipeline import TestPipeline 29 from apache_beam.testing.util import assert_that 30 from apache_beam.testing.util import equal_to 31 32 33 class LeaderBoardTest(unittest.TestCase): 34 35 SAMPLE_DATA = [ 36 'user1_team1,team1,18,1447686663000,2015-11-16 15:11:03.921', 37 'user1_team1,team1,18,1447690263000,2015-11-16 16:11:03.921', 38 'user2_team2,team2,2,1447690263000,2015-11-16 16:11:03.955', 39 'user3_team3,team3,8,1447690263000,2015-11-16 16:11:03.955', 40 'user4_team3,team3,5,1447690263000,2015-11-16 16:11:03.959', 41 'user1_team1,team1,14,1447697463000,2015-11-16 18:11:03.955', 42 ] 43 44 def create_data(self, p): 45 return (p 46 | beam.Create(LeaderBoardTest.SAMPLE_DATA) 47 | beam.ParDo(leader_board.ParseGameEventFn()) 48 | beam.Map(lambda elem:\ 49 beam.window.TimestampedValue(elem, elem['timestamp']))) 50 51 def test_leader_board_teams(self): 52 with TestPipeline() as p: 53 result = ( 54 self.create_data(p) 55 | leader_board.CalculateTeamScores( 56 team_window_duration=60, allowed_lateness=120)) 57 assert_that( 58 result, 59 equal_to([('team1', 14), ('team1', 18), ('team1', 18), ('team2', 2), 60 ('team3', 13)])) 61 62 def test_leader_board_users(self): 63 test_options = PipelineOptions(flags=['--allow_unsafe_triggers']) 64 with TestPipeline(options=test_options) as p: 65 result = ( 66 self.create_data(p) 67 | leader_board.CalculateUserScores(allowed_lateness=120)) 68 assert_that(result, equal_to([])) 69 70 71 if __name__ == '__main__': 72 logging.getLogger().setLevel(logging.INFO) 73 unittest.main()