3636from apache_beam .testing import test_utils
3737from apache_beam .testing .pipeline_verifiers import PipelineStateMatcher
3838from apache_beam .testing .test_pipeline import TestPipeline
39+ from apache_beam .options .pipeline_options import PipelineOptions
40+ from apache_beam .options .pipeline_options import StandardOptions
41+ from apache_beam .transforms import Create
42+ from google .pubsub_v1 .types import Subscription
43+ from google .cloud import pubsub
3944
4045INPUT_TOPIC = 'psit_topic_input'
4146OUTPUT_TOPIC = 'psit_topic_output'
@@ -139,7 +144,6 @@ def setUp(self):
139144 self .uuid = str (uuid .uuid4 ())
140145
141146 # Set up PubSub environment.
142- from google .cloud import pubsub
143147 self .pub_client = pubsub .PublisherClient ()
144148 self .input_topic = self .pub_client .create_topic (
145149 name = self .pub_client .topic_path (self .project , INPUT_TOPIC + self .uuid ))
@@ -228,9 +232,6 @@ def _test_batch_write(self, with_attributes):
228232 with_attributes: False - Writes message data only.
229233 True - Writes message data and attributes.
230234 """
231- from apache_beam .options .pipeline_options import PipelineOptions
232- from apache_beam .options .pipeline_options import StandardOptions
233- from apache_beam .transforms import Create
234235
235236 # Create test messages for batch mode
236237 test_messages = [
@@ -309,24 +310,19 @@ def test_batch_write_with_attributes(self):
309310 def test_batch_write_with_ordering_key (self ):
310311 """Test WriteToPubSub in batch mode with ordering keys.
311312
312- Dataflow Native PubSub Sink does not support ordering_key
313- (https://github.com/apache/beam/issues/36201), therefore this
314- test only applies to runners that use Beam's Python WriteToPubSub Sink.
315- To use ordering_key, Dataflow users should use the XLang WriteToPubSub
316- path instead.
313+ Dataflow's Native Pub/Sub Sink does not support ordering_key
314+ (see https://github.com/apache/beam/issues/36201), so this test
315+ only applies to runners using Beam's Python WriteToPubSub Sink.
316+ Dataflow users should use the XLang WriteToPubSub path instead
317+ (apache_beam.io.external.gcp.pubsub.WriteToPubSub with
318+ publish_with_ordering_key=True).
317319 """
318320 if self .runner_name == 'TestDataflowRunner' :
319321 self .skipTest (
320322 'Dataflow Native PubSub Sink does not support ordering_key '
321- '(see https://github.com/apache/beam/issues/36201), therefore '
322- 'this test only applies to runners that use Beam\' s Python '
323- 'WriteToPubSub Sink. To use ordering_key, Dataflow users should '
324- 'use the XLang WriteToPubSub path instead.' )
325-
326- from apache_beam .options .pipeline_options import PipelineOptions
327- from apache_beam .options .pipeline_options import StandardOptions
328- from apache_beam .transforms import Create
329- from google .pubsub_v1 .types import Subscription
323+ '(see https://github.com/apache/beam/issues/36201). '
324+ 'Use apache_beam.io.external.gcp.pubsub.WriteToPubSub '
325+ 'with publish_with_ordering_key=True instead.' )
330326
331327 ordering_topic = self .pub_client .create_topic (
332328 name = self .pub_client .topic_path (
@@ -336,7 +332,8 @@ def test_batch_write_with_ordering_key(self):
336332 name = self .sub_client .subscription_path (
337333 self .project , 'psit_sub_ordering' + self .uuid ),
338334 topic = ordering_topic .name ,
339- enable_message_ordering = True ))
335+ enable_message_ordering = True ,
336+ ))
340337 time .sleep (10 )
341338
342339 try :
@@ -346,7 +343,7 @@ def test_batch_write_with_ordering_key(self):
346343 PubsubMessage (
347344 b'order_data002' , {'attr' : 'value2' }, ordering_key = 'key1' ),
348345 PubsubMessage (
349- b'order_data003' , {'attr' : 'value3' }, ordering_key = 'key2' )
346+ b'order_data003' , {'attr' : 'value3' }, ordering_key = 'key2' ),
350347 ]
351348
352349 pipeline_options = PipelineOptions ()
@@ -361,30 +358,30 @@ def test_batch_write_with_ordering_key(self):
361358
362359 response = self .sub_client .pull (
363360 request = {
364- " subscription" : ordering_sub .name ,
365- " max_messages" : 10 ,
361+ ' subscription' : ordering_sub .name ,
362+ ' max_messages' : 10 ,
366363 })
367364
368365 self .assertEqual (len (response .received_messages ), len (test_messages ))
369366
370- received_map = {
371- msg .message .data : msg .message
372- for msg in response .received_messages
373- }
374- self .assertEqual (received_map [b'order_data001' ].ordering_key , 'key1' )
375- self .assertEqual (received_map [b'order_data002' ].ordering_key , 'key1' )
376- self .assertEqual (received_map [b'order_data003' ].ordering_key , 'key2' )
367+ received_ordering_keys = [
368+ msg .message .ordering_key for msg in response .received_messages
369+ ]
370+ expected_ordering_keys = sorted (
371+ [msg .ordering_key for msg in test_messages ])
372+ self .assertEqual (sorted (received_ordering_keys ), expected_ordering_keys )
377373
378374 ack_ids = [msg .ack_id for msg in response .received_messages ]
379- if ack_ids :
380- self .sub_client .acknowledge (
381- request = {
382- "subscription" : ordering_sub .name ,
383- "ack_ids" : ack_ids ,
384- })
375+ self .sub_client .acknowledge (
376+ request = {
377+ 'subscription' : ordering_sub .name ,
378+ 'ack_ids' : ack_ids ,
379+ })
385380 finally :
386- test_utils .cleanup_subscriptions (self .sub_client , [ordering_sub ])
387- test_utils .cleanup_topics (self .pub_client , [ordering_topic ])
381+ self .sub_client .delete_subscription (
382+ request = {'subscription' : ordering_sub .name })
383+ self .pub_client .delete_topic (request = {'topic' : ordering_topic .name })
384+
388385
389386if __name__ == '__main__' :
390387 logging .getLogger ().setLevel (logging .DEBUG )
0 commit comments