diff --git a/.github/workflows/runtests.py b/.github/workflows/runtests.py index 62610b28..41d12812 100755 --- a/.github/workflows/runtests.py +++ b/.github/workflows/runtests.py @@ -21,6 +21,7 @@ "apps", "async", "auth_tests", + "backend_", "backends", "basic", "bulk_create", diff --git a/django_mongodb/base.py b/django_mongodb/base.py index a3f4379d..4a48d873 100644 --- a/django_mongodb/base.py +++ b/django_mongodb/base.py @@ -1,5 +1,6 @@ import contextlib +from django.core.exceptions import ImproperlyConfigured from django.db.backends.base.base import BaseDatabaseWrapper from pymongo.collection import Collection from pymongo.mongo_client import MongoClient @@ -151,13 +152,13 @@ def __getattr__(self, attr): raise AttributeError(attr) def init_connection_state(self): - db_name = self.settings_dict["NAME"] - if db_name: - self.database = self.connection[db_name] + self.database = self.connection[self.settings_dict["NAME"]] super().init_connection_state() def get_connection_params(self): settings_dict = self.settings_dict + if not settings_dict["NAME"]: + raise ImproperlyConfigured('settings.DATABASES is missing the "NAME" value.') return { "host": settings_dict["HOST"] or None, "port": int(settings_dict["PORT"] or 27017), diff --git a/tests/backend_/__init__.py b/tests/backend_/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/backend_/test_base.py b/tests/backend_/test_base.py new file mode 100644 index 00000000..9b084926 --- /dev/null +++ b/tests/backend_/test_base.py @@ -0,0 +1,14 @@ +from django.core.exceptions import ImproperlyConfigured +from django.db import connection +from django.test import SimpleTestCase + +from django_mongodb.base import DatabaseWrapper + + +class DatabaseWrapperTests(SimpleTestCase): + def test_database_name_empty(self): + settings = connection.settings_dict.copy() + settings["NAME"] = "" + msg = 'settings.DATABASES is missing the "NAME" value.' + with self.assertRaisesMessage(ImproperlyConfigured, msg): + DatabaseWrapper(settings).get_connection_params()