|
4 | 4 | from django.db import IntegrityError, transaction
|
5 | 5 | from django.test import TestCase
|
6 | 6 | from django.test.utils import override_settings
|
| 7 | +from django.utils import unittest |
| 8 | + |
| 9 | +try: |
| 10 | + import line_profiler |
| 11 | +except ImportError: |
| 12 | + line_profiler = None |
| 13 | + |
| 14 | +from debug_toolbar.panels import profiling |
| 15 | + |
| 16 | +from ..base import BaseTestCase |
| 17 | +from ..views import regular_view |
| 18 | + |
| 19 | + |
| 20 | +@override_settings(DEBUG_TOOLBAR_PANELS=['debug_toolbar.panels.profiling.ProfilingDebugPanel']) |
| 21 | +class ProfilingPanelTestCase(BaseTestCase): |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + super(ProfilingPanelTestCase, self).setUp() |
| 25 | + self.panel = self.toolbar.get_panel_by_id('ProfilingDebugPanel') |
| 26 | + |
| 27 | + def _test_render_with_or_without_line_profiler(self): |
| 28 | + self.panel.process_view(self.request, regular_view, ('profiling',), {}) |
| 29 | + self.panel.process_response(self.request, self.response) |
| 30 | + self.assertIn('func_list', self.panel.get_stats()) |
| 31 | + self.assertIn('regular_view', self.panel.content()) |
| 32 | + |
| 33 | + @unittest.skipIf(line_profiler is None, "line_profiler isn't available") |
| 34 | + def test_render_with_line_profiler(self): |
| 35 | + self._test_render_with_or_without_line_profiler() |
| 36 | + |
| 37 | + def test_without_line_profiler(self): |
| 38 | + _use_line_profiler = profiling.DJ_PROFILE_USE_LINE_PROFILER |
| 39 | + profiling.DJ_PROFILE_USE_LINE_PROFILER = False |
| 40 | + try: |
| 41 | + self._test_render_with_or_without_line_profiler() |
| 42 | + finally: |
| 43 | + profiling.DJ_PROFILE_USE_LINE_PROFILER = _use_line_profiler |
| 44 | + |
7 | 45 |
|
8 | 46 |
|
9 | 47 | @override_settings(DEBUG=True,
|
10 | 48 | DEBUG_TOOLBAR_PANELS=['debug_toolbar.panels.profiling.ProfilingDebugPanel'])
|
11 | 49 | class ProfilingPanelIntegrationTestCase(TestCase):
|
12 | 50 |
|
13 | 51 | def test_view_executed_once(self):
|
14 |
| - |
15 | 52 | self.assertEqual(User.objects.count(), 0)
|
16 | 53 |
|
17 | 54 | response = self.client.get('/new_user/')
|
|
0 commit comments