-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.py
More file actions
150 lines (133 loc) · 5.27 KB
/
Copy pathmodule.py
File metadata and controls
150 lines (133 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import typing as t
from pathlib import Path
from uuid import uuid4
from ellar.app import App, AppFactory
from ellar.common import ControllerBase, Module, ModuleRouter
from ellar.common.types import T
from ellar.core import DynamicModule, ModuleBase, ModuleSetup
from ellar.core.routing import EllarMount
from ellar.di import ProviderConfig
from ellar.utils import get_name
from starlette.routing import Host, Mount
from starlette.testclient import TestClient as TestClient
if t.TYPE_CHECKING: # pragma: no cover
from ellar.common import GuardCanActivate
class TestingModule:
def __init__(
self,
testing_module: t.Type[t.Union[ModuleBase, t.Any]],
global_guards: t.Optional[
t.List[t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]]
] = None,
config_module: t.Optional[t.Union[str, t.Dict]] = None,
) -> None:
self._testing_module = testing_module
self._config_module = config_module
self._global_guards = global_guards
self._providers: t.List[ProviderConfig] = []
self._app: t.Optional[App] = None
def override_provider(
self,
base_type: t.Union[t.Type[T], t.Type],
*,
use_value: t.Optional[T] = None,
use_class: t.Optional[t.Union[t.Type[T], t.Any]] = None,
) -> "TestingModule":
"""
Overrides Service at module level.
Use this function before creating an application instance.
"""
provider_config = ProviderConfig(
base_type, use_class=use_class, use_value=use_value
)
self._providers.append(provider_config)
return self
def create_application(self) -> App:
if self._app:
return self._app
self._app = AppFactory.create_app(
modules=[self._testing_module],
global_guards=self._global_guards,
config_module=self._config_module,
providers=self._providers,
)
return self._app
def get_test_client(
self,
base_url: str = "http://testserver",
raise_server_exceptions: bool = True,
root_path: str = "",
backend: t.Literal["asyncio", "trio"] = "asyncio",
backend_options: t.Optional[t.Dict[str, t.Any]] = None,
**kwargs: t.Any,
) -> TestClient:
return TestClient(
app=self.create_application(),
base_url=base_url,
raise_server_exceptions=raise_server_exceptions,
backend=backend,
backend_options=backend_options,
root_path=root_path,
**kwargs,
)
def get(self, interface: t.Type[T]) -> T:
return self.create_application().injector.get(interface) # type: ignore[no-any-return]
class Test:
TESTING_MODULE = TestingModule
@classmethod
def create_test_module(
cls,
controllers: t.Sequence[t.Union[t.Type[ControllerBase], t.Type]] = (),
routers: t.Sequence[t.Union[ModuleRouter, EllarMount, Mount, Host]] = (),
providers: t.Sequence[t.Union[t.Type, "ProviderConfig"]] = (),
template_folder: t.Optional[str] = "templates",
base_directory: t.Optional[t.Union[Path, str]] = None,
static_folder: str = "static",
modules: t.Sequence[t.Union[t.Type, t.Any]] = (),
global_guards: t.Optional[
t.List[t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]]
] = None,
config_module: t.Optional[t.Union[str, t.Dict]] = None,
modify_modules: bool = True,
) -> TESTING_MODULE: # type: ignore[valid-type]
"""
Create a TestingModule to test controllers and services in isolation
:param modules: Other module dependencies
:param controllers: Module Controllers
:param routers: Module router
:param providers: Module Services
:param template_folder: Module Templating folder
:param base_directory: Base Directory for static folder and template
:param static_folder: Module Static folder
:param config_module: Application Config
:param global_guards: Application Guard
:param modify_modules: Modifies Modules
if setup or register_setup is used to avoid module sharing metadata between tests
:return:
"""
if modify_modules:
def modifier_module(
_module: t.Union[t.Type, t.Any],
) -> t.Union[t.Type, t.Any]:
return Module()(
type(
f"{get_name(_module)}Modified_{uuid4().hex[:6]}", (_module,), {}
)
)
for module_ in modules:
if isinstance(module_, (ModuleSetup, DynamicModule)):
module_.module = modifier_module(module_.module)
module = Module(
modules=modules,
controllers=controllers,
routers=routers,
providers=providers,
template_folder=template_folder,
base_directory=base_directory,
static_folder=static_folder,
)
testing_module = type(f"TestingModule_{uuid4().hex[:6]}", (ModuleBase,), {})
module(testing_module)
return cls.TESTING_MODULE(
testing_module, global_guards=global_guards, config_module=config_module
)