MCPcopy
hub / github.com/encode/starlette / test_templates_with_directories

Function test_templates_with_directories

tests/test_templates.py:103–135  ·  view source on GitHub ↗
(tmp_path: Path, test_client_factory: TestClientFactory)

Source from the content-addressed store, hash-verified

101
102
103def test_templates_with_directories(tmp_path: Path, test_client_factory: TestClientFactory) -> None:
104 dir_a = tmp_path.resolve() / "a"
105 dir_a.mkdir()
106 template_a = dir_a / "template_a.html"
107 template_a.write_text("<html><a href='{{ url_for('page_a') }}'></a> a</html>")
108
109 async def page_a(request: Request) -> Response:
110 return templates.TemplateResponse(request, "template_a.html")
111
112 dir_b = tmp_path.resolve() / "b"
113 dir_b.mkdir()
114 template_b = dir_b / "template_b.html"
115 template_b.write_text("<html><a href='{{ url_for('page_b') }}'></a> b</html>")
116
117 async def page_b(request: Request) -> Response:
118 return templates.TemplateResponse(request, "template_b.html")
119
120 app = Starlette(
121 debug=True,
122 routes=[Route("/a", endpoint=page_a), Route("/b", endpoint=page_b)],
123 )
124 templates = Jinja2Templates(directory=[dir_a, dir_b])
125
126 client = test_client_factory(app)
127 response = client.get("/a")
128 assert response.text == "<html><a href='http://testserver/a'></a> a</html>"
129 assert response.template.name == "template_a.html" # type: ignore
130 assert set(response.context.keys()) == {"request"} # type: ignore
131
132 response = client.get("/b")
133 assert response.text == "<html><a href='http://testserver/b'></a> b</html>"
134 assert response.template.name == "template_b.html" # type: ignore
135 assert set(response.context.keys()) == {"request"} # type: ignore
136
137
138def test_templates_require_directory_or_environment() -> None:

Callers

nothing calls this directly

Calls 6

StarletteClass · 0.90
RouteClass · 0.90
Jinja2TemplatesClass · 0.90
test_client_factoryFunction · 0.85
getMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected