summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2012-11-26 22:52:44 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2012-11-27 19:47:20 +0200
commit64f6e0370a8d8d2c088f189b622fa4b0726f41b5 (patch)
tree59c674968c732f4ecf29ec59af8e9d0f1b5ea0cc
parent905ea9619b871ded3204b1d0deb7e6fb9b081679 (diff)
Made some tests behave nicer re connection handling
-rw-r--r--tests/regressiontests/backends/tests.py16
-rw-r--r--tests/regressiontests/delete_regress/tests.py16
2 files changed, 15 insertions, 17 deletions
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
index 14b3e0053c..9cfdd73962 100644
--- a/tests/regressiontests/backends/tests.py
+++ b/tests/regressiontests/backends/tests.py
@@ -559,21 +559,27 @@ class ThreadTests(TestCase):
"""
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):
diff --git a/tests/regressiontests/delete_regress/tests.py b/tests/regressiontests/delete_regress/tests.py
index f94bb2f20c..c3e4a6a0b7 100644
--- a/tests/regressiontests/delete_regress/tests.py
+++ b/tests/regressiontests/delete_regress/tests.py
@@ -3,7 +3,8 @@ from __future__ import absolute_import
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 @@ from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
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)