Skip to content

Commit f80773d

Browse files
author
DanielePalaia
committed
adding fixtures in tests
1 parent 12b73d7 commit f80773d

File tree

5 files changed

+49
-56
lines changed

5 files changed

+49
-56
lines changed

rabbitmq_amqp_python_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
BindingSpecification,
88
ExchangeSpecification,
99
)
10+
from .management import Management
1011
from .publisher import Publisher
1112
from .qpid.proton._message import Message
1213
from .queues import (
@@ -26,6 +27,7 @@
2627

2728
__all__ = [
2829
"Connection",
30+
"Management",
2931
"ExchangeSpecification",
3032
"QuorumQueueSpecification",
3133
"ClassicQueueSpecification",

rabbitmq_amqp_python_client/management.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ def purge_queue(self, queue_name: str) -> int:
330330
logger.debug("purge_queue operation called")
331331
path = purge_queue_address(queue_name)
332332

333-
print("path: " + path)
334-
335333
response = self.request(
336334
None,
337335
path,

tests/conftest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
3+
from rabbitmq_amqp_python_client import Connection
4+
5+
6+
@pytest.fixture()
7+
def connection(pytestconfig):
8+
connection = Connection("amqp://guest:guest@localhost:5672/")
9+
connection.dial()
10+
try:
11+
yield connection
12+
13+
finally:
14+
connection.close()
15+
16+
17+
@pytest.fixture()
18+
def management(pytestconfig):
19+
connection = Connection("amqp://guest:guest@localhost:5672/")
20+
connection.dial()
21+
try:
22+
management = connection.management()
23+
yield management
24+
25+
finally:
26+
management.close()
27+
connection.close()

tests/test_management.py

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from rabbitmq_amqp_python_client import (
22
BindingSpecification,
33
ClassicQueueSpecification,
4-
Connection,
54
ExchangeSpecification,
5+
Management,
66
QueueType,
77
QuorumQueueSpecification,
88
StreamSpecification,
@@ -12,12 +12,9 @@
1212
)
1313

1414

15-
def test_declare_delete_exchange() -> None:
16-
connection = Connection("amqp://guest:guest@localhost:5672/")
17-
connection.dial()
15+
def test_declare_delete_exchange(management: Management) -> None:
1816

1917
exchange_name = "test-exchange"
20-
management = connection.management()
2118

2219
exchange_info = management.declare_exchange(
2320
ExchangeSpecification(name=exchange_name, arguments={})
@@ -27,15 +24,9 @@ def test_declare_delete_exchange() -> None:
2724

2825
management.delete_exchange(exchange_name)
2926

30-
connection.close()
31-
32-
33-
def test_declare_purge_delete_queue() -> None:
34-
connection = Connection("amqp://guest:guest@localhost:5672/")
35-
connection.dial()
3627

28+
def test_declare_purge_delete_queue(management: Management) -> None:
3729
queue_name = "my_queue"
38-
management = connection.management()
3930

4031
queue_info = management.declare_queue(QuorumQueueSpecification(name=queue_name))
4132

@@ -45,17 +36,12 @@ def test_declare_purge_delete_queue() -> None:
4536

4637
management.delete_queue(queue_name)
4738

48-
connection.close()
49-
5039

51-
def test_bind_exchange_to_queue() -> None:
52-
connection = Connection("amqp://guest:guest@localhost:5672/")
53-
connection.dial()
40+
def test_bind_exchange_to_queue(management: Management) -> None:
5441

5542
exchange_name = "test-bind-exchange-to-queue-exchange"
5643
queue_name = "test-bind-exchange-to-queue-queue"
5744
routing_key = "routing-key"
58-
management = connection.management()
5945

6046
management.declare_exchange(ExchangeSpecification(name=exchange_name, arguments={}))
6147

@@ -89,12 +75,9 @@ def test_bind_exchange_to_queue() -> None:
8975
management.unbind(binding_exchange_queue_path)
9076

9177

92-
def test_queue_info_with_validations() -> None:
93-
connection = Connection("amqp://guest:guest@localhost:5672/")
94-
connection.dial()
78+
def test_queue_info_with_validations(management: Management) -> None:
9579

9680
queue_name = "test_queue_info_with_validation"
97-
management = connection.management()
9881

9982
queue_specification = QuorumQueueSpecification(
10083
name=queue_name,
@@ -111,12 +94,9 @@ def test_queue_info_with_validations() -> None:
11194
assert queue_info.message_count == 0
11295

11396

114-
def test_queue_info_for_stream_with_validations() -> None:
115-
connection = Connection("amqp://guest:guest@localhost:5672/")
116-
connection.dial()
97+
def test_queue_info_for_stream_with_validations(management: Management) -> None:
11798

11899
stream_name = "test_stream_info_with_validation"
119-
management = connection.management()
120100

121101
queue_specification = StreamSpecification(
122102
name=stream_name,
@@ -132,13 +112,10 @@ def test_queue_info_for_stream_with_validations() -> None:
132112
assert stream_info.message_count == 0
133113

134114

135-
def test_queue_precondition_fail() -> None:
136-
connection = Connection("amqp://guest:guest@localhost:5672/")
137-
connection.dial()
115+
def test_queue_precondition_fail(management: Management) -> None:
138116
test_failure = True
139117

140118
queue_name = "test-queue_precondition_fail"
141-
management = connection.management()
142119

143120
queue_specification = QuorumQueueSpecification(
144121
name=queue_name, is_auto_delete=False
@@ -162,12 +139,9 @@ def test_queue_precondition_fail() -> None:
162139
assert test_failure is False
163140

164141

165-
def test_declare_classic_queue() -> None:
166-
connection = Connection("amqp://guest:guest@localhost:5672/")
167-
connection.dial()
142+
def test_declare_classic_queue(management: Management) -> None:
168143

169144
queue_name = "test-declare_classic_queue"
170-
management = connection.management()
171145

172146
queue_specification = QuorumQueueSpecification(
173147
name=queue_name,
@@ -182,12 +156,9 @@ def test_declare_classic_queue() -> None:
182156
management.delete_queue(queue_name)
183157

184158

185-
def test_declare_classic_queue_with_args() -> None:
186-
connection = Connection("amqp://guest:guest@localhost:5672/")
187-
connection.dial()
159+
def test_declare_classic_queue_with_args(management: Management) -> None:
188160

189161
queue_name = "test-queue_with_args"
190-
management = connection.management()
191162

192163
queue_specification = ClassicQueueSpecification(
193164
name=queue_name,
@@ -220,12 +191,8 @@ def test_declare_classic_queue_with_args() -> None:
220191
management.delete_queue(queue_name)
221192

222193

223-
def test_declare_classic_queue_with_invalid_args() -> None:
224-
connection = Connection("amqp://guest:guest@localhost:5672/")
225-
connection.dial()
226-
194+
def test_declare_classic_queue_with_invalid_args(management: Management) -> None:
227195
queue_name = "test-queue_with_args"
228-
management = connection.management()
229196
test_failure = True
230197

231198
queue_specification = ClassicQueueSpecification(
@@ -244,12 +211,8 @@ def test_declare_classic_queue_with_invalid_args() -> None:
244211
assert test_failure is False
245212

246213

247-
def test_declare_stream_with_args() -> None:
248-
connection = Connection("amqp://guest:guest@localhost:5672/")
249-
connection.dial()
250-
214+
def test_declare_stream_with_args(management: Management) -> None:
251215
stream_name = "test-stream_with_args"
252-
management = connection.management()
253216

254217
stream_specification = StreamSpecification(
255218
name=stream_name,

tests/test_publisher.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import time
2+
13
from rabbitmq_amqp_python_client import (
24
Connection,
35
Message,
46
QuorumQueueSpecification,
57
)
68

79

8-
def test_publish_exchange() -> None:
9-
connection = Connection("amqp://guest:guest@localhost:5672/")
10-
connection.dial()
10+
def test_publish_exchange(connection: Connection) -> None:
1111

1212
queue_name = "test-queue"
1313
management = connection.management()
@@ -29,7 +29,7 @@ def test_publish_exchange() -> None:
2929
management.delete_queue(queue_name)
3030

3131

32-
def test_publish_purge() -> None:
32+
def test_publish_purge(connection: Connection) -> None:
3333
connection = Connection("amqp://guest:guest@localhost:5672/")
3434
connection.dial()
3535

@@ -42,14 +42,17 @@ def test_publish_purge() -> None:
4242

4343
try:
4444
publisher = connection.publisher("/queues/" + queue_name)
45-
publisher.publish(Message(body="test"))
45+
for i in range(20):
46+
publisher.publish(Message(body="test"))
4647
except Exception:
4748
raised = True
4849

50+
time.sleep(4)
51+
4952
message_purged = management.purge_queue(queue_name)
5053

5154
assert raised is False
52-
assert message_purged == 1
55+
assert message_purged == 20
5356

5457
publisher.close()
5558

0 commit comments

Comments
 (0)