Skip to content

Commit 8820d8a

Browse files
author
DanielePalaia
committed
adding basic tests
1 parent 1ffb97a commit 8820d8a

File tree

7 files changed

+85
-85
lines changed

7 files changed

+85
-85
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/configuration_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

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: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
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
1514
from .configuration_options import (
@@ -21,7 +20,6 @@
2120
QueueSpecification,
2221
)
2322

24-
import pickle
2523

2624
class Management:
2725
def __init__(self, conn: BlockingConnection):
@@ -59,7 +57,6 @@ def request(
5957
method: str,
6058
expected_response_codes: list[int],
6159
) -> None:
62-
print("im in request")
6360
self._request(str(uuid.uuid4()), body, path, method, expected_response_codes)
6461

6562
def _request(
@@ -70,63 +67,37 @@ def _request(
7067
method: str,
7168
expected_response_codes: list[int],
7269
) -> None:
73-
print("path is: " + path)
74-
75-
## test exchange message
7670
amq_message = Message(
7771
id=id,
7872
body=body,
7973
reply_to="$me",
8074
address=path,
8175
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,
9876
)
9977

100-
message_bytes= amq_message.encode()
101-
list_bytes = list(message_bytes)
102-
10378
if self._sender is not None:
10479
self._sender.send(amq_message)
10580

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)
81+
if self._receiver is not None:
82+
msg = self._receiver.receive()
11283

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

11586
# TODO
11687
# def delete_queue(self, name:str):
11788

118-
def declare_exchange(self, exchange_specification: ExchangeSpecification):
89+
def declare_exchange(
90+
self, exchange_specification: ExchangeSpecification
91+
) -> ExchangeSpecification:
11992
body = {}
12093
body["auto_delete"] = exchange_specification.is_auto_delete
12194
body["durable"] = exchange_specification.is_durable
122-
body["type"] = exchange_specification.exchange_type.value
123-
#body["internal"] = False
124-
body["arguments"] = {}
95+
body["type"] = exchange_specification.exchange_type.value # type: ignore
96+
body["internal"] = exchange_specification.is_internal
97+
body["arguments"] = {} # type: ignore
12598

12699
path = exchange_address(exchange_specification.name)
127100

128-
print(path)
129-
130101
self.request(
131102
body,
132103
path,
@@ -138,11 +109,15 @@ def declare_exchange(self, exchange_specification: ExchangeSpecification):
138109
],
139110
)
140111

141-
def declare_queue(self, queue_specification: QueueSpecification):
112+
return exchange_specification
113+
114+
def declare_queue(
115+
self, queue_specification: QueueSpecification
116+
) -> QueueSpecification:
142117
body = {}
143118
body["auto_delete"] = queue_specification.is_auto_delete
144119
body["durable"] = queue_specification.is_durable
145-
body["arguments"] = {
120+
body["arguments"] = { # type: ignore
146121
"x-queue-type": queue_specification.queue_type.value,
147122
"x-dead-letter-exchange": queue_specification.dead_letter_exchange,
148123
"x-dead-letter-routing-key": queue_specification.dead_letter_routing_key,
@@ -164,8 +139,9 @@ def declare_queue(self, queue_specification: QueueSpecification):
164139
],
165140
)
166141

167-
def delete_exchange(self, exchange_name:str):
142+
return queue_specification
168143

144+
def delete_exchange(self, exchange_name: str) -> None:
169145
path = exchange_address(exchange_name)
170146

171147
print(path)
@@ -179,9 +155,7 @@ def delete_exchange(self, exchange_name:str):
179155
],
180156
)
181157

182-
183-
def delete_queue(self, queue_name:str):
184-
158+
def delete_queue(self, queue_name: str) -> None:
185159
path = queue_address(queue_name)
186160

187161
print(path)
@@ -195,11 +169,10 @@ def delete_queue(self, queue_name:str):
195169
],
196170
)
197171

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:
172+
def _validate_reponse_code(
173+
self, response_code: int, expected_response_codes: list[int]
174+
) -> None:
175+
if response_code == CommonValues.response_code_409.value:
203176
# TODO replace with a new defined Exception
204177
raise Exception("ErrPreconditionFailed")
205178

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

210183
raise Exception("wrong response code received")
211184

212-
213185
# TODO
214186
# def bind(self, bind_specification:BindSpecification):
215187

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)