diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/admin_scripts/tests.py | 9 | ||||
| -rw-r--r-- | tests/backends/tests.py | 5 | ||||
| -rw-r--r-- | tests/bash_completion/tests.py | 5 | ||||
| -rw-r--r-- | tests/handlers/views.py | 6 | ||||
| -rw-r--r-- | tests/mail/tests.py | 9 | ||||
| -rw-r--r-- | tests/postgres_tests/test_aggregates.py | 5 | ||||
| -rw-r--r-- | tests/postgres_tests/test_array.py | 5 | ||||
| -rw-r--r-- | tests/postgres_tests/test_hstore.py | 5 | ||||
| -rw-r--r-- | tests/postgres_tests/test_json.py | 5 | ||||
| -rw-r--r-- | tests/postgres_tests/test_ranges.py | 5 | ||||
| -rwxr-xr-x | tests/runtests.py | 9 | ||||
| -rw-r--r-- | tests/staticfiles_tests/storage.py | 5 | ||||
| -rw-r--r-- | tests/transaction_hooks/tests.py | 34 |
13 files changed, 68 insertions, 39 deletions
diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index b8bcea35f0..096984032f 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -12,7 +12,6 @@ import subprocess import sys import tempfile import unittest -from contextlib import suppress from io import StringIO from unittest import mock @@ -96,10 +95,12 @@ class AdminScriptTestCase(unittest.TestCase): # Also try to remove the compiled file; if it exists, it could # mess up later tests that depend upon the .py file not existing - with suppress(OSError): + try: if sys.platform.startswith('java'): # Jython produces module$py.class files os.remove(re.sub(r'\.py$', '$py.class', full_name)) + except OSError: + pass # Also remove a __pycache__ directory, if it exists cache_name = os.path.join(self.test_dir, '__pycache__') if os.path.isdir(cache_name): @@ -165,8 +166,10 @@ class AdminScriptTestCase(unittest.TestCase): def run_manage(self, args, settings_file=None): def safe_remove(path): - with suppress(OSError): + try: os.remove(path) + except OSError: + pass conf_dir = os.path.dirname(conf.__file__) template_manage_py = os.path.join(conf_dir, 'project_template', 'manage.py-tpl') diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 30c1cbf86d..6d38625a98 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -3,7 +3,6 @@ import datetime import threading import unittest import warnings -from contextlib import suppress from django.core.management.color import no_style from django.db import ( @@ -390,8 +389,10 @@ class BackendTestCase(TransactionTestCase): finally: # Clean up the mess created by connection._close(). Since the # connection is already closed, this crashes on some backends. - with suppress(Exception): + try: connection.close() + except Exception: + pass @override_settings(DEBUG=True) def test_queries(self): diff --git a/tests/bash_completion/tests.py b/tests/bash_completion/tests.py index ba2a5ea773..1d35e1f28e 100644 --- a/tests/bash_completion/tests.py +++ b/tests/bash_completion/tests.py @@ -4,7 +4,6 @@ A series of tests to establish that the command-line bash completion works. import os import sys import unittest -from contextlib import suppress from django.apps import apps from django.core.management import ManagementUtility @@ -51,8 +50,10 @@ class BashCompletionTests(unittest.TestCase): def _run_autocomplete(self): util = ManagementUtility(argv=sys.argv) with captured_stdout() as stdout: - with suppress(SystemExit): + try: util.autocomplete() + except SystemExit: + pass return stdout.getvalue().strip().split('\n') def test_django_admin_py(self): diff --git a/tests/handlers/views.py b/tests/handlers/views.py index 3c1fa5b802..22b94de3b9 100644 --- a/tests/handlers/views.py +++ b/tests/handlers/views.py @@ -1,12 +1,12 @@ -from contextlib import suppress - from django.core.exceptions import SuspiciousOperation from django.db import connection, transaction from django.http import HttpResponse, StreamingHttpResponse from django.views.decorators.csrf import csrf_exempt -with suppress(ImportError): # Python < 3.5 +try: from http import HTTPStatus +except ImportError: # Python < 3.5 + pass def regular(request): diff --git a/tests/mail/tests.py b/tests/mail/tests.py index b759484e04..29a56d6e74 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -8,7 +8,6 @@ import socket import sys import tempfile import threading -from contextlib import suppress from email import message_from_binary_file, message_from_bytes from email.header import Header from email.mime.text import MIMEText @@ -1135,10 +1134,12 @@ class ConsoleBackendTests(BaseEmailBackendTests, SimpleTestCase): class FakeSMTPChannel(smtpd.SMTPChannel): def collect_incoming_data(self, data): - # Ignore decode error in SSL/TLS connection tests as the test only - # cares whether the connection attempt was made. - with suppress(UnicodeDecodeError): + try: smtpd.SMTPChannel.collect_incoming_data(self, data) + except UnicodeDecodeError: + # Ignore decode error in SSL/TLS connection tests as the test only + # cares whether the connection attempt was made. + pass def smtp_AUTH(self, arg): if arg == 'CRAM-MD5': diff --git a/tests/postgres_tests/test_aggregates.py b/tests/postgres_tests/test_aggregates.py index 1fe8a1bf03..056d08441b 100644 --- a/tests/postgres_tests/test_aggregates.py +++ b/tests/postgres_tests/test_aggregates.py @@ -1,5 +1,4 @@ import json -from contextlib import suppress from django.db.models.expressions import F, Value from django.test.testcases import skipUnlessDBFeature @@ -8,12 +7,14 @@ from django.test.utils import Approximate from . import PostgreSQLTestCase from .models import AggregateTestModel, StatTestModel -with suppress(ImportError): # psycopg2 is not installed +try: from django.contrib.postgres.aggregates import ( ArrayAgg, BitAnd, BitOr, BoolAnd, BoolOr, Corr, CovarPop, JSONBAgg, RegrAvgX, RegrAvgY, RegrCount, RegrIntercept, RegrR2, RegrSlope, RegrSXX, RegrSXY, RegrSYY, StatAggregate, StringAgg, ) +except ImportError: + pass # psycopg2 is not installed class TestGeneralAggregate(PostgreSQLTestCase): diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 7378b5c12d..e2e4ccdeb2 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -2,7 +2,6 @@ import decimal import json import unittest import uuid -from contextlib import suppress from django import forms from django.core import exceptions, serializers, validators @@ -20,11 +19,13 @@ from .models import ( PostgreSQLModel, Tag, ) -with suppress(ImportError): +try: from django.contrib.postgres.fields import ArrayField from django.contrib.postgres.forms import ( SimpleArrayField, SplitArrayField, SplitArrayWidget, ) +except ImportError: + pass class TestSaveLoad(PostgreSQLTestCase): diff --git a/tests/postgres_tests/test_hstore.py b/tests/postgres_tests/test_hstore.py index 55b179ba5e..069e570f51 100644 --- a/tests/postgres_tests/test_hstore.py +++ b/tests/postgres_tests/test_hstore.py @@ -1,5 +1,4 @@ import json -from contextlib import suppress from django.core import exceptions, serializers from django.forms import Form @@ -8,10 +7,12 @@ from django.test.utils import modify_settings from . import PostgreSQLTestCase from .models import HStoreModel -with suppress(ImportError): +try: from django.contrib.postgres import forms from django.contrib.postgres.fields import HStoreField from django.contrib.postgres.validators import KeysValidator +except ImportError: + pass @modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'}) diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py index 20650ae95b..2506fc36d6 100644 --- a/tests/postgres_tests/test_json.py +++ b/tests/postgres_tests/test_json.py @@ -1,6 +1,5 @@ import datetime import uuid -from contextlib import suppress from decimal import Decimal from django.core import exceptions, serializers @@ -12,9 +11,11 @@ from django.utils.html import escape from . import PostgreSQLTestCase from .models import JSONModel -with suppress(ImportError): +try: from django.contrib.postgres import forms from django.contrib.postgres.fields import JSONField +except ImportError: + pass @skipUnlessDBFeature('has_jsonb_datatype') diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py index da72240bf4..d87ad36438 100644 --- a/tests/postgres_tests/test_ranges.py +++ b/tests/postgres_tests/test_ranges.py @@ -1,6 +1,5 @@ import datetime import json -from contextlib import suppress from django import forms from django.core import exceptions, serializers @@ -11,12 +10,14 @@ from django.utils import timezone from . import PostgreSQLTestCase from .models import RangeLookupsModel, RangesModel -with suppress(ImportError): +try: from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange from django.contrib.postgres import fields as pg_fields, forms as pg_forms from django.contrib.postgres.validators import ( RangeMaxValueValidator, RangeMinValueValidator, ) +except ImportError: + pass class TestSaveLoad(PostgreSQLTestCase): diff --git a/tests/runtests.py b/tests/runtests.py index 08e53f4cb1..7f4f1670c5 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -8,7 +8,6 @@ import subprocess import sys import tempfile import warnings -from contextlib import suppress import django from django.apps import apps @@ -316,8 +315,10 @@ def bisect_tests(bisection_label, options, test_labels, parallel): # Make sure the bisection point isn't in the test list # Also remove tests that need to be run in specific combinations for label in [bisection_label, 'model_inheritance_same_model_name']: - with suppress(ValueError): + try: test_labels.remove(label) + except ValueError: + pass subprocess_args = get_subprocess_args(options) @@ -365,8 +366,10 @@ def paired_tests(paired_test, options, test_labels, parallel): # Make sure the constant member of the pair isn't in the test list # Also remove tests that need to be run in specific combinations for label in [paired_test, 'model_inheritance_same_model_name']: - with suppress(ValueError): + try: test_labels.remove(label) + except ValueError: + pass subprocess_args = get_subprocess_args(options) diff --git a/tests/staticfiles_tests/storage.py b/tests/staticfiles_tests/storage.py index 0dcfd77a70..7a1f72c130 100644 --- a/tests/staticfiles_tests/storage.py +++ b/tests/staticfiles_tests/storage.py @@ -1,5 +1,4 @@ import os -from contextlib import suppress from datetime import datetime, timedelta from django.conf import settings @@ -49,8 +48,10 @@ class PathNotImplementedStorage(storage.Storage): def delete(self, name): name = self._path(name) - with suppress(FileNotFoundError): + try: os.remove(name) + except FileNotFoundError: + pass def path(self, name): raise NotImplementedError diff --git a/tests/transaction_hooks/tests.py b/tests/transaction_hooks/tests.py index ed3cf18be2..81ff0066a1 100644 --- a/tests/transaction_hooks/tests.py +++ b/tests/transaction_hooks/tests.py @@ -1,5 +1,3 @@ -from contextlib import suppress - from django.db import connection, transaction from django.test import TransactionTestCase, skipUnlessDBFeature @@ -50,10 +48,12 @@ class TestConnectionOnCommit(TransactionTestCase): self.assertDone([1]) def test_does_not_execute_if_transaction_rolled_back(self): - with suppress(ForcedError): + try: with transaction.atomic(): self.do(1) raise ForcedError() + except ForcedError: + pass self.assertDone([]) @@ -71,10 +71,12 @@ class TestConnectionOnCommit(TransactionTestCase): with transaction.atomic(): self.do(1) # one failed savepoint - with suppress(ForcedError): + try: with transaction.atomic(): self.do(2) raise ForcedError() + except ForcedError: + pass # another successful savepoint with transaction.atomic(): self.do(3) @@ -84,21 +86,25 @@ class TestConnectionOnCommit(TransactionTestCase): def test_no_hooks_run_from_failed_transaction(self): """If outer transaction fails, no hooks from within it run.""" - with suppress(ForcedError): + try: with transaction.atomic(): with transaction.atomic(): self.do(1) raise ForcedError() + except ForcedError: + pass self.assertDone([]) def test_inner_savepoint_rolled_back_with_outer(self): with transaction.atomic(): - with suppress(ForcedError): + try: with transaction.atomic(): with transaction.atomic(): self.do(1) raise ForcedError() + except ForcedError: + pass self.do(2) self.assertDone([2]) @@ -107,9 +113,11 @@ class TestConnectionOnCommit(TransactionTestCase): with transaction.atomic(): with transaction.atomic(): self.do(1) - with suppress(ForcedError): + try: with transaction.atomic(savepoint=False): raise ForcedError() + except ForcedError: + pass self.assertDone([]) @@ -117,9 +125,11 @@ class TestConnectionOnCommit(TransactionTestCase): with transaction.atomic(): with transaction.atomic(): self.do(1) - with suppress(ForcedError): + try: with transaction.atomic(): raise ForcedError() + except ForcedError: + pass self.assertDone([1]) @@ -141,10 +151,12 @@ class TestConnectionOnCommit(TransactionTestCase): self.assertDone([1, 2]) # not [1, 1, 2] def test_hooks_cleared_after_rollback(self): - with suppress(ForcedError): + try: with transaction.atomic(): self.do(1) raise ForcedError() + except ForcedError: + pass with transaction.atomic(): self.do(2) @@ -165,9 +177,11 @@ class TestConnectionOnCommit(TransactionTestCase): self.assertDone([2]) def test_error_in_hook_doesnt_prevent_clearing_hooks(self): - with suppress(ForcedError): + try: with transaction.atomic(): transaction.on_commit(lambda: self.notify('error')) + except ForcedError: + pass with transaction.atomic(): self.do(1) |
