Debian Codegraph @

tests/test_async.py

raw
1 import asyncio
2
3 import pytest
4 import trio
5
6 from jinja2 import ChainableUndefined
7 from jinja2 import DictLoader
8 from jinja2 import Environment
9 from jinja2 import Template
10 from jinja2.async_utils import auto_aiter
11 from jinja2.exceptions import TemplateNotFound
12 from jinja2.exceptions import TemplatesNotFound
13 from jinja2.exceptions import UndefinedError
14 from jinja2.nativetypes import NativeEnvironment
15
16
17 def _asyncio_run(async_fn, *args):
18 return asyncio.run(async_fn(*args))
19
20
21 @pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
22 def run_async_fn(request):
23 return request.param
24
25
26 def test_basic_async(run_async_fn):
27 t = Template(
28 "{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True
29 )
30
31 async def func():
32 return await t.render_async()
33
34 rv = run_async_fn(func)
35 assert rv == "[1][2][3]"
36
37
38 def test_await_on_calls(run_async_fn):
39 t = Template("{{ async_func() + normal_func() }}", enable_async=True)
40
41 async def async_func():
42 return 42
43
44 def normal_func():
45 return 23
46
47 async def func():
48 return await t.render_async(async_func=async_func, normal_func=normal_func)
49
50 rv = run_async_fn(func)
51 assert rv == "65"
52
53
54 def test_await_on_calls_normal_render():
55 t = Template("{{ async_func() + normal_func() }}", enable_async=True)
56
57 async def async_func():
58 return 42
59
60 def normal_func():
61 return 23
62
63 rv = t.render(async_func=async_func, normal_func=normal_func)
64 assert rv == "65"
65
66
67 def test_await_and_macros(run_async_fn):
68 t = Template(
69 "{% macro foo(x) %}[{{ x }}][{{ async_func() }}]{% endmacro %}{{ foo(42) }}",
70 enable_async=True,
71 )
72
73 async def async_func():
74 return 42
75
76 async def func():
77 return await t.render_async(async_func=async_func)
78
79 rv = run_async_fn(func)
80 assert rv == "[42][42]"
81
82
83 def test_async_blocks(run_async_fn):
84 t = Template(
85 "{% block foo %}<Test>{% endblock %}{{ self.foo() }}",
86 enable_async=True,
87 autoescape=True,
88 )
89
90 async def func():
91 return await t.render_async()
92
93 rv = run_async_fn(func)
94 assert rv == "<Test><Test>"
95
96
97 def test_async_generate():
98 t = Template("{% for x in [1, 2, 3] %}{{ x }}{% endfor %}", enable_async=True)
99 rv = list(t.generate())
100 assert rv == ["1", "2", "3"]
101
102
103 def test_async_iteration_in_templates():
104 t = Template("{% for x in rng %}{{ x }}{% endfor %}", enable_async=True)
105
106 async def async_iterator():
107 for item in [1, 2, 3]:
108 yield item
109
110 rv = list(t.generate(rng=async_iterator()))
111 assert rv == ["1", "2", "3"]
112
113
114 def test_async_iteration_in_templates_extended():
115 t = Template(
116 "{% for x in rng %}{{ loop.index0 }}/{{ x }}{% endfor %}", enable_async=True
117 )
118 stream = t.generate(rng=auto_aiter(range(1, 4)))
119 assert next(stream) == "0"
120 assert "".join(stream) == "/11/22/3"
121
122
123 @pytest.fixture
124 def test_env_async():
125 env = Environment(
126 loader=DictLoader(
127 dict(
128 module="{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}",
129 header="[{{ foo }}|{{ 23 }}]",
130 o_printer="({{ o }})",
131 )
132 ),
133 enable_async=True,
134 )
135 env.globals["bar"] = 23
136 return env
137
138
139 class TestAsyncImports:
140 def test_context_imports(self, test_env_async):
141 t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')
142 assert t.render(foo=42) == "[|23]"
143 t = test_env_async.from_string(
144 '{% import "module" as m without context %}{{ m.test() }}'
145 )
146 assert t.render(foo=42) == "[|23]"
147 t = test_env_async.from_string(
148 '{% import "module" as m with context %}{{ m.test() }}'
149 )
150 assert t.render(foo=42) == "[42|23]"
151 t = test_env_async.from_string('{% from "module" import test %}{{ test() }}')
152 assert t.render(foo=42) == "[|23]"
153 t = test_env_async.from_string(
154 '{% from "module" import test without context %}{{ test() }}'
155 )
156 assert t.render(foo=42) == "[|23]"
157 t = test_env_async.from_string(
158 '{% from "module" import test with context %}{{ test() }}'
159 )
160 assert t.render(foo=42) == "[42|23]"
161
162 def test_trailing_comma(self, test_env_async):
163 test_env_async.from_string('{% from "foo" import bar, baz with context %}')
164 test_env_async.from_string('{% from "foo" import bar, baz, with context %}')
165 test_env_async.from_string('{% from "foo" import bar, with context %}')
166 test_env_async.from_string('{% from "foo" import bar, with, context %}')
167 test_env_async.from_string('{% from "foo" import bar, with with context %}')
168
169 def test_exports(self, test_env_async, run_async_fn):
170 coro_fn = test_env_async.from_string(
171 """
172 {% macro toplevel() %}...{% endmacro %}
173 {% macro __private() %}...{% endmacro %}
174 {% set variable = 42 %}
175 {% for item in [1] %}
176 {% macro notthere() %}{% endmacro %}
177 {% endfor %}
178 """
179 )._get_default_module_async
180 m = run_async_fn(coro_fn)
181 assert run_async_fn(m.toplevel) == "..."
182 assert not hasattr(m, "__missing")
183 assert m.variable == 42
184 assert not hasattr(m, "notthere")
185
186 def test_import_with_globals(self, test_env_async):
187 t = test_env_async.from_string(
188 '{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}
189 )
190 assert t.render() == "[42|23]"
191
192 t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')
193 assert t.render() == "[|23]"
194
195 def test_import_with_globals_override(self, test_env_async):
196 t = test_env_async.from_string(
197 '{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',
198 globals={"foo": 42},
199 )
200 assert t.render() == "[42|23]"
201
202 def test_from_import_with_globals(self, test_env_async):
203 t = test_env_async.from_string(
204 '{% from "module" import test %}{{ test() }}',
205 globals={"foo": 42},
206 )
207 assert t.render() == "[42|23]"
208
209
210 class TestAsyncIncludes:
211 def test_context_include(self, test_env_async):
212 t = test_env_async.from_string('{% include "header" %}')
213 assert t.render(foo=42) == "[42|23]"
214 t = test_env_async.from_string('{% include "header" with context %}')
215 assert t.render(foo=42) == "[42|23]"
216 t = test_env_async.from_string('{% include "header" without context %}')
217 assert t.render(foo=42) == "[|23]"
218
219 def test_choice_includes(self, test_env_async):
220 t = test_env_async.from_string('{% include ["missing", "header"] %}')
221 assert t.render(foo=42) == "[42|23]"
222
223 t = test_env_async.from_string(
224 '{% include ["missing", "missing2"] ignore missing %}'
225 )
226 assert t.render(foo=42) == ""
227
228 t = test_env_async.from_string('{% include ["missing", "missing2"] %}')
229 pytest.raises(TemplateNotFound, t.render)
230 with pytest.raises(TemplatesNotFound) as e:
231 t.render()
232
233 assert e.value.templates == ["missing", "missing2"]
234 assert e.value.name == "missing2"
235
236 def test_includes(t, **ctx):
237 ctx["foo"] = 42
238 assert t.render(ctx) == "[42|23]"
239
240 t = test_env_async.from_string('{% include ["missing", "header"] %}')
241 test_includes(t)
242 t = test_env_async.from_string("{% include x %}")
243 test_includes(t, x=["missing", "header"])
244 t = test_env_async.from_string('{% include [x, "header"] %}')
245 test_includes(t, x="missing")
246 t = test_env_async.from_string("{% include x %}")
247 test_includes(t, x="header")
248 t = test_env_async.from_string("{% include x %}")
249 test_includes(t, x="header")
250 t = test_env_async.from_string("{% include [x] %}")
251 test_includes(t, x="header")
252
253 def test_include_ignoring_missing(self, test_env_async):
254 t = test_env_async.from_string('{% include "missing" %}')
255 pytest.raises(TemplateNotFound, t.render)
256 for extra in "", "with context", "without context":
257 t = test_env_async.from_string(
258 '{% include "missing" ignore missing ' + extra + " %}"
259 )
260 assert t.render() == ""
261
262 def test_context_include_with_overrides(self, test_env_async):
263 env = Environment(
264 loader=DictLoader(
265 dict(
266 main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",
267 item="{{ item }}",
268 )
269 )
270 )
271 assert env.get_template("main").render() == "123"
272
273 def test_unoptimized_scopes(self, test_env_async):
274 t = test_env_async.from_string(
275 """
276 {% macro outer(o) %}
277 {% macro inner() %}
278 {% include "o_printer" %}
279 {% endmacro %}
280 {{ inner() }}
281 {% endmacro %}
282 {{ outer("FOO") }}
283 """
284 )
285 assert t.render().strip() == "(FOO)"
286
287 def test_unoptimized_scopes_autoescape(self):
288 env = Environment(
289 loader=DictLoader({"o_printer": "({{ o }})"}),
290 autoescape=True,
291 enable_async=True,
292 )
293 t = env.from_string(
294 """
295 {% macro outer(o) %}
296 {% macro inner() %}
297 {% include "o_printer" %}
298 {% endmacro %}
299 {{ inner() }}
300 {% endmacro %}
301 {{ outer("FOO") }}
302 """
303 )
304 assert t.render().strip() == "(FOO)"
305
306
307 class TestAsyncForLoop:
308 def test_simple(self, test_env_async):
309 tmpl = test_env_async.from_string("{% for item in seq %}{{ item }}{% endfor %}")
310 assert tmpl.render(seq=list(range(10))) == "0123456789"
311
312 def test_else(self, test_env_async):
313 tmpl = test_env_async.from_string(
314 "{% for item in seq %}XXX{% else %}...{% endfor %}"
315 )
316 assert tmpl.render() == "..."
317
318 def test_empty_blocks(self, test_env_async):
319 tmpl = test_env_async.from_string(
320 "<{% for item in seq %}{% else %}{% endfor %}>"
321 )
322 assert tmpl.render() == "<>"
323
324 @pytest.mark.parametrize(
325 "transform", [lambda x: x, iter, reversed, lambda x: (i for i in x), auto_aiter]
326 )
327 def test_context_vars(self, test_env_async, transform):
328 t = test_env_async.from_string(
329 "{% for item in seq %}{{ loop.index }}|{{ loop.index0 }}"
330 "|{{ loop.revindex }}|{{ loop.revindex0 }}|{{ loop.first }}"
331 "|{{ loop.last }}|{{ loop.length }}\n{% endfor %}"
332 )
333 out = t.render(seq=transform([42, 24]))
334 assert out == "1|0|2|1|True|False|2\n2|1|1|0|False|True|2\n"
335
336 def test_cycling(self, test_env_async):
337 tmpl = test_env_async.from_string(
338 """{% for item in seq %}{{
339 loop.cycle('<1>', '<2>') }}{% endfor %}{%
340 for item in seq %}{{ loop.cycle(*through) }}{% endfor %}"""
341 )
342 output = tmpl.render(seq=list(range(4)), through=("<1>", "<2>"))
343 assert output == "<1><2>" * 4
344
345 def test_lookaround(self, test_env_async):
346 tmpl = test_env_async.from_string(
347 """{% for item in seq -%}
348 {{ loop.previtem|default('x') }}-{{ item }}-{{
349 loop.nextitem|default('x') }}|
350 {%- endfor %}"""
351 )
352 output = tmpl.render(seq=list(range(4)))
353 assert output == "x-0-1|0-1-2|1-2-3|2-3-x|"
354
355 def test_changed(self, test_env_async):
356 tmpl = test_env_async.from_string(
357 """{% for item in seq -%}
358 {{ loop.changed(item) }},
359 {%- endfor %}"""
360 )
361 output = tmpl.render(seq=[None, None, 1, 2, 2, 3, 4, 4, 4])
362 assert output == "True,False,True,True,False,True,True,False,False,"
363
364 def test_scope(self, test_env_async):
365 tmpl = test_env_async.from_string("{% for item in seq %}{% endfor %}{{ item }}")
366 output = tmpl.render(seq=list(range(10)))
367 assert not output
368
369 def test_varlen(self, test_env_async):
370 def inner():
371 yield from range(5)
372
373 tmpl = test_env_async.from_string(
374 "{% for item in iter %}{{ item }}{% endfor %}"
375 )
376 output = tmpl.render(iter=inner())
377 assert output == "01234"
378
379 def test_noniter(self, test_env_async):
380 tmpl = test_env_async.from_string("{% for item in none %}...{% endfor %}")
381 pytest.raises(TypeError, tmpl.render)
382
383 def test_recursive(self, test_env_async):
384 tmpl = test_env_async.from_string(
385 """{% for item in seq recursive -%}
386 [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]
387 {%- endfor %}"""
388 )
389 assert (
390 tmpl.render(
391 seq=[
392 dict(a=1, b=[dict(a=1), dict(a=2)]),
393 dict(a=2, b=[dict(a=1), dict(a=2)]),
394 dict(a=3, b=[dict(a="a")]),
395 ]
396 )
397 == "[1<[1][2]>][2<[1][2]>][3<[a]>]"
398 )
399
400 def test_recursive_lookaround(self, test_env_async):
401 tmpl = test_env_async.from_string(
402 """{% for item in seq recursive -%}
403 [{{ loop.previtem.a if loop.previtem is defined else 'x' }}.{{
404 item.a }}.{{ loop.nextitem.a if loop.nextitem is defined else 'x'
405 }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]
406 {%- endfor %}"""
407 )
408 assert (
409 tmpl.render(
410 seq=[
411 dict(a=1, b=[dict(a=1), dict(a=2)]),
412 dict(a=2, b=[dict(a=1), dict(a=2)]),
413 dict(a=3, b=[dict(a="a")]),
414 ]
415 )
416 == "[x.1.2<[x.1.2][1.2.x]>][1.2.3<[x.1.2][1.2.x]>][2.3.x<[x.a.x]>]"
417 )
418
419 def test_recursive_depth0(self, test_env_async):
420 tmpl = test_env_async.from_string(
421 "{% for item in seq recursive %}[{{ loop.depth0 }}:{{ item.a }}"
422 "{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}"
423 )
424 assert (
425 tmpl.render(
426 seq=[
427 dict(a=1, b=[dict(a=1), dict(a=2)]),
428 dict(a=2, b=[dict(a=1), dict(a=2)]),
429 dict(a=3, b=[dict(a="a")]),
430 ]
431 )
432 == "[0:1<[1:1][1:2]>][0:2<[1:1][1:2]>][0:3<[1:a]>]"
433 )
434
435 def test_recursive_depth(self, test_env_async):
436 tmpl = test_env_async.from_string(
437 "{% for item in seq recursive %}[{{ loop.depth }}:{{ item.a }}"
438 "{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}"
439 )
440 assert (
441 tmpl.render(
442 seq=[
443 dict(a=1, b=[dict(a=1), dict(a=2)]),
444 dict(a=2, b=[dict(a=1), dict(a=2)]),
445 dict(a=3, b=[dict(a="a")]),
446 ]
447 )
448 == "[1:1<[2:1][2:2]>][1:2<[2:1][2:2]>][1:3<[2:a]>]"
449 )
450
451 def test_looploop(self, test_env_async):
452 tmpl = test_env_async.from_string(
453 """{% for row in table %}
454 {%- set rowloop = loop -%}
455 {% for cell in row -%}
456 [{{ rowloop.index }}|{{ loop.index }}]
457 {%- endfor %}
458 {%- endfor %}"""
459 )
460 assert tmpl.render(table=["ab", "cd"]) == "[1|1][1|2][2|1][2|2]"
461
462 def test_reversed_bug(self, test_env_async):
463 tmpl = test_env_async.from_string(
464 "{% for i in items %}{{ i }}"
465 "{% if not loop.last %}"
466 ",{% endif %}{% endfor %}"
467 )
468 assert tmpl.render(items=reversed([3, 2, 1])) == "1,2,3"
469
470 def test_loop_errors(self, test_env_async, run_async_fn):
471 tmpl = test_env_async.from_string(
472 """{% for item in [1] if loop.index
473 == 0 %}...{% endfor %}"""
474 )
475 with pytest.raises(UndefinedError):
476 run_async_fn(tmpl.render_async)
477
478 tmpl = test_env_async.from_string(
479 """{% for item in [] %}...{% else
480 %}{{ loop }}{% endfor %}"""
481 )
482 assert run_async_fn(tmpl.render_async) == ""
483
484 def test_loop_filter(self, test_env_async):
485 tmpl = test_env_async.from_string(
486 "{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}"
487 )
488 assert tmpl.render() == "[0][2][4][6][8]"
489 tmpl = test_env_async.from_string(
490 """
491 {%- for item in range(10) if item is even %}[{{
492 loop.index }}:{{ item }}]{% endfor %}"""
493 )
494 assert tmpl.render() == "[1:0][2:2][3:4][4:6][5:8]"
495
496 def test_scoped_special_var(self, test_env_async):
497 t = test_env_async.from_string(
498 "{% for s in seq %}[{{ loop.first }}{% for c in s %}"
499 "|{{ loop.first }}{% endfor %}]{% endfor %}"
500 )
501 assert t.render(seq=("ab", "cd")) == "[True|True|False][False|True|False]"
502
503 def test_scoped_loop_var(self, test_env_async):
504 t = test_env_async.from_string(
505 "{% for x in seq %}{{ loop.first }}"
506 "{% for y in seq %}{% endfor %}{% endfor %}"
507 )
508 assert t.render(seq="ab") == "TrueFalse"
509 t = test_env_async.from_string(
510 "{% for x in seq %}{% for y in seq %}"
511 "{{ loop.first }}{% endfor %}{% endfor %}"
512 )
513 assert t.render(seq="ab") == "TrueFalseTrueFalse"
514
515 def test_recursive_empty_loop_iter(self, test_env_async):
516 t = test_env_async.from_string(
517 """
518 {%- for item in foo recursive -%}{%- endfor -%}
519 """
520 )
521 assert t.render(dict(foo=[])) == ""
522
523 def test_call_in_loop(self, test_env_async):
524 t = test_env_async.from_string(
525 """
526 {%- macro do_something() -%}
527 [{{ caller() }}]
528 {%- endmacro %}
529
530 {%- for i in [1, 2, 3] %}
531 {%- call do_something() -%}
532 {{ i }}
533 {%- endcall %}
534 {%- endfor -%}
535 """
536 )
537 assert t.render() == "[1][2][3]"
538
539 def test_scoping_bug(self, test_env_async):
540 t = test_env_async.from_string(
541 """
542 {%- for item in foo %}...{{ item }}...{% endfor %}
543 {%- macro item(a) %}...{{ a }}...{% endmacro %}
544 {{- item(2) -}}
545 """
546 )
547 assert t.render(foo=(1,)) == "...1......2..."
548
549 def test_unpacking(self, test_env_async):
550 tmpl = test_env_async.from_string(
551 "{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}"
552 )
553 assert tmpl.render() == "1|2|3"
554
555 def test_recursive_loop_filter(self, test_env_async):
556 t = test_env_async.from_string(
557 """
558 <?xml version="1.0" encoding="UTF-8"?>
559 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
560 {%- for page in [site.root] if page.url != this recursive %}
561 <url><loc>{{ page.url }}</loc></url>
562 {{- loop(page.children) }}
563 {%- endfor %}
564 </urlset>
565 """
566 )
567 sm = t.render(
568 this="/foo",
569 site={"root": {"url": "/", "children": [{"url": "/foo"}, {"url": "/bar"}]}},
570 )
571 lines = [x.strip() for x in sm.splitlines() if x.strip()]
572 assert lines == [
573 '<?xml version="1.0" encoding="UTF-8"?>',
574 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
575 "<url><loc>/</loc></url>",
576 "<url><loc>/bar</loc></url>",
577 "</urlset>",
578 ]
579
580 def test_nonrecursive_loop_filter(self, test_env_async):
581 t = test_env_async.from_string(
582 """
583 <?xml version="1.0" encoding="UTF-8"?>
584 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
585 {%- for page in items if page.url != this %}
586 <url><loc>{{ page.url }}</loc></url>
587 {%- endfor %}
588 </urlset>
589 """
590 )
591 sm = t.render(
592 this="/foo", items=[{"url": "/"}, {"url": "/foo"}, {"url": "/bar"}]
593 )
594 lines = [x.strip() for x in sm.splitlines() if x.strip()]
595 assert lines == [
596 '<?xml version="1.0" encoding="UTF-8"?>',
597 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
598 "<url><loc>/</loc></url>",
599 "<url><loc>/bar</loc></url>",
600 "</urlset>",
601 ]
602
603 def test_bare_async(self, test_env_async):
604 t = test_env_async.from_string('{% extends "header" %}')
605 assert t.render(foo=42) == "[42|23]"
606
607 def test_awaitable_property_slicing(self, test_env_async):
608 t = test_env_async.from_string("{% for x in a.b[:1] %}{{ x }}{% endfor %}")
609 assert t.render(a=dict(b=[1, 2, 3])) == "1"
610
611
612 def test_namespace_awaitable(test_env_async, run_async_fn):
613 async def _test():
614 t = test_env_async.from_string(
615 '{% set ns = namespace(foo="Bar") %}{{ ns.foo }}'
616 )
617 actual = await t.render_async()
618 assert actual == "Bar"
619
620 run_async_fn(_test)
621
622
623 def test_chainable_undefined_aiter(run_async_fn):
624 async def _test():
625 t = Template(
626 "{% for x in a['b']['c'] %}{{ x }}{% endfor %}",
627 enable_async=True,
628 undefined=ChainableUndefined,
629 )
630 rv = await t.render_async(a={})
631 assert rv == ""
632
633 run_async_fn(_test)
634
635
636 @pytest.fixture
637 def async_native_env():
638 return NativeEnvironment(enable_async=True)
639
640
641 def test_native_async(async_native_env, run_async_fn):
642 async def _test():
643 t = async_native_env.from_string("{{ x }}")
644 rv = await t.render_async(x=23)
645 assert rv == 23
646
647 run_async_fn(_test)
648
649
650 def test_native_list_async(async_native_env, run_async_fn):
651 async def _test():
652 t = async_native_env.from_string("{{ x }}")
653 rv = await t.render_async(x=list(range(3)))
654 assert rv == [0, 1, 2]
655
656 run_async_fn(_test)
657
658
659 def test_getitem_after_filter():
660 env = Environment(enable_async=True)
661 env.filters["add_each"] = lambda v, x: [i + x for i in v]
662 t = env.from_string("{{ (a|add_each(2))[1:] }}")
663 out = t.render(a=range(3))
664 assert out == "[3, 4]"
665
666
667 def test_getitem_after_call():
668 env = Environment(enable_async=True)
669 env.globals["add_each"] = lambda v, x: [i + x for i in v]
670 t = env.from_string("{{ add_each(a, 2)[1:] }}")
671 out = t.render(a=range(3))
672 assert out == "[3, 4]"
673
674
675 def test_basic_generate_async(run_async_fn):
676 t = Template(
677 "{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True
678 )
679
680 async def func():
681 agen = t.generate_async()
682 try:
683 return await agen.__anext__()
684 finally:
685 await agen.aclose()
686
687 rv = run_async_fn(func)
688 assert rv == "["
689
690
691 def test_include_generate_async(run_async_fn, test_env_async):
692 t = test_env_async.from_string('{% include "header" %}')
693
694 async def func():
695 agen = t.generate_async()
696 try:
697 return await agen.__anext__()
698 finally:
699 await agen.aclose()
700
701 rv = run_async_fn(func)
702 assert rv == "["
703
704
705 def test_blocks_generate_async(run_async_fn):
706 t = Template(
707 "{% block foo %}<Test>{% endblock %}{{ self.foo() }}",
708 enable_async=True,
709 autoescape=True,
710 )
711
712 async def func():
713 agen = t.generate_async()
714 try:
715 return await agen.__anext__()
716 finally:
717 await agen.aclose()
718
719 rv = run_async_fn(func)
720 assert rv == "<Test>"
721
722
723 def test_async_extend(run_async_fn, test_env_async):
724 t = test_env_async.from_string('{% extends "header" %}')
725
726 async def func():
727 agen = t.generate_async()
728 try:
729 return await agen.__anext__()
730 finally:
731 await agen.aclose()
732
733 rv = run_async_fn(func)
734 assert rv == "["