Ticket #27835: 27835.diff

File 27835.diff, 7.0 KB (added by Tim Graham, 7 years ago)
  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index b8d5737..6abbf3b 100644
    a b class TransactionTestCase(SimpleTestCase):  
    794794    # are not available, we allow queries to be run.
    795795    allow_database_queries = True
    796796
     797    @classmethod
     798    def setUpClass(cls):
     799        super().setUpClass()
     800        if not cls.multi_db:
     801            for alias in connections:
     802                if alias != DEFAULT_DB_ALIAS:
     803                    connection = connections[alias]
     804                    connection.cursor = _CursorFailure(cls.__name__, connection.cursor)
     805                    connection.chunked_cursor = _CursorFailure(cls.__name__, connection.chunked_cursor)
     806
     807    @classmethod
     808    def tearDownClass(cls):
     809        super().tearDownClass()
     810        if not cls.multi_db:
     811            for alias in connections:
     812                if alias != DEFAULT_DB_ALIAS:
     813                    connection = connections[alias]
     814                    connection.cursor = connection.cursor.wrapped
     815                    connection.chunked_cursor = connection.chunked_cursor.wrapped
     816
    797817    def _pre_setup(self):
    798818        """Performs any pre-test setup. This includes:
    799819
    class TransactionTestCase(SimpleTestCase):  
    943963            func(*args, **kwargs)
    944964
    945965
    946 def connections_support_transactions():
    947     """
    948     Returns True if all connections support transactions.
    949     """
    950     return all(conn.features.supports_transactions
    951                for conn in connections.all())
    952 
    953 
    954966class TestCase(TransactionTestCase):
    955967    """
    956968    Similar to TransactionTestCase, but uses `transaction.atomic()` to achieve
    class TestCase(TransactionTestCase):  
    981993            atomics[db_name].__exit__(None, None, None)
    982994
    983995    @classmethod
     996    def connections_support_transactions(cls):
     997        """Return True if all connections support transactions."""
     998        return all(
     999            connections[db_name].features.supports_transactions
     1000            for db_name in cls._databases_names()
     1001        )
     1002
     1003    @classmethod
    9841004    def setUpClass(cls):
    9851005        super().setUpClass()
    986         if not connections_support_transactions():
     1006        if not cls.connections_support_transactions():
    9871007            return
    9881008        cls.cls_atomics = cls._enter_atomics()
    9891009
    class TestCase(TransactionTestCase):  
    10061026
    10071027    @classmethod
    10081028    def tearDownClass(cls):
    1009         if connections_support_transactions():
     1029        if cls.connections_support_transactions():
    10101030            cls._rollback_atomics(cls.cls_atomics)
    10111031            for conn in connections.all():
    10121032                conn.close()
    class TestCase(TransactionTestCase):  
    10181038        pass
    10191039
    10201040    def _should_reload_connections(self):
    1021         if connections_support_transactions():
     1041        if self.connections_support_transactions():
    10221042            return False
    10231043        return super()._should_reload_connections()
    10241044
    10251045    def _fixture_setup(self):
    1026         if not connections_support_transactions():
     1046        if not self.connections_support_transactions():
    10271047            # If the backend does not support transactions, we should reload
    10281048            # class data before each test
    10291049            self.setUpTestData()
    class TestCase(TransactionTestCase):  
    10331053        self.atomics = self._enter_atomics()
    10341054
    10351055    def _fixture_teardown(self):
    1036         if not connections_support_transactions():
     1056        if not self.connections_support_transactions():
    10371057            return super()._fixture_teardown()
    10381058        try:
    10391059            for db_name in reversed(self._databases_names()):
  • tests/servers/tests.py

    diff --git a/tests/servers/tests.py b/tests/servers/tests.py
    index b114b96..4f4d3fb 100644
    a b  
    11"""
    22Tests for django.core.servers.
    33"""
    4 import errno
    54import os
    6 import socket
    75from urllib.error import HTTPError
    86from urllib.parse import urlencode
    97from urllib.request import urlopen
    class LiveServerDatabase(LiveServerBase):  
    103101        )
    104102
    105103
    106 class LiveServerPort(LiveServerBase):
    107 
    108     def test_port_bind(self):
    109         """
    110         Each LiveServerTestCase binds to a unique port or fails to start a
    111         server thread when run concurrently (#26011).
    112         """
    113         TestCase = type("TestCase", (LiveServerBase,), {})
    114         try:
    115             TestCase.setUpClass()
    116         except socket.error as e:
    117             if e.errno == errno.EADDRINUSE:
    118                 # We're out of ports, LiveServerTestCase correctly fails with
    119                 # a socket error.
    120                 return
    121             # Unexpected error.
    122             raise
    123         try:
    124             # We've acquired a port, ensure our server threads acquired
    125             # different addresses.
    126             self.assertNotEqual(
    127                 self.live_server_url, TestCase.live_server_url,
    128                 "Acquired duplicate server addresses for server threads: %s" % self.live_server_url
    129             )
    130         finally:
    131             if hasattr(TestCase, 'server_thread'):
    132                 TestCase.server_thread.terminate()
     104# Remove for debugging purposes since this test isn't isolated
    133105
    134106
    135107class LiverServerThreadedTests(LiveServerBase):
  • tests/test_runner/tests.py

    diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py
    index 708a7c2..63194dc 100644
    a b from django.test import (  
    1414    TestCase, TransactionTestCase, skipUnlessDBFeature, testcases,
    1515)
    1616from django.test.runner import DiscoverRunner
    17 from django.test.testcases import connections_support_transactions
    1817from django.test.utils import dependency_ordered
    1918
    2019from .models import Person
    class Sqlite3InMemoryTestDbs(TestCase):  
    229228                    # Transaction support should be properly initialized for the 'other' DB
    230229                    self.assertTrue(other.features.supports_transactions, msg)
    231230                    # And all the DBs should report that they support transactions
    232                     self.assertTrue(connections_support_transactions(), msg)
    233231
    234232
    235233class DummyBackendTest(unittest.TestCase):
    class EmptyDefaultDatabaseTest(unittest.TestCase):  
    358356        testcases.connections = db.ConnectionHandler({'default': {}})
    359357        connection = testcases.connections[db.utils.DEFAULT_DB_ALIAS]
    360358        self.assertEqual(connection.settings_dict['ENGINE'], 'django.db.backends.dummy')
    361         connections_support_transactions()
  • tests/test_utils/tests.py

    diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
    index 19ef405..242bea4 100644
    a b class TestBadSetUpTestData(TestCase):  
    10221022            cls._in_atomic_block = connection.in_atomic_block
    10231023
    10241024    @classmethod
    1025     def tearDownClass(Cls):
     1025    def tearDownClass(cls):
    10261026        # override to avoid a second cls._rollback_atomics() which would fail.
    10271027        # Normal setUpClass() methods won't have exception handling so this
    10281028        # method wouldn't typically be run.
    1029         pass
     1029        super(TestCase, cls).tearDownClass()
    10301030
    10311031    @classmethod
    10321032    def setUpTestData(cls):
Back to Top