Skip to content

Commit

Permalink
test: coverage for contrib.minijinja (#2593)
Browse files Browse the repository at this point in the history
This PR extends test coverage for the minijinja contrib.

For: #2535
  • Loading branch information
peterschutt authored Nov 1, 2023
1 parent 47281b2 commit 2d63576
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
16 changes: 10 additions & 6 deletions litestar/contrib/minijinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def render(self, *args: Any, **kwargs: Any) -> str:
Returns:
Rendered template as a string
"""
return str(self.engine.render_template(self.template_name, *args, **kwargs))
try:
return str(self.engine.render_template(self.template_name, *args, **kwargs))
except MiniJinjaTemplateNotFound as err:
raise TemplateNotFoundException(template_name=self.template_name) from err


class MiniJinjaTemplateEngine(TemplateEngineProtocol["MiniJinjaTemplate", StateProtocol]):
Expand Down Expand Up @@ -134,6 +137,10 @@ def _loader(name: str) -> str:
self.engine = Environment(loader=_loader)
elif engine_instance:
self.engine = engine_instance
else:
raise ImproperlyConfiguredException(
"You must provide either a directory or a minijinja Environment instance."
)

self.register_template_callable("url_for", _transform_state(url_for))
self.register_template_callable("csrf_token", _transform_state(csrf_token))
Expand All @@ -151,10 +158,7 @@ def get_template(self, template_name: str) -> MiniJinjaTemplate:
Raises:
TemplateNotFoundException: if no template is found.
"""
try:
return MiniJinjaTemplate(self.engine, template_name)
except MiniJinjaTemplateNotFound as exc:
raise TemplateNotFoundException(template_name=template_name) from exc
return MiniJinjaTemplate(self.engine, template_name)

def register_template_callable(
self, key: str, template_callable: TemplateCallableType[StateProtocol, P, T]
Expand Down Expand Up @@ -184,7 +188,7 @@ def from_environment(cls, minijinja_environment: Environment) -> MiniJinjaTempla


@pass_state
def _minijinja_from_state(func: Callable, state: StateProtocol, *args: Any, **kwargs: Any) -> str:
def _minijinja_from_state(func: Callable, state: StateProtocol, *args: Any, **kwargs: Any) -> str: # pragma: no cover
template_context = {"request": state.lookup("request"), "csrf_input": state.lookup("csrf_input")}
return cast(str, func(template_context, *args, **kwargs))

Expand Down
39 changes: 39 additions & 0 deletions tests/unit/test_contrib/test_minijinja.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from minijinja import Environment # type: ignore[import-untyped]

from litestar.contrib.minijinja import MiniJinjaTemplateEngine
from litestar.exceptions import ImproperlyConfiguredException, TemplateNotFoundException

if TYPE_CHECKING:
from pathlib import Path


def test_mini_jinja_template_engine_instantiation_error(tmp_path: Path) -> None:
with pytest.raises(ImproperlyConfiguredException):
MiniJinjaTemplateEngine(directory=tmp_path, engine_instance=Environment())

with pytest.raises(ImproperlyConfiguredException):
MiniJinjaTemplateEngine()


def test_mini_jinja_template_engine_instantiated_with_engine() -> None:
engine = Environment()
template_engine = MiniJinjaTemplateEngine(engine_instance=engine)
assert template_engine.engine is engine


def test_mini_jinja_template_render_raises_template_not_found(tmp_path: Path) -> None:
template_engine = MiniJinjaTemplateEngine(engine_instance=Environment())
with pytest.raises(TemplateNotFoundException):
tmpl = template_engine.get_template("not_found.html")
tmpl.render()


def test_from_environment() -> None:
engine = Environment()
template_engine = MiniJinjaTemplateEngine.from_environment(engine)
assert template_engine.engine is engine

0 comments on commit 2d63576

Please sign in to comment.