summaryrefslogtreecommitdiff
path: root/tests/template_tests/test_logging.py
diff options
context:
space:
mode:
authorCaroline Simpson <github@hoojiboo.com>2014-04-14 17:18:03 -0400
committerTim Graham <timograham@gmail.com>2015-03-27 19:19:48 -0400
commitdc5b01ad05e50ccde688c73c2ed3334a956076b0 (patch)
tree53a0fc9a4be53387f8b625e3b6b8deceb79d22c5 /tests/template_tests/test_logging.py
parent0c91a419f8de791b3051d014db3c946396138969 (diff)
Fixed #18773 -- Added logging for template variable resolving
Added a django.template logger without a default handler. Added logging if there is an exception while resolving variables in a template.
Diffstat (limited to 'tests/template_tests/test_logging.py')
-rw-r--r--tests/template_tests/test_logging.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/template_tests/test_logging.py b/tests/template_tests/test_logging.py
new file mode 100644
index 0000000000..a8ac82ecba
--- /dev/null
+++ b/tests/template_tests/test_logging.py
@@ -0,0 +1,71 @@
+from __future__ import unicode_literals
+
+import logging
+
+from django.template import Template, Variable, VariableDoesNotExist
+from django.test import SimpleTestCase
+
+
+class TestHandler(logging.Handler):
+ def __init__(self):
+ super(TestHandler, self).__init__()
+ self.log_record = None
+
+ def emit(self, record):
+ self.log_record = record
+
+
+class VariableResolveLoggingTests(SimpleTestCase):
+ def setUp(self):
+ self.test_handler = TestHandler()
+ self.logger = logging.getLogger('django.template')
+ self.original_level = self.logger.level
+ self.logger.addHandler(self.test_handler)
+ self.logger.setLevel(logging.DEBUG)
+
+ def tearDown(self):
+ self.logger.removeHandler(self.test_handler)
+ self.logger.level = self.original_level
+
+ def test_log_on_variable_does_not_exist_silent(self):
+ class TestObject(object):
+ class SilentDoesNotExist(Exception):
+ silent_variable_failure = True
+
+ @property
+ def template_name(self):
+ return "template"
+
+ @property
+ def template(self):
+ return Template('')
+
+ @property
+ def article(self):
+ raise TestObject.SilentDoesNotExist("Attribute does not exist.")
+
+ def __iter__(self):
+ return iter(attr for attr in dir(TestObject) if attr[:2] != "__")
+
+ def __getitem__(self, item):
+ return self.__dict__[item]
+
+ Variable('article').resolve(TestObject())
+ self.assertEqual(
+ self.test_handler.log_record.msg,
+ 'template - Attribute does not exist.'
+ )
+
+ def test_log_on_variable_does_not_exist_not_silent(self):
+ with self.assertRaises(VariableDoesNotExist):
+ Variable('article.author').resolve({'article': {'section': 'News'}})
+
+ self.assertEqual(
+ self.test_handler.log_record.msg,
+ 'unknown - Failed lookup for key [author] in %r' %
+ ("{%r: %r}" % ('section', 'News'), )
+ )
+
+ def test_no_log_when_variable_exists(self):
+ Variable('article.section').resolve({'article': {'section': 'News'}})
+ self.assertIsNone(self.test_handler.log_record)