From 677d0333f4906fbfd590a2c32262cad14b995180 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 9 Jul 2014 16:26:54 +0100 Subject: [PATCH] suppress DeprecationWarnings thrown in tests by classes with old-style indexed_fields definitions --- wagtail/tests/utils.py | 14 ++++++++++++++ wagtail/wagtailsearch/tests/test_backends.py | 8 +++++--- wagtail/wagtailsearch/tests/test_indexed_class.py | 14 +++++++++----- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/wagtail/tests/utils.py b/wagtail/tests/utils.py index 9360f2206..2e95d0210 100644 --- a/wagtail/tests/utils.py +++ b/wagtail/tests/utils.py @@ -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) diff --git a/wagtail/wagtailsearch/tests/test_backends.py b/wagtail/wagtailsearch/tests/test_backends.py index e9b656298..9baf64957 100644 --- a/wagtail/wagtailsearch/tests/test_backends.py +++ b/wagtail/wagtailsearch/tests/test_backends.py @@ -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) diff --git a/wagtail/wagtailsearch/tests/test_indexed_class.py b/wagtail/wagtailsearch/tests/test_indexed_class.py index a54f135c3..983d8e0ba 100644 --- a/wagtail/wagtailsearch/tests/test_indexed_class.py +++ b/wagtail/wagtailsearch/tests/test_indexed_class.py @@ -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)