From 5a6a5ce0d56b9e307c74a55ed1239aad07841ab9 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 13 Nov 2015 15:54:05 -0500 Subject: [PATCH] [1.8.x] Fixed #25745 -- Promoted RuntimeWarnings to errors in the test suite. Conflicts: tests/apps/tests.py tests/migrations/test_graph.py tests/queryset_pickle/tests.py tests/runtests.py Backport of 1c5f4e86bc9c7bae5d5f3830d9cfbb0043297261 from master --- tests/apps/tests.py | 14 ++++++-------- tests/backends/tests.py | 1 + tests/model_regress/test_pickle.py | 17 ++++------------- tests/queryset_pickle/tests.py | 17 ++++------------- tests/runtests.py | 2 ++ 5 files changed, 17 insertions(+), 34 deletions(-) diff --git a/tests/apps/tests.py b/tests/apps/tests.py index 8c9f815a368..caf41fa17dd 100644 --- a/tests/apps/tests.py +++ b/tests/apps/tests.py @@ -2,7 +2,6 @@ from __future__ import unicode_literals import os import sys -import warnings from unittest import skipUnless from django.apps import AppConfig, apps @@ -229,14 +228,13 @@ class AppsTests(TestCase): body = {} body['Meta'] = type(str("Meta"), tuple(), meta_contents) body['__module__'] = TotallyNormal.__module__ - with warnings.catch_warnings(record=True) as w: + msg = ( + "Model 'apps.southponies' was already registered. " + "Reloading models is not advised as it can lead to inconsistencies, " + "most notably with related models." + ) + with self.assertRaisesMessage(RuntimeWarning, msg): type(str("SouthPonies"), (models.Model,), body) - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[-1].category, RuntimeWarning)) - self.assertEqual(str(w[-1].message), - "Model 'apps.southponies' was already registered. " - "Reloading models is not advised as it can lead to inconsistencies, " - "most notably with related models.") # If it doesn't appear to be a reloaded module then we expect # a RuntimeError. diff --git a/tests/backends/tests.py b/tests/backends/tests.py index bd929e440f6..e4b277a95ad 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -204,6 +204,7 @@ class PostgreSQLTests(TestCase): with warnings.catch_warnings(record=True) as w: with mock.patch('django.db.backends.base.base.BaseDatabaseWrapper.connect', side_effect=mocked_connect, autospec=True): + warnings.simplefilter('always', RuntimeWarning) nodb_conn = connection._nodb_connection del connection._nodb_connection self.assertIsNotNone(nodb_conn.settings_dict['NAME']) diff --git a/tests/model_regress/test_pickle.py b/tests/model_regress/test_pickle.py index baf53a18541..44b190cfb6e 100644 --- a/tests/model_regress/test_pickle.py +++ b/tests/model_regress/test_pickle.py @@ -3,13 +3,11 @@ import os import pickle import subprocess import sys -import warnings from django.core.files.temp import NamedTemporaryFile from django.db import DJANGO_VERSION_PICKLE_KEY, models from django.test import TestCase, mock from django.utils._os import npath, upath -from django.utils.encoding import force_text from django.utils.version import get_version from .models import Article @@ -31,11 +29,9 @@ class ModelPickleTestCase(TestCase): return reduce_list p = MissingDjangoVersion(title="FooBar") - with warnings.catch_warnings(record=True) as recorded: + msg = "Pickled model instance's Django version is not specified." + with self.assertRaisesMessage(RuntimeWarning, msg): pickle.loads(pickle.dumps(p)) - msg = force_text(recorded.pop().message) - self.assertEqual(msg, - "Pickled model instance's Django version is not specified.") def test_unsupported_unpickle(self): """ @@ -52,14 +48,9 @@ class ModelPickleTestCase(TestCase): return reduce_list p = DifferentDjangoVersion(title="FooBar") - with warnings.catch_warnings(record=True) as recorded: + msg = "Pickled model instance's Django version 1.0 does not match the current version %s." % get_version() + with self.assertRaisesMessage(RuntimeWarning, msg): pickle.loads(pickle.dumps(p)) - msg = force_text(recorded.pop().message) - self.assertEqual( - msg, - "Pickled model instance's Django version 1.0 does not " - "match the current version %s." % get_version() - ) def test_unpickling_when_appregistrynotready(self): """ diff --git a/tests/queryset_pickle/tests.py b/tests/queryset_pickle/tests.py index 6a3145e29c2..0cfa8aec8d5 100644 --- a/tests/queryset_pickle/tests.py +++ b/tests/queryset_pickle/tests.py @@ -2,10 +2,8 @@ from __future__ import unicode_literals import datetime import pickle -import warnings from django.test import TestCase -from django.utils.encoding import force_text from django.utils.version import get_version from .models import Container, Event, Group, Happening, M2MModel @@ -140,11 +138,9 @@ class PickleabilityTestCase(TestCase): unpickled without a Django version """ qs = Group.missing_django_version_objects.all() - with warnings.catch_warnings(record=True) as recorded: + msg = "Pickled queryset instance's Django version is not specified." + with self.assertRaisesMessage(RuntimeWarning, msg): pickle.loads(pickle.dumps(qs)) - msg = force_text(recorded.pop().message) - self.assertEqual(msg, - "Pickled queryset instance's Django version is not specified.") def test_unsupported_unpickle(self): """ @@ -152,11 +148,6 @@ class PickleabilityTestCase(TestCase): unpickled with a different Django version than the current """ qs = Group.previous_django_version_objects.all() - with warnings.catch_warnings(record=True) as recorded: + msg = "Pickled queryset instance's Django version 1.0 does not match the current version %s." % get_version() + with self.assertRaisesMessage(RuntimeWarning, msg): pickle.loads(pickle.dumps(qs)) - msg = force_text(recorded.pop().message) - self.assertEqual( - msg, - "Pickled queryset instance's Django version 1.0 does not " - "match the current version %s." % get_version() - ) diff --git a/tests/runtests.py b/tests/runtests.py index 2ab29262b28..339fb8ba95d 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -23,6 +23,8 @@ from django.utils.deprecation import ( # Make deprecation warnings errors to ensure no usage of deprecated features. warnings.simplefilter("error", RemovedInDjango19Warning) warnings.simplefilter("error", RemovedInDjango110Warning) +# Make runtime warning errors to ensure no usage of error prone patterns. +warnings.simplefilter("error", RuntimeWarning) # Ignore known warnings in test dependencies. warnings.filterwarnings("ignore", "'U' mode is deprecated", DeprecationWarning, module='docutils.io')