1
1
from unittest .mock import patch
2
2
3
3
import pymongo
4
+ from django .core .exceptions import ImproperlyConfigured
4
5
from django .test import SimpleTestCase
5
6
6
7
from django_mongodb_backend import parse_uri
@@ -14,9 +15,13 @@ def test_simple_uri(self):
14
15
self .assertEqual (settings_dict ["HOST" ], "cluster0.example.mongodb.net" )
15
16
16
17
def test_no_database (self ):
17
- settings_dict = parse_uri ("mongodb://cluster0.example.mongodb.net" )
18
- self .assertIsNone (settings_dict ["NAME" ])
19
- self .assertEqual (settings_dict ["HOST" ], "cluster0.example.mongodb.net" )
18
+ msg = (
19
+ "You must include the name of your database to the connection "
20
+ "string passed to parse_uri(), e.g. "
21
+ "mongodb://cluster0.example.mongodb.net/db_name?retryWrites=true&w=majority."
22
+ )
23
+ with self .assertRaisesMessage (ImproperlyConfigured , msg ):
24
+ parse_uri ("mongodb://cluster0.example.mongodb.net" )
20
25
21
26
def test_srv_uri_with_options (self ):
22
27
uri = "mongodb+srv://my_user:[email protected] /my_database?retryWrites=true&w=majority"
@@ -34,31 +39,31 @@ def test_srv_uri_with_options(self):
34
39
)
35
40
36
41
def test_localhost (self ):
37
- settings_dict = parse_uri ("mongodb://localhost" )
42
+ settings_dict = parse_uri ("mongodb://localhost/db " )
38
43
self .assertEqual (settings_dict ["HOST" ], "localhost" )
39
44
self .assertEqual (settings_dict ["PORT" ], 27017 )
40
45
41
46
def test_localhost_with_port (self ):
42
- settings_dict = parse_uri ("mongodb://localhost:27018" )
47
+ settings_dict = parse_uri ("mongodb://localhost:27018/db " )
43
48
self .assertEqual (settings_dict ["HOST" ], "localhost" )
44
49
self .assertEqual (settings_dict ["PORT" ], 27018 )
45
50
46
51
def test_hosts_with_ports (self ):
47
- settings_dict = parse_uri ("mongodb://localhost:27017,localhost:27018" )
52
+ settings_dict = parse_uri ("mongodb://localhost:27017,localhost:27018/db " )
48
53
self .assertEqual (settings_dict ["HOST" ], "localhost:27017,localhost:27018" )
49
54
self .assertEqual (settings_dict ["PORT" ], None )
50
55
51
56
def test_hosts_without_ports (self ):
52
- settings_dict = parse_uri ("mongodb://host1.net,host2.net" )
57
+ settings_dict = parse_uri ("mongodb://host1.net,host2.net/db " )
53
58
self .assertEqual (settings_dict ["HOST" ], "host1.net:27017,host2.net:27017" )
54
59
self .assertEqual (settings_dict ["PORT" ], None )
55
60
56
61
def test_conn_max_age (self ):
57
- settings_dict = parse_uri ("mongodb://localhost" , conn_max_age = 600 )
62
+ settings_dict = parse_uri ("mongodb://localhost/db " , conn_max_age = 600 )
58
63
self .assertEqual (settings_dict ["CONN_MAX_AGE" ], 600 )
59
64
60
65
def test_test_kwarg (self ):
61
- settings_dict = parse_uri ("mongodb://localhost" , test = {"NAME" : "test_db" })
66
+ settings_dict = parse_uri ("mongodb://localhost/db " , test = {"NAME" : "test_db" })
62
67
self .assertEqual (settings_dict ["TEST" ], {"NAME" : "test_db" })
63
68
64
69
def test_invalid_credentials (self ):
0 commit comments