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