Skip to content

Commit 7c3e26b

Browse files
committed
Partial revert of ec0e219
functools.singledispatchmethod is Python 3.8+ only
1 parent 24ebf4c commit 7c3e26b

File tree

2 files changed

+51
-94
lines changed

2 files changed

+51
-94
lines changed

src/zocalo/util/rabbitmq.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import datetime
22
import enum
3-
import functools
43
import logging
54
import pathlib
65
import urllib
@@ -630,14 +629,6 @@ def delete(self, endpoint: str, params: Dict[str, Any] = None) -> requests.Respo
630629
f"{self._url}/{endpoint}", auth=self._auth, params=params
631630
)
632631

633-
@functools.singledispatchmethod
634-
def create_component(self, component):
635-
raise NotImplementedError(f"Component {component} not recognised")
636-
637-
@functools.singledispatchmethod
638-
def delete_component(self, component):
639-
raise NotImplementedError(f"Component {component} not recognised")
640-
641632
def connections(
642633
self, name: Optional[str] = None
643634
) -> Union[List[ConnectionInfo], ConnectionInfo]:
@@ -677,7 +668,6 @@ def exchanges(
677668
logger.debug(response)
678669
return [ExchangeInfo(**qi) for qi in response.json()]
679670

680-
@create_component.register
681671
def exchange_declare(self, exchange: ExchangeSpec):
682672
endpoint = f"exchanges/{exchange.vhost}/{exchange.name}/"
683673
response = self.put(
@@ -691,10 +681,6 @@ def exchange_delete(self, vhost: str, name: str, if_unused: bool = False):
691681
response = self.delete(endpoint, params={"if_unused": if_unused})
692682
logger.debug(response)
693683

694-
@delete_component.register
695-
def _delete_exchange(self, exchange: ExchangeSpec, **kwargs):
696-
self.exchange_delete(vhost=exchange.vhost, name=exchange.name, **kwargs)
697-
698684
def policies(
699685
self, vhost: Optional[str] = None, name: Optional[str] = None
700686
) -> Union[List[PolicyInfo], PolicyInfo]:
@@ -711,7 +697,6 @@ def policies(
711697
logger.debug(response)
712698
return [PolicyInfo(**p) for p in response.json()]
713699

714-
@create_component.register
715700
def set_policy(self, policy: PolicySpec):
716701
endpoint = f"policies/{policy.vhost}/{policy.name}/"
717702
response = self.put(
@@ -727,10 +712,6 @@ def clear_policy(self, vhost: str, name: str):
727712
response = self.delete(endpoint)
728713
logger.debug(response)
729714

730-
@delete_component.register
731-
def _delete_policy(self, policy: PolicySpec):
732-
self.clear_policy(vhost=policy.vhost, name=policy.name)
733-
734715
def queues(
735716
self, vhost: Optional[str] = None, name: Optional[str] = None
736717
) -> Union[List[QueueInfo], QueueInfo]:
@@ -747,7 +728,6 @@ def queues(
747728
logger.debug(response)
748729
return [QueueInfo(**qi) for qi in response.json()]
749730

750-
@create_component.register
751731
def queue_declare(self, queue: QueueSpec):
752732
endpoint = f"queues/{queue.vhost}/{queue.name}"
753733
response = self.put(
@@ -764,14 +744,6 @@ def queue_delete(
764744
)
765745
logger.debug(response)
766746

767-
@delete_component.register
768-
def _delete_queue(
769-
self, queue: QueueSpec, if_unused: bool = False, if_empty: bool = False
770-
):
771-
self.queue_delete(
772-
vhost=queue.vhost, name=queue.name, if_unused=if_unused, if_empty=if_empty
773-
)
774-
775747
def users(self, name: str = None) -> Union[List[UserInfo], UserInfo]:
776748
endpoint = "users"
777749
if name:
@@ -781,7 +753,6 @@ def users(self, name: str = None) -> Union[List[UserInfo], UserInfo]:
781753
response = self.get(endpoint)
782754
return [UserInfo(**u) for u in response.json()]
783755

784-
@create_component.register
785756
def add_user(self, user: UserSpec):
786757
endpoint = f"users/{user.name}/"
787758
response = self.put(
@@ -793,7 +764,3 @@ def delete_user(self, name: str):
793764
endpoint = f"users/{name}/"
794765
response = self.delete(endpoint)
795766
logger.debug(response)
796-
797-
@delete_component.register
798-
def _delete_user(self, user: UserSpec):
799-
self.delete_user(name=user.name)

tests/util/test_rabbitmq.py

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,23 @@ def queue_spec():
109109
def test_api_queue_declare(requests_mock, rmqapi, queue_spec):
110110
requests_mock.put("/api/queues/zocalo/foo")
111111
rmqapi.queue_declare(queue=queue_spec)
112-
rmqapi.create_component(queue_spec)
113-
assert requests_mock.call_count == 2
114-
for history in requests_mock.request_history:
115-
assert history.method == "PUT"
116-
assert history.url.endswith("/api/queues/zocalo/foo")
117-
assert history.json() == {
118-
"auto_delete": True,
119-
"arguments": queue_spec.arguments,
120-
}
112+
assert requests_mock.call_count == 1
113+
history = requests_mock.request_history[0]
114+
assert history.method == "PUT"
115+
assert history.url.endswith("/api/queues/zocalo/foo")
116+
assert history.json() == {
117+
"auto_delete": True,
118+
"arguments": queue_spec.arguments,
119+
}
121120

122121

123122
def test_api_queue_delete(requests_mock, rmqapi, queue_spec):
124123
requests_mock.delete("/api/queues/zocalo/foo")
125124
rmqapi.queue_delete(vhost="zocalo", name="foo", if_unused=True, if_empty=True)
126-
rmqapi.delete_component(queue_spec, if_unused=True, if_empty=True)
127-
assert requests_mock.call_count == 2
128-
for history in requests_mock.request_history:
129-
assert history.method == "DELETE"
130-
assert history.url.endswith(
131-
"/api/queues/zocalo/foo?if_unused=True&if_empty=True"
132-
)
125+
assert requests_mock.call_count == 1
126+
history = requests_mock.request_history[0]
127+
assert history.method == "DELETE"
128+
assert history.url.endswith("/api/queues/zocalo/foo?if_unused=True&if_empty=True")
133129

134130

135131
def test_api_nodes(requests_mock, rmqapi):
@@ -215,26 +211,24 @@ def exchange_spec(name):
215211
def test_api_exchange_declare(name, requests_mock, rmqapi):
216212
requests_mock.put(f"/api/exchanges/zocalo/{name}/")
217213
rmqapi.exchange_declare(exchange=exchange_spec(name))
218-
rmqapi.create_component(exchange_spec(name))
219-
assert requests_mock.call_count == 2
220-
for history in requests_mock.request_history:
221-
assert history.method == "PUT"
222-
assert history.url.endswith(f"/api/exchanges/zocalo/{name}/")
223-
assert history.json() == {
224-
"type": "fanout",
225-
"auto_delete": True,
226-
"durable": True,
227-
}
214+
assert requests_mock.call_count == 1
215+
history = requests_mock.request_history[0]
216+
assert history.method == "PUT"
217+
assert history.url.endswith(f"/api/exchanges/zocalo/{name}/")
218+
assert history.json() == {
219+
"type": "fanout",
220+
"auto_delete": True,
221+
"durable": True,
222+
}
228223

229224

230225
def test_api_exchange_delete(requests_mock, rmqapi):
231226
requests_mock.delete("/api/exchanges/zocalo/foo")
232227
rmqapi.exchange_delete(vhost="zocalo", name="foo", if_unused=True)
233-
rmqapi.delete_component(exchange_spec("foo"), if_unused=True)
234-
assert requests_mock.call_count == 2
235-
for history in requests_mock.request_history:
236-
assert history.method == "DELETE"
237-
assert history.url.endswith("/api/exchanges/zocalo/foo?if_unused=True")
228+
assert requests_mock.call_count == 1
229+
history = requests_mock.request_history[0]
230+
assert history.method == "DELETE"
231+
assert history.url.endswith("/api/exchanges/zocalo/foo?if_unused=True")
238232

239233

240234
def test_api_connections(requests_mock, rmqapi):
@@ -298,26 +292,24 @@ def user_spec():
298292
def test_api_add_user(requests_mock, rmqapi, user_spec):
299293
requests_mock.put(f"/api/users/{user_spec.name}/")
300294
rmqapi.add_user(user=user_spec)
301-
rmqapi.create_component(user_spec)
302-
assert requests_mock.call_count == 2
303-
for history in requests_mock.request_history:
304-
assert history.method == "PUT"
305-
assert history.url.endswith(f"/api/users/{user_spec.name}/")
306-
assert history.json() == {
307-
"password_hash": "guest",
308-
"hashing_algorithm": "rabbit_password_hashing_sha256",
309-
"tags": "administrator",
310-
}
295+
assert requests_mock.call_count == 1
296+
history = requests_mock.request_history[0]
297+
assert history.method == "PUT"
298+
assert history.url.endswith(f"/api/users/{user_spec.name}/")
299+
assert history.json() == {
300+
"password_hash": "guest",
301+
"hashing_algorithm": "rabbit_password_hashing_sha256",
302+
"tags": "administrator",
303+
}
311304

312305

313306
def test_api_delete_user(requests_mock, rmqapi, user_spec):
314307
requests_mock.delete("/api/users/guest/")
315308
rmqapi.delete_user(name="guest")
316-
rmqapi.delete_component(user_spec)
317-
assert requests_mock.call_count == 2
318-
for history in requests_mock.request_history:
319-
assert history.method == "DELETE"
320-
assert history.url.endswith("/api/users/guest/")
309+
assert requests_mock.call_count == 1
310+
history = requests_mock.request_history[0]
311+
assert history.method == "DELETE"
312+
assert history.url.endswith("/api/users/guest/")
321313

322314

323315
def test_api_policies(requests_mock, rmqapi):
@@ -359,23 +351,21 @@ def policy_spec():
359351
def test_api_set_policy(requests_mock, rmqapi, policy_spec):
360352
requests_mock.put(f"/api/policies/foo/{policy_spec.name}/")
361353
rmqapi.set_policy(policy=policy_spec)
362-
rmqapi.create_component(policy_spec)
363-
assert requests_mock.call_count == 2
364-
for history in requests_mock.request_history:
365-
assert history.method == "PUT"
366-
assert history.url.endswith(f"/api/policies/foo/{policy_spec.name}/")
367-
assert history.json() == {
368-
"pattern": "^amq.",
369-
"apply-to": "queues",
370-
"definition": {"delivery-limit": 5},
371-
}
354+
assert requests_mock.call_count == 1
355+
history = requests_mock.request_history[0]
356+
assert history.method == "PUT"
357+
assert history.url.endswith(f"/api/policies/foo/{policy_spec.name}/")
358+
assert history.json() == {
359+
"pattern": "^amq.",
360+
"apply-to": "queues",
361+
"definition": {"delivery-limit": 5},
362+
}
372363

373364

374365
def test_api_clear_policy(requests_mock, rmqapi, policy_spec):
375366
requests_mock.delete("/api/policies/foo/bar/")
376367
rmqapi.clear_policy(vhost="foo", name="bar")
377-
rmqapi.delete_component(policy_spec)
378-
assert requests_mock.call_count == 2
379-
for history in requests_mock.request_history:
380-
assert history.method == "DELETE"
381-
assert history.url.endswith("/api/policies/foo/bar/")
368+
assert requests_mock.call_count == 1
369+
history = requests_mock.request_history[0]
370+
assert history.method == "DELETE"
371+
assert history.url.endswith("/api/policies/foo/bar/")

0 commit comments

Comments
 (0)