Skip to content

Commit 26bb1b4

Browse files
committed
change tests
Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com>
1 parent 62fe446 commit 26bb1b4

File tree

8 files changed

+50
-22
lines changed

8 files changed

+50
-22
lines changed

.ci/ubuntu/gha-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -o xtrace
77
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
88
readonly script_dir
99
echo "[INFO] script_dir: '$script_dir'"
10-
readonly rabbitmq_image=rabbitmq:4.1.0-beta.4-management-alpine
10+
readonly rabbitmq_image=rabbitmq:4.1.0-alpine
1111

1212

1313
readonly docker_name_prefix='rabbitmq-amqp-python-client'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ __pycache__/
1515
local*
1616
.githooks/
1717
.venv/
18+
.ci/ubuntu/log/*

examples/getting_started/getting_started.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
OutcomeState,
1414
QuorumQueueSpecification,
1515
)
16+
from rabbitmq_amqp_python_client.utils import string_to_bytes, bytes_to_string
1617

1718
MESSAGES_TO_PUBLISH = 100
1819

@@ -24,7 +25,7 @@ def __init__(self):
2425
self._count = 0
2526

2627
def on_amqp_message(self, event: Event):
27-
print("received message: {} ".format("".join(map(chr, event.message.body))))
28+
print("received message: {} ".format(bytes_to_string(event.message.body)))
2829

2930
# accepting
3031
self.delivery_context.accept(event)
@@ -123,7 +124,7 @@ def main() -> None:
123124
for i in range(MESSAGES_TO_PUBLISH):
124125
print("publishing")
125126
status = publisher.publish(
126-
Message(body=str.encode("test message {} ".format(i)))
127+
Message(body=string_to_bytes("test message {} ".format(i)))
127128
)
128129
if status.remote_state == OutcomeState.ACCEPTED:
129130
print("message accepted")

rabbitmq_amqp_python_client/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,29 @@ def validate_annotations(annotations: []) -> bool: # type: ignore
77
validated = False
88
return validated
99
return validated
10+
11+
12+
def bytes_to_string(body: bytes) -> str:
13+
"""
14+
Convert the body of a message to a string.
15+
16+
Args:
17+
body: The body of the message
18+
19+
Returns:
20+
str: The string representation of the body
21+
"""
22+
return "".join(map(chr, body))
23+
24+
25+
def string_to_bytes(body: str) -> bytes:
26+
"""
27+
Convert a string to the body of a message.
28+
29+
Args:
30+
body: The string to convert
31+
32+
Returns:
33+
bytes: The byte representation of the string
34+
"""
35+
return str.encode(body)

tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ def __init__(self):
263263
self._received = 0
264264

265265
def on_message(self, event: Event):
266-
annotations = {}
267-
annotations[symbol("x-opt-string")] = "x-test1"
266+
annotations = {symbol("x-opt-string"): "x-test1"}
268267
self.delivery_context.requeue_with_annotations(event, annotations)
269268
self._received = self._received + 1
270269
if self._received == 1000:

tests/test_consumer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Environment,
66
QuorumQueueSpecification,
77
)
8+
from rabbitmq_amqp_python_client.utils import bytes_to_string
89

910
from .conftest import (
1011
ConsumerTestException,
@@ -42,7 +43,7 @@ def test_consumer_sync_queue_accept(connection: Connection) -> None:
4243
# consumer synchronously without handler
4344
for i in range(messages_to_send):
4445
message = consumer.consume()
45-
if message.body == "test" + str(i):
46+
if bytes_to_string(message.body) == "test{}".format(i):
4647
consumed = consumed + 1
4748

4849
consumer.close()

tests/test_publisher.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
StreamSpecification,
1414
ValidationCodeException,
1515
)
16+
from rabbitmq_amqp_python_client.utils import string_to_bytes
1617

1718
from .http_requests import delete_all_connections
1819
from .utils import create_binding, publish_per_message
@@ -34,7 +35,7 @@ def test_publish_queue(connection: Connection) -> None:
3435
publisher = connection.publisher(
3536
destination=AddressHelper.queue_address(queue_name)
3637
)
37-
status = publisher.publish(Message(body="test"))
38+
status = publisher.publish(Message(body=string_to_bytes("test")))
3839
if status.remote_state == OutcomeState.ACCEPTED:
3940
accepted = True
4041
except Exception:
@@ -130,7 +131,7 @@ def test_publish_to_invalid_destination(connection: Connection) -> None:
130131
publisher = None
131132
try:
132133
publisher = connection.publisher("/invalid-destination/" + queue_name)
133-
publisher.publish(Message(body="test"))
134+
publisher.publish(Message(body=string_to_bytes("test")))
134135
except ArgumentOutOfRangeException:
135136
raised = True
136137
except Exception:
@@ -147,7 +148,7 @@ def test_publish_per_message_to_invalid_destination(connection: Connection) -> N
147148
queue_name = "test-queue-1"
148149
raised = False
149150

150-
message = Message(body="test")
151+
message = Message(body=string_to_bytes("test"))
151152
message = AddressHelper.message_to_address_helper(
152153
message, "/invalid_destination/" + queue_name
153154
)
@@ -179,7 +180,7 @@ def test_publish_per_message_both_address(connection: Connection) -> None:
179180
)
180181

181182
try:
182-
message = Message(body="test")
183+
message = Message(body=string_to_bytes("test"))
183184
message = AddressHelper.message_to_address_helper(
184185
message, AddressHelper.queue_address(queue_name)
185186
)
@@ -212,7 +213,7 @@ def test_publish_exchange(connection: Connection) -> None:
212213

213214
try:
214215
publisher = connection.publisher(addr)
215-
status = publisher.publish(Message(body="test"))
216+
status = publisher.publish(Message(body=string_to_bytes("test")))
216217
if status.ACCEPTED:
217218
accepted = True
218219
except Exception:
@@ -244,7 +245,7 @@ def test_publish_purge(connection: Connection) -> None:
244245
destination=AddressHelper.queue_address(queue_name)
245246
)
246247
for i in range(messages_to_publish):
247-
publisher.publish(Message(body="test"))
248+
publisher.publish(Message(body=string_to_bytes("test")))
248249
except Exception:
249250
raised = True
250251

@@ -289,7 +290,7 @@ def test_disconnection_reconnection() -> None:
289290
# simulate a disconnection
290291
delete_all_connections()
291292
try:
292-
publisher.publish(Message(body="test"))
293+
publisher.publish(Message(body=string_to_bytes("test")))
293294

294295
except ConnectionClosed:
295296
disconnected = True
@@ -331,8 +332,7 @@ def test_queue_info_for_stream_with_validations(connection: Connection) -> None:
331332
)
332333

333334
for i in range(messages_to_send):
334-
335-
publisher.publish(Message(body="test"))
335+
publisher.publish(Message(body=string_to_bytes("test")))
336336

337337

338338
def test_publish_per_message_exchange(connection: Connection) -> None:

tests/utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Publisher,
1717
QuorumQueueSpecification,
1818
)
19+
from rabbitmq_amqp_python_client.utils import string_to_bytes
1920

2021

2122
def publish_messages(
@@ -26,25 +27,26 @@ def publish_messages(
2627
) -> None:
2728
annotations = {}
2829
if filters is not None:
29-
for filter in filters:
30-
annotations = {"x-stream-filter-value": filter}
30+
for filterItem in filters:
31+
annotations = {"x-stream-filter-value": filterItem}
3132

3233
publisher = connection.publisher("/queues/" + queue_name)
3334
# publish messages_to_send messages
3435
for i in range(messages_to_send):
35-
publisher.publish(Message(body="test" + str(i), annotations=annotations))
36+
publisher.publish(
37+
Message(body=string_to_bytes("test{}".format(i)), annotations=annotations)
38+
)
3639
publisher.close()
3740

3841

3942
def publish_per_message(publisher: Publisher, addr: str) -> Delivery:
40-
message = Message(body="test")
43+
message = Message(body=string_to_bytes("test"))
4144
message = AddressHelper.message_to_address_helper(message, addr)
4245
status = publisher.publish(message)
4346
return status
4447

4548

4649
def setup_dead_lettering(management: Management) -> str:
47-
4850
exchange_dead_lettering = "exchange-dead-letter"
4951
queue_dead_lettering = "queue-dead-letter"
5052
binding_key = "key_dead_letter"
@@ -72,7 +74,6 @@ def setup_dead_lettering(management: Management) -> str:
7274
def create_binding(
7375
management: Management, exchange_name: str, queue_name: str, routing_key: str
7476
) -> str:
75-
7677
management.declare_exchange(ExchangeSpecification(name=exchange_name))
7778

7879
management.declare_queue(QuorumQueueSpecification(name=queue_name))
@@ -89,7 +90,6 @@ def create_binding(
8990

9091

9192
def cleanup_dead_lettering(management: Management, bind_path: str) -> None:
92-
9393
exchange_dead_lettering = "exchange-dead-letter"
9494
queue_dead_lettering = "queue-dead-letter"
9595

0 commit comments

Comments
 (0)