Additional test coverage for async rendering of native type templates (#1807)

This commit is contained in:
James Addison 2024-07-10 17:14:52 +01:00 committed by GitHub
parent d7225e65f3
commit f8323cf404
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 23 deletions

View File

@ -1,11 +1,22 @@
import asyncio
from pathlib import Path
import pytest
import trio
from jinja2 import loaders
from jinja2.environment import Environment
def _asyncio_run(async_fn, *args):
return asyncio.run(async_fn(*args))
@pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
def run_async_fn(request):
return request.param
@pytest.fixture
def env():
"""returns a new environment."""

View File

@ -1,7 +1,4 @@
import asyncio
import pytest
import trio
from jinja2 import ChainableUndefined
from jinja2 import DictLoader
@ -14,15 +11,6 @@ from jinja2.exceptions import UndefinedError
from jinja2.nativetypes import NativeEnvironment
def _asyncio_run(async_fn, *args):
return asyncio.run(async_fn(*args))
@pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
def run_async_fn(request):
return request.param
def test_basic_async(run_async_fn):
t = Template(
"{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True

View File

@ -1,9 +1,7 @@
import asyncio
import contextlib
from collections import namedtuple
import pytest
import trio
from markupsafe import Markup
from jinja2 import Environment
@ -29,15 +27,6 @@ def env_async():
return Environment(enable_async=True)
def _asyncio_run(async_fn, *args):
return asyncio.run(async_fn(*args))
@pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
def run_async_fn(request):
return request.param
@contextlib.asynccontextmanager
async def closing_factory():
async with contextlib.AsyncExitStack() as stack:

View File

@ -13,6 +13,11 @@ def env():
return NativeEnvironment()
@pytest.fixture
def async_native_env():
return NativeEnvironment(enable_async=True)
def test_is_defined_native_return(env):
t = env.from_string("{{ missing is defined }}")
assert not t.render()
@ -122,6 +127,18 @@ def test_string_top_level(env):
assert result == "Jinja"
def test_string_concatenation(async_native_env, run_async_fn):
async def async_render():
t = async_native_env.from_string(
"{%- macro x(y) -%}{{ y }}{%- endmacro -%}{{- x('not') }} {{ x('bad') -}}"
)
result = await t.render_async()
assert isinstance(result, str)
assert result == "not bad"
run_async_fn(async_render)
def test_tuple_of_variable_strings(env):
t = env.from_string("'{{ a }}', 'data', '{{ b }}', b'{{ c }}'")
result = t.render(a=1, b=2, c="bytes")