Skip to content

Commit 7ca6682

Browse files
committed
INTPYTHON-509 add a helpful error message when database name is missing
1 parent 75d42fa commit 7ca6682

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

django_mongodb_backend/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def parse_uri(uri, conn_max_age=0, test=None):
4545
host, port = nodelist[0]
4646
elif len(nodelist) > 1:
4747
host = ",".join([f"{host}:{port}" for host, port in nodelist])
48+
if not uri["database"]:
49+
raise ImproperlyConfigured(
50+
"You must include the name of your database to the connection "
51+
"string passed to parse_uri(), e.g. "
52+
"mongodb://cluster0.example.mongodb.net/db_name?retryWrites=true&w=majority."
53+
)
4854
settings_dict = {
4955
"ENGINE": "django_mongodb_backend",
5056
"NAME": uri["database"],

tests/backend_/utils/test_parse_uri.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import patch
22

33
import pymongo
4+
from django.core.exceptions import ImproperlyConfigured
45
from django.test import SimpleTestCase
56

67
from django_mongodb_backend import parse_uri
@@ -14,9 +15,13 @@ def test_simple_uri(self):
1415
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net")
1516

1617
def test_no_database(self):
17-
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net")
18-
self.assertIsNone(settings_dict["NAME"])
19-
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net")
18+
msg = (
19+
"You must include the name of your database to the connection "
20+
"string passed to parse_uri(), e.g. "
21+
"mongodb://cluster0.example.mongodb.net/db_name?retryWrites=true&w=majority."
22+
)
23+
with self.assertRaisesMessage(ImproperlyConfigured, msg):
24+
parse_uri("mongodb://cluster0.example.mongodb.net")
2025

2126
def test_srv_uri_with_options(self):
2227
uri = "mongodb+srv://my_user:[email protected]/my_database?retryWrites=true&w=majority"
@@ -34,31 +39,31 @@ def test_srv_uri_with_options(self):
3439
)
3540

3641
def test_localhost(self):
37-
settings_dict = parse_uri("mongodb://localhost")
42+
settings_dict = parse_uri("mongodb://localhost/db")
3843
self.assertEqual(settings_dict["HOST"], "localhost")
3944
self.assertEqual(settings_dict["PORT"], 27017)
4045

4146
def test_localhost_with_port(self):
42-
settings_dict = parse_uri("mongodb://localhost:27018")
47+
settings_dict = parse_uri("mongodb://localhost:27018/db")
4348
self.assertEqual(settings_dict["HOST"], "localhost")
4449
self.assertEqual(settings_dict["PORT"], 27018)
4550

4651
def test_hosts_with_ports(self):
47-
settings_dict = parse_uri("mongodb://localhost:27017,localhost:27018")
52+
settings_dict = parse_uri("mongodb://localhost:27017,localhost:27018/db")
4853
self.assertEqual(settings_dict["HOST"], "localhost:27017,localhost:27018")
4954
self.assertEqual(settings_dict["PORT"], None)
5055

5156
def test_hosts_without_ports(self):
52-
settings_dict = parse_uri("mongodb://host1.net,host2.net")
57+
settings_dict = parse_uri("mongodb://host1.net,host2.net/db")
5358
self.assertEqual(settings_dict["HOST"], "host1.net:27017,host2.net:27017")
5459
self.assertEqual(settings_dict["PORT"], None)
5560

5661
def test_conn_max_age(self):
57-
settings_dict = parse_uri("mongodb://localhost", conn_max_age=600)
62+
settings_dict = parse_uri("mongodb://localhost/db", conn_max_age=600)
5863
self.assertEqual(settings_dict["CONN_MAX_AGE"], 600)
5964

6065
def test_test_kwarg(self):
61-
settings_dict = parse_uri("mongodb://localhost", test={"NAME": "test_db"})
66+
settings_dict = parse_uri("mongodb://localhost/db", test={"NAME": "test_db"})
6267
self.assertEqual(settings_dict["TEST"], {"NAME": "test_db"})
6368

6469
def test_invalid_credentials(self):

0 commit comments

Comments
 (0)