plugin.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. """A pytest plugin which helps testing Django applications
  2. This plugin handles creating and destroying the test environment and
  3. test database and provides some useful text fixtures.
  4. """
  5. import contextlib
  6. import inspect
  7. import os
  8. import pathlib
  9. import sys
  10. from functools import reduce
  11. from typing import Generator, List, Optional, Tuple, Union
  12. import pytest
  13. from .django_compat import is_django_unittest # noqa
  14. from .fixtures import _django_db_helper # noqa
  15. from .fixtures import _live_server_helper # noqa
  16. from .fixtures import admin_client # noqa
  17. from .fixtures import admin_user # noqa
  18. from .fixtures import async_client # noqa
  19. from .fixtures import async_rf # noqa
  20. from .fixtures import client # noqa
  21. from .fixtures import db # noqa
  22. from .fixtures import django_assert_max_num_queries # noqa
  23. from .fixtures import django_assert_num_queries # noqa
  24. from .fixtures import django_capture_on_commit_callbacks # noqa
  25. from .fixtures import django_db_createdb # noqa
  26. from .fixtures import django_db_keepdb # noqa
  27. from .fixtures import django_db_modify_db_settings # noqa
  28. from .fixtures import django_db_modify_db_settings_parallel_suffix # noqa
  29. from .fixtures import django_db_modify_db_settings_tox_suffix # noqa
  30. from .fixtures import django_db_modify_db_settings_xdist_suffix # noqa
  31. from .fixtures import django_db_reset_sequences # noqa
  32. from .fixtures import django_db_serialized_rollback # noqa
  33. from .fixtures import django_db_setup # noqa
  34. from .fixtures import django_db_use_migrations # noqa
  35. from .fixtures import django_user_model # noqa
  36. from .fixtures import django_username_field # noqa
  37. from .fixtures import live_server # noqa
  38. from .fixtures import rf # noqa
  39. from .fixtures import settings # noqa
  40. from .fixtures import transactional_db # noqa
  41. from .fixtures import validate_django_db
  42. from .lazy_django import django_settings_is_configured, skip_if_no_django
  43. TYPE_CHECKING = False
  44. if TYPE_CHECKING:
  45. from typing import ContextManager, NoReturn
  46. import django
  47. SETTINGS_MODULE_ENV = "DJANGO_SETTINGS_MODULE"
  48. CONFIGURATION_ENV = "DJANGO_CONFIGURATION"
  49. INVALID_TEMPLATE_VARS_ENV = "FAIL_INVALID_TEMPLATE_VARS"
  50. _report_header = []
  51. # ############### pytest hooks ################
  52. @pytest.hookimpl()
  53. def pytest_addoption(parser) -> None:
  54. group = parser.getgroup("django")
  55. group.addoption(
  56. "--reuse-db",
  57. action="store_true",
  58. dest="reuse_db",
  59. default=False,
  60. help="Re-use the testing database if it already exists, "
  61. "and do not remove it when the test finishes.",
  62. )
  63. group.addoption(
  64. "--create-db",
  65. action="store_true",
  66. dest="create_db",
  67. default=False,
  68. help="Re-create the database, even if it exists. This "
  69. "option can be used to override --reuse-db.",
  70. )
  71. group.addoption(
  72. "--ds",
  73. action="store",
  74. type=str,
  75. dest="ds",
  76. default=None,
  77. help="Set DJANGO_SETTINGS_MODULE.",
  78. )
  79. group.addoption(
  80. "--dc",
  81. action="store",
  82. type=str,
  83. dest="dc",
  84. default=None,
  85. help="Set DJANGO_CONFIGURATION.",
  86. )
  87. group.addoption(
  88. "--nomigrations",
  89. "--no-migrations",
  90. action="store_true",
  91. dest="nomigrations",
  92. default=False,
  93. help="Disable Django migrations on test setup",
  94. )
  95. group.addoption(
  96. "--migrations",
  97. action="store_false",
  98. dest="nomigrations",
  99. default=False,
  100. help="Enable Django migrations on test setup",
  101. )
  102. parser.addini(
  103. CONFIGURATION_ENV, "django-configurations class to use by pytest-django."
  104. )
  105. group.addoption(
  106. "--liveserver",
  107. default=None,
  108. help="Address and port for the live_server fixture.",
  109. )
  110. parser.addini(
  111. SETTINGS_MODULE_ENV, "Django settings module to use by pytest-django."
  112. )
  113. parser.addini(
  114. "django_find_project",
  115. "Automatically find and add a Django project to the " "Python path.",
  116. type="bool",
  117. default=True,
  118. )
  119. parser.addini(
  120. "django_debug_mode",
  121. "How to set the Django DEBUG setting (default `False`). "
  122. "Use `keep` to not override.",
  123. default="False",
  124. )
  125. group.addoption(
  126. "--fail-on-template-vars",
  127. action="store_true",
  128. dest="itv",
  129. default=False,
  130. help="Fail for invalid variables in templates.",
  131. )
  132. parser.addini(
  133. INVALID_TEMPLATE_VARS_ENV,
  134. "Fail for invalid variables in templates.",
  135. type="bool",
  136. default=False,
  137. )
  138. PROJECT_FOUND = (
  139. "pytest-django found a Django project in %s "
  140. "(it contains manage.py) and added it to the Python path.\n"
  141. 'If this is wrong, add "django_find_project = false" to '
  142. "pytest.ini and explicitly manage your Python path."
  143. )
  144. PROJECT_NOT_FOUND = (
  145. "pytest-django could not find a Django project "
  146. "(no manage.py file could be found). You must "
  147. "explicitly add your Django project to the Python path "
  148. "to have it picked up."
  149. )
  150. PROJECT_SCAN_DISABLED = (
  151. "pytest-django did not search for Django "
  152. "projects since it is disabled in the configuration "
  153. '("django_find_project = false")'
  154. )
  155. @contextlib.contextmanager
  156. def _handle_import_error(extra_message: str) -> Generator[None, None, None]:
  157. try:
  158. yield
  159. except ImportError as e:
  160. django_msg = (e.args[0] + "\n\n") if e.args else ""
  161. msg = django_msg + extra_message
  162. raise ImportError(msg)
  163. def _add_django_project_to_path(args) -> str:
  164. def is_django_project(path: pathlib.Path) -> bool:
  165. try:
  166. return path.is_dir() and (path / "manage.py").exists()
  167. except OSError:
  168. return False
  169. def arg_to_path(arg: str) -> pathlib.Path:
  170. # Test classes or functions can be appended to paths separated by ::
  171. arg = arg.split("::", 1)[0]
  172. return pathlib.Path(arg)
  173. def find_django_path(args) -> Optional[pathlib.Path]:
  174. str_args = (str(arg) for arg in args)
  175. path_args = [arg_to_path(x) for x in str_args if not x.startswith("-")]
  176. cwd = pathlib.Path.cwd()
  177. if not path_args:
  178. path_args.append(cwd)
  179. elif cwd not in path_args:
  180. path_args.append(cwd)
  181. for arg in path_args:
  182. if is_django_project(arg):
  183. return arg
  184. for parent in arg.parents:
  185. if is_django_project(parent):
  186. return parent
  187. return None
  188. project_dir = find_django_path(args)
  189. if project_dir:
  190. sys.path.insert(0, str(project_dir.absolute()))
  191. return PROJECT_FOUND % project_dir
  192. return PROJECT_NOT_FOUND
  193. def _setup_django() -> None:
  194. if "django" not in sys.modules:
  195. return
  196. import django.conf
  197. # Avoid force-loading Django when settings are not properly configured.
  198. if not django.conf.settings.configured:
  199. return
  200. import django.apps
  201. if not django.apps.apps.ready:
  202. django.setup()
  203. _blocking_manager.block()
  204. def _get_boolean_value(
  205. x: Union[None, bool, str],
  206. name: str,
  207. default: Optional[bool] = None,
  208. ) -> bool:
  209. if x is None:
  210. return bool(default)
  211. if isinstance(x, bool):
  212. return x
  213. possible_values = {"true": True, "false": False, "1": True, "0": False}
  214. try:
  215. return possible_values[x.lower()]
  216. except KeyError:
  217. raise ValueError(
  218. "{} is not a valid value for {}. "
  219. "It must be one of {}.".format(x, name, ", ".join(possible_values.keys()))
  220. )
  221. @pytest.hookimpl()
  222. def pytest_load_initial_conftests(
  223. early_config,
  224. parser,
  225. args: List[str],
  226. ) -> None:
  227. # Register the marks
  228. early_config.addinivalue_line(
  229. "markers",
  230. "django_db(transaction=False, reset_sequences=False, databases=None, "
  231. "serialized_rollback=False): "
  232. "Mark the test as using the Django test database. "
  233. "The *transaction* argument allows you to use real transactions "
  234. "in the test like Django's TransactionTestCase. "
  235. "The *reset_sequences* argument resets database sequences before "
  236. "the test. "
  237. "The *databases* argument sets which database aliases the test "
  238. "uses (by default, only 'default'). Use '__all__' for all databases. "
  239. "The *serialized_rollback* argument enables rollback emulation for "
  240. "the test.",
  241. )
  242. early_config.addinivalue_line(
  243. "markers",
  244. "urls(modstr): Use a different URLconf for this test, similar to "
  245. "the `urls` attribute of Django's `TestCase` objects. *modstr* is "
  246. "a string specifying the module of a URL config, e.g. "
  247. '"my_app.test_urls".',
  248. )
  249. early_config.addinivalue_line(
  250. "markers",
  251. "ignore_template_errors(): ignore errors from invalid template "
  252. "variables (if --fail-on-template-vars is used).",
  253. )
  254. options = parser.parse_known_args(args)
  255. if options.version or options.help:
  256. return
  257. django_find_project = _get_boolean_value(
  258. early_config.getini("django_find_project"), "django_find_project"
  259. )
  260. if django_find_project:
  261. _django_project_scan_outcome = _add_django_project_to_path(args)
  262. else:
  263. _django_project_scan_outcome = PROJECT_SCAN_DISABLED
  264. if (
  265. options.itv
  266. or _get_boolean_value(
  267. os.environ.get(INVALID_TEMPLATE_VARS_ENV), INVALID_TEMPLATE_VARS_ENV
  268. )
  269. or early_config.getini(INVALID_TEMPLATE_VARS_ENV)
  270. ):
  271. os.environ[INVALID_TEMPLATE_VARS_ENV] = "true"
  272. def _get_option_with_source(
  273. option: Optional[str],
  274. envname: str,
  275. ) -> Union[Tuple[str, str], Tuple[None, None]]:
  276. if option:
  277. return option, "option"
  278. if envname in os.environ:
  279. return os.environ[envname], "env"
  280. cfgval = early_config.getini(envname)
  281. if cfgval:
  282. return cfgval, "ini"
  283. return None, None
  284. ds, ds_source = _get_option_with_source(options.ds, SETTINGS_MODULE_ENV)
  285. dc, dc_source = _get_option_with_source(options.dc, CONFIGURATION_ENV)
  286. if ds:
  287. _report_header.append("settings: {} (from {})".format(ds, ds_source))
  288. os.environ[SETTINGS_MODULE_ENV] = ds
  289. if dc:
  290. _report_header.append("configuration: {} (from {})".format(dc, dc_source))
  291. os.environ[CONFIGURATION_ENV] = dc
  292. # Install the django-configurations importer
  293. import configurations.importer
  294. configurations.importer.install()
  295. # Forcefully load Django settings, throws ImportError or
  296. # ImproperlyConfigured if settings cannot be loaded.
  297. from django.conf import settings as dj_settings
  298. with _handle_import_error(_django_project_scan_outcome):
  299. dj_settings.DATABASES
  300. _setup_django()
  301. @pytest.hookimpl()
  302. def pytest_report_header() -> Optional[List[str]]:
  303. if _report_header:
  304. return ["django: " + ", ".join(_report_header)]
  305. return None
  306. @pytest.hookimpl(trylast=True)
  307. def pytest_configure() -> None:
  308. # Allow Django settings to be configured in a user pytest_configure call,
  309. # but make sure we call django.setup()
  310. _setup_django()
  311. @pytest.hookimpl(tryfirst=True)
  312. def pytest_collection_modifyitems(items: List[pytest.Item]) -> None:
  313. # If Django is not configured we don't need to bother
  314. if not django_settings_is_configured():
  315. return
  316. from django.test import TestCase, TransactionTestCase
  317. def get_order_number(test: pytest.Item) -> int:
  318. test_cls = getattr(test, "cls", None)
  319. if test_cls and issubclass(test_cls, TransactionTestCase):
  320. # Note, TestCase is a subclass of TransactionTestCase.
  321. uses_db = True
  322. transactional = not issubclass(test_cls, TestCase)
  323. else:
  324. marker_db = test.get_closest_marker("django_db")
  325. if marker_db:
  326. (
  327. transaction,
  328. reset_sequences,
  329. databases,
  330. serialized_rollback,
  331. ) = validate_django_db(marker_db)
  332. uses_db = True
  333. transactional = transaction or reset_sequences
  334. else:
  335. uses_db = False
  336. transactional = False
  337. fixtures = getattr(test, "fixturenames", [])
  338. transactional = transactional or "transactional_db" in fixtures
  339. uses_db = uses_db or "db" in fixtures
  340. if transactional:
  341. return 1
  342. elif uses_db:
  343. return 0
  344. else:
  345. return 2
  346. items.sort(key=get_order_number)
  347. @pytest.fixture(autouse=True, scope="session")
  348. def django_test_environment(request) -> None:
  349. """
  350. Ensure that Django is loaded and has its testing environment setup.
  351. XXX It is a little dodgy that this is an autouse fixture. Perhaps
  352. an email fixture should be requested in order to be able to
  353. use the Django email machinery just like you need to request a
  354. db fixture for access to the Django database, etc. But
  355. without duplicating a lot more of Django's test support code
  356. we need to follow this model.
  357. """
  358. if django_settings_is_configured():
  359. _setup_django()
  360. from django.test.utils import (
  361. setup_test_environment, teardown_test_environment,
  362. )
  363. debug_ini = request.config.getini("django_debug_mode")
  364. if debug_ini == "keep":
  365. debug = None
  366. else:
  367. debug = _get_boolean_value(debug_ini, "django_debug_mode", False)
  368. setup_test_environment(debug=debug)
  369. request.addfinalizer(teardown_test_environment)
  370. @pytest.fixture(scope="session")
  371. def django_db_blocker() -> "Optional[_DatabaseBlocker]":
  372. """Wrapper around Django's database access.
  373. This object can be used to re-enable database access. This fixture is used
  374. internally in pytest-django to build the other fixtures and can be used for
  375. special database handling.
  376. The object is a context manager and provides the methods
  377. .unblock()/.block() and .restore() to temporarily enable database access.
  378. This is an advanced feature that is meant to be used to implement database
  379. fixtures.
  380. """
  381. if not django_settings_is_configured():
  382. return None
  383. return _blocking_manager
  384. @pytest.fixture(autouse=True)
  385. def _django_db_marker(request) -> None:
  386. """Implement the django_db marker, internal to pytest-django."""
  387. marker = request.node.get_closest_marker("django_db")
  388. if marker:
  389. request.getfixturevalue("_django_db_helper")
  390. @pytest.fixture(autouse=True, scope="class")
  391. def _django_setup_unittest(
  392. request,
  393. django_db_blocker: "_DatabaseBlocker",
  394. ) -> Generator[None, None, None]:
  395. """Setup a django unittest, internal to pytest-django."""
  396. if not django_settings_is_configured() or not is_django_unittest(request):
  397. yield
  398. return
  399. # Fix/patch pytest.
  400. # Before pytest 5.4: https://github.com/pytest-dev/pytest/issues/5991
  401. # After pytest 5.4: https://github.com/pytest-dev/pytest-django/issues/824
  402. from _pytest.unittest import TestCaseFunction
  403. original_runtest = TestCaseFunction.runtest
  404. def non_debugging_runtest(self) -> None:
  405. self._testcase(result=self)
  406. try:
  407. TestCaseFunction.runtest = non_debugging_runtest # type: ignore[assignment]
  408. request.getfixturevalue("django_db_setup")
  409. with django_db_blocker.unblock():
  410. yield
  411. finally:
  412. TestCaseFunction.runtest = original_runtest # type: ignore[assignment]
  413. @pytest.fixture(scope="function", autouse=True)
  414. def _dj_autoclear_mailbox() -> None:
  415. if not django_settings_is_configured():
  416. return
  417. from django.core import mail
  418. del mail.outbox[:]
  419. @pytest.fixture(scope="function")
  420. def mailoutbox(
  421. django_mail_patch_dns: None,
  422. _dj_autoclear_mailbox: None,
  423. ) -> "Optional[List[django.core.mail.EmailMessage]]":
  424. if not django_settings_is_configured():
  425. return None
  426. from django.core import mail
  427. return mail.outbox
  428. @pytest.fixture(scope="function")
  429. def django_mail_patch_dns(
  430. monkeypatch,
  431. django_mail_dnsname: str,
  432. ) -> None:
  433. from django.core import mail
  434. monkeypatch.setattr(mail.message, "DNS_NAME", django_mail_dnsname)
  435. @pytest.fixture(scope="function")
  436. def django_mail_dnsname() -> str:
  437. return "fake-tests.example.com"
  438. @pytest.fixture(autouse=True, scope="function")
  439. def _django_set_urlconf(request) -> None:
  440. """Apply the @pytest.mark.urls marker, internal to pytest-django."""
  441. marker = request.node.get_closest_marker("urls")
  442. if marker:
  443. skip_if_no_django()
  444. import django.conf
  445. from django.urls import clear_url_caches, set_urlconf
  446. urls = validate_urls(marker)
  447. original_urlconf = django.conf.settings.ROOT_URLCONF
  448. django.conf.settings.ROOT_URLCONF = urls
  449. clear_url_caches()
  450. set_urlconf(None)
  451. def restore() -> None:
  452. django.conf.settings.ROOT_URLCONF = original_urlconf
  453. # Copy the pattern from
  454. # https://github.com/django/django/blob/main/django/test/signals.py#L152
  455. clear_url_caches()
  456. set_urlconf(None)
  457. request.addfinalizer(restore)
  458. @pytest.fixture(autouse=True, scope="session")
  459. def _fail_for_invalid_template_variable():
  460. """Fixture that fails for invalid variables in templates.
  461. This fixture will fail each test that uses django template rendering
  462. should a template contain an invalid template variable.
  463. The fail message will include the name of the invalid variable and
  464. in most cases the template name.
  465. It does not raise an exception, but fails, as the stack trace doesn't
  466. offer any helpful information to debug.
  467. This behavior can be switched off using the marker:
  468. ``pytest.mark.ignore_template_errors``
  469. """
  470. class InvalidVarException:
  471. """Custom handler for invalid strings in templates."""
  472. def __init__(self) -> None:
  473. self.fail = True
  474. def __contains__(self, key: str) -> bool:
  475. return key == "%s"
  476. @staticmethod
  477. def _get_origin():
  478. stack = inspect.stack()
  479. # Try to use topmost `self.origin` first (Django 1.9+, and with
  480. # TEMPLATE_DEBUG)..
  481. for f in stack[2:]:
  482. func = f[3]
  483. if func == "render":
  484. frame = f[0]
  485. try:
  486. origin = frame.f_locals["self"].origin
  487. except (AttributeError, KeyError):
  488. continue
  489. if origin is not None:
  490. return origin
  491. from django.template import Template
  492. # finding the ``render`` needle in the stack
  493. frameinfo = reduce(
  494. lambda x, y: y[3] == "render" and "base.py" in y[1] and y or x, stack
  495. )
  496. # assert 0, stack
  497. frame = frameinfo[0]
  498. # finding only the frame locals in all frame members
  499. f_locals = reduce(
  500. lambda x, y: y[0] == "f_locals" and y or x, inspect.getmembers(frame)
  501. )[1]
  502. # ``django.template.base.Template``
  503. template = f_locals["self"]
  504. if isinstance(template, Template):
  505. return template.name
  506. def __mod__(self, var: str) -> str:
  507. origin = self._get_origin()
  508. if origin:
  509. msg = "Undefined template variable '{}' in '{}'".format(var, origin)
  510. else:
  511. msg = "Undefined template variable '%s'" % var
  512. if self.fail:
  513. pytest.fail(msg)
  514. else:
  515. return msg
  516. if (
  517. os.environ.get(INVALID_TEMPLATE_VARS_ENV, "false") == "true"
  518. and django_settings_is_configured()
  519. ):
  520. from django.conf import settings as dj_settings
  521. if dj_settings.TEMPLATES:
  522. dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = InvalidVarException()
  523. @pytest.fixture(autouse=True)
  524. def _template_string_if_invalid_marker(request) -> None:
  525. """Apply the @pytest.mark.ignore_template_errors marker,
  526. internal to pytest-django."""
  527. marker = request.keywords.get("ignore_template_errors", None)
  528. if os.environ.get(INVALID_TEMPLATE_VARS_ENV, "false") == "true":
  529. if marker and django_settings_is_configured():
  530. from django.conf import settings as dj_settings
  531. if dj_settings.TEMPLATES:
  532. dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"].fail = False
  533. @pytest.fixture(autouse=True, scope="function")
  534. def _django_clear_site_cache() -> None:
  535. """Clears ``django.contrib.sites.models.SITE_CACHE`` to avoid
  536. unexpected behavior with cached site objects.
  537. """
  538. if django_settings_is_configured():
  539. from django.conf import settings as dj_settings
  540. if "django.contrib.sites" in dj_settings.INSTALLED_APPS:
  541. from django.contrib.sites.models import Site
  542. Site.objects.clear_cache()
  543. # ############### Helper Functions ################
  544. class _DatabaseBlockerContextManager:
  545. def __init__(self, db_blocker) -> None:
  546. self._db_blocker = db_blocker
  547. def __enter__(self) -> None:
  548. pass
  549. def __exit__(self, exc_type, exc_value, traceback) -> None:
  550. self._db_blocker.restore()
  551. class _DatabaseBlocker:
  552. """Manager for django.db.backends.base.base.BaseDatabaseWrapper.
  553. This is the object returned by django_db_blocker.
  554. """
  555. def __init__(self):
  556. self._history = []
  557. self._real_ensure_connection = None
  558. @property
  559. def _dj_db_wrapper(self) -> "django.db.backends.base.base.BaseDatabaseWrapper":
  560. from django.db.backends.base.base import BaseDatabaseWrapper
  561. # The first time the _dj_db_wrapper is accessed, we will save a
  562. # reference to the real implementation.
  563. if self._real_ensure_connection is None:
  564. self._real_ensure_connection = BaseDatabaseWrapper.ensure_connection
  565. return BaseDatabaseWrapper
  566. def _save_active_wrapper(self) -> None:
  567. self._history.append(self._dj_db_wrapper.ensure_connection)
  568. def _blocking_wrapper(*args, **kwargs) -> "NoReturn":
  569. __tracebackhide__ = True
  570. __tracebackhide__ # Silence pyflakes
  571. raise RuntimeError(
  572. "Database access not allowed, "
  573. 'use the "django_db" mark, or the '
  574. '"db" or "transactional_db" fixtures to enable it.'
  575. )
  576. def unblock(self) -> "ContextManager[None]":
  577. """Enable access to the Django database."""
  578. self._save_active_wrapper()
  579. self._dj_db_wrapper.ensure_connection = self._real_ensure_connection
  580. return _DatabaseBlockerContextManager(self)
  581. def block(self) -> "ContextManager[None]":
  582. """Disable access to the Django database."""
  583. self._save_active_wrapper()
  584. self._dj_db_wrapper.ensure_connection = self._blocking_wrapper
  585. return _DatabaseBlockerContextManager(self)
  586. def restore(self) -> None:
  587. self._dj_db_wrapper.ensure_connection = self._history.pop()
  588. _blocking_manager = _DatabaseBlocker()
  589. def validate_urls(marker) -> List[str]:
  590. """Validate the urls marker.
  591. It checks the signature and creates the `urls` attribute on the
  592. marker which will have the correct value.
  593. """
  594. def apifun(urls: List[str]) -> List[str]:
  595. return urls
  596. return apifun(*marker.args, **marker.kwargs)