django-components/tests/test_template.py
Juro Oravec 8677ee7941
refactor: deprecate template caching, get_template_name, get_template, assoc template with Comp cls (#1222)
* refactor: deprecate template caching, get_template_name, get_template, assoc template with Comp cls

* refactor: change implementation

* refactor: handle cached template loader

* refactor: fix tests

* refactor: fix test on windows

* refactor: try to  fix type errors

* refactor: Re-cast `context` to fix type errors

* refactor: fix linter error

* refactor: fix typing

* refactor: more linter fixes

* refactor: more linter errors

* refactor: revert extra node metadata
2025-06-01 19:20:22 +02:00

53 lines
1.9 KiB
Python

from django.template import Template
from django_components import Component, cached_template, types
from django_components.template import _get_component_template
from django_components.testing import djc_test
from .testutils import setup_test_config
setup_test_config({"autodiscover": False})
@djc_test
class TestTemplateCache:
# TODO_v1 - Remove
def test_cached_template(self):
template_1 = cached_template("Variable: <strong>{{ variable }}</strong>")
template_1._test_id = "123"
template_2 = cached_template("Variable: <strong>{{ variable }}</strong>")
assert template_2._test_id == "123"
# TODO_v1 - Remove
def test_cached_template_accepts_class(self):
class MyTemplate(Template):
pass
template = cached_template("Variable: <strong>{{ variable }}</strong>", MyTemplate)
assert isinstance(template, MyTemplate)
# TODO_v1 - Move to `test_component.py`. While `cached_template()` will be removed,
# we will internally still cache templates by class, and we will want to test for that.
def test_component_template_is_cached(self):
class SimpleComponent(Component):
def get_template(self, context):
content: types.django_html = """
Variable: <strong>{{ variable }}</strong>
"""
return content
def get_template_data(self, args, kwargs, slots, context):
return {
"variable": kwargs.get("variable", None),
}
comp = SimpleComponent(kwargs={"variable": "test"})
# Check that we get the same template instance
template_1 = _get_component_template(comp)
template_1._test_id = "123" # type: ignore[union-attr]
template_2 = _get_component_template(comp)
assert template_2._test_id == "123" # type: ignore[union-attr]