utils.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. import collections
  2. import gc
  3. import logging
  4. import os
  5. import re
  6. import sys
  7. import time
  8. import warnings
  9. from contextlib import contextmanager
  10. from functools import wraps
  11. from io import StringIO
  12. from itertools import chain
  13. from types import SimpleNamespace
  14. from unittest import TestCase, skipIf, skipUnless
  15. from xml.dom.minidom import Node, parseString
  16. from asgiref.sync import iscoroutinefunction
  17. from django.apps import apps
  18. from django.apps.registry import Apps
  19. from django.conf import UserSettingsHolder, settings
  20. from django.core import mail
  21. from django.core.exceptions import ImproperlyConfigured
  22. from django.core.signals import request_started, setting_changed
  23. from django.db import DEFAULT_DB_ALIAS, connections, reset_queries
  24. from django.db.models.options import Options
  25. from django.template import Template
  26. from django.test.signals import template_rendered
  27. from django.urls import get_script_prefix, set_script_prefix
  28. from django.utils.translation import deactivate
  29. from django.utils.version import PYPY
  30. try:
  31. import jinja2
  32. except ImportError:
  33. jinja2 = None
  34. __all__ = (
  35. "Approximate",
  36. "ContextList",
  37. "isolate_lru_cache",
  38. "garbage_collect",
  39. "get_runner",
  40. "CaptureQueriesContext",
  41. "ignore_warnings",
  42. "isolate_apps",
  43. "modify_settings",
  44. "override_settings",
  45. "override_system_checks",
  46. "tag",
  47. "requires_tz_support",
  48. "setup_databases",
  49. "setup_test_environment",
  50. "teardown_test_environment",
  51. )
  52. TZ_SUPPORT = hasattr(time, "tzset")
  53. class Approximate:
  54. def __init__(self, val, places=7):
  55. self.val = val
  56. self.places = places
  57. def __repr__(self):
  58. return repr(self.val)
  59. def __eq__(self, other):
  60. return self.val == other or round(abs(self.val - other), self.places) == 0
  61. class ContextList(list):
  62. """
  63. A wrapper that provides direct key access to context items contained
  64. in a list of context objects.
  65. """
  66. def __getitem__(self, key):
  67. if isinstance(key, str):
  68. for subcontext in self:
  69. if key in subcontext:
  70. return subcontext[key]
  71. raise KeyError(key)
  72. else:
  73. return super().__getitem__(key)
  74. def get(self, key, default=None):
  75. try:
  76. return self.__getitem__(key)
  77. except KeyError:
  78. return default
  79. def __contains__(self, key):
  80. try:
  81. self[key]
  82. except KeyError:
  83. return False
  84. return True
  85. def keys(self):
  86. """
  87. Flattened keys of subcontexts.
  88. """
  89. return set(chain.from_iterable(d for subcontext in self for d in subcontext))
  90. def instrumented_test_render(self, context):
  91. """
  92. An instrumented Template render method, providing a signal that can be
  93. intercepted by the test Client.
  94. """
  95. template_rendered.send(sender=self, template=self, context=context)
  96. return self.nodelist.render(context)
  97. class _TestState:
  98. pass
  99. def setup_test_environment(debug=None):
  100. """
  101. Perform global pre-test setup, such as installing the instrumented template
  102. renderer and setting the email backend to the locmem email backend.
  103. """
  104. if hasattr(_TestState, "saved_data"):
  105. # Executing this function twice would overwrite the saved values.
  106. raise RuntimeError(
  107. "setup_test_environment() was already called and can't be called "
  108. "again without first calling teardown_test_environment()."
  109. )
  110. if debug is None:
  111. debug = settings.DEBUG
  112. saved_data = SimpleNamespace()
  113. _TestState.saved_data = saved_data
  114. saved_data.allowed_hosts = settings.ALLOWED_HOSTS
  115. # Add the default host of the test client.
  116. settings.ALLOWED_HOSTS = [*settings.ALLOWED_HOSTS, "testserver"]
  117. saved_data.debug = settings.DEBUG
  118. settings.DEBUG = debug
  119. saved_data.email_backend = settings.EMAIL_BACKEND
  120. settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
  121. saved_data.template_render = Template._render
  122. Template._render = instrumented_test_render
  123. mail.outbox = []
  124. deactivate()
  125. def teardown_test_environment():
  126. """
  127. Perform any global post-test teardown, such as restoring the original
  128. template renderer and restoring the email sending functions.
  129. """
  130. saved_data = _TestState.saved_data
  131. settings.ALLOWED_HOSTS = saved_data.allowed_hosts
  132. settings.DEBUG = saved_data.debug
  133. settings.EMAIL_BACKEND = saved_data.email_backend
  134. Template._render = saved_data.template_render
  135. del _TestState.saved_data
  136. del mail.outbox
  137. def setup_databases(
  138. verbosity,
  139. interactive,
  140. *,
  141. time_keeper=None,
  142. keepdb=False,
  143. debug_sql=False,
  144. parallel=0,
  145. aliases=None,
  146. serialized_aliases=None,
  147. **kwargs,
  148. ):
  149. """Create the test databases."""
  150. if time_keeper is None:
  151. time_keeper = NullTimeKeeper()
  152. test_databases, mirrored_aliases = get_unique_databases_and_mirrors(aliases)
  153. old_names = []
  154. for db_name, aliases in test_databases.values():
  155. first_alias = None
  156. for alias in aliases:
  157. connection = connections[alias]
  158. old_names.append((connection, db_name, first_alias is None))
  159. # Actually create the database for the first connection
  160. if first_alias is None:
  161. first_alias = alias
  162. with time_keeper.timed(" Creating '%s'" % alias):
  163. serialize_alias = (
  164. serialized_aliases is None or alias in serialized_aliases
  165. )
  166. connection.creation.create_test_db(
  167. verbosity=verbosity,
  168. autoclobber=not interactive,
  169. keepdb=keepdb,
  170. serialize=serialize_alias,
  171. )
  172. if parallel > 1:
  173. for index in range(parallel):
  174. with time_keeper.timed(" Cloning '%s'" % alias):
  175. connection.creation.clone_test_db(
  176. suffix=str(index + 1),
  177. verbosity=verbosity,
  178. keepdb=keepdb,
  179. )
  180. # Configure all other connections as mirrors of the first one
  181. else:
  182. connections[alias].creation.set_as_test_mirror(
  183. connections[first_alias].settings_dict
  184. )
  185. # Configure the test mirrors.
  186. for alias, mirror_alias in mirrored_aliases.items():
  187. connections[alias].creation.set_as_test_mirror(
  188. connections[mirror_alias].settings_dict
  189. )
  190. if debug_sql:
  191. for alias in connections:
  192. connections[alias].force_debug_cursor = True
  193. return old_names
  194. def iter_test_cases(tests):
  195. """
  196. Return an iterator over a test suite's unittest.TestCase objects.
  197. The tests argument can also be an iterable of TestCase objects.
  198. """
  199. for test in tests:
  200. if isinstance(test, str):
  201. # Prevent an unfriendly RecursionError that can happen with
  202. # strings.
  203. raise TypeError(
  204. f"Test {test!r} must be a test case or test suite not string "
  205. f"(was found in {tests!r})."
  206. )
  207. if isinstance(test, TestCase):
  208. yield test
  209. else:
  210. # Otherwise, assume it is a test suite.
  211. yield from iter_test_cases(test)
  212. def dependency_ordered(test_databases, dependencies):
  213. """
  214. Reorder test_databases into an order that honors the dependencies
  215. described in TEST[DEPENDENCIES].
  216. """
  217. ordered_test_databases = []
  218. resolved_databases = set()
  219. # Maps db signature to dependencies of all its aliases
  220. dependencies_map = {}
  221. # Check that no database depends on its own alias
  222. for sig, (_, aliases) in test_databases:
  223. all_deps = set()
  224. for alias in aliases:
  225. all_deps.update(dependencies.get(alias, []))
  226. if not all_deps.isdisjoint(aliases):
  227. raise ImproperlyConfigured(
  228. "Circular dependency: databases %r depend on each other, "
  229. "but are aliases." % aliases
  230. )
  231. dependencies_map[sig] = all_deps
  232. while test_databases:
  233. changed = False
  234. deferred = []
  235. # Try to find a DB that has all its dependencies met
  236. for signature, (db_name, aliases) in test_databases:
  237. if dependencies_map[signature].issubset(resolved_databases):
  238. resolved_databases.update(aliases)
  239. ordered_test_databases.append((signature, (db_name, aliases)))
  240. changed = True
  241. else:
  242. deferred.append((signature, (db_name, aliases)))
  243. if not changed:
  244. raise ImproperlyConfigured("Circular dependency in TEST[DEPENDENCIES]")
  245. test_databases = deferred
  246. return ordered_test_databases
  247. def get_unique_databases_and_mirrors(aliases=None):
  248. """
  249. Figure out which databases actually need to be created.
  250. Deduplicate entries in DATABASES that correspond the same database or are
  251. configured as test mirrors.
  252. Return two values:
  253. - test_databases: ordered mapping of signatures to (name, list of aliases)
  254. where all aliases share the same underlying database.
  255. - mirrored_aliases: mapping of mirror aliases to original aliases.
  256. """
  257. if aliases is None:
  258. aliases = connections
  259. mirrored_aliases = {}
  260. test_databases = {}
  261. dependencies = {}
  262. default_sig = connections[DEFAULT_DB_ALIAS].creation.test_db_signature()
  263. for alias in connections:
  264. connection = connections[alias]
  265. test_settings = connection.settings_dict["TEST"]
  266. if test_settings["MIRROR"]:
  267. # If the database is marked as a test mirror, save the alias.
  268. mirrored_aliases[alias] = test_settings["MIRROR"]
  269. elif alias in aliases:
  270. # Store a tuple with DB parameters that uniquely identify it.
  271. # If we have two aliases with the same values for that tuple,
  272. # we only need to create the test database once.
  273. item = test_databases.setdefault(
  274. connection.creation.test_db_signature(),
  275. (connection.settings_dict["NAME"], []),
  276. )
  277. # The default database must be the first because data migrations
  278. # use the default alias by default.
  279. if alias == DEFAULT_DB_ALIAS:
  280. item[1].insert(0, alias)
  281. else:
  282. item[1].append(alias)
  283. if "DEPENDENCIES" in test_settings:
  284. dependencies[alias] = test_settings["DEPENDENCIES"]
  285. else:
  286. if (
  287. alias != DEFAULT_DB_ALIAS
  288. and connection.creation.test_db_signature() != default_sig
  289. ):
  290. dependencies[alias] = test_settings.get(
  291. "DEPENDENCIES", [DEFAULT_DB_ALIAS]
  292. )
  293. test_databases = dict(dependency_ordered(test_databases.items(), dependencies))
  294. return test_databases, mirrored_aliases
  295. def teardown_databases(old_config, verbosity, parallel=0, keepdb=False):
  296. """Destroy all the non-mirror databases."""
  297. for connection, old_name, destroy in old_config:
  298. if destroy:
  299. if parallel > 1:
  300. for index in range(parallel):
  301. connection.creation.destroy_test_db(
  302. suffix=str(index + 1),
  303. verbosity=verbosity,
  304. keepdb=keepdb,
  305. )
  306. connection.creation.destroy_test_db(old_name, verbosity, keepdb)
  307. def get_runner(settings, test_runner_class=None):
  308. test_runner_class = test_runner_class or settings.TEST_RUNNER
  309. test_path = test_runner_class.split(".")
  310. # Allow for relative paths
  311. if len(test_path) > 1:
  312. test_module_name = ".".join(test_path[:-1])
  313. else:
  314. test_module_name = "."
  315. test_module = __import__(test_module_name, {}, {}, test_path[-1])
  316. return getattr(test_module, test_path[-1])
  317. class TestContextDecorator:
  318. """
  319. A base class that can either be used as a context manager during tests
  320. or as a test function or unittest.TestCase subclass decorator to perform
  321. temporary alterations.
  322. `attr_name`: attribute assigned the return value of enable() if used as
  323. a class decorator.
  324. `kwarg_name`: keyword argument passing the return value of enable() if
  325. used as a function decorator.
  326. """
  327. def __init__(self, attr_name=None, kwarg_name=None):
  328. self.attr_name = attr_name
  329. self.kwarg_name = kwarg_name
  330. def enable(self):
  331. raise NotImplementedError
  332. def disable(self):
  333. raise NotImplementedError
  334. def __enter__(self):
  335. return self.enable()
  336. def __exit__(self, exc_type, exc_value, traceback):
  337. self.disable()
  338. def decorate_class(self, cls):
  339. if issubclass(cls, TestCase):
  340. decorated_setUp = cls.setUp
  341. def setUp(inner_self):
  342. context = self.enable()
  343. inner_self.addCleanup(self.disable)
  344. if self.attr_name:
  345. setattr(inner_self, self.attr_name, context)
  346. decorated_setUp(inner_self)
  347. cls.setUp = setUp
  348. return cls
  349. raise TypeError("Can only decorate subclasses of unittest.TestCase")
  350. def decorate_callable(self, func):
  351. if iscoroutinefunction(func):
  352. # If the inner function is an async function, we must execute async
  353. # as well so that the `with` statement executes at the right time.
  354. @wraps(func)
  355. async def inner(*args, **kwargs):
  356. with self as context:
  357. if self.kwarg_name:
  358. kwargs[self.kwarg_name] = context
  359. return await func(*args, **kwargs)
  360. else:
  361. @wraps(func)
  362. def inner(*args, **kwargs):
  363. with self as context:
  364. if self.kwarg_name:
  365. kwargs[self.kwarg_name] = context
  366. return func(*args, **kwargs)
  367. return inner
  368. def __call__(self, decorated):
  369. if isinstance(decorated, type):
  370. return self.decorate_class(decorated)
  371. elif callable(decorated):
  372. return self.decorate_callable(decorated)
  373. raise TypeError("Cannot decorate object of type %s" % type(decorated))
  374. class override_settings(TestContextDecorator):
  375. """
  376. Act as either a decorator or a context manager. If it's a decorator, take a
  377. function and return a wrapped function. If it's a contextmanager, use it
  378. with the ``with`` statement. In either event, entering/exiting are called
  379. before and after, respectively, the function/block is executed.
  380. """
  381. enable_exception = None
  382. def __init__(self, **kwargs):
  383. self.options = kwargs
  384. super().__init__()
  385. def enable(self):
  386. # Keep this code at the beginning to leave the settings unchanged
  387. # in case it raises an exception because INSTALLED_APPS is invalid.
  388. if "INSTALLED_APPS" in self.options:
  389. try:
  390. apps.set_installed_apps(self.options["INSTALLED_APPS"])
  391. except Exception:
  392. apps.unset_installed_apps()
  393. raise
  394. override = UserSettingsHolder(settings._wrapped)
  395. for key, new_value in self.options.items():
  396. setattr(override, key, new_value)
  397. self.wrapped = settings._wrapped
  398. settings._wrapped = override
  399. for key, new_value in self.options.items():
  400. try:
  401. setting_changed.send(
  402. sender=settings._wrapped.__class__,
  403. setting=key,
  404. value=new_value,
  405. enter=True,
  406. )
  407. except Exception as exc:
  408. self.enable_exception = exc
  409. self.disable()
  410. def disable(self):
  411. if "INSTALLED_APPS" in self.options:
  412. apps.unset_installed_apps()
  413. settings._wrapped = self.wrapped
  414. del self.wrapped
  415. responses = []
  416. for key in self.options:
  417. new_value = getattr(settings, key, None)
  418. responses_for_setting = setting_changed.send_robust(
  419. sender=settings._wrapped.__class__,
  420. setting=key,
  421. value=new_value,
  422. enter=False,
  423. )
  424. responses.extend(responses_for_setting)
  425. if self.enable_exception is not None:
  426. exc = self.enable_exception
  427. self.enable_exception = None
  428. raise exc
  429. for _, response in responses:
  430. if isinstance(response, Exception):
  431. raise response
  432. def save_options(self, test_func):
  433. if test_func._overridden_settings is None:
  434. test_func._overridden_settings = self.options
  435. else:
  436. # Duplicate dict to prevent subclasses from altering their parent.
  437. test_func._overridden_settings = {
  438. **test_func._overridden_settings,
  439. **self.options,
  440. }
  441. def decorate_class(self, cls):
  442. from django.test import SimpleTestCase
  443. if not issubclass(cls, SimpleTestCase):
  444. raise ValueError(
  445. "Only subclasses of Django SimpleTestCase can be decorated "
  446. "with override_settings"
  447. )
  448. self.save_options(cls)
  449. return cls
  450. class modify_settings(override_settings):
  451. """
  452. Like override_settings, but makes it possible to append, prepend, or remove
  453. items instead of redefining the entire list.
  454. """
  455. def __init__(self, *args, **kwargs):
  456. if args:
  457. # Hack used when instantiating from SimpleTestCase.setUpClass.
  458. assert not kwargs
  459. self.operations = args[0]
  460. else:
  461. assert not args
  462. self.operations = list(kwargs.items())
  463. super(override_settings, self).__init__()
  464. def save_options(self, test_func):
  465. if test_func._modified_settings is None:
  466. test_func._modified_settings = self.operations
  467. else:
  468. # Duplicate list to prevent subclasses from altering their parent.
  469. test_func._modified_settings = (
  470. list(test_func._modified_settings) + self.operations
  471. )
  472. def enable(self):
  473. self.options = {}
  474. for name, operations in self.operations:
  475. try:
  476. # When called from SimpleTestCase.setUpClass, values may be
  477. # overridden several times; cumulate changes.
  478. value = self.options[name]
  479. except KeyError:
  480. value = list(getattr(settings, name, []))
  481. for action, items in operations.items():
  482. # items may be a single value or an iterable.
  483. if isinstance(items, str):
  484. items = [items]
  485. if action == "append":
  486. value += [item for item in items if item not in value]
  487. elif action == "prepend":
  488. value = [item for item in items if item not in value] + value
  489. elif action == "remove":
  490. value = [item for item in value if item not in items]
  491. else:
  492. raise ValueError("Unsupported action: %s" % action)
  493. self.options[name] = value
  494. super().enable()
  495. class override_system_checks(TestContextDecorator):
  496. """
  497. Act as a decorator. Override list of registered system checks.
  498. Useful when you override `INSTALLED_APPS`, e.g. if you exclude `auth` app,
  499. you also need to exclude its system checks.
  500. """
  501. def __init__(self, new_checks, deployment_checks=None):
  502. from django.core.checks.registry import registry
  503. self.registry = registry
  504. self.new_checks = new_checks
  505. self.deployment_checks = deployment_checks
  506. super().__init__()
  507. def enable(self):
  508. self.old_checks = self.registry.registered_checks
  509. self.registry.registered_checks = set()
  510. for check in self.new_checks:
  511. self.registry.register(check, *getattr(check, "tags", ()))
  512. self.old_deployment_checks = self.registry.deployment_checks
  513. if self.deployment_checks is not None:
  514. self.registry.deployment_checks = set()
  515. for check in self.deployment_checks:
  516. self.registry.register(check, *getattr(check, "tags", ()), deploy=True)
  517. def disable(self):
  518. self.registry.registered_checks = self.old_checks
  519. self.registry.deployment_checks = self.old_deployment_checks
  520. def compare_xml(want, got):
  521. """
  522. Try to do a 'xml-comparison' of want and got. Plain string comparison
  523. doesn't always work because, for example, attribute ordering should not be
  524. important. Ignore comment nodes, processing instructions, document type
  525. node, and leading and trailing whitespaces.
  526. Based on https://github.com/lxml/lxml/blob/master/src/lxml/doctestcompare.py
  527. """
  528. _norm_whitespace_re = re.compile(r"[ \t\n][ \t\n]+")
  529. def norm_whitespace(v):
  530. return _norm_whitespace_re.sub(" ", v)
  531. def child_text(element):
  532. return "".join(
  533. c.data for c in element.childNodes if c.nodeType == Node.TEXT_NODE
  534. )
  535. def children(element):
  536. return [c for c in element.childNodes if c.nodeType == Node.ELEMENT_NODE]
  537. def norm_child_text(element):
  538. return norm_whitespace(child_text(element))
  539. def attrs_dict(element):
  540. return dict(element.attributes.items())
  541. def check_element(want_element, got_element):
  542. if want_element.tagName != got_element.tagName:
  543. return False
  544. if norm_child_text(want_element) != norm_child_text(got_element):
  545. return False
  546. if attrs_dict(want_element) != attrs_dict(got_element):
  547. return False
  548. want_children = children(want_element)
  549. got_children = children(got_element)
  550. if len(want_children) != len(got_children):
  551. return False
  552. return all(
  553. check_element(want, got) for want, got in zip(want_children, got_children)
  554. )
  555. def first_node(document):
  556. for node in document.childNodes:
  557. if node.nodeType not in (
  558. Node.COMMENT_NODE,
  559. Node.DOCUMENT_TYPE_NODE,
  560. Node.PROCESSING_INSTRUCTION_NODE,
  561. ):
  562. return node
  563. want = want.strip().replace("\\n", "\n")
  564. got = got.strip().replace("\\n", "\n")
  565. # If the string is not a complete xml document, we may need to add a
  566. # root element. This allow us to compare fragments, like "<foo/><bar/>"
  567. if not want.startswith("<?xml"):
  568. wrapper = "<root>%s</root>"
  569. want = wrapper % want
  570. got = wrapper % got
  571. # Parse the want and got strings, and compare the parsings.
  572. want_root = first_node(parseString(want))
  573. got_root = first_node(parseString(got))
  574. return check_element(want_root, got_root)
  575. class CaptureQueriesContext:
  576. """
  577. Context manager that captures queries executed by the specified connection.
  578. """
  579. def __init__(self, connection):
  580. self.connection = connection
  581. def __iter__(self):
  582. return iter(self.captured_queries)
  583. def __getitem__(self, index):
  584. return self.captured_queries[index]
  585. def __len__(self):
  586. return len(self.captured_queries)
  587. @property
  588. def captured_queries(self):
  589. return self.connection.queries[self.initial_queries : self.final_queries]
  590. def __enter__(self):
  591. self.force_debug_cursor = self.connection.force_debug_cursor
  592. self.connection.force_debug_cursor = True
  593. # Run any initialization queries if needed so that they won't be
  594. # included as part of the count.
  595. self.connection.ensure_connection()
  596. self.initial_queries = len(self.connection.queries_log)
  597. self.final_queries = None
  598. request_started.disconnect(reset_queries)
  599. return self
  600. def __exit__(self, exc_type, exc_value, traceback):
  601. self.connection.force_debug_cursor = self.force_debug_cursor
  602. request_started.connect(reset_queries)
  603. if exc_type is not None:
  604. return
  605. self.final_queries = len(self.connection.queries_log)
  606. class ignore_warnings(TestContextDecorator):
  607. def __init__(self, **kwargs):
  608. self.ignore_kwargs = kwargs
  609. if "message" in self.ignore_kwargs or "module" in self.ignore_kwargs:
  610. self.filter_func = warnings.filterwarnings
  611. else:
  612. self.filter_func = warnings.simplefilter
  613. super().__init__()
  614. def enable(self):
  615. self.catch_warnings = warnings.catch_warnings()
  616. self.catch_warnings.__enter__()
  617. self.filter_func("ignore", **self.ignore_kwargs)
  618. def disable(self):
  619. self.catch_warnings.__exit__(*sys.exc_info())
  620. # On OSes that don't provide tzset (Windows), we can't set the timezone
  621. # in which the program runs. As a consequence, we must skip tests that
  622. # don't enforce a specific timezone (with timezone.override or equivalent),
  623. # or attempt to interpret naive datetimes in the default timezone.
  624. requires_tz_support = skipUnless(
  625. TZ_SUPPORT,
  626. "This test relies on the ability to run a program in an arbitrary "
  627. "time zone, but your operating system isn't able to do that.",
  628. )
  629. @contextmanager
  630. def extend_sys_path(*paths):
  631. """Context manager to temporarily add paths to sys.path."""
  632. _orig_sys_path = sys.path[:]
  633. sys.path.extend(paths)
  634. try:
  635. yield
  636. finally:
  637. sys.path = _orig_sys_path
  638. @contextmanager
  639. def isolate_lru_cache(lru_cache_object):
  640. """Clear the cache of an LRU cache object on entering and exiting."""
  641. lru_cache_object.cache_clear()
  642. try:
  643. yield
  644. finally:
  645. lru_cache_object.cache_clear()
  646. @contextmanager
  647. def captured_output(stream_name):
  648. """Return a context manager used by captured_stdout/stdin/stderr
  649. that temporarily replaces the sys stream *stream_name* with a StringIO.
  650. Note: This function and the following ``captured_std*`` are copied
  651. from CPython's ``test.support`` module."""
  652. orig_stdout = getattr(sys, stream_name)
  653. setattr(sys, stream_name, StringIO())
  654. try:
  655. yield getattr(sys, stream_name)
  656. finally:
  657. setattr(sys, stream_name, orig_stdout)
  658. def captured_stdout():
  659. """Capture the output of sys.stdout:
  660. with captured_stdout() as stdout:
  661. print("hello")
  662. self.assertEqual(stdout.getvalue(), "hello\n")
  663. """
  664. return captured_output("stdout")
  665. def captured_stderr():
  666. """Capture the output of sys.stderr:
  667. with captured_stderr() as stderr:
  668. print("hello", file=sys.stderr)
  669. self.assertEqual(stderr.getvalue(), "hello\n")
  670. """
  671. return captured_output("stderr")
  672. def captured_stdin():
  673. """Capture the input to sys.stdin:
  674. with captured_stdin() as stdin:
  675. stdin.write('hello\n')
  676. stdin.seek(0)
  677. # call test code that consumes from sys.stdin
  678. captured = input()
  679. self.assertEqual(captured, "hello")
  680. """
  681. return captured_output("stdin")
  682. @contextmanager
  683. def freeze_time(t):
  684. """
  685. Context manager to temporarily freeze time.time(). This temporarily
  686. modifies the time function of the time module. Modules which import the
  687. time function directly (e.g. `from time import time`) won't be affected
  688. This isn't meant as a public API, but helps reduce some repetitive code in
  689. Django's test suite.
  690. """
  691. _real_time = time.time
  692. time.time = lambda: t
  693. try:
  694. yield
  695. finally:
  696. time.time = _real_time
  697. def require_jinja2(test_func):
  698. """
  699. Decorator to enable a Jinja2 template engine in addition to the regular
  700. Django template engine for a test or skip it if Jinja2 isn't available.
  701. """
  702. test_func = skipIf(jinja2 is None, "this test requires jinja2")(test_func)
  703. return override_settings(
  704. TEMPLATES=[
  705. {
  706. "BACKEND": "django.template.backends.django.DjangoTemplates",
  707. "APP_DIRS": True,
  708. },
  709. {
  710. "BACKEND": "django.template.backends.jinja2.Jinja2",
  711. "APP_DIRS": True,
  712. "OPTIONS": {"keep_trailing_newline": True},
  713. },
  714. ]
  715. )(test_func)
  716. class override_script_prefix(TestContextDecorator):
  717. """Decorator or context manager to temporary override the script prefix."""
  718. def __init__(self, prefix):
  719. self.prefix = prefix
  720. super().__init__()
  721. def enable(self):
  722. self.old_prefix = get_script_prefix()
  723. set_script_prefix(self.prefix)
  724. def disable(self):
  725. set_script_prefix(self.old_prefix)
  726. class LoggingCaptureMixin:
  727. """
  728. Capture the output from the 'django' logger and store it on the class's
  729. logger_output attribute.
  730. """
  731. def setUp(self):
  732. self.logger = logging.getLogger("django")
  733. self.old_stream = self.logger.handlers[0].stream
  734. self.logger_output = StringIO()
  735. self.logger.handlers[0].stream = self.logger_output
  736. def tearDown(self):
  737. self.logger.handlers[0].stream = self.old_stream
  738. class isolate_apps(TestContextDecorator):
  739. """
  740. Act as either a decorator or a context manager to register models defined
  741. in its wrapped context to an isolated registry.
  742. The list of installed apps the isolated registry should contain must be
  743. passed as arguments.
  744. Two optional keyword arguments can be specified:
  745. `attr_name`: attribute assigned the isolated registry if used as a class
  746. decorator.
  747. `kwarg_name`: keyword argument passing the isolated registry if used as a
  748. function decorator.
  749. """
  750. def __init__(self, *installed_apps, **kwargs):
  751. self.installed_apps = installed_apps
  752. super().__init__(**kwargs)
  753. def enable(self):
  754. self.old_apps = Options.default_apps
  755. apps = Apps(self.installed_apps)
  756. setattr(Options, "default_apps", apps)
  757. return apps
  758. def disable(self):
  759. setattr(Options, "default_apps", self.old_apps)
  760. class TimeKeeper:
  761. def __init__(self):
  762. self.records = collections.defaultdict(list)
  763. @contextmanager
  764. def timed(self, name):
  765. self.records[name]
  766. start_time = time.perf_counter()
  767. try:
  768. yield
  769. finally:
  770. end_time = time.perf_counter() - start_time
  771. self.records[name].append(end_time)
  772. def print_results(self):
  773. for name, end_times in self.records.items():
  774. for record_time in end_times:
  775. record = "%s took %.3fs" % (name, record_time)
  776. sys.stderr.write(record + os.linesep)
  777. class NullTimeKeeper:
  778. @contextmanager
  779. def timed(self, name):
  780. yield
  781. def print_results(self):
  782. pass
  783. def tag(*tags):
  784. """Decorator to add tags to a test class or method."""
  785. def decorator(obj):
  786. if hasattr(obj, "tags"):
  787. obj.tags = obj.tags.union(tags)
  788. else:
  789. setattr(obj, "tags", set(tags))
  790. return obj
  791. return decorator
  792. @contextmanager
  793. def register_lookup(field, *lookups, lookup_name=None):
  794. """
  795. Context manager to temporarily register lookups on a model field using
  796. lookup_name (or the lookup's lookup_name if not provided).
  797. """
  798. try:
  799. for lookup in lookups:
  800. field.register_lookup(lookup, lookup_name)
  801. yield
  802. finally:
  803. for lookup in lookups:
  804. field._unregister_lookup(lookup, lookup_name)
  805. def garbage_collect():
  806. gc.collect()
  807. if PYPY:
  808. # Collecting weakreferences can take two collections on PyPy.
  809. gc.collect()