Skip to content

Commit 33b5481

Browse files
committed
edits
1 parent 6e636cd commit 33b5481

File tree

3 files changed

+34
-51
lines changed

3 files changed

+34
-51
lines changed

README.md

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ DATABASES = {
121121
`OPTIONS` is an optional dictionary of parameters that will be passed to
122122
[`MongoClient`](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html).
123123

124-
Alternatively, those that follow the [twelve-factor app](
125-
https://www.12factor.net/backing-services) methodology can configure Django's
126-
`DATABASES` with `django_mongodb.parse_uri(MONGODB_URI)`:
124+
Alternatively, if you prefer to simply paste in a MongoDB URI rather than
125+
parsing it into the format above, you can use:
127126

128127
```python
129128
import django_mongodb
@@ -132,22 +131,12 @@ MONGODB_URI = "mongodb+srv://myDatabaseUser:D1fficultP%[email protected]
132131
DATABASES["default"] = django_mongodb.parse_uri(MONGODB_URI)
133132
```
134133

135-
#### Additional `parse_uri` options
134+
#### `django_mongodb.parse_uri(uri, conn_max_age=0, conn_health_checks=False, test=None)`
136135

137-
The `parse_uri` function accepts these keyword arguments:
138-
139-
| Keyword argument | Default setting |
140-
| -------------------- | --------------------- |
141-
| `conn_max_age` | `0` |
142-
| `conn_health_checks` | `False` |
143-
| `test` | `None` |
144-
145-
- The `conn_max_age` and `conn_health_checks` options can be used with
146-
[persistent database connections](
147-
https://docs.djangoproject.com/en/latest/ref/databases/#persistent-database-connections).
148-
149-
- The `test` option can be used to provide a dictionary of [settings for test databases](
150-
https://docs.djangoproject.com/en/latest/ref/settings/#test).
136+
- Use `conn_max_age` and `conn_health_checks` to configure [persistent database
137+
connections](https://docs.djangoproject.com/en/stable/ref/databases/#persistent-database-connections).
138+
- Use `test` to provide a dictionary of [settings for test databases](
139+
https://docs.djangoproject.com/en/stable/ref/settings/#test).
151140

152141
Congratulations, your project is ready to go!
153142

django_mongodb/utils.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@ def check_django_compatability():
2828

2929
def parse_uri(uri, conn_max_age=0, conn_health_checks=False, test=None):
3030
"""
31-
Parse a MongoDB URI and return a dictionary of Django database
32-
settings. This function is a wrapper around PyMongo's
33-
``pymongo.uri_parser.parse_uri()`` function that converts PyMongo's
34-
settings dictionary into a Django database settings dictionary.
31+
Convert the given uri into a dictionary suitable for Django's DATABASES
32+
setting.
3533
"""
3634
uri = parse_uri_mongo(uri)
37-
3835
host = None
3936
port = None
40-
4137
if uri["fqdn"] is not None:
4238
# If the fqdn is present, this is a SRV URI and the host is the fqdn.
4339
host = f"mongodb+srv://{uri['fqdn']}"
@@ -47,7 +43,6 @@ def parse_uri(uri, conn_max_age=0, conn_health_checks=False, test=None):
4743
host, port = nodelist[0]
4844
elif len(nodelist) > 1:
4945
host = ",".join([f"{host}:{port}" for host, port in nodelist])
50-
5146
settings_dict = {
5247
"ENGINE": "django_mongodb",
5348
"NAME": uri["database"],
@@ -59,10 +54,8 @@ def parse_uri(uri, conn_max_age=0, conn_health_checks=False, test=None):
5954
"CONN_MAX_AGE": conn_max_age,
6055
"CONN_HEALTH_CHECKS": conn_health_checks,
6156
}
62-
6357
if test:
6458
settings_dict["TEST"] = test
65-
6659
return settings_dict
6760

6861

tests/utils_/test_parse.py renamed to tests/utils_/test_parse_uri.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
import pymongo
44
from django.test import SimpleTestCase
55

6-
import django_mongodb
6+
from django_mongodb import parse_uri
77

88
URI = "mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/myDatabase?retryWrites=true&w=majority&tls=false"
99

1010

11-
class MongoParseURITests(SimpleTestCase):
12-
"""
13-
Test django_mongodb.parse_uri(uri) function
14-
"""
15-
11+
class ParseURITests(SimpleTestCase):
1612
def setUp(self):
1713
self.srv_record = MagicMock()
1814
self.srv_record.target.to_text.return_value = "cluster0.example.mongodb.net"
@@ -21,53 +17,58 @@ def setUp(self):
2117
self.addCleanup(self.patcher.stop)
2218

2319
@patch("dns.resolver.resolve")
24-
def test_uri(self, mock_resolver):
25-
settings_dict = django_mongodb.parse_uri(
26-
"mongodb://cluster0.example.mongodb.net/myDatabase"
27-
)
20+
def test_simple_uri(self, mock_resolver):
21+
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/myDatabase")
2822
self.assertEqual(settings_dict["ENGINE"], "django_mongodb")
2923
self.assertEqual(settings_dict["NAME"], "myDatabase")
3024
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net")
3125

26+
@patch("dns.resolver.resolve")
27+
def test_no_database(self, mock_resolver):
28+
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/")
29+
self.assertEqual(settings_dict["ENGINE"], "django_mongodb")
30+
self.assertIsNone(settings_dict["NAME"])
31+
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net")
32+
3233
@patch("dns.resolver.resolve")
3334
def test_srv_uri_with_options(self, mock_resolver):
34-
settings_dict = django_mongodb.parse_uri(URI)
35+
settings_dict = parse_uri(URI)
3536
self.assertEqual(settings_dict["ENGINE"], "django_mongodb")
3637
self.assertEqual(settings_dict["NAME"], "myDatabase")
3738
self.assertEqual(settings_dict["HOST"], "mongodb+srv://cluster0.example.mongodb.net")
3839
self.assertEqual(settings_dict["USER"], "myDatabaseUser")
3940
self.assertEqual(settings_dict["PASSWORD"], "D1fficultP@ssw0rd")
40-
self.assertEqual(settings_dict["PORT"], None)
41+
self.assertIsNone(settings_dict["PORT"])
4142
self.assertEqual(
4243
settings_dict["OPTIONS"], {"retryWrites": True, "w": "majority", "tls": False}
4344
)
4445

4546
def test_localhost(self):
46-
settings_dict = django_mongodb.parse_uri("mongodb://localhost/myDatabase")
47+
settings_dict = parse_uri("mongodb://localhost/myDatabase")
4748
self.assertEqual(settings_dict["ENGINE"], "django_mongodb")
4849
self.assertEqual(settings_dict["NAME"], "myDatabase")
4950
self.assertEqual(settings_dict["HOST"], "localhost")
5051

51-
def test_localhost_bad_credentials(self):
52-
with self.assertRaises(pymongo.errors.InvalidURI):
53-
django_mongodb.parse_uri("mongodb://:@localhost/myDatabase")
54-
5552
@patch("dns.resolver.resolve")
56-
def test_conn_max_age_kwarg(self, mock_resolver):
57-
settings_dict = django_mongodb.parse_uri(URI, conn_max_age=600)
53+
def test_conn_max_age(self, mock_resolver):
54+
settings_dict = parse_uri(URI, conn_max_age=600)
5855
self.assertEqual(settings_dict["CONN_MAX_AGE"], 600)
5956

6057
@patch("dns.resolver.resolve")
61-
def test_conn_health_checks_kwarg(self, mock_resolver):
62-
settings_dict = django_mongodb.parse_uri(URI, conn_health_checks=True)
58+
def test_conn_health_checks(self, mock_resolver):
59+
settings_dict = parse_uri(URI, conn_health_checks=True)
6360
self.assertEqual(settings_dict["CONN_HEALTH_CHECKS"], True)
6461

6562
@patch("dns.resolver.resolve")
6663
def test_test_kwarg(self, mock_resolver):
67-
settings_dict = django_mongodb.parse_uri(URI, test={"NAME": "test_db"})
64+
settings_dict = parse_uri(URI, test={"NAME": "test_db"})
6865
self.assertEqual(settings_dict["TEST"]["NAME"], "test_db")
6966

67+
def test_invalid_credentials(self):
68+
with self.assertRaises(pymongo.errors.InvalidURI):
69+
parse_uri("mongodb://:@localhost/myDatabase")
70+
7071
@patch("dns.resolver.resolve")
71-
def test_uri_no_prefix(self, mock_resolver):
72+
def test_no_prefix(self, mock_resolver):
7273
with self.assertRaises(pymongo.errors.InvalidURI):
73-
django_mongodb.parse_uri("cluster0.example.mongodb.net/myDatabase")
74+
parse_uri("cluster0.example.mongodb.net/myDatabase")

0 commit comments

Comments
 (0)