Skip to content

Commit 8a38d9c

Browse files
nikitagrover19Nikita Grover
authored andcommitted
Update pubsub_integration_test.py
1 parent 81d31b3 commit 8a38d9c

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

sdks/python/apache_beam/io/gcp/pubsub_integration_test.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
from apache_beam.testing import test_utils
3737
from apache_beam.testing.pipeline_verifiers import PipelineStateMatcher
3838
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
4439

4540
INPUT_TOPIC = 'psit_topic_input'
4641
OUTPUT_TOPIC = 'psit_topic_output'
@@ -144,6 +139,7 @@ def setUp(self):
144139
self.uuid = str(uuid.uuid4())
145140

146141
# Set up PubSub environment.
142+
from google.cloud import pubsub
147143
self.pub_client = pubsub.PublisherClient()
148144
self.input_topic = self.pub_client.create_topic(
149145
name=self.pub_client.topic_path(self.project, INPUT_TOPIC + self.uuid))
@@ -232,7 +228,10 @@ def _test_batch_write(self, with_attributes):
232228
with_attributes: False - Writes message data only.
233229
True - Writes message data and attributes.
234230
"""
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+
236235
# Create test messages for batch mode
237236
test_messages = [
238237
PubsubMessage(b'batch_data001', {'batch_attr': 'value1'}),
@@ -323,6 +322,11 @@ def test_batch_write_with_ordering_key(self):
323322
'(see https://github.com/apache/beam/issues/36201). '
324323
'Use apache_beam.io.external.gcp.pubsub.WriteToPubSub '
325324
'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
326330

327331
ordering_topic = self.pub_client.create_topic(
328332
name=self.pub_client.topic_path(
@@ -364,12 +368,13 @@ def test_batch_write_with_ordering_key(self):
364368

365369
self.assertEqual(len(response.received_messages), len(test_messages))
366370

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')
373378

374379
ack_ids = [msg.ack_id for msg in response.received_messages]
375380
self.sub_client.acknowledge(

0 commit comments

Comments
 (0)