Skip to content

Commit 42d15f6

Browse files
committed
INTPYTHON-654 Add options kwarg to parse_uri
1 parent fe862bf commit 42d15f6

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

django_mongodb_backend/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def check_django_compatability():
2828
)
2929

3030

31-
def parse_uri(uri, *, db_name=None, test=None):
31+
def parse_uri(uri, *, db_name=None, test=None, options=None):
3232
"""
3333
Convert the given uri into a dictionary suitable for Django's DATABASES
3434
setting.
@@ -48,14 +48,17 @@ def parse_uri(uri, *, db_name=None, test=None):
4848
db_name = db_name or uri["database"]
4949
if not db_name:
5050
raise ImproperlyConfigured("You must provide the db_name parameter.")
51+
opts = uri.get("options")
52+
if options:
53+
opts = {**opts, **options}
5154
settings_dict = {
5255
"ENGINE": "django_mongodb_backend",
5356
"NAME": db_name,
5457
"HOST": host,
5558
"PORT": port,
5659
"USER": uri.get("username"),
5760
"PASSWORD": uri.get("password"),
58-
"OPTIONS": uri.get("options"),
61+
"OPTIONS": opts,
5962
}
6063
if "authSource" not in settings_dict["OPTIONS"] and uri["database"]:
6164
settings_dict["OPTIONS"]["authSource"] = uri["database"]

tests/backend_/utils/test_parse_uri.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ def test_invalid_credentials(self):
9494
def test_no_scheme(self):
9595
with self.assertRaisesMessage(pymongo.errors.InvalidURI, "Invalid URI scheme"):
9696
parse_uri("cluster0.example.mongodb.net")
97+
98+
def test_options_kwarg(self):
99+
options = {"authSource": "auth"}
100+
settings_dict = parse_uri(
101+
"mongodb://cluster0.example.mongodb.net/myDatabase",
102+
options=options,
103+
)
104+
self.assertEqual(settings_dict["OPTIONS"], {"authSource": "auth"})

0 commit comments

Comments
 (0)