Skip to content

Commit a42eedb

Browse files
committed
kwargs docs; remove engine; "fix" test
1 parent f40a275 commit a42eedb

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ 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 methodology](
125-
https://www.12factor.net/backing-services) can configure Django's `DATABASES`
126-
with `django_mongodb.parse_uri(MONGODB_URI)`:
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)`:
127127

128128
```python
129129
import django_mongodb
@@ -132,6 +132,22 @@ MONGODB_URI = "mongodb+srv://myDatabaseUser:D1fficultP%[email protected]
132132
DATABASES["default"] = django_mongodb.parse_uri(MONGODB_URI)
133133
```
134134

135+
#### Additional options
136+
137+
`parse_uri` also accepts these keyword arguments:
138+
139+
| Argument | Default |
140+
| -------------------- | --------------------- |
141+
| `conn_max_age` | `0` |
142+
| `conn_health_checks` | `False` |
143+
| `test` | `{}` |
144+
145+
`conn_max_age` and `conn_health_checks` can be used with [persistent database
146+
connections](https://docs.djangoproject.com/en/latest/ref/databases/#persistent-database-connections)
147+
148+
`test` can be used to provide a dictionary of [settings for test databases](
149+
https://docs.djangoproject.com/en/latest/ref/settings/#test).
150+
135151
Congratulations, your project is ready to go!
136152

137153
## Notes on Django QuerySets

django_mongodb/utils.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def check_django_compatability():
2626
)
2727

2828

29-
def parse_uri(uri, engine="django_mongodb", conn_max_age=None, conn_health_checks=None, test=None):
29+
def parse_uri(uri, conn_max_age=0, conn_health_checks=False, test=None):
3030
"""
3131
Parse a MongoDB URI and return a dictionary of Django database
3232
settings. This function is a wrapper around PyMongo's
@@ -49,22 +49,18 @@ def parse_uri(uri, engine="django_mongodb", conn_max_age=None, conn_health_check
4949
host = ",".join([f"{host}:{port}" for host, port in nodelist])
5050

5151
settings_dict = {
52-
"ENGINE": engine,
52+
"ENGINE": "django_mongodb",
5353
"NAME": uri["database"],
5454
"HOST": host,
5555
"PORT": port,
5656
"USER": uri.get("username"),
5757
"PASSWORD": uri.get("password"),
5858
"OPTIONS": uri.get("options"),
59+
"CONN_MAX_AGE": conn_max_age,
60+
"CONN_HEALTH_CHECKS": conn_health_checks,
5961
}
6062

61-
if conn_max_age is not None:
62-
settings_dict["CONN_MAX_AGE"] = conn_max_age
63-
64-
if conn_health_checks is not None:
65-
settings_dict["CONN_HEALTH_CHECKS"] = conn_health_checks
66-
67-
if test is not None:
63+
if test:
6864
settings_dict["TEST"] = test
6965

7066
return settings_dict

tests/utils_/test_parse.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ def test_localhost_bad_credentials(self):
5252
with self.assertRaises(pymongo.errors.InvalidURI):
5353
django_mongodb.parse_uri("mongodb://:@localhost/myDatabase")
5454

55-
@patch("dns.resolver.resolve")
56-
def test_engine_kwarg(self, mock_resolver):
57-
settings_dict = django_mongodb.parse_uri(URI, engine="some_other_engine")
58-
self.assertEqual(settings_dict["ENGINE"], "some_other_engine")
59-
6055
@patch("dns.resolver.resolve")
6156
def test_conn_max_age_kwarg(self, mock_resolver):
6257
settings_dict = django_mongodb.parse_uri(URI, conn_max_age=600)

0 commit comments

Comments
 (0)