|
36 | 36 | from apache_beam.testing import test_utils |
37 | 37 | from apache_beam.testing.pipeline_verifiers import PipelineStateMatcher |
38 | 38 | from 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 |
44 | 39 |
|
45 | 40 | INPUT_TOPIC = 'psit_topic_input' |
46 | 41 | OUTPUT_TOPIC = 'psit_topic_output' |
@@ -144,6 +139,7 @@ def setUp(self): |
144 | 139 | self.uuid = str(uuid.uuid4()) |
145 | 140 |
|
146 | 141 | # Set up PubSub environment. |
| 142 | + from google.cloud import pubsub |
147 | 143 | self.pub_client = pubsub.PublisherClient() |
148 | 144 | self.input_topic = self.pub_client.create_topic( |
149 | 145 | name=self.pub_client.topic_path(self.project, INPUT_TOPIC + self.uuid)) |
@@ -232,7 +228,10 @@ def _test_batch_write(self, with_attributes): |
232 | 228 | with_attributes: False - Writes message data only. |
233 | 229 | True - Writes message data and attributes. |
234 | 230 | """ |
235 | | - |
| 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 |
| 234 | + |
236 | 235 | # Create test messages for batch mode |
237 | 236 | test_messages = [ |
238 | 237 | PubsubMessage(b'batch_data001', {'batch_attr': 'value1'}), |
@@ -323,6 +322,11 @@ def test_batch_write_with_ordering_key(self): |
323 | 322 | '(see https://github.com/apache/beam/issues/36201). ' |
324 | 323 | 'Use apache_beam.io.external.gcp.pubsub.WriteToPubSub ' |
325 | 324 | 'with publish_with_ordering_key=True 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 |
326 | 330 |
|
327 | 331 | ordering_topic = self.pub_client.create_topic( |
328 | 332 | name=self.pub_client.topic_path( |
@@ -364,12 +368,13 @@ def test_batch_write_with_ordering_key(self): |
364 | 368 |
|
365 | 369 | self.assertEqual(len(response.received_messages), len(test_messages)) |
366 | 370 |
|
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) |
| 371 | + received_map = { |
| 372 | + msg.message.data: msg.message |
| 373 | + for msg in response.received_messages |
| 374 | + } |
| 375 | + self.assertEqual(received_map[b'order_data001'].ordering_key, 'key1') |
| 376 | + self.assertEqual(received_map[b'order_data002'].ordering_key, 'key1') |
| 377 | + self.assertEqual(received_map[b'order_data003'].ordering_key, 'key2') |
373 | 378 |
|
374 | 379 | ack_ids = [msg.ack_id for msg in response.received_messages] |
375 | 380 | self.sub_client.acknowledge( |
|
0 commit comments