Skip to content

Commit 7316b5f

Browse files
committed
Added test for ensure build context
1 parent 232a0d7 commit 7316b5f

File tree

8 files changed

+38
-14
lines changed

8 files changed

+38
-14
lines changed

ellar/common/serializer/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Serializer(SerializerBase, BaseModel, __skip_filter__=True):
122122

123123

124124
def _lazy_current_config() -> t.Any:
125-
return LazyStrImport("ellar.core:config")
125+
return LazyStrImport("ellar.core:current_config")
126126

127127

128128
def serialize_object(

ellar/core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
ExecutionContext,
77
HostContext,
88
HttpRequestConnectionContext,
9-
config,
9+
current_config,
1010
current_connection,
1111
current_injector,
1212
injector_context,
@@ -45,7 +45,7 @@
4545
"mount",
4646
"host",
4747
"current_injector",
48-
"config",
48+
"current_config",
4949
"ForwardRefModule",
5050
"TemplateRenderingService",
5151
"HttpRequestConnectionContext",

ellar/core/execution_context/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .execution import ExecutionContext
55
from .factory import ExecutionContextFactory, HostContextFactory
66
from .host import HostContext
7-
from .injector import config, current_injector, injector_context
7+
from .injector import current_config, current_injector, injector_context
88
from .request import HttpRequestConnectionContext, current_connection
99

1010
__all__ = [
@@ -13,7 +13,7 @@
1313
"ExecutionContextFactory",
1414
"HostContextFactory",
1515
"current_injector",
16-
"config",
16+
"current_config",
1717
"HttpRequestConnectionContext",
1818
"current_connection",
1919
"injector_context",

ellar/core/execution_context/injector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _get_application_config() -> Config:
3636
return Config(config_module=config_module)
3737

3838

39-
config: Config = t.cast(Config, SimpleLazyObject(func=_get_application_config))
39+
current_config: Config = t.cast(Config, SimpleLazyObject(func=_get_application_config))
4040

4141
current_injector: EllarInjector = t.cast(
4242
EllarInjector, SimpleLazyObject(func=_get_injector)
@@ -45,7 +45,7 @@ def _get_application_config() -> Config:
4545

4646
def _clear_lazy_objects() -> None:
4747
current_injector._wrapped = empty # type:ignore[attr-defined]
48-
config._wrapped = empty
48+
current_config._wrapped = empty
4949

5050

5151
@asynccontextmanager

ellar/socket_io/gateway.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ellar.common.exceptions import WebSocketRequestValidationError
1717
from ellar.common.logging import logger
1818
from ellar.common.params import WebsocketEndpointArgsModel
19-
from ellar.core.execution_context import config, current_injector
19+
from ellar.core.execution_context import current_config, current_injector
2020
from ellar.reflect import reflect
2121
from ellar.socket_io.context import GatewayContext
2222
from ellar.socket_io.model import GatewayBase
@@ -107,7 +107,7 @@ async def catch(self, sid: t.Any) -> t.AsyncGenerator:
107107
await self._handle_error(
108108
sid=sid,
109109
code=status.WS_1011_INTERNAL_ERROR,
110-
reason=str(ex) if config.DEBUG else "Something went wrong",
110+
reason=str(ex) if current_config.DEBUG else "Something went wrong",
111111
)
112112

113113
async def _handle_error(self, sid: str, code: int, reason: t.Any) -> None:

ellar/testing/module.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def __init__(
3333
self._global_guards = global_guards
3434
self._app: t.Optional[App] = None
3535

36+
@property
37+
def module(self) -> t.Type[ModuleBase]:
38+
return self._testing_module
39+
3640
def override_provider(
3741
self,
3842
base_type: t.Union[t.Type[T], t.Type],

tests/test_application/test_application_factory.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from ellar.common.constants import MODULE_WATERMARK
55
from ellar.common.exceptions import ImproperConfiguration
66
from ellar.core import LazyModuleImport as lazyLoad
7-
from ellar.core import ModuleBase, ModuleSetup, injector_context
7+
from ellar.core import ModuleBase, ModuleSetup, current_config, injector_context
88
from ellar.di import ProviderConfig
9+
from ellar.events import ensure_build_context
910
from ellar.reflect import reflect
1011
from ellar.testing import TestClient
1112
from ellar.utils import get_unique_type
@@ -117,3 +118,20 @@ def test_config_overrider_core_service_registration():
117118
AppFactory.create_app(
118119
config_module={"OVERRIDE_CORE_SERVICE": [ProviderConfig(provider_type)]}
119120
)
121+
122+
123+
async def test_ensure_build_works(anyio_backend):
124+
@ensure_build_context
125+
def set_xxx(key, value):
126+
current_config[key] = value
127+
128+
set_xxx("DES", 12)
129+
set_xxx("DES_34", 34)
130+
131+
app = AppFactory.create_app()
132+
async with injector_context(app.injector):
133+
set_xxx("DES_345", 23432)
134+
135+
assert app.config.DES == 12
136+
assert app.config.DES_34 == 34
137+
assert app.config.DES_345 == 23432

tests/test_modules/test_forward_ref.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ class ModuleWithModuleRefByType:
6060

6161

6262
def test_sample_controller_fails_for_module_without_module_ref_as_dependency():
63-
app = Test.create_test_module(
64-
modules=[ModuleToBeReference, ModuleWithoutModuleRef]
65-
).create_application()
66-
module_ref = app.injector.tree_manager.get_module(ModuleWithoutModuleRef).value
63+
tm = Test.create_test_module(modules=[ModuleToBeReference, ModuleWithoutModuleRef])
64+
app = tm.create_application()
65+
tm_module_ref = app.injector.tree_manager.get_module(tm.module).value
66+
67+
assert ModuleToBeReference in tm_module_ref.modules
6768

6869
with pytest.raises(
6970
di_exceptions.UnsatisfiedRequirement,
7071
match="SampleController has an unsatisfied requirement on UserService",
7172
):
73+
module_ref = app.injector.tree_manager.get_module(ModuleWithoutModuleRef).value
7274
assert module_ref.get(SampleController)
7375

7476

0 commit comments

Comments
 (0)