Skip to content

Commit

Permalink
test: added some testing for the doc code snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicAbdel committed Dec 29, 2024
1 parent 771b355 commit 313d823
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
20 changes: 12 additions & 8 deletions docs/docs_src/rabbit/bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
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 = await broker.declare_queue(
RabbitQueue(
name="some-queue",
durable=True,
)
some_queue
) # This gives a aio_pika.RobustQueue object

exchange = await broker.declare_exchange(
RabbitExchange(
name="some-exchange",
type=ExchangeType.FANOUT,
)
some_exchange
)

await queue.bind(
Expand Down
29 changes: 29 additions & 0 deletions tests/a_docs/rabbit/test_bind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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

async_mock.assert_awaited_once_with(
queue=broker.declarer._RabbitDeclarer__queues[some_queue],
exchange=broker.declarer._RabbitDeclarer__exchanges[some_exchange],
routing_key=some_queue.name,
)

0 comments on commit 313d823

Please sign in to comment.