@@ -259,11 +259,7 @@ def create_squares(self, args, paramstyle, multiple):
259259 if paramstyle == "format" :
260260 query = "INSERT INTO %s (%s, %s) VALUES" % (tbl , f1 , f2 )
261261 elif paramstyle == "pyformat" :
262- query = "INSERT INTO %s (%s, %s) VALUES" % (
263- tbl ,
264- f1 ,
265- f2 ,
266- )
262+ query = "INSERT INTO %s (%s, %s) VALUES" % (tbl , f1 , f2 )
267263 else :
268264 raise ValueError ("unsupported paramstyle in test" )
269265 with connection .cursor () as cursor :
@@ -369,7 +365,7 @@ def test_unicode_password(self):
369365 self .fail ("Unexpected error raised with Unicode password: %s" % e )
370366 finally :
371367 connection .settings_dict ["PASSWORD" ] = old_password
372- connection .connection . close ()
368+ connection .close ()
373369 connection .connection = None
374370
375371 def test_database_operations_helper_class (self ):
@@ -431,11 +427,10 @@ def test_is_usable_after_database_disconnects(self):
431427 # Open a connection to the database.
432428 with connection .cursor ():
433429 pass
434- # Emulate a connection close by the database .
430+ # _close is a noop, because underlying connection is shared between threads .
435431 connection ._close ()
436- # Even then is_usable() should not raise an exception.
437432 try :
438- self .assertFalse (connection .is_usable ())
433+ self .assertTrue (connection .is_usable ())
439434 finally :
440435 # Clean up the mess created by connection._close(). Since the
441436 # connection is already closed, this crashes on some backends.
@@ -705,6 +700,7 @@ def test_check_constraints_sql_keywords(self):
705700
706701class ThreadTests (TransactionTestCase ):
707702 available_apps = ["backends" ]
703+ databases = {"default" , "s1r2" , "s2r1" }
708704
709705 def test_default_connection_thread_local (self ):
710706 """
@@ -736,15 +732,17 @@ def runner():
736732 t = threading .Thread (target = runner )
737733 t .start ()
738734 t .join ()
739- # Each created connection got different inner connection .
735+ # The inner connection is shared between threads .
740736 self .assertEqual (
741- len ({conn .connection for conn in connections_dict .values ()}), 3
737+ len ({conn .connection for conn in connections_dict .values ()}), 1
742738 )
743739 finally :
744740 # Finish by closing the connections opened by the other threads
745741 # (the connection opened in the main thread will automatically be
746742 # closed on teardown).
747743 for conn in connections_dict .values ():
744+ # Regression test for https://github.com/jayvynl/django-clickhouse-backend/issues/53
745+ self .assertIs (conn .connection , connection .connection )
748746 if conn is not connection and conn .allow_thread_sharing :
749747 conn .close ()
750748 conn .dec_thread_sharing ()
@@ -783,6 +781,10 @@ def runner():
783781 # (the connection opened in the main thread will automatically be
784782 # closed on teardown).
785783 for conn in connections_dict .values ():
784+ # Regression test for https://github.com/jayvynl/django-clickhouse-backend/issues/53
785+ if conn .vendor == "clickhouse" :
786+ conn .ensure_connection ()
787+ self .assertIs (conn .connection , connections [conn .alias ].connection )
786788 if conn is not connection and conn .allow_thread_sharing :
787789 conn .close ()
788790 conn .dec_thread_sharing ()
0 commit comments