-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_html.py
More file actions
139 lines (110 loc) · 4.23 KB
/
Copy pathtest_html.py
File metadata and controls
139 lines (110 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pytest
from ellar.common import (
Controller,
Inject,
get,
render,
template_filter,
template_global,
)
from ellar.common.constants import (
CONTROLLER_OPERATION_HANDLER_KEY,
RESPONSE_OVERRIDE_KEY,
TEMPLATE_FILTER_KEY,
TEMPLATE_GLOBAL_KEY,
)
from ellar.common.decorators.html import RenderDecoratorException
from ellar.common.exceptions import ImproperConfiguration
from ellar.common.responses.models import HTMLResponseModel
from ellar.common.templating import TemplateFunctionData
from ellar.core import Request
from ellar.core.router_builders import ControllerRouterBuilder
from ellar.core.routing import RouteOperation
from ellar.reflect import reflect
def test_render_decorator_works():
@render("index")
def endpoint_render(request: Inject[Request]):
pass # pragma: no cover
response_override = reflect.get_metadata(RESPONSE_OVERRIDE_KEY, endpoint_render)
assert isinstance(response_override, dict)
html_response: HTMLResponseModel = response_override[200]
assert isinstance(html_response, HTMLResponseModel)
assert html_response.template_name == "index"
def test_render_decorator_wont_work_after_route_action_definition():
@render("index")
class Whatever:
pass
response_override = reflect.get_metadata(RESPONSE_OVERRIDE_KEY, Whatever)
assert response_override is None
def test_render_decorator_raise_exception_for_invalid_template_name():
with pytest.raises(
AssertionError, match="Render Operation must invoked eg. @render()"
):
@render
def endpoint_render(request: Inject[Request]):
pass # pragma: no cover
def test_render_decorator_uses_endpoint_name_as_template_name():
@Controller
class AController:
@get("/endpoint_render")
@render()
def endpoint_render(self, request: Inject[Request]):
pass # pragma: no cover
ControllerRouterBuilder.build(AController)
a_controller_operations = reflect.get_metadata(
CONTROLLER_OPERATION_HANDLER_KEY, AController
)
assert len(a_controller_operations) == 1
endpoint_render_operation: RouteOperation = a_controller_operations[0]
assert (
reflect.get_metadata(RESPONSE_OVERRIDE_KEY, endpoint_render_operation.endpoint)
is None
)
html_response = endpoint_render_operation.response_model.models[200]
assert isinstance(html_response, HTMLResponseModel)
assert html_response.template_name == "endpoint_render"
assert html_response.use_mvc
def test_render_decorator_fails_for_missing_template_name():
with pytest.raises(
RenderDecoratorException,
match="template_name is required for function endpoints.",
):
@render()
def endpoint_render(request):
pass # pragma: no cover
def test_template_global_function_applies_template_global_key():
@template_global()
def global_function_test():
pass # pragma: no cover
assert hasattr(global_function_test, TEMPLATE_GLOBAL_KEY)
template_data: TemplateFunctionData = getattr(
global_function_test, TEMPLATE_GLOBAL_KEY
)
assert template_data.func is global_function_test
assert template_data.name == "global_function_test"
def test_template_global_function_fails_for_async_functions():
with pytest.raises(
ImproperConfiguration,
match="TemplateGlobalCallable must be a non coroutine function",
):
@template_global()
async def global_function_test():
pass # pragma: no cover
def test_template_filter_function_applies_template_filter_key():
@template_filter()
def filter_function_test():
pass # pragma: no cover
assert hasattr(filter_function_test, TEMPLATE_FILTER_KEY)
template_data: TemplateFunctionData = getattr(
filter_function_test, TEMPLATE_FILTER_KEY
)
assert template_data.func is filter_function_test
assert template_data.name == "filter_function_test"
def test_template_filter_function_fails_for_async_functions():
with pytest.raises(
ImproperConfiguration,
match="TemplateGlobalCallable must be a non coroutine function",
):
@template_filter()
async def filter_function_test():
pass # pragma: no cover