Commit 10cf3c64 authored by Claude Paroz's avatar Claude Paroz
Browse files

Used catch_warnings instead of save/restore methods. Refs #17049.

parent e9a56606
Loading
Loading
Loading
Loading
+13 −14
Original line number Diff line number Diff line
@@ -7,16 +7,12 @@ from django.conf import settings
from django.contrib.formtools import preview, utils
from django.contrib.formtools.wizard import FormWizard
from django.test import TestCase
from django.test.utils import get_warnings_state, restore_warnings_state
from django.test.utils import override_settings
from django.utils import unittest

from django.contrib.formtools.tests.wizard import *
from django.contrib.formtools.tests.forms import *

warnings.filterwarnings('ignore', category=PendingDeprecationWarning,
                        module='django.contrib.formtools.wizard')

success_string = "Done was called!"

class TestFormPreview(preview.FormPreview):
@@ -41,20 +37,12 @@ class PreviewTests(TestCase):

    def setUp(self):
        super(PreviewTests, self).setUp()
        self.save_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning,
                                module='django.contrib.formtools.wizard.legacy')

        # Create a FormPreview instance to share between tests
        self.preview = preview.FormPreview(TestForm)
        input_template = '<input type="hidden" name="%s" value="%s" />'
        self.input = input_template % (self.preview.unused_name('stage'), "%d")
        self.test_data = {'field1':u'foo', 'field1_':u'asdf'}

    def tearDown(self):
        super(PreviewTests, self).tearDown()
        self.restore_warnings_state()

    def test_unused_name(self):
        """
        Verifies name mangling to get uniue field name.
@@ -130,6 +118,7 @@ class PreviewTests(TestCase):
        self.test_data.update({'stage':2})
        hash = self.preview.security_hash(None, TestForm(self.test_data))
        self.test_data.update({'hash':hash, 'bool1':u'False'})
        with warnings.catch_warnings(record=True):
            response = self.client.post('/preview/', self.test_data)
            self.assertEqual(response.content, success_string)

@@ -241,6 +230,16 @@ class WizardTests(TestCase):
        }
    )

    def setUp(self):
        super(WizardTests, self).setUp()
        self.save_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning,
                                module='django.contrib.formtools.wizard')

    def tearDown(self):
        super(WizardTests, self).tearDown()
        self.restore_warnings_state()

    def test_step_starts_at_zero(self):
        """
        step should be zero for the first form
+9 −14
Original line number Diff line number Diff line
@@ -12,11 +12,10 @@ from django.contrib.sessions.backends.file import SessionStore as FileSession
from django.contrib.sessions.backends.signed_cookies import SessionStore as CookieSession
from django.contrib.sessions.models import Session
from django.contrib.sessions.middleware import SessionMiddleware
from django.core.cache.backends.base import CacheKeyWarning
from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
from django.http import HttpResponse
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings, get_warnings_state, restore_warnings_state
from django.test.utils import override_settings
from django.utils import timezone
from django.utils import unittest

@@ -302,12 +301,10 @@ class CacheDBSessionTests(SessionTestsMixin, TestCase):
            self.assertTrue(self.session.exists(self.session.session_key))

    def test_load_overlong_key(self):
        warnings_state = get_warnings_state()
        warnings.filterwarnings('ignore',
                                category=CacheKeyWarning)
        with warnings.catch_warnings(record=True) as w:
            self.session._session_key = (string.ascii_letters + string.digits) * 20
            self.assertEqual(self.session.load(), {})
        restore_warnings_state(warnings_state)
            self.assertEqual(len(w), 1)


@override_settings(USE_TZ=True)
@@ -353,12 +350,10 @@ class CacheSessionTests(SessionTestsMixin, unittest.TestCase):
    backend = CacheSession

    def test_load_overlong_key(self):
        warnings_state = get_warnings_state()
        warnings.filterwarnings('ignore',
                                category=CacheKeyWarning)
        with warnings.catch_warnings(record=True) as w:
            self.session._session_key = (string.ascii_letters + string.digits) * 20
            self.assertEqual(self.session.load(), {})
        restore_warnings_state(warnings_state)
            self.assertEqual(len(w), 1)


class SessionMiddlewareTests(unittest.TestCase):
+12 −12
Original line number Diff line number Diff line
@@ -466,20 +466,19 @@ class BaseCacheTests(object):

        old_func = self.cache.key_func
        self.cache.key_func = func
        # On Python 2.6+ we could use the catch_warnings context
        # manager to test this warning nicely. Since we can't do that
        # yet, the cleanest option is to temporarily ask for
        # CacheKeyWarning to be raised as an exception.
        _warnings_state = get_warnings_state()
        warnings.simplefilter("error", CacheKeyWarning)

        try:
            with warnings.catch_warnings(record=True) as w:
                # memcached does not allow whitespace or control characters in keys
            self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value')
                self.cache.set('key with spaces', 'value')
                self.assertEqual(len(w), 2)
                self.assertTrue(isinstance(w[0].message, CacheKeyWarning))
            with warnings.catch_warnings(record=True) as w:
                # memcached limits key length to 250
            self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value')
                self.cache.set('a' * 251, 'value')
                self.assertEqual(len(w), 1)
                self.assertTrue(isinstance(w[0].message, CacheKeyWarning))
        finally:
            restore_warnings_state(_warnings_state)
            self.cache.key_func = old_func

    def test_cache_versioning_get_set(self):
@@ -1450,7 +1449,8 @@ class CacheMiddlewareTest(TestCase):
        self.default_cache = get_cache('default')
        self.other_cache = get_cache('other')
        self.save_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.views.decorators.cache')
        warnings.filterwarnings('ignore', category=DeprecationWarning,
            module='django.views.decorators.cache')

    def tearDown(self):
        self.restore_warnings_state()
+10 −17
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
from django.http import HttpResponse, HttpRequest, HttpResponseNotAllowed
from django.middleware.clickjacking import XFrameOptionsMiddleware
from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils.decorators import method_decorator
from django.utils.functional import allow_lazy, lazy, memoize
from django.utils.unittest import TestCase
@@ -68,14 +67,6 @@ fully_decorated = full_decorator(fully_decorated)

class DecoratorsTest(TestCase):

    def setUp(self):
        self.warning_state = get_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning,
                                module='django.views.decorators.cache')

    def tearDown(self):
        restore_warnings_state(self.warning_state)

    def test_attributes(self):
        """
        Tests that django decorators set certain attributes of the wrapped
@@ -131,6 +122,7 @@ class DecoratorsTest(TestCase):
        """
        def my_view(request):
            return "response"
        with warnings.catch_warnings(record=True) as w:
            my_view_cached = cache_page(my_view, 123)
            self.assertEqual(my_view_cached(HttpRequest()), "response")
            my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
@@ -139,6 +131,7 @@ class DecoratorsTest(TestCase):
            self.assertEqual(my_view_cached3(HttpRequest()), "response")
            my_view_cached4 = cache_page()(my_view)
            self.assertEqual(my_view_cached4(HttpRequest()), "response")
            self.assertEqual(len(w), 4)

    def test_require_safe_accepts_only_safe_methods(self):
        """
+0 −1
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import datetime
import pickle
import re
import os
import warnings
from decimal import Decimal

from django.core.files.uploadedfile import SimpleUploadedFile
Loading