1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
"""
Dynamically load all Django assertion cases and expose them for importing.
"""
from __future__ import annotations
from functools import wraps
from typing import TYPE_CHECKING, Any, Callable, Sequence
from django import VERSION
from django.test import LiveServerTestCase, SimpleTestCase, TestCase, TransactionTestCase
USE_CONTRIB_MESSAGES = VERSION >= (5, 0)
if USE_CONTRIB_MESSAGES:
from django.contrib.messages import Message
from django.contrib.messages.test import MessagesTestMixin
class MessagesTestCase(MessagesTestMixin, TestCase):
pass
test_case = MessagesTestCase("run")
else:
test_case = TestCase("run")
def _wrapper(name: str):
func = getattr(test_case, name)
@wraps(func)
def assertion_func(*args, **kwargs):
return func(*args, **kwargs)
return assertion_func
__all__ = []
assertions_names: set[str] = set()
assertions_names.update(
{attr for attr in vars(TestCase) if attr.startswith("assert")},
{attr for attr in vars(SimpleTestCase) if attr.startswith("assert")},
{attr for attr in vars(LiveServerTestCase) if attr.startswith("assert")},
{attr for attr in vars(TransactionTestCase) if attr.startswith("assert")},
)
if USE_CONTRIB_MESSAGES:
assertions_names.update(
{attr for attr in vars(MessagesTestMixin) if attr.startswith("assert")},
)
for assert_func in assertions_names:
globals()[assert_func] = _wrapper(assert_func)
__all__.append(assert_func) # noqa: PYI056
if TYPE_CHECKING:
from django import forms
from django.http.response import HttpResponseBase
def assertRedirects(
response: HttpResponseBase,
expected_url: str,
status_code: int = ...,
target_status_code: int = ...,
msg_prefix: str = ...,
fetch_redirect_response: bool = ...,
) -> None: ...
def assertURLEqual(
url1: str,
url2: str,
msg_prefix: str = ...,
) -> None: ...
def assertContains(
response: HttpResponseBase,
text: object,
count: int | None = ...,
status_code: int = ...,
msg_prefix: str = ...,
html: bool = False,
) -> None: ...
def assertNotContains(
response: HttpResponseBase,
text: object,
status_code: int = ...,
msg_prefix: str = ...,
html: bool = False,
) -> None: ...
def assertFormError(
form: forms.BaseForm,
field: str | None,
errors: str | Sequence[str],
msg_prefix: str = ...,
) -> None: ...
def assertFormSetError(
formset: forms.BaseFormSet,
form_index: int | None,
field: str | None,
errors: str | Sequence[str],
msg_prefix: str = ...,
) -> None: ...
def assertTemplateUsed(
response: HttpResponseBase | str | None = ...,
template_name: str | None = ...,
msg_prefix: str = ...,
count: int | None = ...,
): ...
def assertTemplateNotUsed(
response: HttpResponseBase | str | None = ...,
template_name: str | None = ...,
msg_prefix: str = ...,
): ...
def assertRaisesMessage(
expected_exception: type[Exception],
expected_message: str,
*args,
**kwargs,
): ...
def assertWarnsMessage(
expected_warning: Warning,
expected_message: str,
*args,
**kwargs,
): ...
def assertFieldOutput(
fieldclass,
valid,
invalid,
field_args=...,
field_kwargs=...,
empty_value: str = ...,
) -> None: ...
def assertHTMLEqual(
html1: str,
html2: str,
msg: str | None = ...,
) -> None: ...
def assertHTMLNotEqual(
html1: str,
html2: str,
msg: str | None = ...,
) -> None: ...
def assertInHTML(
needle: str,
haystack: str,
count: int | None = ...,
msg_prefix: str = ...,
) -> None: ...
def assertJSONEqual(
raw: str,
expected_data: Any,
msg: str | None = ...,
) -> None: ...
def assertJSONNotEqual(
raw: str,
expected_data: Any,
msg: str | None = ...,
) -> None: ...
def assertXMLEqual(
xml1: str,
xml2: str,
msg: str | None = ...,
) -> None: ...
def assertXMLNotEqual(
xml1: str,
xml2: str,
msg: str | None = ...,
) -> None: ...
# Removed in Django 5.1: use assertQuerySetEqual.
def assertQuerysetEqual(
qs,
values,
transform=...,
ordered: bool = ...,
msg: str | None = ...,
) -> None: ...
def assertQuerySetEqual(
qs,
values,
transform=...,
ordered: bool = ...,
msg: str | None = ...,
) -> None: ...
def assertNumQueries(
num: int,
func=...,
*args,
using: str = ...,
**kwargs,
): ...
# Added in Django 5.0.
def assertMessages(
response: HttpResponseBase,
expected_messages: Sequence[Message],
*args,
ordered: bool = ...,
) -> None: ...
# Fallback in case Django adds new asserts.
def __getattr__(name: str) -> Callable[..., Any]: ...
|