0
# Settings Management
1
2
Django settings configuration and temporary modification fixtures. The settings fixture allows temporary modification of Django settings that are automatically restored after test completion.
3
4
## Capabilities
5
6
### Settings Wrapper
7
8
Fixture providing temporary Django settings modification with automatic restoration.
9
10
```python { .api }
11
def settings() -> SettingsWrapper:
12
"""
13
Django settings wrapper for temporary modification.
14
15
Provides a wrapper around Django settings that allows temporary
16
modification of settings values. Changes are automatically reverted
17
after test completion. Supports both attribute access and context
18
manager usage.
19
20
Returns:
21
SettingsWrapper: Settings wrapper with modification capabilities
22
"""
23
24
class SettingsWrapper:
25
"""Django settings wrapper with context manager support."""
26
27
def __setattr__(self, name: str, value: Any) -> None:
28
"""
29
Set a Django setting temporarily.
30
31
Args:
32
name: Setting name (e.g., 'DEBUG', 'SECRET_KEY')
33
value: Setting value
34
"""
35
36
def __getattr__(self, name: str) -> Any:
37
"""
38
Get a Django setting value.
39
40
Args:
41
name: Setting name
42
43
Returns:
44
Any: Setting value
45
"""
46
47
def __enter__(self) -> SettingsWrapper:
48
"""Context manager entry."""
49
50
def __exit__(self, exc_type, exc_value, traceback) -> None:
51
"""Context manager exit with automatic restoration."""
52
```
53
54
Usage examples:
55
56
```python
57
# Direct attribute modification
58
def test_with_debug_enabled(settings):
59
settings.DEBUG = True
60
settings.SECRET_KEY = "test-secret-key"
61
settings.ALLOWED_HOSTS = ["testserver", "localhost"]
62
63
# Test code that depends on these settings
64
assert settings.DEBUG is True
65
# Settings automatically restored after test
66
67
# Context manager usage
68
def test_with_context_manager(settings):
69
original_debug = settings.DEBUG
70
71
with settings:
72
settings.DEBUG = False
73
settings.USE_I18N = False
74
# Test code with modified settings
75
assert settings.DEBUG is False
76
77
# Settings restored to original values
78
assert settings.DEBUG == original_debug
79
80
# Testing cache configuration
81
def test_cache_settings(settings):
82
settings.CACHES = {
83
'default': {
84
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
85
'LOCATION': 'test-cache',
86
}
87
}
88
# Test cache-dependent functionality
89
90
# Testing database configuration
91
def test_custom_database(settings):
92
settings.DATABASES = {
93
'default': {
94
'ENGINE': 'django.db.backends.sqlite3',
95
'NAME': ':memory:',
96
}
97
}
98
# Test with custom database settings
99
100
# Testing middleware modification
101
def test_custom_middleware(settings):
102
settings.MIDDLEWARE = [
103
'django.middleware.security.SecurityMiddleware',
104
'django.contrib.sessions.middleware.SessionMiddleware',
105
'myapp.middleware.CustomMiddleware',
106
]
107
# Test with custom middleware stack
108
109
# Testing email configuration
110
def test_email_settings(settings):
111
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
112
settings.DEFAULT_FROM_EMAIL = 'test@example.com'
113
# Test email functionality
114
```
115
116
## Settings Types
117
118
```python { .api }
119
from typing import Any, Dict, List, Optional, Union
120
from types import ModuleType
121
122
# Common Django setting types
123
DatabaseConfig = Dict[str, Union[str, Dict[str, Any]]]
124
CacheConfig = Dict[str, Dict[str, Any]]
125
MiddlewareList = List[str]
126
AppsList = List[str]
127
EmailBackend = str
128
129
# Settings wrapper implementation
130
class SettingsWrapper:
131
"""Wrapper for Django settings with temporary modification support."""
132
133
# Common Django settings with type hints
134
DEBUG: bool
135
SECRET_KEY: str
136
ALLOWED_HOSTS: List[str]
137
INSTALLED_APPS: List[str]
138
MIDDLEWARE: List[str]
139
ROOT_URLCONF: str
140
DATABASES: Dict[str, DatabaseConfig]
141
CACHES: Dict[str, CacheConfig]
142
USE_I18N: bool
143
USE_L10N: bool
144
USE_TZ: bool
145
TIME_ZONE: str
146
LANGUAGE_CODE: str
147
STATIC_URL: str
148
MEDIA_URL: str
149
STATIC_ROOT: Optional[str]
150
MEDIA_ROOT: Optional[str]
151
EMAIL_BACKEND: str
152
DEFAULT_FROM_EMAIL: str
153
LOGGING: Dict[str, Any]
154
155
# Authentication settings
156
AUTH_USER_MODEL: str
157
LOGIN_URL: str
158
LOGIN_REDIRECT_URL: str
159
LOGOUT_REDIRECT_URL: str
160
161
# Session settings
162
SESSION_ENGINE: str
163
SESSION_COOKIE_AGE: int
164
SESSION_COOKIE_NAME: str
165
166
# Security settings
167
CSRF_COOKIE_SECURE: bool
168
SESSION_COOKIE_SECURE: bool
169
SECURE_SSL_REDIRECT: bool
170
171
def __setitem__(self, key: str, value: Any) -> None: ...
172
def __getitem__(self, key: str) -> Any: ...
173
def __delattr__(self, name: str) -> None: ...
174
def __contains__(self, key: str) -> bool: ...
175
```