shunping commented on code in PR #36621: URL: https://github.com/apache/beam/pull/36621#discussion_r2462779547
########## sdks/python/apache_beam/examples/cookbook/ordered_window_elements/batch_test.py: ########## @@ -0,0 +1,325 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging +import random +import unittest + +from parameterized import param +from parameterized import parameterized + +import apache_beam as beam +from apache_beam.examples.cookbook.ordered_window_elements.batch import OrderedWindowElements # pylint: disable=line-too-long +from apache_beam.examples.cookbook.ordered_window_elements.batch import WindowGapStrategy # pylint: disable=line-too-long +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.testing.test_pipeline import TestPipeline +from apache_beam.testing.util import assert_that +from apache_beam.testing.util import equal_to +from apache_beam.utils.timestamp import Timestamp + +logging.basicConfig(level=logging.INFO) +#logging.basicConfig(level=logging.WARNING) + +options = PipelineOptions([ + "--environment_type=LOOPBACK", + "--runner=PrismRunner", #"--runner=FnApiRunner", + "--prism_log_kind=dev", + # "--runner=PortableRunner", + # "--job_endpoint=localhost:8073", +]) + +ENABLE_LOGGING = False +WINDOW_SIZE = 3 + + +def _maybe_log_elements(pcoll, prefix="result="): + if ENABLE_LOGGING: + return pcoll | beam.LogElements( + prefix=prefix, + level=logging.WARNING, + with_timestamp=True, + with_window=True, + use_epoch_time=True) + else: + return pcoll + + +def _create_input_batch(elements: list[int], shuffle_data=True): + if shuffle_data: + random.shuffle(elements) + return beam.Create([(Timestamp.of(e), e) for e in elements]) + + +def _create_input_batch_without_timestamp( + elements: list[int], shuffle_data=True): + if shuffle_data: + random.shuffle(elements) + return beam.Create(elements) + + +def _convert_timestamp_to_int(): + return beam.MapTuple( + lambda window, elements: + ((int(window[0].micros // 1e6), int(window[1].micros // 1e6)), + [(int(t.micros // 1e6), v) for t, v in elements])) + + +class OrderedWindowElementsTest(unittest.TestCase): + def setUp(self) -> None: + self.options = PipelineOptions([ + "--streaming", Review Comment: Good catch. Fixed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
