Skip to content

Commit 9e7bec5

Browse files
author
DanielePalaia
committed
adding basic tests
1 parent 1ffb97a commit 9e7bec5

File tree

7 files changed

+86
-89
lines changed

7 files changed

+86
-89
lines changed

examples/getting_started/main.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# from proton import Message
2-
31
from rabbitmq_amqp_python_client import (
42
Connection,
53
ExchangeSpecification,
@@ -8,7 +6,7 @@
86
)
97

108

11-
def main():
9+
def main() -> None:
1210
exchange_name = "getting-started-exchange"
1311
queue_name = "example-queue"
1412
connection = Connection("amqp://guest:guest@localhost:5672/")
@@ -17,20 +15,15 @@ def main():
1715

1816
management = connection.management()
1917

20-
exchange_info = management.declare_exchange(
21-
ExchangeSpecification(name=exchange_name, arguments={})
22-
)
18+
management.declare_exchange(ExchangeSpecification(name=exchange_name, arguments={}))
2319

24-
#queue_info = management.declare_queue(
25-
# QueueSpecification(name=queue_name, queue_type=QueueType.quorum, arguments={})
26-
#)
20+
management.declare_queue(
21+
QueueSpecification(name=queue_name, queue_type=QueueType.quorum, arguments={})
22+
)
2723

2824
"""
29-
#management.bind(BindingSpecification{
30-
source_exchange: exchange_name,
31-
destination_queue: queue_name,
32-
binding_key: routing_key,
33-
})
25+
#management.bind(BindingSpecification(source_exchange=exchange_name, destination_queue=queue_name, \
26+
binding_key=routing_key))
3427
"""
3528

3629
"""
@@ -59,9 +52,9 @@ def main():
5952
management.purge_queue(queue_info.name)
6053
"""
6154

62-
#management.delete_queue(queue_name)
55+
# management.delete_queue(queue_name)
6356

64-
#management.delete_exchange(exchange_name)
57+
# management.delete_exchange(exchange_name)
6558

6659
management.close()
6760

rabbitmq_amqp_python_client/connection.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
from proton.utils import (
2-
BlockingConnection,
3-
BlockingReceiver,
4-
BlockingSender,
5-
)
6-
7-
from .configuration_options import (
8-
ReceiverOption,
9-
SenderOption,
10-
)
1+
from proton.utils import BlockingConnection
2+
113
from .management import Management
124

135

rabbitmq_amqp_python_client/entities.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
@dataclass
88
class ExchangeSpecification:
99
name: str
10-
arguments: dict
10+
arguments: dict[str, str]
1111
exchange_type: ExchangeType = ExchangeType.direct
1212
is_auto_delete: bool = False
13+
is_internal: bool = False
1314
is_durable: bool = True
1415

1516

1617
@dataclass
1718
class QueueSpecification:
1819
name: str
19-
arguments: dict
20+
arguments: dict[str, str]
2021
queue_type: QueueType = QueueType.quorum
2122
dead_letter_routing_key: str = ""
2223
is_exclusive: Optional[bool] = None

rabbitmq_amqp_python_client/management.py

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
import uuid
22
from typing import Any, Optional
3-
import json
4-
from proton import Message, Receiver, Sender
3+
4+
from proton import Message
5+
from proton._data import Data
56
from proton.utils import (
67
BlockingConnection,
78
BlockingReceiver,
89
BlockingSender,
910
)
1011

11-
from proton._data import Data
12-
1312
from .address_helper import exchange_address, queue_address
1413
from .common import CommonValues
15-
from .configuration_options import (
16-
ReceiverOption,
17-
SenderOption,
18-
)
1914
from .entities import (
2015
ExchangeSpecification,
2116
QueueSpecification,
2217
)
18+
from .options import ReceiverOption, SenderOption
2319

24-
import pickle
2520

2621
class Management:
2722
def __init__(self, conn: BlockingConnection):
@@ -59,7 +54,6 @@ def request(
5954
method: str,
6055
expected_response_codes: list[int],
6156
) -> None:
62-
print("im in request")
6357
self._request(str(uuid.uuid4()), body, path, method, expected_response_codes)
6458

6559
def _request(
@@ -70,63 +64,37 @@ def _request(
7064
method: str,
7165
expected_response_codes: list[int],
7266
) -> None:
73-
print("path is: " + path)
74-
75-
## test exchange message
7667
amq_message = Message(
7768
id=id,
7869
body=body,
7970
reply_to="$me",
8071
address=path,
8172
subject=method,
82-
#properties={"id": id, "to": path, "subject": method, "reply_to": "$me"},
83-
)
84-
85-
kvBody = {
86-
"auto_delete": False,
87-
"durable": True,
88-
"type": "direct",
89-
"arguments": {},
90-
}
91-
92-
amq_message = Message(
93-
body=kvBody,
94-
reply_to="$me",
95-
address=path,
96-
subject=method,
97-
id = id,
9873
)
9974

100-
message_bytes= amq_message.encode()
101-
list_bytes = list(message_bytes)
102-
10375
if self._sender is not None:
10476
self._sender.send(amq_message)
10577

106-
msg = self._receiver.receive()
107-
108-
109-
print("response received: " + str(msg.subject))
110-
111-
#self._validate_reponse_code(int(msg.properties["http:response"]), expected_response_codes)
78+
if self._receiver is not None:
79+
msg = self._receiver.receive()
11280

113-
# TO_COMPLETE HERE
81+
self._validate_reponse_code(int(msg.subject), expected_response_codes)
11482

11583
# TODO
11684
# def delete_queue(self, name:str):
11785

118-
def declare_exchange(self, exchange_specification: ExchangeSpecification):
86+
def declare_exchange(
87+
self, exchange_specification: ExchangeSpecification
88+
) -> ExchangeSpecification:
11989
body = {}
12090
body["auto_delete"] = exchange_specification.is_auto_delete
12191
body["durable"] = exchange_specification.is_durable
122-
body["type"] = exchange_specification.exchange_type.value
123-
#body["internal"] = False
124-
body["arguments"] = {}
92+
body["type"] = exchange_specification.exchange_type.value # type: ignore
93+
body["internal"] = exchange_specification.is_internal
94+
body["arguments"] = {} # type: ignore
12595

12696
path = exchange_address(exchange_specification.name)
12797

128-
print(path)
129-
13098
self.request(
13199
body,
132100
path,
@@ -138,11 +106,15 @@ def declare_exchange(self, exchange_specification: ExchangeSpecification):
138106
],
139107
)
140108

141-
def declare_queue(self, queue_specification: QueueSpecification):
109+
return exchange_specification
110+
111+
def declare_queue(
112+
self, queue_specification: QueueSpecification
113+
) -> QueueSpecification:
142114
body = {}
143115
body["auto_delete"] = queue_specification.is_auto_delete
144116
body["durable"] = queue_specification.is_durable
145-
body["arguments"] = {
117+
body["arguments"] = { # type: ignore
146118
"x-queue-type": queue_specification.queue_type.value,
147119
"x-dead-letter-exchange": queue_specification.dead_letter_exchange,
148120
"x-dead-letter-routing-key": queue_specification.dead_letter_routing_key,
@@ -164,8 +136,9 @@ def declare_queue(self, queue_specification: QueueSpecification):
164136
],
165137
)
166138

167-
def delete_exchange(self, exchange_name:str):
139+
return queue_specification
168140

141+
def delete_exchange(self, exchange_name: str) -> None:
169142
path = exchange_address(exchange_name)
170143

171144
print(path)
@@ -179,9 +152,7 @@ def delete_exchange(self, exchange_name:str):
179152
],
180153
)
181154

182-
183-
def delete_queue(self, queue_name:str):
184-
155+
def delete_queue(self, queue_name: str) -> None:
185156
path = queue_address(queue_name)
186157

187158
print(path)
@@ -195,11 +166,10 @@ def delete_queue(self, queue_name:str):
195166
],
196167
)
197168

198-
def _validate_reponse_code(self, response_code: int, expected_response_codes: list[int]) -> None:
199-
200-
print("response code: " + str(response_code))
201-
202-
if response_code == CommonValues.response_code_409:
169+
def _validate_reponse_code(
170+
self, response_code: int, expected_response_codes: list[int]
171+
) -> None:
172+
if response_code == CommonValues.response_code_409.value:
203173
# TODO replace with a new defined Exception
204174
raise Exception("ErrPreconditionFailed")
205175

@@ -209,7 +179,6 @@ def _validate_reponse_code(self, response_code: int, expected_response_codes: li
209179

210180
raise Exception("wrong response code received")
211181

212-
213182
# TODO
214183
# def bind(self, bind_specification:BindSpecification):
215184

rabbitmq_amqp_python_client/configuration_options.py renamed to rabbitmq_amqp_python_client/options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from proton.reactor import LinkOption # noqa: E402
44

55

6-
class SenderOption(LinkOption):
6+
class SenderOption(LinkOption): # type: ignore
77
def __init__(self, addr: str):
88
self._addr = addr
99

@@ -18,7 +18,7 @@ def test(self, link: Link) -> bool:
1818
return bool(link.is_sender)
1919

2020

21-
class ReceiverOption(LinkOption):
21+
class ReceiverOption(LinkOption): # type: ignore
2222
def __init__(self, addr: str):
2323
self._addr = addr
2424

tests/test_connection.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from rabbitmq_amqp_python_client import Connection
22

33

4-
# Temporary this will be replaced by our connection Deal when we start the implementation
5-
# For the moment we just need a test to run poetry run pytest without failing
64
def test_connection() -> None:
75
connection = Connection("amqp://guest:guest@localhost:5672/")
86
connection.dial()

tests/test_management.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from rabbitmq_amqp_python_client import (
2+
Connection,
3+
ExchangeSpecification,
4+
QueueSpecification,
5+
QueueType,
6+
)
7+
8+
9+
def test_declare_delete_exchange() -> None:
10+
connection = Connection("amqp://guest:guest@localhost:5672/")
11+
connection.dial()
12+
13+
exchange_name = "test-exchange"
14+
management = connection.management()
15+
16+
exchange_info = management.declare_exchange(
17+
ExchangeSpecification(name=exchange_name, arguments={})
18+
)
19+
20+
assert exchange_info.name == exchange_name
21+
22+
# Still not working
23+
# management.delete_exchange(exchange_name)
24+
25+
connection.close()
26+
27+
28+
def test_declare_delete_queue() -> None:
29+
connection = Connection("amqp://guest:guest@localhost:5672/")
30+
connection.dial()
31+
32+
queue_name = "test-queue"
33+
management = connection.management()
34+
35+
exchange_info = management.declare_queue(
36+
QueueSpecification(name=queue_name, queue_type=QueueType.quorum, arguments={})
37+
)
38+
39+
assert exchange_info.name == queue_name
40+
41+
# Still not working
42+
# management.delete_queue(queue_name)
43+
44+
connection.close()

0 commit comments

Comments
 (0)