-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: ability to declare queue/exchange binding (#2011)
* feat: declare queue/exchange binding * docs: generate API References * docs: reverting the changes and adding queue/exchange binding to the documentation * docs: reverting the changes and adding queue/exchange binding to the documentation * docs: reverting the changes and adding queue/exchange binding to the documentation * docs: generate API References * test: added some testing for the doc code snippet * chore: polish docs * tests: fix RMQ bind test --------- Co-authored-by: MagicAbdel <[email protected]> Co-authored-by: Nikita Pastukhov <[email protected]>
- Loading branch information
1 parent
9603989
commit 4a5c491
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import aio_pika | ||
from faststream import FastStream | ||
from faststream.rabbit import ( | ||
ExchangeType, | ||
RabbitBroker, | ||
RabbitExchange, | ||
RabbitQueue, | ||
) | ||
|
||
broker = RabbitBroker() | ||
app = FastStream(broker) | ||
|
||
|
||
some_queue = RabbitQueue( | ||
name="some-queue", | ||
durable=True, | ||
) | ||
|
||
some_exchange = RabbitExchange( | ||
name="some-exchange", | ||
type=ExchangeType.FANOUT, | ||
) | ||
|
||
@app.after_startup | ||
async def bind_queue_exchange(): | ||
queue: aio_pika.RobustQueue = await broker.declare_queue( | ||
some_queue | ||
) | ||
|
||
exchange: aio_pika.RobustExchange = await broker.declare_exchange( | ||
some_exchange | ||
) | ||
|
||
await queue.bind( | ||
exchange=exchange, | ||
routing_key=queue.name # Optional parameter | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from unittest.mock import AsyncMock | ||
|
||
import pytest | ||
from aio_pika import RobustQueue | ||
|
||
from faststream import TestApp | ||
from tests.marks import require_aiopika | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.rabbit | ||
@require_aiopika | ||
async def test_bind(monkeypatch, async_mock: AsyncMock): | ||
from docs.docs_src.rabbit.bind import app, broker, some_exchange, some_queue | ||
|
||
with monkeypatch.context() as m: | ||
m.setattr(RobustQueue, "bind", async_mock) | ||
|
||
async with TestApp(app): | ||
assert len(broker.declarer._RabbitDeclarer__queues) == 2 # with `reply-to` | ||
assert len(broker.declarer._RabbitDeclarer__exchanges) == 1 | ||
|
||
assert some_queue in broker.declarer._RabbitDeclarer__queues | ||
assert some_exchange in broker.declarer._RabbitDeclarer__exchanges | ||
|
||
row_exchange = await broker.declarer.declare_exchange(some_exchange) | ||
async_mock.assert_awaited_once_with( | ||
exchange=row_exchange, | ||
routing_key=some_queue.name, | ||
) |