Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Made some tests behave nicer re connection handling

  • Loading branch information...
commit 64f6e0370a8d8d2c088f189b622fa4b0726f41b5 1 parent 905ea96
@akaariai akaariai authored
View
16 tests/regressiontests/backends/tests.py
@@ -559,21 +559,27 @@ def test_default_connection_thread_local(self):
"""
connections_set = set()
connection.cursor()
- connections_set.add(connection.connection)
+ connections_set.add(connection)
def runner():
- from django.db import connection
+ # Passing django.db.connection between threads doesn't work while
+ # connections[DEFAULT_DB_ALIAS] does.
+ from django.db import connections
+ connection = connections[DEFAULT_DB_ALIAS]
connection.cursor()
- connections_set.add(connection.connection)
+ connections_set.add(connection)
for x in range(2):
t = threading.Thread(target=runner)
t.start()
t.join()
- self.assertEqual(len(connections_set), 3)
+ # Check that each created connection got different inner connection.
+ self.assertEqual(
+ len(set([conn.connection for conn in connections_set])),
+ 3)
# Finish by closing the connections opened by the other threads (the
# connection opened in the main thread will automatically be closed on
# teardown).
for conn in connections_set:
- if conn != connection.connection:
+ if conn != connection:
conn.close()
def test_connections_thread_local(self):
View
16 tests/regressiontests/delete_regress/tests.py
@@ -3,7 +3,8 @@
import datetime
from django.conf import settings
-from django.db import backend, transaction, DEFAULT_DB_ALIAS, models
+from django.db import transaction, DEFAULT_DB_ALIAS, models
+from django.db.utils import ConnectionHandler
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
@@ -17,17 +18,8 @@
class DeleteLockingTest(TransactionTestCase):
def setUp(self):
# Create a second connection to the default database
- conn_settings = settings.DATABASES[DEFAULT_DB_ALIAS]
- self.conn2 = backend.DatabaseWrapper({
- 'HOST': conn_settings['HOST'],
- 'NAME': conn_settings['NAME'],
- 'OPTIONS': conn_settings['OPTIONS'],
- 'PASSWORD': conn_settings['PASSWORD'],
- 'PORT': conn_settings['PORT'],
- 'USER': conn_settings['USER'],
- 'TIME_ZONE': settings.TIME_ZONE,
- })
-
+ new_connections = ConnectionHandler(settings.DATABASES)
+ self.conn2 = new_connections[DEFAULT_DB_ALIAS]
# Put both DB connections into managed transaction mode
transaction.enter_transaction_management()
transaction.managed(True)
Please sign in to comment.
Something went wrong with that request. Please try again.