summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-03 07:47:36 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-03 07:47:36 +0000
commit1f6a9795ed529247bb3370c5efeb009a81d30c8b (patch)
treeb0bb57ba2ba39cb69b7426cbc73632d1d0186c79 /tests
parentAdding debian version 0.45+dfsg-1. (diff)
downloadjinjax-1f6a9795ed529247bb3370c5efeb009a81d30c8b.tar.xz
jinjax-1f6a9795ed529247bb3370c5efeb009a81d30c8b.zip
Merging upstream version 0.46.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_render.py134
1 files changed, 131 insertions, 3 deletions
diff --git a/tests/test_render.py b/tests/test_render.py
index 1e52cda..0f37ea7 100644
--- a/tests/test_render.py
+++ b/tests/test_render.py
@@ -1,6 +1,7 @@
import time
from pathlib import Path
from textwrap import dedent
+from threading import Thread
import jinja2
import pytest
@@ -923,7 +924,7 @@ def test_vue_like_syntax(catalog, folder):
)
html = catalog.render("Caller")
print(html)
- expected = """4 2+2 {'lorem': 'ipsum'} False""".strip()
+ expected = """4 2+2 {'lorem': 'ipsum'} False""".strip()
assert html == Markup(expected)
@@ -937,7 +938,7 @@ def test_jinja_like_syntax(catalog, folder):
)
html = catalog.render("Caller")
print(html)
- expected = """4 2+2 {'lorem': 'ipsum'} False""".strip()
+ expected = """4 2+2 {'lorem': 'ipsum'} False""".strip()
assert html == Markup(expected)
@@ -951,7 +952,7 @@ def test_mixed_syntax(catalog, folder):
)
html = catalog.render("Caller")
print(html)
- expected = """4 {{2+2}} {'lorem': 'ipsum'} False""".strip()
+ expected = """4 {{2+2}} {'lorem': 'ipsum'} False""".strip()
assert html == Markup(expected)
@@ -990,3 +991,130 @@ def test_slots(catalog, folder, autoescape):
<p>Default</p>
""".strip()
assert html == Markup(expected)
+
+
+class ThreadWithReturnValue(Thread):
+ def __init__(self, group=None, target=None, name=None, args=None, kwargs=None):
+ args = args or ()
+ kwargs = kwargs or {}
+ Thread.__init__(
+ self,
+ group=group,
+ target=target,
+ name=name,
+ args=args,
+ kwargs=kwargs,
+ )
+ self._target = target
+ self._args = args
+ self._kwargs = kwargs
+ self._return = None
+
+ def run(self):
+ if self._target is not None:
+ self._return = self._target(*self._args, **self._kwargs)
+
+ def join(self, *args):
+ Thread.join(self, *args)
+ return self._return
+
+
+def test_thread_safety_of_render_assets(catalog, folder):
+ NUM_THREADS = 5
+
+ child_tmpl = """
+{#css "c{i}.css" #}
+{#js "c{i}.js" #}
+<p>Child {i}</p>""".strip()
+
+ parent_tmpl = """
+{{ catalog.render_assets() }}
+{{ content }}""".strip()
+
+ comp_tmpl = """
+{#css "a{i}.css", "b{i}.css" #}
+{#js "a{i}.js", "b{i}.js" #}
+<Parent{i}><Child{i} /></Parent{i}>""".strip()
+
+ expected_tmpl = """
+<link rel="stylesheet" href="/static/components/a{i}.css">
+<link rel="stylesheet" href="/static/components/b{i}.css">
+<link rel="stylesheet" href="/static/components/c{i}.css">
+<script type="module" src="/static/components/a{i}.js"></script>
+<script type="module" src="/static/components/b{i}.js"></script>
+<script type="module" src="/static/components/c{i}.js"></script>
+<p>Child {i}</p>""".strip()
+
+ def render(i):
+ return catalog.render(f"Page{i}")
+
+
+ for i in range(NUM_THREADS):
+ si = str(i)
+ child_name = f"Child{i}.jinja"
+ child_src = child_tmpl.replace("{i}", si)
+
+ parent_name = f"Parent{i}.jinja"
+ parent_src = parent_tmpl.replace("{i}", si)
+
+ comp_name = f"Page{i}.jinja"
+ comp_src = comp_tmpl.replace("{i}", si)
+
+ (folder / child_name).write_text(child_src)
+ (folder / comp_name).write_text(comp_src)
+ (folder / parent_name).write_text(parent_src)
+
+ threads = []
+
+ for i in range(NUM_THREADS):
+ thread = ThreadWithReturnValue(target=render, args=(i,))
+ threads.append(thread)
+ thread.start()
+
+ results = [thread.join() for thread in threads]
+
+ for i, result in enumerate(results):
+ expected = expected_tmpl.replace("{i}", str(i))
+ print(f"---- EXPECTED {i}----")
+ print(expected)
+ print(f"---- RESULT {i}----")
+ print(result)
+ assert result == Markup(expected)
+
+
+def test_same_thread_assets_independence(catalog, folder):
+ catalog2 = jinjax.Catalog()
+ catalog2.add_folder(folder)
+
+ print(catalog._key)
+ print(catalog2._key)
+
+ (folder / "Parent.jinja").write_text("""
+{{ catalog.render_assets() }}
+{{ content }}""".strip())
+
+ (folder / "Comp1.jinja").write_text("""
+{#css "a.css" #}
+{#js "a.js" #}
+<Parent />""".strip())
+
+ (folder / "Comp2.jinja").write_text("""
+{#css "b.css" #}
+{#js "b.js" #}
+<Parent />""".strip())
+
+ expected_1 = """
+<link rel="stylesheet" href="/static/components/a.css">
+<script type="module" src="/static/components/a.js"></script>""".strip()
+
+ expected_2 = """
+<link rel="stylesheet" href="/static/components/b.css">
+<script type="module" src="/static/components/b.js"></script>""".strip()
+
+ html1 = catalog.render("Comp1")
+ # `irender` instead of `render` so the assets are not cleared
+ html2 = catalog2.irender("Comp2")
+ print(html1)
+ print(html2)
+ assert html1 == Markup(expected_1)
+ assert html2 == Markup(expected_2)