django-components/tests/testutils.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

73 lines
2.2 KiB
Python

from pathlib import Path
from typing import Dict, Optional
import django
from django.conf import settings
# 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
# tuple to `djc_test` as the `parametrize` argument.
PARAMETRIZE_CONTEXT_BEHAVIOR = (
["components_settings"],
[
[{"context_behavior": "django"}],
[{"context_behavior": "isolated"}],
],
["django", "isolated"],
)
def setup_test_config(
components: Optional[Dict] = None,
extra_settings: Optional[Dict] = None,
):
if settings.configured:
return
default_settings = {
"BASE_DIR": Path(__file__).resolve().parent,
"INSTALLED_APPS": ("django_components", "tests.test_app"),
"TEMPLATES": [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
"tests/templates/",
"tests/components/", # Required for template relative imports in tests
],
"OPTIONS": {
"builtins": [
"django_components.templatetags.component_tags",
],
'loaders': [
# Default Django loader
'django.template.loaders.filesystem.Loader',
# Including this is the same as APP_DIRS=True
'django.template.loaders.app_directories.Loader',
# Components loader
'django_components.template_loader.Loader',
],
},
}
],
"COMPONENTS": {
**(components or {}),
},
"MIDDLEWARE": [],
"DATABASES": {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
},
"SECRET_KEY": "secret",
"ROOT_URLCONF": "django_components.urls",
}
settings.configure(
**{
**default_settings,
**(extra_settings or {}),
}
)
django.setup()