Skip to content

Commit 6c9e74d

Browse files
committed
Moved TestClientFactory from core to testing package and refactored testing module
1 parent e857ce4 commit 6c9e74d

File tree

22 files changed

+117
-95
lines changed

22 files changed

+117
-95
lines changed

ellar/common/decorators/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing as t
22

33
from ellar.constants import ON_REQUEST_SHUTDOWN_KEY, ON_REQUEST_STARTUP_KEY
4-
from ellar.core.events import EventHandler
4+
from ellar.events import EventHandler
55

66

77
def set_attr_key(handle: t.Callable, key: str, value: t.Any) -> None:

ellar/core/conf/mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from starlette.types import ASGIApp
66

77
from ellar.constants import LOG_LEVELS as log_levels
8-
from ellar.core.events import EventHandler
98
from ellar.core.exceptions.interfaces import IExceptionHandler
109
from ellar.core.middleware import Middleware
1110
from ellar.core.versioning import BaseAPIVersioning
11+
from ellar.events import EventHandler
1212

1313
if t.TYPE_CHECKING: # pragma: no cover
1414
from ellar.core import App

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import typing as t
2+
13
from ellar.di import EllarInjector
24
from ellar.services.reflector import Reflector
35

4-
from .conf import Config
56
from .context import (
67
ExecutionContextFactory,
78
HostContextFactory,
@@ -14,13 +15,16 @@
1415
from .exceptions.interfaces import IExceptionMiddlewareService
1516
from .exceptions.service import ExceptionMiddlewareService
1617

18+
if t.TYPE_CHECKING: # pragma: no cover
19+
from ellar.core.conf import Config
20+
1721

18-
class _CoreServiceRegistration:
22+
class CoreServiceRegistration:
1923
"""Create Binding for all application service"""
2024

2125
__slots__ = ("injector", "config")
2226

23-
def __init__(self, injector: EllarInjector, config: Config) -> None:
27+
def __init__(self, injector: EllarInjector, config: "Config") -> None:
2428
self.injector = injector
2529
self.config = config
2630

@@ -42,4 +46,4 @@ def register_all(self) -> None:
4246
IWebSocketContextFactory, WebSocketContextFactory
4347
)
4448

45-
self.injector.container.register_instance(Reflector())
49+
self.injector.container.register(Reflector)

ellar/core/exceptions/__init__.py

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import typing as t
2-
3-
from starlette import status
41
from starlette.exceptions import HTTPException, WebSocketException
52

63
from .base import APIException
4+
from .exceptions_types import (
5+
AuthenticationFailed,
6+
ImproperConfiguration,
7+
MethodNotAllowed,
8+
NotAcceptable,
9+
NotAuthenticated,
10+
NotFound,
11+
PermissionDenied,
12+
UnsupportedMediaType,
13+
)
714
from .interfaces import IExceptionHandler, IExceptionMiddlewareService
815
from .validation import RequestValidationError, WebSocketRequestValidationError
916

@@ -24,49 +31,3 @@
2431
"UnsupportedMediaType",
2532
"WebSocketException",
2633
]
27-
28-
29-
class ImproperConfiguration(Exception):
30-
pass
31-
32-
33-
class AuthenticationFailed(APIException):
34-
status_code = status.HTTP_401_UNAUTHORIZED
35-
code = "authentication_failed"
36-
37-
def __init__(
38-
self, detail: t.Union[t.List, t.Dict, str] = None, **kwargs: t.Any
39-
) -> None:
40-
if detail is None:
41-
detail = "Incorrect authentication credentials."
42-
super(AuthenticationFailed, self).__init__(detail=detail, **kwargs)
43-
44-
45-
class NotAuthenticated(APIException):
46-
status_code = status.HTTP_401_UNAUTHORIZED
47-
code = "not_authenticated"
48-
49-
50-
class PermissionDenied(APIException):
51-
status_code = status.HTTP_403_FORBIDDEN
52-
code = "permission_denied"
53-
54-
55-
class NotFound(APIException):
56-
status_code = status.HTTP_404_NOT_FOUND
57-
code = "not_found"
58-
59-
60-
class MethodNotAllowed(APIException):
61-
status_code = status.HTTP_405_METHOD_NOT_ALLOWED
62-
code = "method_not_allowed"
63-
64-
65-
class NotAcceptable(APIException):
66-
status_code = status.HTTP_406_NOT_ACCEPTABLE
67-
code = "not_acceptable"
68-
69-
70-
class UnsupportedMediaType(APIException):
71-
status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
72-
code = "unsupported_media_type"

ellar/core/exceptions/callable_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from starlette.concurrency import run_in_threadpool
44
from starlette.responses import Response
55

6+
from ellar.core.context import IHostContext
67
from ellar.helper import is_async_callable
78

8-
from ..context import IHostContext
99
from .interfaces import IExceptionHandler
1010

1111

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import typing as t
2+
3+
from starlette import status
4+
5+
from .base import APIException
6+
7+
8+
class ImproperConfiguration(Exception):
9+
pass
10+
11+
12+
class AuthenticationFailed(APIException):
13+
status_code = status.HTTP_401_UNAUTHORIZED
14+
code = "authentication_failed"
15+
16+
def __init__(
17+
self, detail: t.Union[t.List, t.Dict, str] = None, **kwargs: t.Any
18+
) -> None:
19+
if detail is None:
20+
detail = "Incorrect authentication credentials."
21+
super(AuthenticationFailed, self).__init__(detail=detail, **kwargs)
22+
23+
24+
class NotAuthenticated(APIException):
25+
status_code = status.HTTP_401_UNAUTHORIZED
26+
code = "not_authenticated"
27+
28+
29+
class PermissionDenied(APIException):
30+
status_code = status.HTTP_403_FORBIDDEN
31+
code = "permission_denied"
32+
33+
34+
class NotFound(APIException):
35+
status_code = status.HTTP_404_NOT_FOUND
36+
code = "not_found"
37+
38+
39+
class MethodNotAllowed(APIException):
40+
status_code = status.HTTP_405_METHOD_NOT_ALLOWED
41+
code = "method_not_allowed"
42+
43+
44+
class NotAcceptable(APIException):
45+
status_code = status.HTTP_406_NOT_ACCEPTABLE
46+
code = "not_acceptable"
47+
48+
49+
class UnsupportedMediaType(APIException):
50+
status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
51+
code = "unsupported_media_type"

ellar/core/exceptions/handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
from starlette.responses import Response
99

1010
from ellar.core.context import IHostContext
11-
from ellar.core.exceptions import APIException, RequestValidationError
1211
from ellar.serializer import serialize_object
1312

13+
from .base import APIException
1414
from .interfaces import IExceptionHandler
15+
from .validation import RequestValidationError
1516

1617

1718
class HTTPExceptionHandler(IExceptionHandler):

ellar/core/factory.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from starlette.routing import Host, Mount
77

88
from ellar.constants import MODULE_METADATA, MODULE_WATERMARK
9-
from ellar.core import Config
109
from ellar.core.main import App
1110
from ellar.core.modules import DynamicModule, ModuleBase, ModuleSetup
1211
from ellar.di import EllarInjector, ProviderConfig
1312
from ellar.reflect import reflect
1413

15-
from ._services_util import _CoreServiceRegistration
14+
from .conf import Config
15+
from .core_service_registration import CoreServiceRegistration
1616

1717
if t.TYPE_CHECKING: # pragma: no cover
1818
from ellar.commands import EllarTyper
@@ -65,7 +65,7 @@ def read_all_module(cls, module_config: ModuleSetup) -> t.Dict[t.Type, ModuleSet
6565
def _build_modules(
6666
cls,
6767
app_module: t.Type[t.Union[ModuleBase, t.Any]],
68-
config: Config,
68+
config: "Config",
6969
injector: EllarInjector,
7070
) -> None:
7171
"""
@@ -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/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
from starlette.routing import BaseRoute, Mount
55

66
from ellar.constants import LOG_LEVELS
7-
from ellar.core.conf import Config
87
from ellar.core.datastructures import State, URLPath
9-
from ellar.core.events import EventHandler, RouterEventManager
108
from ellar.core.exceptions.interfaces import (
119
IExceptionHandler,
1210
IExceptionMiddlewareService,
@@ -31,14 +29,17 @@
3129
from ellar.core.templating import AppTemplating, Environment
3230
from ellar.core.versioning import BaseAPIVersioning, VersioningSchemes
3331
from ellar.di.injector import EllarInjector
32+
from ellar.events import EventHandler, RouterEventManager
3433
from ellar.logger import logger
3534
from ellar.types import ASGIApp, T, TReceive, TScope, TSend
3635

36+
from .conf import Config
37+
3738

3839
class App(AppTemplating):
3940
def __init__(
4041
self,
41-
config: Config,
42+
config: "Config",
4243
injector: EllarInjector,
4344
on_startup_event_handlers: t.Optional[t.Sequence[EventHandler]] = None,
4445
on_shutdown_event_handlers: t.Optional[t.Sequence[EventHandler]] = None,
@@ -186,7 +187,7 @@ def has_static_files(self) -> bool: # type: ignore
186187
)
187188

188189
@property
189-
def config(self) -> Config: # type: ignore
190+
def config(self) -> "Config": # type: ignore
190191
return self._config
191192

192193
def build_middleware_stack(self) -> ASGIApp:

ellar/core/middleware/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from ellar.types import ASGIApp, TMessage, TReceive, TScope, TSend
88

99
if t.TYPE_CHECKING: # pragma: no cover
10-
from ellar.core.exceptions.service import ExceptionMiddlewareService
1110
from ellar.di import EllarInjector
11+
from ellar.exceptions.service import ExceptionMiddlewareService
1212

1313

1414
class ExceptionMiddleware:

0 commit comments

Comments
 (0)