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