diff options
| author | Claude Paroz <claude@2xlibre.net> | 2012-05-03 18:19:18 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2012-05-03 18:30:07 +0200 |
| commit | 10cf3c64273db407402ea9723569dfc8d059e186 (patch) | |
| tree | 09278a014019f628169ec4774a3ad6fb13650fb4 /tests | |
| parent | e9a56606e738c478373d052bbd876ff84afdb995 (diff) | |
Used catch_warnings instead of save/restore methods. Refs #17049.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/cache/tests.py | 24 | ||||
| -rw-r--r-- | tests/regressiontests/decorators/tests.py | 27 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests/fields.py | 1 | ||||
| -rw-r--r-- | tests/regressiontests/generic_views/dates.py | 8 | ||||
| -rw-r--r-- | tests/regressiontests/i18n/patterns/tests.py | 1 | ||||
| -rw-r--r-- | tests/regressiontests/logging_tests/tests.py | 12 | ||||
| -rw-r--r-- | tests/regressiontests/requests/tests.py | 16 | ||||
| -rw-r--r-- | tests/regressiontests/staticfiles_tests/tests.py | 4 | ||||
| -rw-r--r-- | tests/regressiontests/test_client_regress/views.py | 1 | ||||
| -rw-r--r-- | tests/regressiontests/utils/text.py | 39 |
10 files changed, 49 insertions, 84 deletions
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index 16368c6fe9..f0e2977b31 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -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: - # memcached does not allow whitespace or control characters in keys - self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value') - # memcached limits key length to 250 - self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value') + with warnings.catch_warnings(record=True) as w: + # memcached does not allow whitespace or control characters in keys + 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.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() diff --git a/tests/regressiontests/decorators/tests.py b/tests/regressiontests/decorators/tests.py index ecec2812ab..975502273a 100644 --- a/tests/regressiontests/decorators/tests.py +++ b/tests/regressiontests/decorators/tests.py @@ -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,14 +122,16 @@ class DecoratorsTest(TestCase): """ def my_view(request): return "response" - 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") - self.assertEqual(my_view_cached2(HttpRequest()), "response") - my_view_cached3 = cache_page(my_view) - self.assertEqual(my_view_cached3(HttpRequest()), "response") - my_view_cached4 = cache_page()(my_view) - self.assertEqual(my_view_cached4(HttpRequest()), "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") + self.assertEqual(my_view_cached2(HttpRequest()), "response") + my_view_cached3 = cache_page(my_view) + 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): """ diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py index 3c91cdc41e..a7d98ec7d4 100644 --- a/tests/regressiontests/forms/tests/fields.py +++ b/tests/regressiontests/forms/tests/fields.py @@ -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 diff --git a/tests/regressiontests/generic_views/dates.py b/tests/regressiontests/generic_views/dates.py index eb1f7d0a95..91d4c29eb2 100644 --- a/tests/regressiontests/generic_views/dates.py +++ b/tests/regressiontests/generic_views/dates.py @@ -10,14 +10,6 @@ from django.utils import timezone from .models import Book, BookSigning -import warnings -warnings.filterwarnings( - 'error', r"DateTimeField received a naive datetime", - RuntimeWarning, r'django\.db\.models\.fields') - - - - class ArchiveIndexViewTests(TestCase): fixtures = ['generic-views-test-data.json'] urls = 'regressiontests.generic_views.urls' diff --git a/tests/regressiontests/i18n/patterns/tests.py b/tests/regressiontests/i18n/patterns/tests.py index e59994a8b0..8ef7377ecc 100644 --- a/tests/regressiontests/i18n/patterns/tests.py +++ b/tests/regressiontests/i18n/patterns/tests.py @@ -1,5 +1,4 @@ import os -import warnings from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse, clear_url_caches diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py index 6ec7f2a645..e4c045b9d8 100644 --- a/tests/regressiontests/logging_tests/tests.py +++ b/tests/regressiontests/logging_tests/tests.py @@ -4,7 +4,7 @@ import warnings from django.conf import compat_patch_logging_config from django.core import mail 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.log import CallbackFilter, RequireDebugFalse, getLogger @@ -42,12 +42,9 @@ class PatchLoggingConfigTest(TestCase): """ config = copy.deepcopy(OLD_LOGGING) - warnings_state = get_warnings_state() - warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.conf') - try: + with warnings.catch_warnings(record=True) as w: compat_patch_logging_config(config) - finally: - restore_warnings_state(warnings_state) + self.assertEqual(len(w), 1) self.assertEqual( config["handlers"]["mail_admins"]["filters"], @@ -60,7 +57,8 @@ class PatchLoggingConfigTest(TestCase): """ config = copy.deepcopy(OLD_LOGGING) - compat_patch_logging_config(config) + with warnings.catch_warnings(record=True): + compat_patch_logging_config(config) flt = config["filters"]["require_debug_false"] self.assertEqual(flt["()"], "django.utils.log.RequireDebugFalse") diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py index c838832b25..d1c0896720 100644 --- a/tests/regressiontests/requests/tests.py +++ b/tests/regressiontests/requests/tests.py @@ -6,7 +6,6 @@ from StringIO import StringIO from django.conf import settings from django.core.handlers.wsgi import WSGIRequest, LimitedStream from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr, UnreadablePostError -from django.test.utils import get_warnings_state, restore_warnings_state from django.utils import unittest from django.utils.http import cookie_date from django.utils.timezone import utc @@ -398,13 +397,9 @@ class RequestsTests(unittest.TestCase): 'wsgi.input': StringIO(payload) }) - warnings_state = get_warnings_state() - warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.http') - try: + with warnings.catch_warnings(record=True) as w: self.assertEqual(request.body, request.raw_post_data) - finally: - restore_warnings_state(warnings_state) - + self.assertEqual(len(w), 1) def test_POST_connection_error(self): """ @@ -420,10 +415,7 @@ class RequestsTests(unittest.TestCase): 'CONTENT_LENGTH': len(payload), 'wsgi.input': ExplodingStringIO(payload)}) - warnings_state = get_warnings_state() - warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.http') - try: + with warnings.catch_warnings(record=True) as w: with self.assertRaises(UnreadablePostError): request.raw_post_data - finally: - restore_warnings_state(warnings_state) + self.assertEqual(len(w), 1) diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py index 6914128989..7f30cb987a 100644 --- a/tests/regressiontests/staticfiles_tests/tests.py +++ b/tests/regressiontests/staticfiles_tests/tests.py @@ -6,7 +6,6 @@ import posixpath import shutil import sys import tempfile -import warnings from StringIO import StringIO from django.template import loader, Context @@ -511,11 +510,8 @@ class TestCollectionCachedStorage(BaseCollectionTestCase, """ name = "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff/some crazy/" + chr(22) + chr(180) cache_key = storage.staticfiles_storage.cache_key(name) - self.save_warnings_state() cache_validator = BaseCache({}) - warnings.filterwarnings('error', category=CacheKeyWarning) cache_validator.validate_key(cache_key) - self.restore_warnings_state() self.assertEqual(cache_key, 'staticfiles:e95bbc36387084582df2a70750d7b351') diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index ffd416655e..ab625a90dc 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -1,5 +1,4 @@ import json -import warnings from django.conf import settings from django.contrib.auth.decorators import login_required diff --git a/tests/regressiontests/utils/text.py b/tests/regressiontests/utils/text.py index bce7f99264..6457845c6f 100644 --- a/tests/regressiontests/utils/text.py +++ b/tests/regressiontests/utils/text.py @@ -8,13 +8,6 @@ class TestUtilsText(SimpleTestCase): # In Django 1.6 truncate_words() and truncate_html_words() will be removed # so these tests will need to be adapted accordingly - def setUp(self): - self.save_warnings_state() - warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.utils.text') - - def tearDown(self): - self.restore_warnings_state() - def test_truncate_chars(self): truncator = text.Truncator( u'The quick brown fox jumped over the lazy dog.' @@ -79,22 +72,26 @@ class TestUtilsText(SimpleTestCase): 'id="mylink">brown...</a></p>', truncator.words(3, '...', html=True)) def test_old_truncate_words(self): - self.assertEqual(u'The quick brown fox jumped over the lazy dog.', - text.truncate_words(u'The quick brown fox jumped over the lazy dog.', 10)) - self.assertEqual(u'The quick brown fox ...', - text.truncate_words('The quick brown fox jumped over the lazy dog.', 4)) - self.assertEqual(u'The quick brown fox ....', - text.truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....')) + with warnings.catch_warnings(record=True) as w: + self.assertEqual(u'The quick brown fox jumped over the lazy dog.', + text.truncate_words(u'The quick brown fox jumped over the lazy dog.', 10)) + self.assertEqual(u'The quick brown fox ...', + text.truncate_words('The quick brown fox jumped over the lazy dog.', 4)) + self.assertEqual(u'The quick brown fox ....', + text.truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....')) + self.assertEqual(len(w), 3) def test_old_truncate_html_words(self): - self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', - text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10)) - self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>', - text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4)) - self.assertEqual(u'<p><strong><em>The quick brown fox ....</em></strong></p>', - text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....')) - self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>', - text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None)) + with warnings.catch_warnings(record=True) as w: + self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', + text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10)) + self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>', + text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4)) + self.assertEqual(u'<p><strong><em>The quick brown fox ....</em></strong></p>', + text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....')) + self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>', + text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None)) + self.assertEqual(len(w), 4) def test_wrap(self): digits = '1234 67 9' |
