Skip to content

Commit

Permalink
[EventHubs] check for any non-None values in amqp header/properties (#…
Browse files Browse the repository at this point in the history
…27444)

* add any method to amqp header/properties

* use count(None) to check non-None header/props vals
  • Loading branch information
swathipil authored Nov 16, 2022
1 parent 4666e53 commit a3ae1e3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def to_outgoing_amqp_message(annotated_message):
:rtype: pyamqp.Message
"""
message_header = None
if annotated_message.header and any(annotated_message.header.values()):
header_vals = annotated_message.header.values() if annotated_message.header else None
# If header and non-None header values, create outgoing header.
if annotated_message.header and header_vals.count(None) != len(header_vals):
message_header = Header(
delivery_count=annotated_message.header.delivery_count,
ttl=annotated_message.header.time_to_live,
Expand All @@ -96,7 +98,9 @@ def to_outgoing_amqp_message(annotated_message):
)

message_properties = None
if annotated_message.properties and any(annotated_message.properties.values()):
properties_vals = annotated_message.properties.values() if annotated_message.properties else None
# If properties and non-None properties values, create outgoing properties.
if annotated_message.properties and properties_vals.count(None) != len(properties_vals):
message_properties = Properties(
message_id=annotated_message.properties.message_id,
user_id=annotated_message.properties.user_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def to_outgoing_amqp_message(annotated_message):
:rtype: uamqp.Message
"""
message_header = None
if annotated_message.header and any(annotated_message.header.values()):
header_vals = annotated_message.header.values() if annotated_message.header else None
# If header and non-None header values, create outgoing header.
if annotated_message.header and header_vals.count(None) != len(header_vals):
message_header = MessageHeader()
message_header.delivery_count = annotated_message.header.delivery_count
message_header.time_to_live = annotated_message.header.time_to_live
Expand All @@ -133,7 +135,9 @@ def to_outgoing_amqp_message(annotated_message):
message_header.priority = annotated_message.header.priority

message_properties = None
if annotated_message.properties and any(annotated_message.properties.values()):
properties_vals = annotated_message.properties.values() if annotated_message.properties else None
# If properties and non-None properties values, create outgoing properties.
if annotated_message.properties and properties_vals.count(None) != len(properties_vals):
message_properties = MessageProperties(
message_id=annotated_message.properties.message_id,
user_id=annotated_message.properties.user_id,
Expand Down
32 changes: 32 additions & 0 deletions sdk/eventhub/azure-eventhub/tests/unittest/test_event_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,38 @@ def test_amqp_message_str_repr():
assert str(message) == 'A'
assert 'AmqpAnnotatedMessage(body=A, body_type=data' in repr(message)

def test_outgoing_amqp_message_header_properties(uamqp_transport):
if uamqp_transport:
amqp_transport = UamqpTransport
else:
amqp_transport = PyamqpTransport
ann_message = AmqpAnnotatedMessage(data_body=b'A')
ann_message.header = AmqpMessageHeader()
ann_message.properties = AmqpMessageProperties()
amqp_message = amqp_transport.to_outgoing_amqp_message(ann_message)

assert not amqp_message.header
assert not amqp_message.properties

ann_message = AmqpAnnotatedMessage(data_body=b'A')
ann_message.header = AmqpMessageHeader()
ann_message.properties = AmqpMessageProperties()
ann_message.header.first_acquirer = False
ann_message.properties.creation_time = 0
amqp_message = amqp_transport.to_outgoing_amqp_message(ann_message)

assert amqp_message.header
assert amqp_message.properties

ann_message = AmqpAnnotatedMessage(data_body=b'A')
ann_message.header = AmqpMessageHeader()
ann_message.properties = AmqpMessageProperties()
ann_message.properties.message_id = ""
amqp_message = amqp_transport.to_outgoing_amqp_message(ann_message)

assert not amqp_message.header
assert amqp_message.properties


def test_amqp_message_from_message(uamqp_transport):
if uamqp_transport:
Expand Down

0 comments on commit a3ae1e3

Please sign in to comment.