2014-04-15 05:18:03 +08:00
|
|
|
import logging
|
|
|
|
|
2017-09-03 08:24:01 +08:00
|
|
|
from django.template import Engine, Variable, VariableDoesNotExist
|
|
|
|
from django.test import SimpleTestCase
|
2014-04-15 05:18:03 +08:00
|
|
|
|
|
|
|
|
2018-04-29 17:02:51 +08:00
|
|
|
class VariableResolveLoggingTests(SimpleTestCase):
|
2015-09-12 07:35:21 +08:00
|
|
|
loglevel = logging.DEBUG
|
|
|
|
|
2014-04-15 05:18:03 +08:00
|
|
|
def test_log_on_variable_does_not_exist_silent(self):
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestObject:
|
2014-04-15 05:18:03 +08:00
|
|
|
class SilentDoesNotExist(Exception):
|
|
|
|
silent_variable_failure = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def template_name(self):
|
2015-09-22 03:20:19 +08:00
|
|
|
return "template_name"
|
2014-04-15 05:18:03 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def template(self):
|
2015-04-12 07:41:45 +08:00
|
|
|
return Engine().from_string("")
|
2014-04-15 05:18:03 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def article(self):
|
|
|
|
raise TestObject.SilentDoesNotExist("Attribute does not exist.")
|
|
|
|
|
|
|
|
def __iter__(self):
|
2019-02-09 22:18:22 +08:00
|
|
|
return (attr for attr in dir(TestObject) if attr[:2] != "__")
|
2014-04-15 05:18:03 +08:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.__dict__[item]
|
|
|
|
|
2018-04-29 17:02:51 +08:00
|
|
|
with self.assertLogs("django.template", self.loglevel) as cm:
|
|
|
|
Variable("article").resolve(TestObject())
|
2015-09-22 03:20:19 +08:00
|
|
|
|
2018-04-29 17:02:51 +08:00
|
|
|
self.assertEqual(len(cm.records), 1)
|
|
|
|
log_record = cm.records[0]
|
2014-04-15 05:18:03 +08:00
|
|
|
self.assertEqual(
|
2018-04-29 17:02:51 +08:00
|
|
|
log_record.getMessage(),
|
2015-09-22 03:20:19 +08:00
|
|
|
"Exception while resolving variable 'article' in template 'template_name'.",
|
2014-04-15 05:18:03 +08:00
|
|
|
)
|
2018-04-29 17:02:51 +08:00
|
|
|
self.assertIsNotNone(log_record.exc_info)
|
|
|
|
raised_exception = log_record.exc_info[1]
|
2015-09-22 03:20:19 +08:00
|
|
|
self.assertEqual(str(raised_exception), "Attribute does not exist.")
|
2014-04-15 05:18:03 +08:00
|
|
|
|
|
|
|
def test_log_on_variable_does_not_exist_not_silent(self):
|
2018-04-29 17:02:51 +08:00
|
|
|
with self.assertLogs("django.template", self.loglevel) as cm:
|
|
|
|
with self.assertRaises(VariableDoesNotExist):
|
|
|
|
Variable("article.author").resolve({"article": {"section": "News"}})
|
2014-04-15 05:18:03 +08:00
|
|
|
|
2018-04-29 17:02:51 +08:00
|
|
|
self.assertEqual(len(cm.records), 1)
|
|
|
|
log_record = cm.records[0]
|
2014-04-15 05:18:03 +08:00
|
|
|
self.assertEqual(
|
2018-04-29 17:02:51 +08:00
|
|
|
log_record.getMessage(),
|
2015-09-22 03:20:19 +08:00
|
|
|
"Exception while resolving variable 'author' in template 'unknown'.",
|
|
|
|
)
|
2018-04-29 17:02:51 +08:00
|
|
|
self.assertIsNotNone(log_record.exc_info)
|
|
|
|
raised_exception = log_record.exc_info[1]
|
2015-09-22 03:20:19 +08:00
|
|
|
self.assertEqual(
|
|
|
|
str(raised_exception),
|
2017-01-27 04:10:15 +08:00
|
|
|
"Failed lookup for key [author] in {'section': 'News'}",
|
2014-04-15 05:18:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_no_log_when_variable_exists(self):
|
2021-01-08 00:54:40 +08:00
|
|
|
with self.assertNoLogs("django.template", self.loglevel):
|
|
|
|
Variable("article.section").resolve({"article": {"section": "News"}})
|