Skip to content

Commit 5593309

Browse files
committed
fixed all tests
1 parent 8cfbb4b commit 5593309

File tree

55 files changed

+524
-470
lines changed

Some content is hidden

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

55 files changed

+524
-470
lines changed

docs/overview/exception_handling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class ExceptionHandlerAction405(IExceptionHandler):
200200
self, ctx: IHostContext, exc: HTTPException
201201
) -> t.Union[Response, t.Any]:
202202
context_kwargs = {}
203-
return render_template('405.html', request=ctx.switch_to_http_connection().get_request(), **context_kwargs)
203+
return render_template('405.html', **context_kwargs)
204204
```
205205
In this code snippet, we've registered a handler for any `HTTP` exception with a `405` status code, and we return a template, `405.html`, as a response.
206206
Similarly, you can create an exception handler for the `500` status code that returns an HTML template.

docs/security/rate-limit.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ from ellar.common import Controller, get
103103

104104

105105
@Controller("/limit")
106-
@Throttle(apply_interceptor=True)
106+
@Throttle(intercept=True)
107107
class LimitController:
108108
def __init__(self, app_service: AppService):
109109
self.app_service = app_service
@@ -113,15 +113,15 @@ class LimitController:
113113
return self.app_service.success(use_auth)
114114
```
115115

116-
In the above example, by setting `apply_interceptor=True` within **@Throttle**,
116+
In the above example, by setting `intercept=True` within **@Throttle**,
117117
the `ThrottlerInterceptor` is applied to all routes within the `LimitController`.
118118
This feature is particularly useful when `ThrottlerInterceptor` is not globally applied.
119119

120120
Additionally, the **@Throttle** decorator can be used on a route level:
121121

122122
```python
123123
@get()
124-
@Throttle(apply_interceptor=True)
124+
@Throttle(intercept=True)
125125
def get_throttled(self, use_auth: bool):
126126
return self.app_service.success(use_auth)
127127
```
@@ -130,7 +130,7 @@ Another application of the **@Throttle** decorator is to override `ttl` and `lim
130130

131131
```python
132132
@Controller("/limit")
133-
@Throttle(apply_interceptor=True, anon={'ttl': 100, 'limit': 30})
133+
@Throttle(intercept=True, anon={'ttl': 100, 'limit': 30})
134134
class LimitController:
135135
def __init__(self, app_service: AppService):
136136
self.app_service = app_service

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,15 @@ warn_unused_ignores = true
107107
disallow_untyped_defs = true
108108
check_untyped_defs = true
109109
implicit_reexport = false
110-
disable_error_code = ['union-attr']
110+
disable_error_code = ['union-attr', 'type-abstract', 'arg-type']
111111

112112
[[tool.mypy.overrides]]
113113
module = "ellar.common.compatible.*"
114114
ignore_errors = true
115115
[[tool.mypy.overrides]]
116+
module = "ellar.core.conf.*"
117+
ignore_errors = true
118+
[[tool.mypy.overrides]]
116119
module = "ellar.core.services.*"
117120
ignore_errors = true
118121
[[tool.mypy.overrides]]

tests/test_application/sample.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ def runtime_error(self, request: Inject[Request]):
103103
controllers=[ClassBaseController],
104104
)
105105
class ApplicationModule(ModuleBase):
106-
@exception_handler(405)
106+
@exception_handler(405, app=True)
107107
async def method_not_allow_exception(self, ctx, exec):
108108
return JSONResponse({"detail": "Custom message"}, status_code=405)
109109

110-
@exception_handler(500)
110+
@exception_handler(500, app=True)
111111
async def error_500(self, ctx, exec):
112112
return JSONResponse({"detail": "Server Error"}, status_code=500)
113113

114-
@exception_handler(HTTPException)
114+
@exception_handler(HTTPException, app=True)
115115
async def http_exception(self, ctx, exc):
116116
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)

tests/test_application/test_app_context.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import logging
22

33
import pytest
4-
from ellar.app import App, config, current_injector
4+
from ellar.app import App
55
from ellar.common import Body, post
6-
from ellar.core import Config
7-
from ellar.core.context import ApplicationContext
6+
from ellar.core import Config, config, current_injector, with_injector_context
87
from ellar.testing import Test
98

109

@@ -24,7 +23,7 @@ async def test_current_config_fails_when_there_is_no_ellar_config_module(
2423
with caplog.at_level(logging.WARNING):
2524
tm = Test.create_test_module()
2625

27-
async with ApplicationContext.create(tm.create_application()):
26+
async with with_injector_context(tm.create_application().injector):
2827
assert current_injector.get(App) is not None
2928
assert config.DEBUG is False
3029

@@ -43,7 +42,7 @@ async def test_current_config_fails_when_there_is_no_ellar_config_module(
4342
async def test_current_injector_works(anyio_backend):
4443
tm = Test.create_test_module()
4544

46-
async with ApplicationContext.create(tm.create_application()):
45+
async with with_injector_context(tm.create_application().injector):
4746
assert current_injector.get(App) is not None
4847

4948
with pytest.raises(RuntimeError):
@@ -53,7 +52,7 @@ async def test_current_injector_works(anyio_backend):
5352
async def test_current_app_works(anyio_backend):
5453
tm = Test.create_test_module()
5554

56-
async with ApplicationContext.create(tm.create_application()):
55+
async with with_injector_context(tm.create_application().injector):
5756
assert isinstance(current_injector.get(Config), Config)
5857

5958
with pytest.raises(RuntimeError):
@@ -63,7 +62,7 @@ async def test_current_app_works(anyio_backend):
6362
async def test_current_config_works(anyio_backend):
6463
tm = Test.create_test_module(config_module={"FRAMEWORK_NAME": "Ellar"})
6564

66-
async with tm.create_application().application_context():
65+
async with with_injector_context(tm.create_application().injector):
6766
app = current_injector.get(App)
6867
assert app.config.FRAMEWORK_NAME == config.FRAMEWORK_NAME
6968

@@ -72,20 +71,19 @@ async def test_current_config_works(anyio_backend):
7271

7372

7473
async def test_current_config_works_(anyio_backend):
75-
tm = Test.create_test_module(config_module={"FRAMEWORK_NAME": "Ellar"})
76-
7774
@post
7875
def add(a: Body[int], b: Body[int]):
79-
from ellar.app import current_injector
76+
from ellar.core import current_injector
8077

8178
config_ = current_injector.get(Config)
8279
assert config_.FRAMEWORK_NAME == config.FRAMEWORK_NAME
8380
return a + b
8481

85-
app = tm.create_application()
86-
app.router.add_route(add)
82+
tm = Test.create_test_module(
83+
config_module={"FRAMEWORK_NAME": "Ellar"}, routers=[add]
84+
)
8785

88-
async with app.application_context():
86+
async with with_injector_context(tm.create_application().injector):
8987
res = tm.get_test_client().post("/", json={"a": 1, "b": 4})
9088
assert res.json() == 5
9189
config_ = current_injector.get(Config)

tests/test_application/test_app_methods.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ async def homepage(request: Inject[Request], ctx: Inject[IExecutionContext]):
3131
res = PlainTextResponse("Ellar Route Handler as an ASGI app")
3232
await res(*ctx.get_args())
3333

34-
app = AppFactory.create_app()
35-
app.router.add_route(homepage)
34+
app = AppFactory.create_app(routers=[homepage])
3635
client = TestClient(app)
3736
response = client.get("/")
3837
assert response.status_code == 200
@@ -44,8 +43,7 @@ async def homepage(request: Inject[Request], ctx: Inject[IExecutionContext]):
4443
res = PlainTextResponse("Ellar Route Handler as an ASGI app")
4544
return res
4645

47-
app = AppFactory.create_app()
48-
app.router.add_route(homepage)
46+
app = AppFactory.create_app(routers=[homepage])
4947
result = app.url_path_for("homepage")
5048
assert result == "/homepage-url"
5149

@@ -129,14 +127,13 @@ async def catch(
129127
) -> t.Union[Response, t.Any]:
130128
return JSONResponse({"detail": str(exc)}, status_code=404)
131129

132-
app = AppFactory.create_app()
133-
app.add_exception_handler(CustomExceptionHandler())
134-
135130
@get("/404")
136131
def raise_custom_exception():
137132
raise CustomException("Raised an Exception")
138133

139-
app.router.add_route(raise_custom_exception)
134+
app = AppFactory.create_app(routers=[raise_custom_exception])
135+
app.add_exception_handler(CustomExceptionHandler())
136+
140137
client = TestClient(app)
141138
res = client.get("/404")
142139
assert res.status_code == 404

tests/test_application/test_app_templating.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
template_global,
66
)
77
from ellar.common.templating import Environment
8-
from ellar.core import ModuleBase
8+
from ellar.core import ModuleBase, with_injector_context
99
from ellar.core.modules import ModuleTemplateRef
1010
from ellar.threading.sync_worker import execute_async_context_manager
1111

1212

1313
class TestAppTemplating:
14-
@Module(template_folder="some_template")
14+
@Module(template_folder="some_template", name="app_template")
1515
class AppTemplateModuleTest(ModuleBase):
1616
@template_filter(name="app_filter")
1717
def template_filter_test(self, value):
@@ -31,7 +31,7 @@ def test_app_get_module_loaders(self):
3131

3232
def test_app_jinja_environment(self):
3333
app = AppFactory.create_from_app_module(module=self.AppTemplateModuleTest)
34-
with execute_async_context_manager(app.application_context()):
34+
with execute_async_context_manager(with_injector_context(app.injector)):
3535
environment = app.injector.get(Environment)
3636
assert isinstance(environment, Environment)
3737

tests/test_application/test_application_factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ellar.common.constants import MODULE_WATERMARK
55
from ellar.common.exceptions import ImproperConfiguration
66
from ellar.core import LazyModuleImport as lazyLoad
7-
from ellar.core import ModuleBase, ModuleSetup
7+
from ellar.core import ModuleBase, ModuleSetup, with_injector_context
88
from ellar.di import ProviderConfig
99
from ellar.reflect import reflect
1010
from ellar.testing import TestClient
@@ -62,7 +62,7 @@ def test_factory_create_from_app_module():
6262

6363
def test_factory_create_app_dynamically_creates_module():
6464
app = AppFactory.create_app()
65-
first_module_ref = app.injector.tree_manager.get_root_module()
65+
first_module_ref = app.injector.tree_manager.get_app_module()
6666
module_instance = first_module_ref.get_module_instance()
6767
assert not isinstance(module_instance, ModuleBase)
6868
assert reflect.get_metadata(MODULE_WATERMARK, type(module_instance))
@@ -95,7 +95,7 @@ class CModule:
9595
res = client.get("/static/example.txt")
9696
assert res.status_code == 200
9797
assert res.text == "<file content>"
98-
async with app.application_context():
98+
async with with_injector_context(app.injector):
9999
template = app.injector.get(Environment).get_template("example.html")
100100
result = template.render()
101101
assert result == "<html>Hello World<html/>"

0 commit comments

Comments
 (0)