github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/complete/top_wikipedia_sessions_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 top wikipedia sessions example.""" 19 20 # pytype: skip-file 21 22 import json 23 import unittest 24 25 import apache_beam as beam 26 from apache_beam.examples.complete import top_wikipedia_sessions 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 ComputeTopSessionsTest(unittest.TestCase): 33 34 EDITS = [ 35 json.dumps({ 36 'timestamp': 0.0, 'contributor_username': 'user1' 37 }), 38 json.dumps({ 39 'timestamp': 0.001, 'contributor_username': 'user1' 40 }), 41 json.dumps({ 42 'timestamp': 0.002, 'contributor_username': 'user1' 43 }), 44 json.dumps({ 45 'timestamp': 0.0, 'contributor_username': 'user2' 46 }), 47 json.dumps({ 48 'timestamp': 0.001, 'contributor_username': 'user2' 49 }), 50 json.dumps({ 51 'timestamp': 3.601, 'contributor_username': 'user2' 52 }), 53 json.dumps({ 54 'timestamp': 3.602, 'contributor_username': 'user2' 55 }), 56 json.dumps({ 57 'timestamp': 2 * 3600.0, 'contributor_username': 'user2' 58 }), 59 json.dumps({ 60 'timestamp': 35 * 24 * 3.600, 'contributor_username': 'user3' 61 }) 62 ] 63 64 EXPECTED = [ 65 'user1 : [0.0, 3600.002) : 3 : [0.0, 2592000.0)', 66 'user2 : [0.0, 3603.602) : 4 : [0.0, 2592000.0)', 67 'user2 : [7200.0, 10800.0) : 1 : [0.0, 2592000.0)', 68 'user3 : [3024.0, 6624.0) : 1 : [0.0, 2592000.0)', 69 ] 70 71 def test_compute_top_sessions(self): 72 with TestPipeline() as p: 73 edits = p | beam.Create(self.EDITS) 74 result = edits | top_wikipedia_sessions.ComputeTopSessions(1.0) 75 76 assert_that(result, equal_to(self.EXPECTED)) 77 78 79 if __name__ == '__main__': 80 unittest.main()