Skip to content

Commit 7929675

Browse files
committed
AsyncAPI 3.0.0 RabbitMQ router tests
1 parent 95d9583 commit 7929675

File tree

2 files changed

+307
-0
lines changed

2 files changed

+307
-0
lines changed

tests/asyncapi/base/v3_0_0/router.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
from typing import Type
2+
3+
from dirty_equals import IsStr
4+
5+
from faststream import FastStream
6+
from faststream.asyncapi.generate import get_app_schema
7+
from faststream.asyncapi.version import AsyncAPIVersion
8+
from faststream.broker.core.usecase import BrokerUsecase
9+
from faststream.broker.router import ArgsContainer, BrokerRouter, SubscriberRoute
10+
11+
12+
class RouterTestcase:
13+
broker_class: Type[BrokerUsecase]
14+
router_class: Type[BrokerRouter]
15+
publisher_class: Type[ArgsContainer]
16+
route_class: Type[SubscriberRoute]
17+
18+
def test_delay_subscriber(self):
19+
broker = self.broker_class()
20+
21+
async def handle(msg): ...
22+
23+
router = self.router_class(
24+
handlers=(self.route_class(handle, "test"),),
25+
)
26+
27+
broker.include_router(router)
28+
29+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0)).to_jsonable()
30+
31+
payload = schema["components"]["schemas"]
32+
key = list(payload.keys())[0] # noqa: RUF015
33+
assert payload[key]["title"] == key == "Handle:Message:Payload"
34+
35+
def test_delay_publisher(self):
36+
broker = self.broker_class()
37+
38+
async def handle(msg): ...
39+
40+
router = self.router_class(
41+
handlers=(
42+
self.route_class(
43+
handle,
44+
"test",
45+
publishers=(self.publisher_class("test2", schema=int),),
46+
),
47+
),
48+
)
49+
50+
broker.include_router(router)
51+
52+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
53+
schemas = schema.components.schemas
54+
del schemas["Handle:Message:Payload"]
55+
56+
for i, j in schemas.items():
57+
assert (
58+
i == j["title"] == IsStr(regex=r"test2[\w:]*:Publisher:Message:Payload")
59+
)
60+
assert j["type"] == "integer"
61+
62+
def test_not_include(self):
63+
broker = self.broker_class()
64+
router = self.router_class(include_in_schema=False)
65+
66+
@router.subscriber("test")
67+
@router.publisher("test")
68+
async def handle(msg): ...
69+
70+
broker.include_router(router)
71+
72+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
73+
assert schema.channels == {}, schema.channels
74+
75+
def test_not_include_in_method(self):
76+
broker = self.broker_class()
77+
router = self.router_class()
78+
79+
@router.subscriber("test")
80+
@router.publisher("test")
81+
async def handle(msg): ...
82+
83+
broker.include_router(router, include_in_schema=False)
84+
85+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
86+
assert schema.channels == {}, schema.channels
87+
88+
def test_respect_subrouter(self):
89+
broker = self.broker_class()
90+
router = self.router_class()
91+
router2 = self.router_class(include_in_schema=False)
92+
93+
@router2.subscriber("test")
94+
@router2.publisher("test")
95+
async def handle(msg): ...
96+
97+
router.include_router(router2)
98+
broker.include_router(router)
99+
100+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
101+
102+
assert schema.channels == {}, schema.channels
103+
104+
def test_not_include_subrouter(self):
105+
broker = self.broker_class()
106+
router = self.router_class(include_in_schema=False)
107+
router2 = self.router_class()
108+
109+
@router2.subscriber("test")
110+
@router2.publisher("test")
111+
async def handle(msg): ...
112+
113+
router.include_router(router2)
114+
broker.include_router(router)
115+
116+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
117+
118+
assert schema.channels == {}
119+
120+
def test_not_include_subrouter_by_method(self):
121+
broker = self.broker_class()
122+
router = self.router_class()
123+
router2 = self.router_class()
124+
125+
@router2.subscriber("test")
126+
@router2.publisher("test")
127+
async def handle(msg): ...
128+
129+
router.include_router(router2, include_in_schema=False)
130+
broker.include_router(router)
131+
132+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
133+
134+
assert schema.channels == {}
135+
136+
def test_all_nested_routers_by_method(self):
137+
broker = self.broker_class()
138+
router = self.router_class()
139+
router2 = self.router_class()
140+
141+
@router2.subscriber("test")
142+
@router2.publisher("test")
143+
async def handle(msg): ...
144+
145+
router.include_router(router2)
146+
broker.include_router(router, include_in_schema=False)
147+
148+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
149+
150+
assert schema.channels == {}
151+
152+
def test_include_subrouter(self):
153+
broker = self.broker_class()
154+
router = self.router_class()
155+
router2 = self.router_class()
156+
157+
@router2.subscriber("test")
158+
@router2.publisher("test")
159+
async def handle(msg): ...
160+
161+
router.include_router(router2)
162+
broker.include_router(router)
163+
164+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0))
165+
166+
assert len(schema.channels) == 2
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from faststream import FastStream
2+
from faststream.asyncapi.generate import get_app_schema
3+
from faststream.asyncapi.version import AsyncAPIVersion
4+
from faststream.rabbit import (
5+
RabbitBroker,
6+
RabbitPublisher,
7+
RabbitQueue,
8+
RabbitRoute,
9+
RabbitRouter,
10+
)
11+
from tests.asyncapi.base.arguments import ArgumentsTestcase
12+
from tests.asyncapi.base.publisher import PublisherTestcase
13+
from tests.asyncapi.base.v3_0_0.router import RouterTestcase
14+
15+
16+
class TestRouter(RouterTestcase):
17+
broker_class = RabbitBroker
18+
router_class = RabbitRouter
19+
route_class = RabbitRoute
20+
publisher_class = RabbitPublisher
21+
22+
def test_prefix(self):
23+
broker = self.broker_class()
24+
25+
router = self.router_class(prefix="test_")
26+
27+
@router.subscriber(RabbitQueue("test", routing_key="key"))
28+
async def handle(msg): ...
29+
30+
broker.include_router(router)
31+
32+
schema = get_app_schema(FastStream(broker, asyncapi_version=AsyncAPIVersion.v3_0)).to_jsonable()
33+
34+
assert (
35+
schema
36+
== {
37+
"info": {
38+
"title": "FastStream",
39+
"version": "0.1.0",
40+
"description": ""
41+
},
42+
"asyncapi": "3.0.0",
43+
"defaultContentType": "application/json",
44+
"servers": {
45+
"development": {
46+
"host": "guest:guest@localhost:5672",
47+
"pathname": "/",
48+
"protocol": "amqp",
49+
"protocolVersion": "0.9.1"
50+
}
51+
},
52+
"channels": {
53+
"test_test:_:Handle": {
54+
"address": "test_test:_:Handle",
55+
"servers": [
56+
{
57+
"$ref": "#/servers/development"
58+
}
59+
],
60+
"messages": {
61+
"SubscribeMessage": {
62+
"$ref": "#/components/messages/test_test:_:Handle:SubscribeMessage"
63+
}
64+
},
65+
"bindings": {
66+
"amqp": {
67+
"is": "routingKey",
68+
"bindingVersion": "0.2.0",
69+
"queue": {
70+
"name": "test_test",
71+
"durable": False,
72+
"exclusive": False,
73+
"autoDelete": False,
74+
"vhost": "/"
75+
},
76+
"exchange": {
77+
"type": "default",
78+
"vhost": "/"
79+
}
80+
}
81+
}
82+
}
83+
},
84+
"operations": {
85+
"test_test:_:HandleSubscribe": {
86+
"action": "receive",
87+
"bindings": {
88+
"amqp": {
89+
"cc": "test_key",
90+
"ack": True,
91+
"bindingVersion": "0.2.0"
92+
}
93+
},
94+
"messages": [
95+
{
96+
"$ref": "#/channels/test_test:_:Handle/messages/SubscribeMessage"
97+
}
98+
],
99+
"channel": {
100+
"$ref": "#/channels/test_test:_:Handle"
101+
}
102+
}
103+
},
104+
"components": {
105+
"messages": {
106+
"test_test:_:Handle:Message": {
107+
"title": "test_test:_:Handle:Message",
108+
"correlationId": {
109+
"location": "$message.header#/correlation_id"
110+
},
111+
"payload": {
112+
"$ref": "#/components/schemas/Handle:Message:Payload"
113+
}
114+
}
115+
},
116+
"schemas": {
117+
"Handle:Message:Payload": {
118+
"title": "Handle:Message:Payload"
119+
}
120+
}
121+
}
122+
}
123+
), schema
124+
125+
126+
class TestRouterArguments(ArgumentsTestcase):
127+
broker_class = RabbitRouter
128+
129+
def build_app(self, router):
130+
broker = RabbitBroker()
131+
broker.include_router(router)
132+
return FastStream(broker)
133+
134+
135+
class TestRouterPublisher(PublisherTestcase):
136+
broker_class = RabbitRouter
137+
138+
def build_app(self, router):
139+
broker = RabbitBroker()
140+
broker.include_router(router)
141+
return FastStream(broker)

0 commit comments

Comments
 (0)