Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bdb8625

Browse files
committedFeb 4, 2025·
INTPYTHON-509 make parse_uri() raise a helpful message when database name is missing
1 parent 75d42fa commit bdb8625

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed
 

‎django_mongodb_backend/utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ 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 in the connection "
51+
"string passed to parse_uri(), e.g. mongodb://host/db_name?query_string"
52+
)
4853
settings_dict = {
4954
"ENGINE": "django_mongodb_backend",
5055
"NAME": uri["database"],

‎tests/backend_/utils/test_parse_uri.py

+13-9
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,12 @@ 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 in the connection "
20+
"string passed to parse_uri(), e.g. mongodb://host/db_name?query_string"
21+
)
22+
with self.assertRaisesMessage(ImproperlyConfigured, msg):
23+
parse_uri("mongodb://cluster0.example.mongodb.net")
2024

2125
def test_srv_uri_with_options(self):
2226
uri = "mongodb+srv://my_user:my_password@cluster0.example.mongodb.net/my_database?retryWrites=true&w=majority"
@@ -34,31 +38,31 @@ def test_srv_uri_with_options(self):
3438
)
3539

3640
def test_localhost(self):
37-
settings_dict = parse_uri("mongodb://localhost")
41+
settings_dict = parse_uri("mongodb://localhost/db")
3842
self.assertEqual(settings_dict["HOST"], "localhost")
3943
self.assertEqual(settings_dict["PORT"], 27017)
4044

4145
def test_localhost_with_port(self):
42-
settings_dict = parse_uri("mongodb://localhost:27018")
46+
settings_dict = parse_uri("mongodb://localhost:27018/db")
4347
self.assertEqual(settings_dict["HOST"], "localhost")
4448
self.assertEqual(settings_dict["PORT"], 27018)
4549

4650
def test_hosts_with_ports(self):
47-
settings_dict = parse_uri("mongodb://localhost:27017,localhost:27018")
51+
settings_dict = parse_uri("mongodb://localhost:27017,localhost:27018/db")
4852
self.assertEqual(settings_dict["HOST"], "localhost:27017,localhost:27018")
4953
self.assertEqual(settings_dict["PORT"], None)
5054

5155
def test_hosts_without_ports(self):
52-
settings_dict = parse_uri("mongodb://host1.net,host2.net")
56+
settings_dict = parse_uri("mongodb://host1.net,host2.net/db")
5357
self.assertEqual(settings_dict["HOST"], "host1.net:27017,host2.net:27017")
5458
self.assertEqual(settings_dict["PORT"], None)
5559

5660
def test_conn_max_age(self):
57-
settings_dict = parse_uri("mongodb://localhost", conn_max_age=600)
61+
settings_dict = parse_uri("mongodb://localhost/db", conn_max_age=600)
5862
self.assertEqual(settings_dict["CONN_MAX_AGE"], 600)
5963

6064
def test_test_kwarg(self):
61-
settings_dict = parse_uri("mongodb://localhost", test={"NAME": "test_db"})
65+
settings_dict = parse_uri("mongodb://localhost/db", test={"NAME": "test_db"})
6266
self.assertEqual(settings_dict["TEST"], {"NAME": "test_db"})
6367

6468
def test_invalid_credentials(self):

0 commit comments

Comments
 (0)
Please sign in to comment.