Skip to content

Commit 961ec66

Browse files
committed
Add a test for connection.database in the connection_created signal.
I contemplated removing the self.database assignment from DatabaseWrapper.init_connection_state() which would have broken this.
1 parent 0ac7ca9 commit 961ec66

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tests/backend_/test_base.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.core.exceptions import ImproperlyConfigured
22
from django.db import connection
3+
from django.db.backends.signals import connection_created
34
from django.test import SimpleTestCase, TestCase
45

56
from django_mongodb_backend.base import DatabaseWrapper
@@ -21,3 +22,23 @@ def test_set_autocommit(self):
2122
self.assertIs(connection.get_autocommit(), False)
2223
connection.set_autocommit(True)
2324
self.assertIs(connection.get_autocommit(), True)
25+
26+
def test_connection_created_database_attr(self):
27+
"""
28+
connection.database is available in the connection_created signal.
29+
"""
30+
data = {}
31+
32+
def receiver(sender, connection, **kwargs): # noqa: ARG001
33+
data["database"] = connection.database
34+
35+
connection_created.connect(receiver)
36+
connection.close()
37+
# Accessing database implicitly connects.
38+
connection.database # noqa: B018
39+
self.assertIs(data["database"], connection.database)
40+
connection.close()
41+
connection_created.disconnect(receiver)
42+
data.clear()
43+
connection.connect()
44+
self.assertEqual(data, {})

0 commit comments

Comments
 (0)