5
5
6
6
from ellar .common import Module , get , template_filter , template_global
7
7
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
16
10
from ellar .core .connection import Request
17
11
from ellar .core .context import IExecutionContext
18
12
from ellar .core .events import EventHandler
19
13
from ellar .core .exceptions .interfaces import IExceptionHandler
20
14
from ellar .core .modules import ModuleTemplateRef
21
- from ellar .core .services import CoreServiceRegistration
22
15
from ellar .core .staticfiles import StaticFiles
23
16
from ellar .core .templating import Environment
24
17
from ellar .core .versioning import (
30
23
from ellar .helper .importer import get_class_import
31
24
from ellar .openapi import OpenAPIDocumentModule
32
25
from ellar .services .reflector import Reflector
26
+ from ellar .testing import Test , TestClient
33
27
34
28
from .config import ConfigTrustHostConfigure
35
29
from .sample import AppAPIKey , ApplicationModule
36
30
37
- test_module = TestClientFactory .create_test_module_from_module (
31
+ test_module = Test .create_test_module_from_module (
38
32
module = ApplicationModule , config_module = get_class_import (ConfigTrustHostConfigure )
39
33
)
40
34
41
35
42
36
class TestStarletteCompatibility :
43
37
def test_func_route (self ):
44
- client = test_module .get_client ()
38
+ client = test_module .get_test_client ()
45
39
response = client .get ("/func" )
46
40
assert response .status_code == 200
47
41
assert response .text == "Hello, world!"
@@ -51,50 +45,50 @@ def test_func_route(self):
51
45
assert response .text == ""
52
46
53
47
def test_async_route (self ):
54
- client = test_module .get_client ()
48
+ client = test_module .get_test_client ()
55
49
response = client .get ("/async" )
56
50
assert response .status_code == 200
57
51
assert response .text == "Hello, world!"
58
52
59
53
def test_class_route (self ):
60
- client = test_module .get_client ()
54
+ client = test_module .get_test_client ()
61
55
response = client .get ("/classbase/class" )
62
56
assert response .status_code == 200
63
57
assert response .text == "Hello, world!"
64
58
65
59
def test_mounted_route (self ):
66
- client = test_module .get_client ()
60
+ client = test_module .get_test_client ()
67
61
response = client .get ("/users/" )
68
62
assert response .status_code == 200
69
63
assert response .text == "Hello, everyone!"
70
64
71
65
def test_mounted_route_path_params (self ):
72
- client = test_module .get_client ()
66
+ client = test_module .get_test_client ()
73
67
response = client .get ("/users/tomchristie" )
74
68
assert response .status_code == 200
75
69
assert response .text == "Hello, tomchristie!"
76
70
77
71
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/" )
79
73
80
74
response = client .get ("/" )
81
75
assert response .status_code == 200
82
76
assert response .text == "Subdomain: foo"
83
77
84
78
def test_websocket_route (self ):
85
- client = test_module .get_client ()
79
+ client = test_module .get_test_client ()
86
80
with client .websocket_connect ("/ws" ) as session :
87
81
text = session .receive_text ()
88
82
assert text == "Hello, world!"
89
83
90
84
def test_400 (self ):
91
- client = test_module .get_client ()
85
+ client = test_module .get_test_client ()
92
86
response = client .get ("/404" )
93
87
assert response .status_code == 404
94
88
assert response .json () == {"detail" : "Not Found" }
95
89
96
90
def test_405 (self ):
97
- client = test_module .get_client ()
91
+ client = test_module .get_test_client ()
98
92
response = client .post ("/func" )
99
93
assert response .status_code == 405
100
94
assert response .json () == {"detail" : "Custom message" }
@@ -104,13 +98,13 @@ def test_405(self):
104
98
assert response .json () == {"detail" : "Custom message" }
105
99
106
100
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 )
108
102
response = client .get ("/classbase/500" )
109
103
assert response .status_code == 500
110
104
assert response .json () == {"detail" : "Server Error" }
111
105
112
106
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" )
114
108
response = client .get ("/func" )
115
109
assert response .status_code == 400
116
110
assert response .text == "Invalid host header"
@@ -216,7 +210,7 @@ def test_app_staticfiles_route(self, tmpdir):
216
210
217
211
config = Config (STATIC_DIRECTORIES = [tmpdir ])
218
212
injector = EllarInjector ()
219
- CoreServiceRegistration (injector , config = Config ()).register_all ()
213
+ _CoreServiceRegistration (injector , config = Config ()).register_all ()
220
214
injector .container .register_instance (config )
221
215
app = App (injector = injector , config = config )
222
216
client = TestClient (app )
@@ -238,7 +232,7 @@ def test_app_staticfiles_with_different_static_path(self, tmpdir):
238
232
STATIC_MOUNT_PATH = "/static-modified" , STATIC_DIRECTORIES = [tmpdir ]
239
233
)
240
234
injector = EllarInjector ()
241
- CoreServiceRegistration (injector , config = Config ()).register_all ()
235
+ _CoreServiceRegistration (injector , config = Config ()).register_all ()
242
236
injector .container .register_instance (config )
243
237
app = App (injector = injector , config = config )
244
238
client = TestClient (app )
@@ -288,7 +282,7 @@ def test_app_initialization_completion(self):
288
282
injector = EllarInjector (
289
283
auto_bind = False
290
284
) # will raise an exception is service is not registered
291
- CoreServiceRegistration (injector , config = Config ()).register_all ()
285
+ _CoreServiceRegistration (injector , config = Config ()).register_all ()
292
286
injector .container .register_instance (config )
293
287
294
288
app = App (config = config , injector = injector )
@@ -312,7 +306,7 @@ async def catch(
312
306
injector = EllarInjector (
313
307
auto_bind = False
314
308
) # will raise an exception is service is not registered
315
- CoreServiceRegistration (injector , config = Config ()).register_all ()
309
+ _CoreServiceRegistration (injector , config = Config ()).register_all ()
316
310
injector .container .register_instance (config )
317
311
app = App (config = config , injector = injector )
318
312
app .add_exception_handler (CustomExceptionHandler ())
0 commit comments