utils.py 30 KB

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