suppress DeprecationWarnings thrown in tests by classes with old-style indexed_fields definitions

This commit is contained in:
Matt Westcott 2014-07-09 16:26:54 +01:00
parent d4c4c08d7c
commit 677d0333f4
3 changed files with 28 additions and 8 deletions

View file

@ -1,3 +1,6 @@
from contextlib import contextmanager
import warnings
from django.contrib.auth.models import User
from django.utils import six
@ -25,3 +28,14 @@ class WagtailTestUtils(object):
def assertRegex(self, *args, **kwargs):
six.assertRegex(self, *args, **kwargs)
@staticmethod
@contextmanager
def ignore_deprecation_warnings():
with warnings.catch_warnings(record=True) as warning_list: # catch all warnings
yield
# rethrow all warnings that were not DeprecationWarnings
for w in warning_list:
if not issubclass(w.category, DeprecationWarning):
warnings.showwarning(message=w.message, category=w.category, filename=w.filename, lineno=w.lineno, file=w.file, line=w.line)

View file

@ -1,17 +1,18 @@
from six import StringIO
import warnings
from django.test import TestCase
from django.test.utils import override_settings
from django.conf import settings
from django.core import management
from wagtail.tests.utils import unittest
from wagtail.tests.utils import unittest, WagtailTestUtils
from wagtail.tests import models
from wagtail.wagtailsearch.backends import get_search_backend, InvalidSearchBackendError
from wagtail.wagtailsearch.backends.db import DBSearch
class BackendTests(object):
class BackendTests(WagtailTestUtils):
# To test a specific backend, subclass BackendTests and define self.backend_path.
def setUp(self):
@ -144,7 +145,8 @@ class BackendTests(object):
self.backend.reset_index()
# Run update_index command
management.call_command('update_index', backend=self.backend, interactive=False, stdout=StringIO())
with self.ignore_deprecation_warnings(): # ignore any DeprecationWarnings thrown by models with old-style indexed_fields definitions
management.call_command('update_index', backend=self.backend, interactive=False, stdout=StringIO())
# Check that there are still 3 results
results = self.backend.search("Hello", models.SearchTest)

View file

@ -1,8 +1,10 @@
import warnings
from django.test import TestCase
from wagtail.tests import models
import json
from wagtail.wagtailsearch import indexed
from wagtail.tests import models
from wagtail.tests.utils import WagtailTestUtils
class TestContentTypeNames(TestCase):
@ -15,10 +17,11 @@ class TestContentTypeNames(TestCase):
self.assertEqual(name, 'tests_searchtest_tests_searchtestchild')
class TestIndexedFieldsBackwardsCompatibility(TestCase):
class TestIndexedFieldsBackwardsCompatibility(TestCase, WagtailTestUtils):
def test_indexed_fields_backwards_compatibility(self):
# Get search fields
search_fields = models.SearchTestOldConfig.get_search_fields()
with self.ignore_deprecation_warnings():
search_fields = models.SearchTestOldConfig.get_search_fields()
search_fields_dict = dict(
((field.field_name, type(field)), field)
@ -36,7 +39,8 @@ class TestIndexedFieldsBackwardsCompatibility(TestCase):
def test_indexed_fields_backwards_compatibility_list(self):
# Get search fields
search_fields = models.SearchTestOldConfigList.get_search_fields()
with self.ignore_deprecation_warnings():
search_fields = models.SearchTestOldConfigList.get_search_fields()
search_fields_dict = dict(
((field.field_name, type(field)), field)