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,12 @@ 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 in the connection "
20
+ "string passed to parse_uri(), e.g. mongodb://host/db_name?query_string"
21
+ )
22
+ with self .assertRaisesMessage (ImproperlyConfigured , msg ):
23
+ parse_uri ("mongodb://cluster0.example.mongodb.net" )
20
24
21
25
def test_srv_uri_with_options (self ):
22
26
uri = "mongodb+srv://my_user:my_password@cluster0.example.mongodb.net/my_database?retryWrites=true&w=majority"
@@ -34,31 +38,31 @@ def test_srv_uri_with_options(self):
34
38
)
35
39
36
40
def test_localhost (self ):
37
- settings_dict = parse_uri ("mongodb://localhost" )
41
+ settings_dict = parse_uri ("mongodb://localhost/db " )
38
42
self .assertEqual (settings_dict ["HOST" ], "localhost" )
39
43
self .assertEqual (settings_dict ["PORT" ], 27017 )
40
44
41
45
def test_localhost_with_port (self ):
42
- settings_dict = parse_uri ("mongodb://localhost:27018" )
46
+ settings_dict = parse_uri ("mongodb://localhost:27018/db " )
43
47
self .assertEqual (settings_dict ["HOST" ], "localhost" )
44
48
self .assertEqual (settings_dict ["PORT" ], 27018 )
45
49
46
50
def test_hosts_with_ports (self ):
47
- settings_dict = parse_uri ("mongodb://localhost:27017,localhost:27018" )
51
+ settings_dict = parse_uri ("mongodb://localhost:27017,localhost:27018/db " )
48
52
self .assertEqual (settings_dict ["HOST" ], "localhost:27017,localhost:27018" )
49
53
self .assertEqual (settings_dict ["PORT" ], None )
50
54
51
55
def test_hosts_without_ports (self ):
52
- settings_dict = parse_uri ("mongodb://host1.net,host2.net" )
56
+ settings_dict = parse_uri ("mongodb://host1.net,host2.net/db " )
53
57
self .assertEqual (settings_dict ["HOST" ], "host1.net:27017,host2.net:27017" )
54
58
self .assertEqual (settings_dict ["PORT" ], None )
55
59
56
60
def test_conn_max_age (self ):
57
- settings_dict = parse_uri ("mongodb://localhost" , conn_max_age = 600 )
61
+ settings_dict = parse_uri ("mongodb://localhost/db " , conn_max_age = 600 )
58
62
self .assertEqual (settings_dict ["CONN_MAX_AGE" ], 600 )
59
63
60
64
def test_test_kwarg (self ):
61
- settings_dict = parse_uri ("mongodb://localhost" , test = {"NAME" : "test_db" })
65
+ settings_dict = parse_uri ("mongodb://localhost/db " , test = {"NAME" : "test_db" })
62
66
self .assertEqual (settings_dict ["TEST" ], {"NAME" : "test_db" })
63
67
64
68
def test_invalid_credentials (self ):
0 commit comments