Skip to content

Commit 89aaaba

Browse files
authored
Merge pull request #10 from eadwinCode/common_test
Common Package Testing
2 parents e79f932 + 044b49e commit 89aaaba

File tree

24 files changed

+461
-51
lines changed

24 files changed

+461
-51
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ You will see the automatic interactive API documentation (provided by <a href="h
106106
## Status
107107
Project is still in development
108108
- Remaining testing modules:
109-
- common
110109
- configuration
111110
- guard
112111
- openapi

ellar/common/decorators/controller.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from ellar.constants import (
77
CONTROLLER_CLASS_KEY,
88
CONTROLLER_METADATA,
9+
CONTROLLER_OPERATION_HANDLER_KEY,
910
CONTROLLER_WATERMARK,
1011
NOT_SET,
1112
OPERATION_ENDPOINT_KEY,
12-
OPERATION_HANDLER_KEY,
1313
)
1414
from ellar.core import ControllerBase
1515
from ellar.core.controller import ControllerType
@@ -33,10 +33,10 @@ def reflect_all_controller_type_routes(cls: t.Type[ControllerBase]) -> None:
3333
for base_cls in reversed(bases):
3434
if base_cls not in [ABC, ControllerBase, object]:
3535
for item in get_route_functions(base_cls):
36-
operation = reflect.get_metadata(OPERATION_HANDLER_KEY, item)
36+
operation = reflect.get_metadata(CONTROLLER_OPERATION_HANDLER_KEY, item)
3737
reflect.define_metadata(CONTROLLER_CLASS_KEY, cls, item)
3838
reflect.define_metadata(
39-
OPERATION_HANDLER_KEY,
39+
CONTROLLER_OPERATION_HANDLER_KEY,
4040
operation,
4141
cls,
4242
default_value=[],

ellar/common/decorators/html.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import inspect
3-
import re
43
import typing as t
54
import warnings
65

@@ -14,7 +13,7 @@
1413
from ellar.core.routing import RouteOperationBase
1514
from ellar.core.templating import TemplateFunctionData
1615
from ellar.exceptions import ImproperConfiguration
17-
from ellar.helper import get_name
16+
from ellar.helper import class_base_function_regex, get_name
1817
from ellar.types import TemplateFilterCallable, TemplateGlobalCallable
1918

2019
from .base import set_meta
@@ -30,9 +29,6 @@ def render(template_name: t.Optional[str] = NOT_SET) -> t.Callable:
3029
template_name, str
3130
), "Render Operation must invoked eg. @render()"
3231
template_name = None if template_name is NOT_SET else template_name
33-
class_base_function_regex = re.compile(
34-
"<\\w+ (\\w+)\\.(\\w+) at \\w+>", re.IGNORECASE
35-
)
3632

3733
def _decorator(func: t.Union[t.Callable, t.Any]) -> t.Union[t.Callable, t.Any]:
3834
if not callable(func) or isinstance(func, RouteOperationBase):

ellar/common/decorators/modules.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,30 @@ def _wrapper(
2222
name: str,
2323
kwargs: AttributeDict,
2424
) -> t.Type:
25-
if not reflect.get_metadata(watermark_key, target):
26-
if not isinstance(target, type):
27-
raise ImproperConfiguration(f"{name} is a class decorator - {target}")
25+
if reflect.get_metadata(watermark_key, target):
26+
raise ImproperConfiguration(f"{target} is already identified as a Module")
2827

29-
if not kwargs.base_directory:
30-
kwargs.update(base_directory=Path(inspect.getfile(target)).resolve().parent)
28+
if not isinstance(target, type):
29+
raise ImproperConfiguration(f"{name} is a class decorator - {target}")
3130

32-
if type(target) != ModuleBaseMeta:
33-
attr: t.Dict = {
34-
item: getattr(target, item) for item in dir(target) if "__" not in item
35-
}
36-
target = type(
37-
target.__name__,
38-
(target, ModuleBase),
39-
attr,
40-
)
31+
if not kwargs.base_directory:
32+
kwargs.update(base_directory=Path(inspect.getfile(target)).resolve().parent)
4133

42-
reflect.define_metadata(watermark_key, True, target)
43-
for key in metadata_keys:
44-
reflect.define_metadata(key, kwargs[key], target)
45-
injectable(SingletonScope)(target)
46-
return target
34+
if type(target) != ModuleBaseMeta:
35+
attr: t.Dict = {
36+
item: getattr(target, item) for item in dir(target) if "__" not in item
37+
}
38+
target = type(
39+
target.__name__,
40+
(target, ModuleBase),
41+
attr,
42+
)
43+
44+
reflect.define_metadata(watermark_key, True, target)
45+
for key in metadata_keys:
46+
reflect.define_metadata(key, kwargs[key], target)
47+
injectable(SingletonScope)(target)
48+
return t.cast(t.Type, target)
4749

4850

4951
def Module(

ellar/constants.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __new__(mcls, name, bases, namespace):
6969
ROUTE_OPENAPI_PARAMETERS = "ROUTE_OPENAPI_PARAMETERS"
7070

7171
OPERATION_ENDPOINT_KEY = "OPERATION_ENDPOINT"
72-
OPERATION_HANDLER_KEY = "OPERATION_HANDLER"
72+
CONTROLLER_OPERATION_HANDLER_KEY = "CONTROLLER_OPERATION_HANDLER"
7373
CONTROLLER_CLASS_KEY = "CONTROLLER_CLASS_KEY"
7474
REFLECT_TYPE = "__REFLECT_TYPE__"
7575
GROUP_METADATA = "GROUP_METADATA"
@@ -121,12 +121,15 @@ class CONTROLLER_METADATA(metaclass=_AnnotationToValue):
121121
REF_PREFIX = "#/components/schemas/"
122122

123123

124-
class _NOT_SET:
124+
class NOT_SET_TYPE:
125+
def __repr__(self) -> str: # pragma: no cover
126+
return f"{__name__}.{self.__class__.__name__}"
127+
125128
def __copy__(self) -> Any: # pragma: no cover
126129
return NOT_SET
127130

128131
def __deepcopy__(self, memodict: Dict = {}) -> Any: # pragma: no cover
129132
return NOT_SET
130133

131134

132-
NOT_SET: Any = _NOT_SET()
135+
NOT_SET: Any = NOT_SET_TYPE()

ellar/core/routing/operation_definitions.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
import typing as t
32
from functools import partial
43

@@ -14,6 +13,7 @@
1413
TRACE,
1514
)
1615
from ellar.core.schema import RouteParameters, WsRouteParameters
16+
from ellar.helper import class_base_function_regex
1717
from ellar.types import TCallable
1818

1919
from .base import RouteOperationBase
@@ -30,28 +30,25 @@
3030

3131

3232
class OperationDefinitions:
33-
__slots__ = ("_routes", "class_base_function_regex")
33+
__slots__ = ("_routes",)
3434

3535
def __init__(
3636
self,
3737
app_routes: t.List[RouteOperationBase] = None,
3838
):
3939
self._routes = app_routes
40-
self.class_base_function_regex = re.compile(
41-
"<\\w+ (\\w+)\\.(\\w+) at \\w+>", re.IGNORECASE
42-
)
4340

4441
@property
4542
def routes(self) -> t.List[RouteOperationBase]:
4643
return self._routes or []
4744

4845
def _get_http_operations_class(self, func: t.Callable) -> t.Type[TOperation]:
49-
if self.class_base_function_regex.match(repr(func)):
46+
if class_base_function_regex.match(repr(func)):
5047
return ControllerRouteOperation
5148
return RouteOperation
5249

5350
def _get_ws_operations_class(self, func: t.Callable) -> t.Type[TWebsocketOperation]:
54-
if self.class_base_function_regex.match(repr(func)):
51+
if class_base_function_regex.match(repr(func)):
5552
return ControllerWebsocketRouteOperation
5653
return WebsocketRouteOperation
5754

ellar/core/routing/route.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
)
99

1010
from ellar.constants import (
11+
CONTROLLER_OPERATION_HANDLER_KEY,
1112
EXTRA_ROUTE_ARGS_KEY,
1213
NOT_SET,
13-
OPERATION_HANDLER_KEY,
1414
RESPONSE_OVERRIDE_KEY,
1515
)
1616
from ellar.core.context import ExecutionContext
@@ -67,7 +67,7 @@ def __init__(
6767
self.endpoint_parameter_model: RequestEndpointArgsModel = NOT_SET
6868
self.response_model: RouteResponseModel = NOT_SET
6969

70-
reflect.define_metadata(OPERATION_HANDLER_KEY, self, self.endpoint)
70+
reflect.define_metadata(CONTROLLER_OPERATION_HANDLER_KEY, self, self.endpoint)
7171
self._load_model()
7272

7373
def build_route_operation( # type:ignore

ellar/core/routing/router/app.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
from starlette.routing import BaseRoute, Router as StarletteRouter
66

7-
from ellar.constants import OPERATION_HANDLER_KEY, SCOPE_API_VERSIONING_RESOLVER
7+
from ellar.constants import (
8+
CONTROLLER_OPERATION_HANDLER_KEY,
9+
SCOPE_API_VERSIONING_RESOLVER,
10+
)
811
from ellar.reflect import reflect
912
from ellar.types import ASGIApp, TReceive, TScope, TSend
1013

@@ -56,7 +59,7 @@ def __init__(
5659
def append(self, item: t.Union[BaseRoute, t.Callable]) -> None:
5760
_item: t.Any = item
5861
if callable(_item) and type(_item) == FunctionType:
59-
_item = reflect.get_metadata(OPERATION_HANDLER_KEY, _item)
62+
_item = reflect.get_metadata(CONTROLLER_OPERATION_HANDLER_KEY, _item)
6063
self.routes.append(_item)
6164

6265
def extend(self, routes: t.Sequence[t.Union[BaseRoute, t.Callable]]) -> None:

ellar/core/routing/router/module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from ellar.compatible import AttributeDict
77
from ellar.constants import (
88
CONTROLLER_METADATA,
9+
CONTROLLER_OPERATION_HANDLER_KEY,
910
GUARDS_KEY,
1011
NOT_SET,
1112
OPENAPI_KEY,
12-
OPERATION_HANDLER_KEY,
1313
VERSIONING_KEY,
1414
)
1515
from ellar.core.controller import ControllerBase
@@ -31,7 +31,7 @@ def controller_router_factory(
3131
controller: t.Union[t.Type[ControllerBase], t.Any]
3232
) -> "ModuleMount":
3333
openapi = reflect.get_metadata(CONTROLLER_METADATA.OPENAPI, controller) or dict()
34-
routes = reflect.get_metadata(OPERATION_HANDLER_KEY, controller) or []
34+
routes = reflect.get_metadata(CONTROLLER_OPERATION_HANDLER_KEY, controller) or []
3535
app = Router()
3636
app.routes = ModuleRouteCollection(routes) # type:ignore
3737
router = ModuleMount(

ellar/core/routing/websocket/route.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
from starlette.routing import WebSocketRoute as StarletteWebSocketRoute, compile_path
44
from starlette.status import WS_1008_POLICY_VIOLATION
55

6-
from ellar.constants import EXTRA_ROUTE_ARGS_KEY, NOT_SET, OPERATION_HANDLER_KEY
6+
from ellar.constants import (
7+
CONTROLLER_OPERATION_HANDLER_KEY,
8+
EXTRA_ROUTE_ARGS_KEY,
9+
NOT_SET,
10+
)
711
from ellar.core.connection import WebSocket
812
from ellar.core.context import ExecutionContext
913
from ellar.core.params import WebsocketEndpointArgsModel
@@ -76,7 +80,7 @@ def __init__(
7680
super().__init__(path=path, endpoint=endpoint, name=name)
7781
self.endpoint_parameter_model: WebsocketEndpointArgsModel = NOT_SET
7882

79-
reflect.define_metadata(OPERATION_HANDLER_KEY, self, self.endpoint)
83+
reflect.define_metadata(CONTROLLER_OPERATION_HANDLER_KEY, self, self.endpoint)
8084

8185
if self._use_extra_handler:
8286
self._handlers_kwargs.update(on_receive=self.endpoint)

0 commit comments

Comments
 (0)