File: test_profiling.py

package info (click to toggle)
python-django-debug-toolbar 1%3A1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,628 kB
  • ctags: 506
  • sloc: python: 2,789; makefile: 181; sh: 1
file content (47 lines) | stat: -rw-r--r-- 1,765 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from __future__ import absolute_import, unicode_literals

from django.contrib.auth.models import User
from django.db import IntegrityError, transaction
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import unittest

from ..base import BaseTestCase
from ..views import regular_view


@override_settings(DEBUG_TOOLBAR_PANELS=['debug_toolbar.panels.profiling.ProfilingPanel'])
class ProfilingPanelTestCase(BaseTestCase):

    def setUp(self):
        super(ProfilingPanelTestCase, self).setUp()
        self.panel = self.toolbar.get_panel_by_id('ProfilingPanel')

    # This test fails randomly for a reason I don't understand.

    @unittest.expectedFailure
    def test_regular_view(self):
        self.panel.process_view(self.request, regular_view, ('profiling',), {})
        self.panel.process_response(self.request, self.response)
        self.assertIn('func_list', self.panel.get_stats())
        self.assertIn('regular_view', self.panel.content)


@override_settings(DEBUG=True,
                   DEBUG_TOOLBAR_PANELS=['debug_toolbar.panels.profiling.ProfilingPanel'])
class ProfilingPanelIntegrationTestCase(TestCase):

    def test_view_executed_once(self):
        self.assertEqual(User.objects.count(), 0)

        response = self.client.get('/new_user/')
        self.assertContains(response, 'Profiling')
        self.assertEqual(User.objects.count(), 1)

        with self.assertRaises(IntegrityError):
            if hasattr(transaction, 'atomic'):      # Django >= 1.6
                with transaction.atomic():
                    response = self.client.get('/new_user/')
            else:
                response = self.client.get('/new_user/')
        self.assertEqual(User.objects.count(), 1)