github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/complete/game/game_stats_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 game_stats 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 game_stats
    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 GameStatsTest(unittest.TestCase):
    33  
    34    SAMPLE_DATA = [
    35        'user1_team1,team1,18,1447686663000,2015-11-16 15:11:03.921',
    36        'user1_team1,team1,18,1447690263000,2015-11-16 16:11:03.921',
    37        'user2_team2,team2,2,1447690263000,2015-11-16 16:11:03.955',
    38        'user3_team3,team3,8,1447690263000,2015-11-16 16:11:03.955',
    39        'user4_team3,team3,5,1447690263000,2015-11-16 16:11:03.959',
    40        'user1_team1,team1,14,1447697463000,2015-11-16 18:11:03.955',
    41        'robot1_team1,team1,9000,1447697463000,2015-11-16 18:11:03.955',
    42        'robot2_team2,team2,1,1447697463000,2015-11-16 20:11:03.955',
    43        'robot2_team2,team2,9000,1447697463000,2015-11-16 21:11:03.955',
    44    ]
    45  
    46    def create_data(self, p):
    47      return (p
    48              | beam.Create(GameStatsTest.SAMPLE_DATA)
    49              | beam.ParDo(game_stats.ParseGameEventFn())
    50              | beam.Map(lambda elem:\
    51                         beam.window.TimestampedValue(elem, elem['timestamp'])))
    52  
    53    def test_spammy_users(self):
    54      with TestPipeline() as p:
    55        result = (
    56            self.create_data(p)
    57            | beam.Map(lambda elem: (elem['user'], elem['score']))
    58            | game_stats.CalculateSpammyUsers())
    59        assert_that(
    60            result, equal_to([('robot1_team1', 9000), ('robot2_team2', 9001)]))
    61  
    62    def test_game_stats_sessions(self):
    63      session_gap = 5 * 60
    64      user_activity_window_duration = 30 * 60
    65      with TestPipeline() as p:
    66        result = (
    67            self.create_data(p)
    68            | beam.Map(lambda elem: (elem['user'], elem['score']))
    69            | 'WindowIntoSessions' >> beam.WindowInto(
    70                beam.window.Sessions(session_gap),
    71                timestamp_combiner=beam.window.TimestampCombiner.OUTPUT_AT_EOW)
    72            | beam.CombinePerKey(lambda _: None)
    73            | beam.ParDo(game_stats.UserSessionActivity())
    74            | 'WindowToExtractSessionMean' >> beam.WindowInto(
    75                beam.window.FixedWindows(user_activity_window_duration))
    76            | beam.CombineGlobally(beam.combiners.MeanCombineFn())\
    77                .without_defaults())
    78        assert_that(result, equal_to([300.0, 300.0, 300.0]))
    79  
    80  
    81  if __name__ == '__main__':
    82    logging.getLogger().setLevel(logging.INFO)
    83    unittest.main()