Skip to content

Commit c242744

Browse files
committed
require settings.DATABASES['NAME']
1 parent 36c5718 commit c242744

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

.github/workflows/runtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"apps",
2222
"async",
2323
"auth_tests",
24+
"backend_",
2425
"backends",
2526
"basic",
2627
"bulk_create",

django_mongodb/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import contextlib
22

3+
from django.core.exceptions import ImproperlyConfigured
34
from django.db.backends.base.base import BaseDatabaseWrapper
45
from pymongo.collection import Collection
56
from pymongo.mongo_client import MongoClient
@@ -151,13 +152,13 @@ def __getattr__(self, attr):
151152
raise AttributeError(attr)
152153

153154
def init_connection_state(self):
154-
db_name = self.settings_dict["NAME"]
155-
if db_name:
156-
self.database = self.connection[db_name]
155+
self.database = self.connection[self.settings_dict["NAME"]]
157156
super().init_connection_state()
158157

159158
def get_connection_params(self):
160159
settings_dict = self.settings_dict
160+
if not settings_dict["NAME"]:
161+
raise ImproperlyConfigured('settings.DATABASES is missing the "NAME" value.')
161162
return {
162163
"host": settings_dict["HOST"] or None,
163164
"port": int(settings_dict["PORT"] or 27017),

tests/backend_/__init__.py

Whitespace-only changes.

tests/backend_/test_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.core.exceptions import ImproperlyConfigured
2+
from django.db import connection
3+
from django.test import SimpleTestCase
4+
5+
from django_mongodb.base import DatabaseWrapper
6+
7+
8+
class DatabaseWrapperTests(SimpleTestCase):
9+
def test_database_name_empty(self):
10+
settings = connection.settings_dict.copy()
11+
settings["NAME"] = ""
12+
msg = 'settings.DATABASES is missing the "NAME" value.'
13+
with self.assertRaisesMessage(ImproperlyConfigured, msg):
14+
DatabaseWrapper(settings).get_connection_params()

0 commit comments

Comments
 (0)