-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
34 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,9 +121,8 @@ DATABASES = { | |
`OPTIONS` is an optional dictionary of parameters that will be passed to | ||
[`MongoClient`](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html). | ||
|
||
Alternatively, those that follow the [twelve-factor app]( | ||
https://www.12factor.net/backing-services) methodology can configure Django's | ||
`DATABASES` with `django_mongodb.parse_uri(MONGODB_URI)`: | ||
Alternatively, if you prefer to simply paste in a MongoDB URI rather than | ||
parsing it into the format above, you can use: | ||
|
||
```python | ||
import django_mongodb | ||
|
@@ -132,22 +131,12 @@ MONGODB_URI = "mongodb+srv://myDatabaseUser:D1fficultP%[email protected] | |
DATABASES["default"] = django_mongodb.parse_uri(MONGODB_URI) | ||
``` | ||
|
||
#### Additional `parse_uri` options | ||
#### `django_mongodb.parse_uri(uri, conn_max_age=0, conn_health_checks=False, test=None)` | ||
|
||
The `parse_uri` function accepts these keyword arguments: | ||
|
||
| Keyword argument | Default setting | | ||
| -------------------- | --------------------- | | ||
| `conn_max_age` | `0` | | ||
| `conn_health_checks` | `False` | | ||
| `test` | `None` | | ||
|
||
- The `conn_max_age` and `conn_health_checks` options can be used with | ||
[persistent database connections]( | ||
https://docs.djangoproject.com/en/latest/ref/databases/#persistent-database-connections). | ||
|
||
- The `test` option can be used to provide a dictionary of [settings for test databases]( | ||
https://docs.djangoproject.com/en/latest/ref/settings/#test). | ||
- Use `conn_max_age` and `conn_health_checks` to configure [persistent database | ||
connections](https://docs.djangoproject.com/en/stable/ref/databases/#persistent-database-connections). | ||
- Use `test` to provide a dictionary of [settings for test databases]( | ||
https://docs.djangoproject.com/en/stable/ref/settings/#test). | ||
|
||
Congratulations, your project is ready to go! | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,12 @@ | |
import pymongo | ||
from django.test import SimpleTestCase | ||
|
||
import django_mongodb | ||
from django_mongodb import parse_uri | ||
|
||
URI = "mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/myDatabase?retryWrites=true&w=majority&tls=false" | ||
|
||
|
||
class MongoParseURITests(SimpleTestCase): | ||
""" | ||
Test django_mongodb.parse_uri(uri) function | ||
""" | ||
|
||
class ParseURITests(SimpleTestCase): | ||
def setUp(self): | ||
self.srv_record = MagicMock() | ||
self.srv_record.target.to_text.return_value = "cluster0.example.mongodb.net" | ||
|
@@ -21,53 +17,58 @@ def setUp(self): | |
self.addCleanup(self.patcher.stop) | ||
|
||
@patch("dns.resolver.resolve") | ||
def test_uri(self, mock_resolver): | ||
settings_dict = django_mongodb.parse_uri( | ||
"mongodb://cluster0.example.mongodb.net/myDatabase" | ||
) | ||
def test_simple_uri(self, mock_resolver): | ||
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/myDatabase") | ||
self.assertEqual(settings_dict["ENGINE"], "django_mongodb") | ||
self.assertEqual(settings_dict["NAME"], "myDatabase") | ||
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net") | ||
|
||
@patch("dns.resolver.resolve") | ||
def test_no_database(self, mock_resolver): | ||
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/") | ||
self.assertEqual(settings_dict["ENGINE"], "django_mongodb") | ||
self.assertIsNone(settings_dict["NAME"]) | ||
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net") | ||
|
||
@patch("dns.resolver.resolve") | ||
def test_srv_uri_with_options(self, mock_resolver): | ||
settings_dict = django_mongodb.parse_uri(URI) | ||
settings_dict = parse_uri(URI) | ||
self.assertEqual(settings_dict["ENGINE"], "django_mongodb") | ||
self.assertEqual(settings_dict["NAME"], "myDatabase") | ||
self.assertEqual(settings_dict["HOST"], "mongodb+srv://cluster0.example.mongodb.net") | ||
self.assertEqual(settings_dict["USER"], "myDatabaseUser") | ||
self.assertEqual(settings_dict["PASSWORD"], "D1fficultP@ssw0rd") | ||
self.assertEqual(settings_dict["PORT"], None) | ||
self.assertIsNone(settings_dict["PORT"]) | ||
self.assertEqual( | ||
settings_dict["OPTIONS"], {"retryWrites": True, "w": "majority", "tls": False} | ||
) | ||
|
||
def test_localhost(self): | ||
settings_dict = django_mongodb.parse_uri("mongodb://localhost/myDatabase") | ||
settings_dict = parse_uri("mongodb://localhost/myDatabase") | ||
self.assertEqual(settings_dict["ENGINE"], "django_mongodb") | ||
self.assertEqual(settings_dict["NAME"], "myDatabase") | ||
self.assertEqual(settings_dict["HOST"], "localhost") | ||
|
||
def test_localhost_bad_credentials(self): | ||
with self.assertRaises(pymongo.errors.InvalidURI): | ||
django_mongodb.parse_uri("mongodb://:@localhost/myDatabase") | ||
|
||
@patch("dns.resolver.resolve") | ||
def test_conn_max_age_kwarg(self, mock_resolver): | ||
settings_dict = django_mongodb.parse_uri(URI, conn_max_age=600) | ||
def test_conn_max_age(self, mock_resolver): | ||
settings_dict = parse_uri(URI, conn_max_age=600) | ||
self.assertEqual(settings_dict["CONN_MAX_AGE"], 600) | ||
|
||
@patch("dns.resolver.resolve") | ||
def test_conn_health_checks_kwarg(self, mock_resolver): | ||
settings_dict = django_mongodb.parse_uri(URI, conn_health_checks=True) | ||
def test_conn_health_checks(self, mock_resolver): | ||
settings_dict = parse_uri(URI, conn_health_checks=True) | ||
self.assertEqual(settings_dict["CONN_HEALTH_CHECKS"], True) | ||
|
||
@patch("dns.resolver.resolve") | ||
def test_test_kwarg(self, mock_resolver): | ||
settings_dict = django_mongodb.parse_uri(URI, test={"NAME": "test_db"}) | ||
settings_dict = parse_uri(URI, test={"NAME": "test_db"}) | ||
self.assertEqual(settings_dict["TEST"]["NAME"], "test_db") | ||
|
||
def test_invalid_credentials(self): | ||
with self.assertRaises(pymongo.errors.InvalidURI): | ||
parse_uri("mongodb://:@localhost/myDatabase") | ||
|
||
@patch("dns.resolver.resolve") | ||
def test_uri_no_prefix(self, mock_resolver): | ||
def test_no_prefix(self, mock_resolver): | ||
with self.assertRaises(pymongo.errors.InvalidURI): | ||
django_mongodb.parse_uri("cluster0.example.mongodb.net/myDatabase") | ||
parse_uri("cluster0.example.mongodb.net/myDatabase") |