tests: add inline syntax highlight, inline template files

This commit is contained in:
Juro Oravec 2024-05-02 22:33:02 +02:00
parent e566d8ecbb
commit 8c4a55901c
40 changed files with 1309 additions and 1168 deletions

View file

@ -3,18 +3,27 @@ from time import perf_counter
from django.template import Context, Template
from django.test import override_settings
from django_components import component
from django_components import component, types
from django_components.middleware import CSS_DEPENDENCY_PLACEHOLDER, JS_DEPENDENCY_PLACEHOLDER
from tests.django_test_setup import * # NOQA
from tests.testutils import BaseTestCase, create_and_process_template_response
class SlottedComponent(component.Component):
template_name = "slotted_template.html"
template: types.django_html = """
{% load component_tags %}
<custom-template>
<header>{% slot "header" %}Default header{% endslot %}</header>
<main>{% slot "main" %}Default main{% endslot %}</main>
<footer>{% slot "footer" %}Default footer{% endslot %}</footer>
</custom-template>
"""
class SimpleComponent(component.Component):
template_name = "simple_template.html"
template: types.django_html = """
Variable: <strong>{{ variable }}</strong>
"""
def get_context_data(self, variable, variable2="default"):
return {
@ -84,8 +93,7 @@ class RenderBenchmarks(BaseTestCase):
return total_elapsed * 1000 / iterations
def test_render_time_for_small_component(self):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}
{% component 'test_component' %}
{% slot "header" %}
@ -93,13 +101,12 @@ class RenderBenchmarks(BaseTestCase):
{% endslot %}
{% endcomponent %}
"""
)
template = Template(template_str)
print(f"{self.timed_loop(lambda: template.render(Context({})))} ms per iteration")
def test_middleware_time_with_dependency_for_small_page(self):
template = Template(
"""
template_str: types.django_html = """
{% load component_tags %}{% component_dependencies %}
{% component 'test_component' %}
{% slot "header" %}
@ -107,7 +114,7 @@ class RenderBenchmarks(BaseTestCase):
{% endslot %}
{% endcomponent %}
"""
)
template = Template(template_str)
# Sanity tests
response_content = create_and_process_template_response(template)
self.assertNotIn(CSS_DEPENDENCY_PLACEHOLDER, response_content)