Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 32KB

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