Skip to content

Commit 374aee7

Browse files
committed
Added more tests
1 parent aabb2f2 commit 374aee7

19 files changed

+63
-53
lines changed

tests/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pydantic import Field
55

66
from ellar.common import ModuleRouter, Path, Query
7+
from ellar.core.connection import Request
78
from ellar.core.factory import AppFactory
89
from ellar.core.schema import Schema
910

@@ -214,7 +215,7 @@ def get_query_aliased_name(query: AliasedSchema = Query(..., alias="aliased.-_~n
214215

215216

216217
@router.get("/query/param")
217-
def get_query_param(request, query=Query(None)):
218+
def get_query_param(request: Request, query=Query(None)):
218219
if query is None:
219220
return "foo bar"
220221
return f"foo bar {query}"

tests/test_application/config.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
from starlette.middleware import Middleware
2-
from starlette.middleware.trustedhost import TrustedHostMiddleware
3-
4-
5-
class Config:
6-
MIDDLEWARE = [
7-
Middleware(TrustedHostMiddleware, allowed_hosts=["testserver", "*.example.org"])
8-
]
1+
class ConfigTrustHostConfigure:
2+
ALLOWED_HOSTS = ["testserver", "*.example.org"]

tests/test_application/sample.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from starlette.routing import Host, Mount, Route, Router
66

77
from ellar.common import Controller, Module, ModuleRouter, exception_handler, get
8+
from ellar.core.connection import Request, WebSocket
89
from ellar.core.guard import APIKeyQuery
910

1011

@@ -60,17 +61,17 @@ def user_page(request):
6061

6162
@router.get("/func")
6263
@router.head("/func")
63-
def func_homepage(request):
64+
def func_homepage(request: Request):
6465
return PlainTextResponse("Hello, world!")
6566

6667

6768
@router.get("/async")
68-
async def async_homepage(request):
69+
async def async_homepage(request: Request):
6970
return PlainTextResponse("Hello, world!")
7071

7172

7273
@router.ws_route("/ws")
73-
async def websocket_endpoint(session):
74+
async def websocket_endpoint(session: WebSocket):
7475
await session.accept()
7576
await session.send_text("Hello, world!")
7677
await session.close()
@@ -83,7 +84,7 @@ def class_function(self):
8384
return PlainTextResponse("Hello, world!")
8485

8586
@get("/500")
86-
def runtime_error(self, request):
87+
def runtime_error(self, request: Request):
8788
raise RuntimeError()
8889

8990

tests/test_application/test_application_factory.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ def test_factory_create_from_app_module():
5858
app = AppFactory.create_from_app_module(
5959
module=BModule,
6060
global_guards=[AppAPIKey],
61-
config_module="tests.test_application.config:Config",
61+
config_module="tests.test_application.config:ConfigTrustHostConfigure",
6262
)
6363
assert app.get_guards() == [AppAPIKey]
64-
assert app.config.config_module == "tests.test_application.config:Config"
64+
assert (
65+
app.config.config_module
66+
== "tests.test_application.config:ConfigTrustHostConfigure"
67+
)
6568

6669

6770
def test_factory_create_app_dynamically_creates_module():
@@ -82,7 +85,7 @@ class CModule:
8285
app = AppFactory.create_app(
8386
modules=(CModule,),
8487
global_guards=[AppAPIKey],
85-
config_module="tests.test_application.config:Config",
88+
config_module="tests.test_application.config:ConfigTrustHostConfigure",
8689
controllers=(ClassBaseController,),
8790
routers=[
8891
Host("{subdomain}.example.org", app=sub_domain),

tests/test_application/test_application_functions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@
1212
TestClient,
1313
TestClientFactory,
1414
)
15+
from ellar.core.connection import Request
1516
from ellar.core.context import IExecutionContext
1617
from ellar.core.events import EventHandler
1718
from ellar.core.modules import ModuleTemplateRef
1819
from ellar.core.staticfiles import StaticFiles
1920
from ellar.core.templating import Environment
2021
from ellar.core.versioning import VERSIONING, DefaultAPIVersioning, UrlPathAPIVersioning
2122
from ellar.di import EllarInjector
23+
from ellar.helper.importer import get_class_import
2224
from ellar.openapi import OpenAPIDocumentModule
2325
from ellar.services.reflector import Reflector
2426

27+
from .config import ConfigTrustHostConfigure
2528
from .sample import AppAPIKey, ApplicationModule
2629

2730
test_module = TestClientFactory.create_test_module_from_module(
28-
module=ApplicationModule, config_module="tests.test_application.config:Config"
31+
module=ApplicationModule, config_module=get_class_import(ConfigTrustHostConfigure)
2932
)
3033

3134

@@ -155,7 +158,7 @@ async def lifespan(app):
155158

156159
def test_app_debug(self):
157160
@get("/")
158-
async def homepage(request):
161+
async def homepage(request: Request):
159162
raise RuntimeError()
160163

161164
app = AppFactory.create_app()

tests/test_common/test_decorators/test_guards.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ellar.common import guards
22
from ellar.constants import GUARDS_KEY
33
from ellar.core import ExecutionContext
4+
from ellar.core.connection import Request
45
from ellar.core.guard import GuardCanActivate
56
from ellar.reflect import reflect
67

@@ -16,12 +17,12 @@ async def can_activate(self, context: ExecutionContext) -> bool:
1617

1718

1819
@guards(SomeGuard)
19-
def endpoint(request):
20+
def endpoint(request: Request):
2021
return "foo"
2122

2223

2324
@guards(SomeGuard, SomeGuard2)
24-
def endpoint2(request):
25+
def endpoint2(request: Request):
2526
return "foo"
2627

2728

tests/test_common/test_decorators/test_html.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
TEMPLATE_FILTER_KEY,
99
TEMPLATE_GLOBAL_KEY,
1010
)
11+
from ellar.core.connection import Request
1112
from ellar.core.response.model import HTMLResponseModel
1213
from ellar.core.templating import TemplateFunctionData
1314
from ellar.exceptions import ImproperConfiguration
@@ -16,7 +17,7 @@
1617

1718
def test_render_decorator_works():
1819
@render("index")
19-
def endpoint_render(request):
20+
def endpoint_render(request: Request):
2021
pass
2122

2223
response_override = reflect.get_metadata(RESPONSE_OVERRIDE_KEY, endpoint_render)
@@ -32,7 +33,7 @@ def test_render_decorator_raise_exception_for_invalid_template_name():
3233
):
3334

3435
@render
35-
def endpoint_render(request):
36+
def endpoint_render(request: Request):
3637
pass
3738

3839

@@ -41,7 +42,7 @@ def test_render_decorator_uses_endpoint_name_as_template_name():
4142
class AController:
4243
@get("/endpoint_render")
4344
@render()
44-
def endpoint_render(request):
45+
def endpoint_render(self, request: Request):
4546
pass
4647

4748
a_controller_operations = reflect.get_metadata(

tests/test_common/test_decorators/test_openapi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ellar.common import openapi_info
22
from ellar.compatible import AttributeDict
33
from ellar.constants import OPENAPI_KEY
4+
from ellar.core.connection import Request
45
from ellar.reflect import reflect
56

67

@@ -11,7 +12,7 @@
1112
operation_id="4524d-z23zd-453ed-2342e",
1213
tags=["endpoint", "endpoint-25"],
1314
)
14-
def endpoint(request):
15+
def endpoint(request: Request):
1516
pass
1617

1718

tests/test_common/test_decorators/test_serializer.py

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

66

77
@serializer_filter(
8-
include=["test1", "test2"],
8+
include={"test1", "test2"},
99
exclude_none=True,
1010
by_alias=False,
11-
exclude=["exclude1", "exclude2"],
11+
exclude={"exclude1", "exclude2"},
1212
skip_defaults=None,
1313
exclude_unset=True,
1414
exclude_defaults=True,

tests/test_controller/test_route_collection.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ellar.core import Config
88
from ellar.core.middleware import RequestVersioningMiddleware
99
from ellar.core.routing import RouteOperation, WebsocketRouteOperation
10-
from ellar.core.routing.router import ModuleRouteCollection, RouteCollection
10+
from ellar.core.routing.router import RouteCollection
1111
from ellar.core.versioning import UrlPathAPIVersioning
1212
from ellar.helper import generate_controller_operation_unique_id
1313

@@ -75,7 +75,7 @@ def __init__(self, path: str, name: str = None):
7575
super().__init__(path=path, name=name, app=self.asgi_app)
7676

7777

78-
@pytest.mark.parametrize("collection_model", [ModuleRouteCollection, RouteCollection])
78+
@pytest.mark.parametrize("collection_model", [RouteCollection])
7979
def test_module_route_collection_for_same_path_but_different_version(
8080
collection_model, test_client_factory
8181
):
@@ -100,7 +100,7 @@ def test_module_route_collection_for_same_path_but_different_version(
100100
}
101101

102102

103-
@pytest.mark.parametrize("collection_model", [ModuleRouteCollection, RouteCollection])
103+
@pytest.mark.parametrize("collection_model", [RouteCollection])
104104
def test_module_route_collection_extend(collection_model):
105105
routes = collection_model()
106106
routes.extend(
@@ -114,7 +114,7 @@ def test_module_route_collection_extend(collection_model):
114114
assert len(routes) == 3
115115

116116

117-
@pytest.mark.parametrize("collection_model", [ModuleRouteCollection, RouteCollection])
117+
@pytest.mark.parametrize("collection_model", [RouteCollection])
118118
def test_module_route_collection_host(collection_model):
119119

120120
routes = collection_model()
@@ -134,7 +134,7 @@ def test_module_route_collection_host(collection_model):
134134
assert _hash in routes._routes
135135

136136

137-
@pytest.mark.parametrize("collection_model", [ModuleRouteCollection, RouteCollection])
137+
@pytest.mark.parametrize("collection_model", [RouteCollection])
138138
def test_module_route_collection_mount(collection_model):
139139
routes = collection_model()
140140
routes.append(MockMountRouteOperation("/mount"))
@@ -155,7 +155,7 @@ def test_module_route_collection_mount(collection_model):
155155

156156
@pytest.mark.parametrize(
157157
"collection_model, expected_result",
158-
[(ModuleRouteCollection, "/sample/1"), (RouteCollection, "/sample")],
158+
[(RouteCollection, "/sample")],
159159
)
160160
def test_module_route_collection_setitem_and_getitem(collection_model, expected_result):
161161
routes = collection_model()
@@ -170,7 +170,7 @@ def test_module_route_collection_setitem_and_getitem(collection_model, expected_
170170
assert routes[0].path == expected_result
171171

172172

173-
@pytest.mark.parametrize("collection_model", [ModuleRouteCollection, RouteCollection])
173+
@pytest.mark.parametrize("collection_model", [RouteCollection])
174174
def test_module_route_collection_for_same_path_different_method(
175175
collection_model, test_client_factory
176176
):

0 commit comments

Comments
 (0)