mirror of
https://github.com/django-components/django-components.git
synced 2025-09-18 11:49:44 +00:00
fix djc_test is not happy with ComponentsSettings (#1370)
Co-authored-by: Juro Oravec <juraj.oravec.josefson@gmail.com>
This commit is contained in:
parent
c5354baf4f
commit
fd2b0128f0
1 changed files with 40 additions and 13 deletions
|
@ -2,7 +2,21 @@ import gc
|
||||||
import inspect
|
import inspect
|
||||||
import sys
|
import sys
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Sequence, Set, Tuple, Type, Union
|
from typing import (
|
||||||
|
TYPE_CHECKING,
|
||||||
|
Any,
|
||||||
|
Callable,
|
||||||
|
Dict,
|
||||||
|
Iterable,
|
||||||
|
List,
|
||||||
|
Mapping,
|
||||||
|
Optional,
|
||||||
|
Sequence,
|
||||||
|
Set,
|
||||||
|
Tuple,
|
||||||
|
Type,
|
||||||
|
Union,
|
||||||
|
)
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from weakref import ReferenceType
|
from weakref import ReferenceType
|
||||||
|
|
||||||
|
@ -13,6 +27,7 @@ from django.template import engines
|
||||||
from django.template.loaders.base import Loader
|
from django.template.loaders.base import Loader
|
||||||
from django.test import override_settings
|
from django.test import override_settings
|
||||||
|
|
||||||
|
from django_components import ComponentsSettings
|
||||||
from django_components.component import ALL_COMPONENTS, Component, component_node_subclasses_by_name
|
from django_components.component import ALL_COMPONENTS, Component, component_node_subclasses_by_name
|
||||||
from django_components.component_registry import ALL_REGISTRIES, ComponentRegistry
|
from django_components.component_registry import ALL_REGISTRIES, ComponentRegistry
|
||||||
from django_components.extension import extensions
|
from django_components.extension import extensions
|
||||||
|
@ -405,24 +420,36 @@ def djc_test(
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
# Merge settings such that the fields in the `COMPONENTS` setting are merged.
|
|
||||||
def _merge_django_settings(
|
def _merge_django_settings(
|
||||||
django_settings: Optional[Dict] = None,
|
django_settings: Optional[Mapping[str, Any]] = None,
|
||||||
components_settings: Optional[Dict] = None,
|
components_settings: Optional[Union[Mapping[str, Any], "ComponentsSettings"]] = None,
|
||||||
) -> Dict:
|
) -> Dict[str, Any]:
|
||||||
merged_settings = {} if not django_settings else django_settings.copy()
|
"""
|
||||||
|
Merge settings such that the fields in the `COMPONENTS` setting are merged.
|
||||||
|
Use components_settings to override fields in the django COMPONENTS setting.
|
||||||
|
"""
|
||||||
|
merged_settings: Dict[str, Any] = dict(django_settings or {})
|
||||||
|
|
||||||
merged_settings["COMPONENTS"] = {
|
defaults = _components_to_mapping(_django_settings.COMPONENTS if _django_settings.configured else {})
|
||||||
# Use the Django settings as they were before the `override_settings`
|
current = _components_to_mapping(merged_settings.get("COMPONENTS"))
|
||||||
# as the defaults.
|
overrides = _components_to_mapping(components_settings)
|
||||||
**(_django_settings.COMPONENTS if _django_settings.configured else {}),
|
|
||||||
**merged_settings.get("COMPONENTS", {}),
|
|
||||||
**(components_settings or {}),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
merged_settings["COMPONENTS"] = {**defaults, **current, **overrides}
|
||||||
return merged_settings
|
return merged_settings
|
||||||
|
|
||||||
|
|
||||||
|
def _components_to_mapping(
|
||||||
|
value: Optional[Union[Mapping[str, Any], "ComponentsSettings"]],
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
if value is None:
|
||||||
|
return {}
|
||||||
|
if isinstance(value, Mapping):
|
||||||
|
return dict(value)
|
||||||
|
if isinstance(value, ComponentsSettings):
|
||||||
|
return dict(value._asdict())
|
||||||
|
raise TypeError("COMPONENTS must be a mapping or ComponentsSettings")
|
||||||
|
|
||||||
|
|
||||||
def _setup_djc_global_state(
|
def _setup_djc_global_state(
|
||||||
gen_id_patcher: GenIdPatcher,
|
gen_id_patcher: GenIdPatcher,
|
||||||
csrf_token_patcher: CsrfTokenPatcher,
|
csrf_token_patcher: CsrfTokenPatcher,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue