|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | from faststream._internal.application import StartAbleApplication |
| 4 | +from faststream._internal.context import ContextRepo |
| 5 | +from faststream._internal.di import FastDependsConfig |
4 | 6 | from faststream.exceptions import SetupError |
5 | 7 | from faststream.rabbit import RabbitBroker |
6 | 8 |
|
@@ -45,3 +47,32 @@ async def test_di_reconfigured() -> None: |
45 | 47 | app.set_broker(broker) |
46 | 48 |
|
47 | 49 | assert broker.context.get("app") is app |
| 50 | + |
| 51 | + |
| 52 | +def test_broker_and_app_contexts_merge() -> None: |
| 53 | + broker = RabbitBroker( |
| 54 | + context=ContextRepo({ |
| 55 | + "broker_dependency": 1, |
| 56 | + "override_dependency": 2, |
| 57 | + }) |
| 58 | + ) |
| 59 | + |
| 60 | + config = FastDependsConfig( |
| 61 | + context=ContextRepo({ |
| 62 | + "application_dependency": 3, |
| 63 | + "override_dependency": 4, |
| 64 | + }) |
| 65 | + ) |
| 66 | + app = StartAbleApplication(config=config) |
| 67 | + application_context = app.context |
| 68 | + |
| 69 | + # if the broker binds to the application, |
| 70 | + # the broker modifies the application context and uses it as its own. |
| 71 | + app.set_broker(broker) |
| 72 | + |
| 73 | + assert app.context is application_context |
| 74 | + assert broker.context is application_context |
| 75 | + assert app.context.get("broker_dependency") == 1 |
| 76 | + assert app.context.get("application_dependency") == 3 |
| 77 | + # the broker context overwrites the application context |
| 78 | + assert app.context.get("override_dependency") == 2 |
0 commit comments