Skip to content

Commit a3dfe25

Browse files
committed
Moved TestClientFactory from core to testing package and refactored testing module
1 parent 34c8cfe commit a3dfe25

File tree

57 files changed

+526
-524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+526
-524
lines changed

ellar/core/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
UJSONResponse,
2121
)
2222
from .templating import render_template, render_template_string
23-
from .testclient import TestClient, TestClientFactory
2423

2524
__all__ = [
2625
"App",
@@ -40,8 +39,6 @@
4039
"BaseHttpAuth",
4140
"GuardCanActivate",
4241
"Config",
43-
"TestClientFactory",
44-
"TestClient",
4542
"JSONResponse",
4643
"UJSONResponse",
4744
"ORJSONResponse",

ellar/core/services.py renamed to ellar/core/_services_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .exceptions.service import ExceptionMiddlewareService
1616

1717

18-
class CoreServiceRegistration:
18+
class _CoreServiceRegistration:
1919
"""Create Binding for all application service"""
2020

2121
__slots__ = ("injector", "config")

ellar/core/factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ellar.di import EllarInjector, ProviderConfig
1313
from ellar.reflect import reflect
1414

15-
from .services import CoreServiceRegistration
15+
from ._services_util import _CoreServiceRegistration
1616

1717
if t.TYPE_CHECKING: # pragma: no cover
1818
from ellar.commands import EllarTyper
@@ -133,7 +133,7 @@ def _get_config_kwargs() -> t.Dict:
133133
config = Config(app_configured=True, **_get_config_kwargs())
134134
injector = EllarInjector(auto_bind=config.INJECTOR_AUTO_BIND)
135135
injector.container.register_instance(config, concrete_type=Config)
136-
CoreServiceRegistration(injector, config).register_all()
136+
_CoreServiceRegistration(injector, config).register_all()
137137

138138
cls._build_modules(app_module=module, injector=injector, config=config)
139139

ellar/core/routing/operation_definitions.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def get(
124124
name: str = None,
125125
include_in_schema: bool = True,
126126
response: t.Union[
127-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type
127+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
128128
] = None,
129129
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
130130
methods = [GET]
@@ -144,7 +144,7 @@ def post(
144144
name: str = None,
145145
include_in_schema: bool = True,
146146
response: t.Union[
147-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
147+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
148148
] = None,
149149
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
150150
methods = [POST]
@@ -164,7 +164,7 @@ def put(
164164
name: str = None,
165165
include_in_schema: bool = True,
166166
response: t.Union[
167-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
167+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
168168
] = None,
169169
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
170170
methods = [PUT]
@@ -184,7 +184,7 @@ def patch(
184184
name: str = None,
185185
include_in_schema: bool = True,
186186
response: t.Union[
187-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
187+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
188188
] = None,
189189
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
190190
methods = [PATCH]
@@ -204,7 +204,7 @@ def delete(
204204
name: str = None,
205205
include_in_schema: bool = True,
206206
response: t.Union[
207-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
207+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
208208
] = None,
209209
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
210210
methods = [DELETE]
@@ -224,7 +224,7 @@ def head(
224224
name: str = None,
225225
include_in_schema: bool = True,
226226
response: t.Union[
227-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
227+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
228228
] = None,
229229
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
230230
methods = [HEAD]
@@ -244,7 +244,7 @@ def options(
244244
name: str = None,
245245
include_in_schema: bool = True,
246246
response: t.Union[
247-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
247+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
248248
] = None,
249249
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
250250
methods = [OPTIONS]
@@ -264,7 +264,7 @@ def trace(
264264
name: str = None,
265265
include_in_schema: bool = True,
266266
response: t.Union[
267-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
267+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
268268
] = None,
269269
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
270270
methods = [TRACE]
@@ -285,7 +285,7 @@ def http_route(
285285
name: str = None,
286286
include_in_schema: bool = True,
287287
response: t.Union[
288-
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, None
288+
t.Dict[int, t.Type], t.List[t.Tuple[int, t.Type]], t.Type, t.Any
289289
] = None,
290290
) -> t.Callable[[TCallable], t.Union[TOperation, TCallable]]:
291291
def _decorator(endpoint_handler: TCallable) -> TCallable:

tests/test_application/test_application_factory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from starlette.routing import Host, Mount
33

44
from ellar.common import Module
5-
from ellar.core import AppFactory, Config, ModuleBase, ModuleSetup, TestClient
5+
from ellar.core import AppFactory, Config, ModuleBase, ModuleSetup
66
from ellar.di import EllarInjector
7+
from ellar.testing import TestClient
78

89
from .sample import (
910
AppAPIKey,

tests/test_application/test_application_functions.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@
55

66
from ellar.common import Module, get, template_filter, template_global
77
from ellar.compatible import asynccontextmanager
8-
from ellar.core import (
9-
App,
10-
AppFactory,
11-
Config,
12-
ModuleBase,
13-
TestClient,
14-
TestClientFactory,
15-
)
8+
from ellar.core import App, AppFactory, Config, ModuleBase
9+
from ellar.core._services_util import _CoreServiceRegistration
1610
from ellar.core.connection import Request
1711
from ellar.core.context import IExecutionContext
1812
from ellar.core.events import EventHandler
1913
from ellar.core.exceptions.interfaces import IExceptionHandler
2014
from ellar.core.modules import ModuleTemplateRef
21-
from ellar.core.services import CoreServiceRegistration
2215
from ellar.core.staticfiles import StaticFiles
2316
from ellar.core.templating import Environment
2417
from ellar.core.versioning import (
@@ -30,18 +23,19 @@
3023
from ellar.helper.importer import get_class_import
3124
from ellar.openapi import OpenAPIDocumentModule
3225
from ellar.services.reflector import Reflector
26+
from ellar.testing import Test, TestClient
3327

3428
from .config import ConfigTrustHostConfigure
3529
from .sample import AppAPIKey, ApplicationModule
3630

37-
test_module = TestClientFactory.create_test_module_from_module(
31+
test_module = Test.create_test_module_from_module(
3832
module=ApplicationModule, config_module=get_class_import(ConfigTrustHostConfigure)
3933
)
4034

4135

4236
class TestStarletteCompatibility:
4337
def test_func_route(self):
44-
client = test_module.get_client()
38+
client = test_module.get_test_client()
4539
response = client.get("/func")
4640
assert response.status_code == 200
4741
assert response.text == "Hello, world!"
@@ -51,50 +45,50 @@ def test_func_route(self):
5145
assert response.text == ""
5246

5347
def test_async_route(self):
54-
client = test_module.get_client()
48+
client = test_module.get_test_client()
5549
response = client.get("/async")
5650
assert response.status_code == 200
5751
assert response.text == "Hello, world!"
5852

5953
def test_class_route(self):
60-
client = test_module.get_client()
54+
client = test_module.get_test_client()
6155
response = client.get("/classbase/class")
6256
assert response.status_code == 200
6357
assert response.text == "Hello, world!"
6458

6559
def test_mounted_route(self):
66-
client = test_module.get_client()
60+
client = test_module.get_test_client()
6761
response = client.get("/users/")
6862
assert response.status_code == 200
6963
assert response.text == "Hello, everyone!"
7064

7165
def test_mounted_route_path_params(self):
72-
client = test_module.get_client()
66+
client = test_module.get_test_client()
7367
response = client.get("/users/tomchristie")
7468
assert response.status_code == 200
7569
assert response.text == "Hello, tomchristie!"
7670

7771
def test_subdomain_route(self):
78-
client = test_module.get_client(base_url="https://foo.example.org/")
72+
client = test_module.get_test_client(base_url="https://foo.example.org/")
7973

8074
response = client.get("/")
8175
assert response.status_code == 200
8276
assert response.text == "Subdomain: foo"
8377

8478
def test_websocket_route(self):
85-
client = test_module.get_client()
79+
client = test_module.get_test_client()
8680
with client.websocket_connect("/ws") as session:
8781
text = session.receive_text()
8882
assert text == "Hello, world!"
8983

9084
def test_400(self):
91-
client = test_module.get_client()
85+
client = test_module.get_test_client()
9286
response = client.get("/404")
9387
assert response.status_code == 404
9488
assert response.json() == {"detail": "Not Found"}
9589

9690
def test_405(self):
97-
client = test_module.get_client()
91+
client = test_module.get_test_client()
9892
response = client.post("/func")
9993
assert response.status_code == 405
10094
assert response.json() == {"detail": "Custom message"}
@@ -104,13 +98,13 @@ def test_405(self):
10498
assert response.json() == {"detail": "Custom message"}
10599

106100
def test_500(self):
107-
client = test_module.get_client(raise_server_exceptions=False)
101+
client = test_module.get_test_client(raise_server_exceptions=False)
108102
response = client.get("/classbase/500")
109103
assert response.status_code == 500
110104
assert response.json() == {"detail": "Server Error"}
111105

112106
def test_middleware(self):
113-
client = test_module.get_client(base_url="http://incorrecthost")
107+
client = test_module.get_test_client(base_url="http://incorrecthost")
114108
response = client.get("/func")
115109
assert response.status_code == 400
116110
assert response.text == "Invalid host header"
@@ -216,7 +210,7 @@ def test_app_staticfiles_route(self, tmpdir):
216210

217211
config = Config(STATIC_DIRECTORIES=[tmpdir])
218212
injector = EllarInjector()
219-
CoreServiceRegistration(injector, config=Config()).register_all()
213+
_CoreServiceRegistration(injector, config=Config()).register_all()
220214
injector.container.register_instance(config)
221215
app = App(injector=injector, config=config)
222216
client = TestClient(app)
@@ -238,7 +232,7 @@ def test_app_staticfiles_with_different_static_path(self, tmpdir):
238232
STATIC_MOUNT_PATH="/static-modified", STATIC_DIRECTORIES=[tmpdir]
239233
)
240234
injector = EllarInjector()
241-
CoreServiceRegistration(injector, config=Config()).register_all()
235+
_CoreServiceRegistration(injector, config=Config()).register_all()
242236
injector.container.register_instance(config)
243237
app = App(injector=injector, config=config)
244238
client = TestClient(app)
@@ -288,7 +282,7 @@ def test_app_initialization_completion(self):
288282
injector = EllarInjector(
289283
auto_bind=False
290284
) # will raise an exception is service is not registered
291-
CoreServiceRegistration(injector, config=Config()).register_all()
285+
_CoreServiceRegistration(injector, config=Config()).register_all()
292286
injector.container.register_instance(config)
293287

294288
app = App(config=config, injector=injector)
@@ -312,7 +306,7 @@ async def catch(
312306
injector = EllarInjector(
313307
auto_bind=False
314308
) # will raise an exception is service is not registered
315-
CoreServiceRegistration(injector, config=Config()).register_all()
309+
_CoreServiceRegistration(injector, config=Config()).register_all()
316310
injector.container.register_instance(config)
317311
app = App(config=config, injector=injector)
318312
app.add_exception_handler(CustomExceptionHandler())

0 commit comments

Comments
 (0)