From 5c4c3e2d1f232df6de24741ca1df32442a01ebc3 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 20 Feb 2021 19:23:56 -0800 Subject: [PATCH] Fixed #32445 -- Fixed LiveServerThreadTest.test_closes_connections() for non-in-memory database on SQLite. --- tests/servers/test_liveserverthread.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/servers/test_liveserverthread.py b/tests/servers/test_liveserverthread.py index 8dc213137ad..09b6ca08c89 100644 --- a/tests/servers/test_liveserverthread.py +++ b/tests/servers/test_liveserverthread.py @@ -1,9 +1,14 @@ from django.db import DEFAULT_DB_ALIAS, connections -from django.test import LiveServerTestCase, TestCase +from django.test import LiveServerTestCase, TransactionTestCase from django.test.testcases import LiveServerThread -class LiveServerThreadTest(TestCase): +# Use TransactionTestCase instead of TestCase to run outside of a transaction, +# otherwise closing the connection would implicitly rollback and not set the +# connection to None. +class LiveServerThreadTest(TransactionTestCase): + + available_apps = [] def run_live_server_thread(self, connections_override=None): thread = LiveServerTestCase._create_server_thread(connections_override) @@ -16,12 +21,13 @@ class LiveServerThreadTest(TestCase): conn = connections[DEFAULT_DB_ALIAS] # Pass a connection to the thread to check they are being closed. connections_override = {DEFAULT_DB_ALIAS: conn} - + # Open a connection to the database. + conn.connect() conn.inc_thread_sharing() try: - self.assertTrue(conn.is_usable()) + self.assertIsNotNone(conn.connection) self.run_live_server_thread(connections_override) - self.assertFalse(conn.is_usable()) + self.assertIsNone(conn.connection) finally: conn.dec_thread_sharing()