-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INTPYTHON-509 add db_name parameter to parse_uri()
- Loading branch information
Showing
4 changed files
with
52 additions
and
25 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
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 |
---|---|---|
|
@@ -37,24 +37,17 @@ $ django-admin startproject example --template https://github.com/mongodb-labs/d | |
|
||
### Connect to the database | ||
|
||
Navigate to your `example/settings.py` file and find the variable named | ||
`DATABASES` Replace the `DATABASES` variable with this: | ||
Navigate to your `example/settings.py` file and replace the `DATABASES` | ||
setting like so: | ||
|
||
```python | ||
DATABASES = { | ||
"default": django_mongodb_backend.parse_uri("<CONNECTION_STRING_URI>"), | ||
"default": django_mongodb_backend.parse_uri( | ||
"<CONNECTION_STRING_URI>", db_name="example" | ||
), | ||
} | ||
``` | ||
|
||
The MongoDB `<CONNECTION_STRING_URI>` must also specify a database for the | ||
`parse_uri` function to work. | ||
If not already included, make sure you provide a value for `<DATABASE_NAME>` | ||
in your URI as shown in the example below: | ||
```bash | ||
mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/<DATABASE_NAME>?retryWrites=true&w=majority | ||
``` | ||
|
||
|
||
### Run the server | ||
To verify that you installed Django MongoDB Backend and correctly configured your project, run the following command from your project root: | ||
```bash | ||
|
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from unittest.mock import patch | ||
|
||
import pymongo | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.test import SimpleTestCase | ||
|
||
from django_mongodb_backend import parse_uri | ||
|
@@ -12,11 +13,28 @@ def test_simple_uri(self): | |
self.assertEqual(settings_dict["ENGINE"], "django_mongodb_backend") | ||
self.assertEqual(settings_dict["NAME"], "myDatabase") | ||
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net") | ||
self.assertEqual(settings_dict["OPTIONS"], {"authSource": "myDatabase"}) | ||
|
||
def test_no_database(self): | ||
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net") | ||
self.assertIsNone(settings_dict["NAME"]) | ||
def test_db_name(self): | ||
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/", db_name="myDatabase") | ||
self.assertEqual(settings_dict["ENGINE"], "django_mongodb_backend") | ||
self.assertEqual(settings_dict["NAME"], "myDatabase") | ||
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net") | ||
self.assertEqual(settings_dict["OPTIONS"], {}) | ||
|
||
def test_db_name_overrides_default_auth_db(self): | ||
settings_dict = parse_uri( | ||
"mongodb://cluster0.example.mongodb.net/default_auth_db", db_name="myDatabase" | ||
) | ||
self.assertEqual(settings_dict["ENGINE"], "django_mongodb_backend") | ||
self.assertEqual(settings_dict["NAME"], "myDatabase") | ||
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net") | ||
self.assertEqual(settings_dict["OPTIONS"], {"authSource": "default_auth_db"}) | ||
|
||
def test_no_database(self): | ||
msg = "You must provide the db_name parameter." | ||
with self.assertRaisesMessage(ImproperlyConfigured, msg): | ||
parse_uri("mongodb://cluster0.example.mongodb.net") | ||
|
||
def test_srv_uri_with_options(self): | ||
uri = "mongodb+srv://my_user:[email protected]/my_database?retryWrites=true&w=majority" | ||
|
@@ -30,35 +48,46 @@ def test_srv_uri_with_options(self): | |
self.assertEqual(settings_dict["PASSWORD"], "my_password") | ||
self.assertIsNone(settings_dict["PORT"]) | ||
self.assertEqual( | ||
settings_dict["OPTIONS"], {"retryWrites": True, "w": "majority", "tls": True} | ||
settings_dict["OPTIONS"], | ||
{"authSource": "my_database", "retryWrites": True, "w": "majority", "tls": True}, | ||
) | ||
|
||
def test_localhost(self): | ||
settings_dict = parse_uri("mongodb://localhost") | ||
settings_dict = parse_uri("mongodb://localhost/db") | ||
self.assertEqual(settings_dict["HOST"], "localhost") | ||
self.assertEqual(settings_dict["PORT"], 27017) | ||
|
||
def test_localhost_with_port(self): | ||
settings_dict = parse_uri("mongodb://localhost:27018") | ||
settings_dict = parse_uri("mongodb://localhost:27018/db") | ||
self.assertEqual(settings_dict["HOST"], "localhost") | ||
self.assertEqual(settings_dict["PORT"], 27018) | ||
|
||
def test_hosts_with_ports(self): | ||
settings_dict = parse_uri("mongodb://localhost:27017,localhost:27018") | ||
settings_dict = parse_uri("mongodb://localhost:27017,localhost:27018/db") | ||
self.assertEqual(settings_dict["HOST"], "localhost:27017,localhost:27018") | ||
self.assertEqual(settings_dict["PORT"], None) | ||
|
||
def test_hosts_without_ports(self): | ||
settings_dict = parse_uri("mongodb://host1.net,host2.net") | ||
settings_dict = parse_uri("mongodb://host1.net,host2.net/db") | ||
self.assertEqual(settings_dict["HOST"], "host1.net:27017,host2.net:27017") | ||
self.assertEqual(settings_dict["PORT"], None) | ||
|
||
def test_auth_source_in_query_string(self): | ||
settings_dict = parse_uri("mongodb://localhost/?authSource=auth", db_name="db") | ||
self.assertEqual(settings_dict["NAME"], "db") | ||
self.assertEqual(settings_dict["OPTIONS"], {"authSource": "auth"}) | ||
|
||
def test_auth_source_in_query_string_overrides_defaultauthdb(self): | ||
settings_dict = parse_uri("mongodb://localhost/db?authSource=auth") | ||
self.assertEqual(settings_dict["NAME"], "db") | ||
self.assertEqual(settings_dict["OPTIONS"], {"authSource": "auth"}) | ||
|
||
def test_conn_max_age(self): | ||
settings_dict = parse_uri("mongodb://localhost", conn_max_age=600) | ||
settings_dict = parse_uri("mongodb://localhost/db", conn_max_age=600) | ||
self.assertEqual(settings_dict["CONN_MAX_AGE"], 600) | ||
|
||
def test_test_kwarg(self): | ||
settings_dict = parse_uri("mongodb://localhost", test={"NAME": "test_db"}) | ||
settings_dict = parse_uri("mongodb://localhost/db", test={"NAME": "test_db"}) | ||
self.assertEqual(settings_dict["TEST"], {"NAME": "test_db"}) | ||
|
||
def test_invalid_credentials(self): | ||
|