blob: fd88805b7f350c00ade2dcc291d88468501b2949 (
plain)
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
|
import pytest
import asyncio
from jinja2 import Template
from jinja2.utils import have_async_gen
def run(func):
loop = asyncio.get_event_loop()
return loop.run_until_complete(func())
@pytest.mark.skipif(not have_async_gen, reason='No async generators')
def test_basic_async():
t = Template('{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}',
enable_async=True)
async def func():
return await t.render_async()
rv = run(func)
assert rv == '[1][2][3]'
@pytest.mark.skipif(not have_async_gen, reason='No async generators')
def test_await_on_calls():
t = Template('{{ async_func() + normal_func() }}',
enable_async=True)
async def async_func():
return 42
def normal_func():
return 23
async def func():
return await t.render_async(
async_func=async_func,
normal_func=normal_func
)
rv = run(func)
assert rv == '65'
@pytest.mark.skipif(not have_async_gen, reason='No async generators')
def test_await_and_macros():
t = Template('{% macro foo(x) %}[{{ x }}][{{ async_func() }}]'
'{% endmacro %}{{ foo(42) }}', enable_async=True)
async def async_func():
return 42
async def func():
return await t.render_async(async_func=async_func)
rv = run(func)
assert rv == '[42][42]'
@pytest.mark.skipif(not have_async_gen, reason='No async generators')
def test_async_blocks():
t = Template('{% block foo %}<Test>{% endblock %}{{ self.foo() }}',
enable_async=True, autoescape=True)
async def func():
return await t.render_async()
rv = run(func)
assert rv == '<Test><Test>'
|