@@ -259,11 +259,7 @@ def create_squares(self, args, paramstyle, multiple):
259
259
if paramstyle == "format" :
260
260
query = "INSERT INTO %s (%s, %s) VALUES" % (tbl , f1 , f2 )
261
261
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 )
267
263
else :
268
264
raise ValueError ("unsupported paramstyle in test" )
269
265
with connection .cursor () as cursor :
@@ -369,7 +365,7 @@ def test_unicode_password(self):
369
365
self .fail ("Unexpected error raised with Unicode password: %s" % e )
370
366
finally :
371
367
connection .settings_dict ["PASSWORD" ] = old_password
372
- connection .connection . close ()
368
+ connection .close ()
373
369
connection .connection = None
374
370
375
371
def test_database_operations_helper_class (self ):
@@ -431,11 +427,10 @@ def test_is_usable_after_database_disconnects(self):
431
427
# Open a connection to the database.
432
428
with connection .cursor ():
433
429
pass
434
- # Emulate a connection close by the database .
430
+ # _close is a noop, because underlying connection is shared between threads .
435
431
connection ._close ()
436
- # Even then is_usable() should not raise an exception.
437
432
try :
438
- self .assertFalse (connection .is_usable ())
433
+ self .assertTrue (connection .is_usable ())
439
434
finally :
440
435
# Clean up the mess created by connection._close(). Since the
441
436
# connection is already closed, this crashes on some backends.
@@ -705,6 +700,7 @@ def test_check_constraints_sql_keywords(self):
705
700
706
701
class ThreadTests (TransactionTestCase ):
707
702
available_apps = ["backends" ]
703
+ databases = {"default" , "s1r2" , "s2r1" }
708
704
709
705
def test_default_connection_thread_local (self ):
710
706
"""
@@ -736,15 +732,17 @@ def runner():
736
732
t = threading .Thread (target = runner )
737
733
t .start ()
738
734
t .join ()
739
- # Each created connection got different inner connection .
735
+ # The inner connection is shared between threads .
740
736
self .assertEqual (
741
- len ({conn .connection for conn in connections_dict .values ()}), 3
737
+ len ({conn .connection for conn in connections_dict .values ()}), 1
742
738
)
743
739
finally :
744
740
# Finish by closing the connections opened by the other threads
745
741
# (the connection opened in the main thread will automatically be
746
742
# closed on teardown).
747
743
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 )
748
746
if conn is not connection and conn .allow_thread_sharing :
749
747
conn .close ()
750
748
conn .dec_thread_sharing ()
@@ -783,6 +781,10 @@ def runner():
783
781
# (the connection opened in the main thread will automatically be
784
782
# closed on teardown).
785
783
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 )
786
788
if conn is not connection and conn .allow_thread_sharing :
787
789
conn .close ()
788
790
conn .dec_thread_sharing ()
0 commit comments