(tmp_path: Path, test_client_factory: TestClientFactory)
| 101 | |
| 102 | |
| 103 | def 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 | |
| 138 | def test_templates_require_directory_or_environment() -> None: |
nothing calls this directly
no test coverage detected