asserts.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """
  2. Dynamically load all Django assertion cases and expose them for importing.
  3. """
  4. from functools import wraps
  5. from typing import Any, Callable, Optional, Sequence, Set, Union
  6. from django.test import (
  7. LiveServerTestCase, SimpleTestCase, TestCase, TransactionTestCase,
  8. )
  9. TYPE_CHECKING = False
  10. test_case = TestCase("run")
  11. def _wrapper(name: str):
  12. func = getattr(test_case, name)
  13. @wraps(func)
  14. def assertion_func(*args, **kwargs):
  15. return func(*args, **kwargs)
  16. return assertion_func
  17. __all__ = []
  18. assertions_names = set() # type: Set[str]
  19. assertions_names.update(
  20. {attr for attr in vars(TestCase) if attr.startswith("assert")},
  21. {attr for attr in vars(SimpleTestCase) if attr.startswith("assert")},
  22. {attr for attr in vars(LiveServerTestCase) if attr.startswith("assert")},
  23. {attr for attr in vars(TransactionTestCase) if attr.startswith("assert")},
  24. )
  25. for assert_func in assertions_names:
  26. globals()[assert_func] = _wrapper(assert_func)
  27. __all__.append(assert_func)
  28. if TYPE_CHECKING:
  29. from django.http import HttpResponse
  30. def assertRedirects(
  31. response: HttpResponse,
  32. expected_url: str,
  33. status_code: int = ...,
  34. target_status_code: int = ...,
  35. msg_prefix: str = ...,
  36. fetch_redirect_response: bool = ...,
  37. ) -> None:
  38. ...
  39. def assertURLEqual(
  40. url1: str,
  41. url2: str,
  42. msg_prefix: str = ...,
  43. ) -> None:
  44. ...
  45. def assertContains(
  46. response: HttpResponse,
  47. text: object,
  48. count: Optional[int] = ...,
  49. status_code: int = ...,
  50. msg_prefix: str = ...,
  51. html: bool = False,
  52. ) -> None:
  53. ...
  54. def assertNotContains(
  55. response: HttpResponse,
  56. text: object,
  57. status_code: int = ...,
  58. msg_prefix: str = ...,
  59. html: bool = False,
  60. ) -> None:
  61. ...
  62. def assertFormError(
  63. response: HttpResponse,
  64. form: str,
  65. field: Optional[str],
  66. errors: Union[str, Sequence[str]],
  67. msg_prefix: str = ...,
  68. ) -> None:
  69. ...
  70. def assertFormsetError(
  71. response: HttpResponse,
  72. formset: str,
  73. form_index: Optional[int],
  74. field: Optional[str],
  75. errors: Union[str, Sequence[str]],
  76. msg_prefix: str = ...,
  77. ) -> None:
  78. ...
  79. def assertTemplateUsed(
  80. response: Optional[HttpResponse] = ...,
  81. template_name: Optional[str] = ...,
  82. msg_prefix: str = ...,
  83. count: Optional[int] = ...,
  84. ):
  85. ...
  86. def assertTemplateNotUsed(
  87. response: Optional[HttpResponse] = ...,
  88. template_name: Optional[str] = ...,
  89. msg_prefix: str = ...,
  90. ):
  91. ...
  92. def assertRaisesMessage(
  93. expected_exception: BaseException,
  94. expected_message: str,
  95. *args,
  96. **kwargs
  97. ):
  98. ...
  99. def assertWarnsMessage(
  100. expected_warning: Warning,
  101. expected_message: str,
  102. *args,
  103. **kwargs
  104. ):
  105. ...
  106. def assertFieldOutput(
  107. fieldclass,
  108. valid,
  109. invalid,
  110. field_args=...,
  111. field_kwargs=...,
  112. empty_value: str = ...,
  113. ) -> None:
  114. ...
  115. def assertHTMLEqual(
  116. html1: str,
  117. html2: str,
  118. msg: Optional[str] = ...,
  119. ) -> None:
  120. ...
  121. def assertHTMLNotEqual(
  122. html1: str,
  123. html2: str,
  124. msg: Optional[str] = ...,
  125. ) -> None:
  126. ...
  127. def assertInHTML(
  128. needle: str,
  129. haystack: str,
  130. count: Optional[int] = ...,
  131. msg_prefix: str = ...,
  132. ) -> None:
  133. ...
  134. def assertJSONEqual(
  135. raw: str,
  136. expected_data: Any,
  137. msg: Optional[str] = ...,
  138. ) -> None:
  139. ...
  140. def assertJSONNotEqual(
  141. raw: str,
  142. expected_data: Any,
  143. msg: Optional[str] = ...,
  144. ) -> None:
  145. ...
  146. def assertXMLEqual(
  147. xml1: str,
  148. xml2: str,
  149. msg: Optional[str] = ...,
  150. ) -> None:
  151. ...
  152. def assertXMLNotEqual(
  153. xml1: str,
  154. xml2: str,
  155. msg: Optional[str] = ...,
  156. ) -> None:
  157. ...
  158. def assertQuerysetEqual(
  159. qs,
  160. values,
  161. transform=...,
  162. ordered: bool = ...,
  163. msg: Optional[str] = ...,
  164. ) -> None:
  165. ...
  166. def assertNumQueries(
  167. num: int,
  168. func=...,
  169. *args,
  170. using: str = ...,
  171. **kwargs
  172. ):
  173. ...
  174. # Fallback in case Django adds new asserts.
  175. def __getattr__(name: str) -> Callable[..., Any]:
  176. ...