test: test to illustrate bug in component caching when using include tag (#1135)

* test: test to illustrate bug in component caching when using include tag

* fix: add cleanup for render context in component rendering

* refactor: clarify cleanup comment in component rendering logic

* refactor: fix linter errors

* test: formatting and unnecessary test setup

---------

Co-authored-by: Juro Oravec <juraj.oravec.josefson@gmail.com>
This commit is contained in:
Oliver Haas 2025-04-20 11:53:06 +02:00 committed by GitHub
parent eed15d32ab
commit e0b718c314
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 1 deletions

View file

@ -1306,6 +1306,8 @@ class Component(
# The component rendering was short-circuited by an extension, skipping
# the rest of the rendering process. This may be for example a cached content.
if result_override is not None:
# Cleanup needs to be done even if short-circuited
context.render_context.pop()
return result_override
# We pass down the components the info about the component's parent.

View file

@ -0,0 +1,4 @@
{% include "test_cached_component_inside_include_sub.html" %}
{% block content %}
THIS_IS_IN_BASE_TEMPLATE_SO_SHOULD_BE_OVERRIDDEN
{% endblock %}

View file

@ -0,0 +1 @@
{% component "test_component" / %}

View file

@ -2,8 +2,10 @@ import time
from typing import Any
from django.core.cache import caches
from django.template import Template
from django.template.context import Context
from django_components import Component
from django_components import Component, register
from django_components.testing import djc_test
from .testutils import setup_test_config
@ -207,3 +209,27 @@ class TestComponentCache:
# The key should use the custom hash methods
expected_key = "components:cache:TestComponent_28880f:custom-args-and-kwargs"
assert component.cache.get_cache_key(1, 2, key="value") == expected_key
def test_cached_component_inside_include(self):
@register("test_component")
class TestComponent(Component):
template = "Hello"
class Cache:
enabled = True
template = Template("""
{% extends "test_cached_component_inside_include_base.html" %}
{% block content %}
THIS_IS_IN_ACTUAL_TEMPLATE_SO_SHOULD_NOT_BE_OVERRIDDEN
{% endblock %}
""")
result = template.render(Context({}))
assert "THIS_IS_IN_BASE_TEMPLATE_SO_SHOULD_BE_OVERRIDDEN" not in result
assert "THIS_IS_IN_ACTUAL_TEMPLATE_SO_SHOULD_NOT_BE_OVERRIDDEN" in result
result_cached = template.render(Context({}))
assert "THIS_IS_IN_BASE_TEMPLATE_SO_SHOULD_BE_OVERRIDDEN" not in result_cached
assert "THIS_IS_IN_ACTUAL_TEMPLATE_SO_SHOULD_NOT_BE_OVERRIDDEN" in result_cached