|
| 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 |
0 commit comments