summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-08-30 13:25:22 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-08-30 13:25:22 +0000
commit62355d8253c299936b7df268f2792b5260c661b9 (patch)
treea9ae8eed4b29fa1011d3eeeb634fb61e85ebdaae
parent2c82a2aaf7e7d22fab5bfa181d153689c3d80c01 (diff)
[1.2.X] Fixed #13798 -- Added connection argument to the connection_created signal. Thanks to liangent for the report, and Alex Gaynor for the patch.
Backport of r13672 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13674 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/gis/db/backends/spatialite/base.py2
-rw-r--r--django/db/backends/mysql/base.py2
-rw-r--r--django/db/backends/oracle/base.py2
-rw-r--r--django/db/backends/postgresql/base.py5
-rw-r--r--django/db/backends/postgresql_psycopg2/base.py2
-rw-r--r--django/db/backends/signals.py2
-rw-r--r--django/db/backends/sqlite3/base.py2
-rw-r--r--tests/regressiontests/backends/tests.py63
8 files changed, 35 insertions, 45 deletions
diff --git a/django/contrib/gis/db/backends/spatialite/base.py b/django/contrib/gis/db/backends/spatialite/base.py
index d419dab5e1..729fc152e7 100644
--- a/django/contrib/gis/db/backends/spatialite/base.py
+++ b/django/contrib/gis/db/backends/spatialite/base.py
@@ -51,7 +51,7 @@ class DatabaseWrapper(SqliteDatabaseWrapper):
self.connection.create_function("django_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
self.connection.create_function("regexp", 2, _sqlite_regexp)
- connection_created.send(sender=self.__class__)
+ connection_created.send(sender=self.__class__, connection=self)
## From here on, customized for GeoDjango ##
diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
index e94e24bff9..a39c41f8d8 100644
--- a/django/db/backends/mysql/base.py
+++ b/django/db/backends/mysql/base.py
@@ -297,7 +297,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection = Database.connect(**kwargs)
self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
self.connection.encoders[SafeString] = self.connection.encoders[str]
- connection_created.send(sender=self.__class__)
+ connection_created.send(sender=self.__class__, connection=self)
cursor = CursorWrapper(self.connection.cursor())
return cursor
diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py
index 369e65baf7..0cf26c406d 100644
--- a/django/db/backends/oracle/base.py
+++ b/django/db/backends/oracle/base.py
@@ -384,7 +384,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
# Django docs specify cx_Oracle version 4.3.1 or higher, but
# stmtcachesize is available only in 4.3.2 and up.
pass
- connection_created.send(sender=self.__class__)
+ connection_created.send(sender=self.__class__, connection=self)
if not cursor:
cursor = FormatStylePlaceholderCursor(self.connection)
return cursor
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
index a1c858bd8f..f84ad1da61 100644
--- a/django/db/backends/postgresql/base.py
+++ b/django/db/backends/postgresql/base.py
@@ -135,8 +135,9 @@ class DatabaseWrapper(BaseDatabaseWrapper):
if settings_dict['PORT']:
conn_string += " port=%s" % settings_dict['PORT']
self.connection = Database.connect(conn_string, **settings_dict['OPTIONS'])
- self.connection.set_isolation_level(1) # make transactions transparent to all cursors
- connection_created.send(sender=self.__class__)
+ # make transactions transparent to all cursors
+ self.connection.set_isolation_level(1)
+ connection_created.send(sender=self.__class__, connection=self)
cursor = self.connection.cursor()
if new_connection:
if set_tz:
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
index 29b7e7ff1a..ce4e48330e 100644
--- a/django/db/backends/postgresql_psycopg2/base.py
+++ b/django/db/backends/postgresql_psycopg2/base.py
@@ -136,7 +136,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection = Database.connect(**conn_params)
self.connection.set_client_encoding('UTF8')
self.connection.set_isolation_level(self.isolation_level)
- connection_created.send(sender=self.__class__)
+ connection_created.send(sender=self.__class__, connection=self)
cursor = self.connection.cursor()
cursor.tzinfo_factory = None
if new_connection:
diff --git a/django/db/backends/signals.py b/django/db/backends/signals.py
index a8079d0d76..c16a63f9f6 100644
--- a/django/db/backends/signals.py
+++ b/django/db/backends/signals.py
@@ -1,3 +1,3 @@
from django.dispatch import Signal
-connection_created = Signal()
+connection_created = Signal(providing_args=["connection"])
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index bc97f5cfd8..1ab2557627 100644
--- a/django/db/backends/sqlite3/base.py
+++ b/django/db/backends/sqlite3/base.py
@@ -176,7 +176,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection.create_function("django_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
self.connection.create_function("regexp", 2, _sqlite_regexp)
- connection_created.send(sender=self.__class__)
+ connection_created.send(sender=self.__class__, connection=self)
return self.connection.cursor(factory=SQLiteCursorWrapper)
def close(self):
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
index 6a26a608eb..4984cdf4f7 100644
--- a/tests/regressiontests/backends/tests.py
+++ b/tests/regressiontests/backends/tests.py
@@ -5,6 +5,7 @@ import models
import unittest
from django.db import backend, connection, DEFAULT_DB_ALIAS
from django.db.backends.signals import connection_created
+from django.db.backends.postgresql import version as pg_version
from django.conf import settings
from django.test import TestCase
@@ -88,46 +89,34 @@ class ParameterHandlingTest(TestCase):
self.assertRaises(Exception, cursor.executemany, query, [(1,2,3),])
self.assertRaises(Exception, cursor.executemany, query, [(1,),])
+class PostgresVersionTest(TestCase):
+ def assert_parses(self, version_string, version):
+ self.assertEqual(pg_version._parse_version(version_string), version)
-def connection_created_test(sender, **kwargs):
- print 'connection_created signal'
-
-__test__ = {'API_TESTS': """
-# Check Postgres version parsing
->>> from django.db.backends.postgresql import version as pg_version
-
->>> pg_version._parse_version("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)")
-(8, 3, 1)
-
->>> pg_version._parse_version("PostgreSQL 8.3.6")
-(8, 3, 6)
-
->>> pg_version._parse_version("PostgreSQL 8.3")
-(8, 3, None)
-
->>> pg_version._parse_version("EnterpriseDB 8.3")
-(8, 3, None)
-
->>> pg_version._parse_version("PostgreSQL 8.3 beta4")
-(8, 3, None)
-
->>> pg_version._parse_version("PostgreSQL 8.4beta1")
-(8, 4, None)
-
-"""}
+ def test_parsing(self):
+ self.assert_parses("PostgreSQL 8.3 beta4", (8, 3, None))
+ self.assert_parses("PostgreSQL 8.3", (8, 3, None))
+ self.assert_parses("EnterpriseDB 8.3", (8, 3, None))
+ self.assert_parses("PostgreSQL 8.3.6", (8, 3, 6))
+ self.assert_parses("PostgreSQL 8.4beta1", (8, 4, None))
+ self.assert_parses("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)", (8, 3, 1))
# Unfortunately with sqlite3 the in-memory test database cannot be
# closed, and so it cannot be re-opened during testing, and so we
# sadly disable this test for now.
-if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
- __test__['API_TESTS'] += """
->>> connection_created.connect(connection_created_test)
->>> connection.close() # Ensure the connection is closed
->>> cursor = connection.cursor()
-connection_created signal
->>> connection_created.disconnect(connection_created_test)
->>> cursor = connection.cursor()
-"""
+if settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"] != "django.db.backends.sqlite3":
+ class ConnectionCreatedSignalTest(TestCase):
+ def test_signal(self):
+ data = {}
+ def receiver(sender, connection, **kwargs):
+ data["connection"] = connection
-if __name__ == '__main__':
- unittest.main()
+ connection_created.connect(receiver)
+ connection.close()
+ cursor = connection.cursor()
+ self.assertTrue(data["connection"] is connection)
+
+ connection_created.disconnect(receiver)
+ data.clear()
+ cursor = connection.cursor()
+ self.assertTrue(data == {})