utils.py 29 KB

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