github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/examples/complete/estimate_pi_it_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 """End-to-end test for Estimate Pi example.""" 19 # pytype: skip-file 20 21 import json 22 import logging 23 import unittest 24 import uuid 25 26 import pytest 27 28 from apache_beam.examples.complete import estimate_pi 29 from apache_beam.testing.test_pipeline import TestPipeline 30 from apache_beam.testing.test_utils import read_files_from_pattern 31 32 33 class EstimatePiIT(unittest.TestCase): 34 @pytest.mark.no_xdist 35 @pytest.mark.examples_postcommit 36 def test_estimate_pi_output_file(self): 37 test_pipeline = TestPipeline(is_integration_test=True) 38 OUTPUT_FILE = \ 39 'gs://temp-storage-for-end-to-end-tests/py-it-cloud/output' 40 output = '/'.join([OUTPUT_FILE, str(uuid.uuid4()), 'result']) 41 extra_opts = {'output': output} 42 estimate_pi.run(test_pipeline.get_full_options_as_args(**extra_opts)) 43 # Load result file and compare. 44 result = read_files_from_pattern('%s*' % output) 45 [_, _, estimated_pi] = json.loads(result.strip()) 46 # Note: Probabilistically speaking this test can fail with a probability 47 # that is very small (VERY) given that we run at least 100 thousand 48 # trials. 49 self.assertTrue(3.125 <= estimated_pi <= 3.155) 50 51 52 if __name__ == '__main__': 53 logging.getLogger().setLevel(logging.INFO) 54 unittest.main()