PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/views/tests/debug.py

https://code.google.com/p/mango-py/
Python | 141 lines | 123 code | 18 blank | 0 comment | 8 complexity | 82f237aa2cc64f360138019a03d0026a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import inspect
  2. import sys
  3. from django.conf import settings
  4. from django.core.files.uploadedfile import SimpleUploadedFile
  5. from django.test import TestCase, RequestFactory
  6. from django.core.urlresolvers import reverse
  7. from django.template import TemplateSyntaxError
  8. from django.views.debug import ExceptionReporter
  9. from regressiontests.views import BrokenException, except_args
  10. class DebugViewTests(TestCase):
  11. def setUp(self):
  12. self.old_debug = settings.DEBUG
  13. settings.DEBUG = True
  14. self.old_template_debug = settings.TEMPLATE_DEBUG
  15. settings.TEMPLATE_DEBUG = True
  16. def tearDown(self):
  17. settings.DEBUG = self.old_debug
  18. settings.TEMPLATE_DEBUG = self.old_template_debug
  19. def test_files(self):
  20. response = self.client.get('/views/raises/')
  21. self.assertEqual(response.status_code, 500)
  22. data = {
  23. 'file_data.txt': SimpleUploadedFile('file_data.txt', 'haha'),
  24. }
  25. response = self.client.post('/views/raises/', data)
  26. self.assertTrue('file_data.txt' in response.content)
  27. self.assertFalse('haha' in response.content)
  28. def test_404(self):
  29. response = self.client.get('/views/raises404/')
  30. self.assertEqual(response.status_code, 404)
  31. def test_view_exceptions(self):
  32. for n in range(len(except_args)):
  33. self.assertRaises(BrokenException, self.client.get,
  34. reverse('view_exception', args=(n,)))
  35. def test_template_exceptions(self):
  36. for n in range(len(except_args)):
  37. try:
  38. self.client.get(reverse('template_exception', args=(n,)))
  39. except TemplateSyntaxError, e:
  40. raising_loc = inspect.trace()[-1][-2][0].strip()
  41. self.assertFalse(raising_loc.find('raise BrokenException') == -1,
  42. "Failed to find 'raise BrokenException' in last frame of traceback, instead found: %s" %
  43. raising_loc)
  44. def test_template_loader_postmortem(self):
  45. response = self.client.get(reverse('raises_template_does_not_exist'))
  46. self.assertContains(response, 'templates/i_dont_exist.html</code> (File does not exist)</li>', status_code=500)
  47. class ExceptionReporterTests(TestCase):
  48. rf = RequestFactory()
  49. def test_request_and_exception(self):
  50. "A simple exception report can be generated"
  51. try:
  52. request = self.rf.get('/test_view/')
  53. raise ValueError("Can't find my keys")
  54. except ValueError:
  55. exc_type, exc_value, tb = sys.exc_info()
  56. reporter = ExceptionReporter(request, exc_type, exc_value, tb)
  57. html = reporter.get_traceback_html()
  58. self.assertIn('<h1>ValueError at /test_view/</h1>', html)
  59. self.assertIn('<pre class="exception_value">Can&#39;t find my keys</pre>', html)
  60. self.assertIn('<th>Request Method:</th>', html)
  61. self.assertIn('<th>Request URL:</th>', html)
  62. self.assertIn('<th>Exception Type:</th>', html)
  63. self.assertIn('<th>Exception Value:</th>', html)
  64. self.assertIn('<h2>Traceback ', html)
  65. self.assertIn('<h2>Request information</h2>', html)
  66. self.assertNotIn('<p>Request data not supplied</p>', html)
  67. def test_no_request(self):
  68. "An exception report can be generated without request"
  69. try:
  70. raise ValueError("Can't find my keys")
  71. except ValueError:
  72. exc_type, exc_value, tb = sys.exc_info()
  73. reporter = ExceptionReporter(None, exc_type, exc_value, tb)
  74. html = reporter.get_traceback_html()
  75. self.assertIn('<h1>ValueError</h1>', html)
  76. self.assertIn('<pre class="exception_value">Can&#39;t find my keys</pre>', html)
  77. self.assertNotIn('<th>Request Method:</th>', html)
  78. self.assertNotIn('<th>Request URL:</th>', html)
  79. self.assertIn('<th>Exception Type:</th>', html)
  80. self.assertIn('<th>Exception Value:</th>', html)
  81. self.assertIn('<h2>Traceback ', html)
  82. self.assertIn('<h2>Request information</h2>', html)
  83. self.assertIn('<p>Request data not supplied</p>', html)
  84. def test_no_exception(self):
  85. "An exception report can be generated for just a request"
  86. request = self.rf.get('/test_view/')
  87. reporter = ExceptionReporter(request, None, None, None)
  88. html = reporter.get_traceback_html()
  89. self.assertIn('<h1>Report at /test_view/</h1>', html)
  90. self.assertIn('<pre class="exception_value">No exception supplied</pre>', html)
  91. self.assertIn('<th>Request Method:</th>', html)
  92. self.assertIn('<th>Request URL:</th>', html)
  93. self.assertNotIn('<th>Exception Type:</th>', html)
  94. self.assertNotIn('<th>Exception Value:</th>', html)
  95. self.assertNotIn('<h2>Traceback ', html)
  96. self.assertIn('<h2>Request information</h2>', html)
  97. self.assertNotIn('<p>Request data not supplied</p>', html)
  98. def test_request_and_message(self):
  99. "A message can be provided in addition to a request"
  100. request = self.rf.get('/test_view/')
  101. reporter = ExceptionReporter(request, None, "I'm a little teapot", None)
  102. html = reporter.get_traceback_html()
  103. self.assertIn('<h1>Report at /test_view/</h1>', html)
  104. self.assertIn('<pre class="exception_value">I&#39;m a little teapot</pre>', html)
  105. self.assertIn('<th>Request Method:</th>', html)
  106. self.assertIn('<th>Request URL:</th>', html)
  107. self.assertNotIn('<th>Exception Type:</th>', html)
  108. self.assertNotIn('<th>Exception Value:</th>', html)
  109. self.assertNotIn('<h2>Traceback ', html)
  110. self.assertIn('<h2>Request information</h2>', html)
  111. self.assertNotIn('<p>Request data not supplied</p>', html)
  112. def test_message_only(self):
  113. reporter = ExceptionReporter(None, None, "I'm a little teapot", None)
  114. html = reporter.get_traceback_html()
  115. self.assertIn('<h1>Report</h1>', html)
  116. self.assertIn('<pre class="exception_value">I&#39;m a little teapot</pre>', html)
  117. self.assertNotIn('<th>Request Method:</th>', html)
  118. self.assertNotIn('<th>Request URL:</th>', html)
  119. self.assertNotIn('<th>Exception Type:</th>', html)
  120. self.assertNotIn('<th>Exception Value:</th>', html)
  121. self.assertNotIn('<h2>Traceback ', html)
  122. self.assertIn('<h2>Request information</h2>', html)
  123. self.assertIn('<p>Request data not supplied</p>', html)