summaryrefslogtreecommitdiff
path: root/django/test/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/test/utils.py')
-rw-r--r--django/test/utils.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/django/test/utils.py b/django/test/utils.py
index 92cef59f72..be586c75a6 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -1,4 +1,7 @@
+from contextlib import contextmanager
+import logging
import re
+import sys
import warnings
from functools import wraps
from xml.dom.minidom import parseString, Node
@@ -57,6 +60,16 @@ class ContextList(list):
return False
return True
+ def keys(self):
+ """
+ Flattened keys of subcontexts.
+ """
+ keys = set()
+ for subcontext in self:
+ for dict in subcontext:
+ keys |= set(dict.keys())
+ return keys
+
def instrumented_test_render(self, context):
"""
@@ -380,3 +393,41 @@ class CaptureQueriesContext(object):
if exc_type is not None:
return
self.final_queries = len(self.connection.queries)
+
+
+class IgnoreDeprecationWarningsMixin(object):
+
+ warning_class = DeprecationWarning
+
+ def setUp(self):
+ super(IgnoreDeprecationWarningsMixin, self).setUp()
+ self.catch_warnings = warnings.catch_warnings()
+ self.catch_warnings.__enter__()
+ warnings.filterwarnings("ignore", category=self.warning_class)
+
+ def tearDown(self):
+ self.catch_warnings.__exit__(*sys.exc_info())
+ super(IgnoreDeprecationWarningsMixin, self).tearDown()
+
+
+class IgnorePendingDeprecationWarningsMixin(IgnoreDeprecationWarningsMixin):
+
+ warning_class = PendingDeprecationWarning
+
+
+@contextmanager
+def patch_logger(logger_name, log_level):
+ """
+ Context manager that takes a named logger and the logging level
+ and provides a simple mock-like list of messages received
+ """
+ calls = []
+ def replacement(msg):
+ calls.append(msg)
+ logger = logging.getLogger(logger_name)
+ orig = getattr(logger, log_level)
+ setattr(logger, log_level, replacement)
+ try:
+ yield calls
+ finally:
+ setattr(logger, log_level, orig)