refactor: remove middleware, add strategy "raw", and call render_deps() from within Template.render() (#1166)

* refactor: remove middleware, add strategy "raw", and call render_deps() from within Template.render()

* refactor: fix formatting

* refactor: fix benchmark tests

* refactor: avoid processing deps if rendered HTML contains no components

* refactor: remove comments

* refactor: rename "raw" to "ignore"
This commit is contained in:
Juro Oravec 2025-05-06 21:36:41 +02:00 committed by GitHub
parent 1049c08324
commit 6253042e9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 771 additions and 657 deletions

View file

@ -1,13 +1,8 @@
from pathlib import Path
from typing import Dict, Optional
from unittest.mock import Mock
import django
from django.conf import settings
from django.template import Context
from django.template.response import TemplateResponse
from django_components.middleware import ComponentDependencyMiddleware
# Common use case in our tests is to check that the component works in both
# "django" and "isolated" context behaviors. If you need only that, pass this
@ -22,28 +17,6 @@ PARAMETRIZE_CONTEXT_BEHAVIOR = (
)
# Create middleware instance
response_stash = None
middleware = ComponentDependencyMiddleware(get_response=lambda _: response_stash)
request = Mock()
mock_template = Mock()
def create_and_process_template_response(template, context=None, use_middleware=True):
context = context if context is not None else Context({})
mock_template.render = lambda context, _: template.render(context)
response = TemplateResponse(request, mock_template, context)
if use_middleware:
response.render()
global response_stash
response_stash = response
response = middleware(request)
else:
response.render()
return response.content.decode("utf-8")
def setup_test_config(
components: Optional[Dict] = None,
extra_settings: Optional[Dict] = None,
@ -72,7 +45,7 @@ def setup_test_config(
"template_cache_size": 128,
**(components or {}),
},
"MIDDLEWARE": ["django_components.middleware.ComponentDependencyMiddleware"],
"MIDDLEWARE": [],
"DATABASES": {
"default": {
"ENGINE": "django.db.backends.sqlite3",