Skip to content

Commit

Permalink
Move unittest to the compat module.
Browse files Browse the repository at this point in the history
Django 1.8 deprecated the django.utils.unittest library as it no
longer supports Python 2.6. In order to reduce warnings and to continue
to test previous versions of Django, create if/else logic to import
unittest from django.utils if Django < 1.8 otherwise use Python's version.
  • Loading branch information
tim-schilling committed May 12, 2015
1 parent ad35ce1 commit 106e9cf
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 13 deletions.
9 changes: 9 additions & 0 deletions debug_toolbar/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ def get_template_context_processors():
from django.templatetags.future import url
else:
from django.template.defaulttags import url # NOQA

if django.VERSION[:2] < (1, 8):
# If the user is using Django < 1.8, then import the unittest
# library from Django so that it supports Python 2.6.
# Django >= 1.8 no longer supports Python 2.6, so in those cases
# simply load Python's unittest
from django.utils import unittest
else:
import unittest # NOQA
6 changes: 3 additions & 3 deletions tests/panels/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import django
from django.core import cache
from django.utils.unittest import skipIf

from ..base import BaseTestCase
from debug_toolbar.compat import unittest


class CachePanelTestCase(BaseTestCase):
Expand All @@ -29,7 +29,7 @@ def test_recording(self):
cache.cache.clear()
self.assertEqual(len(self.panel.calls), 4)

@skipIf(django.VERSION < (1, 7), "Caches was added in Django 1.7")
@unittest.skipIf(django.VERSION < (1, 7), "Caches was added in Django 1.7")
def test_recording_caches(self):
self.assertEqual(len(self.panel.calls), 0)
default_cache = cache.caches[cache.DEFAULT_CACHE_ALIAS]
Expand All @@ -38,7 +38,7 @@ def test_recording_caches(self):
second_cache.get('foo')
self.assertEqual(len(self.panel.calls), 2)

@skipIf(django.VERSION > (1, 6), "get_cache was deprecated in Django 1.7")
@unittest.skipIf(django.VERSION > (1, 6), "get_cache was deprecated in Django 1.7")
def test_recording_get_cache(self):
self.assertEqual(len(self.panel.calls), 0)
default_cache = cache.get_cache(cache.DEFAULT_CACHE_ALIAS)
Expand Down
2 changes: 1 addition & 1 deletion tests/panels/test_profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
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
from debug_toolbar.compat import unittest


@override_settings(DEBUG_TOOLBAR_PANELS=['debug_toolbar.panels.profiling.ProfilingPanel'])
Expand Down
2 changes: 1 addition & 1 deletion tests/panels/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from django.conf import settings
from django.http import HttpResponse
from django.test.utils import override_settings
from django.utils import unittest

from ..base import BaseTestCase
from debug_toolbar.compat import unittest


@override_settings(DEBUG_TOOLBAR_CONFIG={'INTERCEPT_REDIRECTS': True})
Expand Down
2 changes: 1 addition & 1 deletion tests/panels/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from django.db import connection
from django.db.utils import DatabaseError
from django.shortcuts import render
from django.utils import unittest
from django.test.utils import override_settings

from ..base import BaseTestCase
from debug_toolbar.compat import unittest


class SQLPanelTestCase(BaseTestCase):
Expand Down
7 changes: 3 additions & 4 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

from django.test import RequestFactory, TestCase
from django.test.utils import override_settings
from django.utils.unittest import skipIf, skipUnless

from debug_toolbar.compat import StaticLiveServerTestCase
from debug_toolbar.compat import StaticLiveServerTestCase, unittest
from debug_toolbar.middleware import DebugToolbarMiddleware, show_toolbar

from .base import BaseTestCase
Expand Down Expand Up @@ -110,8 +109,8 @@ def test_xml_validation(self):
ET.fromstring(response.content) # shouldn't raise ParseError


@skipIf(webdriver is None, "selenium isn't installed")
@skipUnless('DJANGO_SELENIUM_TESTS' in os.environ, "selenium tests not requested")
@unittest.skipIf(webdriver is None, "selenium isn't installed")
@unittest.skipUnless('DJANGO_SELENIUM_TESTS' in os.environ, "selenium tests not requested")
@override_settings(DEBUG=True)
class DebugToolbarLiveTestCase(StaticLiveServerTestCase):

Expand Down
5 changes: 2 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import absolute_import, unicode_literals

from django.utils.unittest import TestCase

from debug_toolbar.compat import unittest
from debug_toolbar.utils import get_name_from_obj


class GetNameFromObjTestCase(TestCase):
class GetNameFromObjTestCase(unittest.TestCase):

def test_func(self):
def x():
Expand Down

0 comments on commit 106e9cf

Please sign in to comment.