5
5
6
6
from django_mongodb import parse_uri
7
7
8
- URI = "mongodb+srv://myDatabaseUser:D1fficultP%[email protected] /myDatabase?retryWrites=true&w=majority&tls=false"
9
-
10
8
11
9
class ParseURITests (SimpleTestCase ):
12
10
def test_simple_uri (self ):
@@ -16,65 +14,58 @@ def test_simple_uri(self):
16
14
self .assertEqual (settings_dict ["HOST" ], "cluster0.example.mongodb.net" )
17
15
18
16
def test_no_database (self ):
19
- settings_dict = parse_uri ("mongodb://cluster0.example.mongodb.net/" )
20
- self .assertEqual (settings_dict ["ENGINE" ], "django_mongodb" )
17
+ settings_dict = parse_uri ("mongodb://cluster0.example.mongodb.net" )
21
18
self .assertIsNone (settings_dict ["NAME" ])
22
19
self .assertEqual (settings_dict ["HOST" ], "cluster0.example.mongodb.net" )
23
20
24
- # PyMongo will try to resolve the SRV record if the URI has the mongodb+srv:// prefix.
25
- # https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient
26
21
def test_srv_uri_with_options (self ):
22
+ uri = "mongodb+srv://my_user:[email protected] /my_database?retryWrites=true&w=majority"
23
+ # patch() prevents a crash when PyMongo attempts to resolve the
24
+ # nonexistent SRV record.
27
25
with patch ("dns.resolver.resolve" ):
28
- settings_dict = parse_uri (URI )
29
- self .assertEqual (settings_dict ["ENGINE" ], "django_mongodb" )
30
- self .assertEqual (settings_dict ["NAME" ], "myDatabase" )
31
- self .assertEqual (settings_dict ["HOST" ], "mongodb+srv://cluster0.example.mongodb.net" )
32
- self .assertEqual (settings_dict ["USER" ], "myDatabaseUser" )
33
- self .assertEqual (settings_dict ["PASSWORD" ], "D1fficultP@ssw0rd" )
34
- self .assertIsNone (settings_dict ["PORT" ])
35
- self .assertEqual (
36
- settings_dict ["OPTIONS" ], {"retryWrites" : True , "w" : "majority" , "tls" : False }
37
- )
26
+ settings_dict = parse_uri (uri )
27
+ self .assertEqual (settings_dict ["NAME" ], "my_database" )
28
+ self .assertEqual (settings_dict ["HOST" ], "mongodb+srv://cluster0.example.mongodb.net" )
29
+ self .assertEqual (settings_dict ["USER" ], "my_user" )
30
+ self .assertEqual (settings_dict ["PASSWORD" ], "my_password" )
31
+ self .assertIsNone (settings_dict ["PORT" ])
32
+ self .assertEqual (
33
+ settings_dict ["OPTIONS" ], {"retryWrites" : True , "w" : "majority" , "tls" : True }
34
+ )
38
35
39
36
def test_localhost (self ):
40
- settings_dict = parse_uri ("mongodb://localhost/myDatabase" )
41
- self .assertEqual (settings_dict ["ENGINE" ], "django_mongodb" )
42
- self .assertEqual (settings_dict ["NAME" ], "myDatabase" )
37
+ settings_dict = parse_uri ("mongodb://localhost" )
43
38
self .assertEqual (settings_dict ["HOST" ], "localhost" )
39
+ self .assertEqual (settings_dict ["PORT" ], 27017 )
44
40
45
41
def test_localhost_with_port (self ):
46
- settings_dict = parse_uri ("mongodb://localhost/myDatabase" )
47
- self .assertEqual (settings_dict ["ENGINE" ], "django_mongodb" )
48
- self .assertEqual (settings_dict ["NAME" ], "myDatabase" )
42
+ settings_dict = parse_uri ("mongodb://localhost:27018" )
49
43
self .assertEqual (settings_dict ["HOST" ], "localhost" )
50
- self .assertEqual (settings_dict ["PORT" ], 27017 )
44
+ self .assertEqual (settings_dict ["PORT" ], 27018 )
51
45
52
- def test_localhosts_with_ports (self ):
53
- settings_dict = parse_uri (
54
- "mongodb://localhost:27017,localhost:27018,localhost:27019/myDatabase"
55
- )
56
- self .assertEqual (settings_dict ["ENGINE" ], "django_mongodb" )
57
- self .assertEqual (settings_dict ["NAME" ], "myDatabase" )
58
- self .assertEqual (settings_dict ["HOST" ], "localhost:27017,localhost:27018,localhost:27019" )
46
+ def test_hosts_with_ports (self ):
47
+ settings_dict = parse_uri ("mongodb://localhost:27017,localhost:27018" )
48
+ self .assertEqual (settings_dict ["HOST" ], "localhost:27017,localhost:27018" )
49
+ self .assertEqual (settings_dict ["PORT" ], None )
50
+
51
+ def test_hosts_without_ports (self ):
52
+ settings_dict = parse_uri ("mongodb://host1.net,host2.net" )
53
+ self .assertEqual (settings_dict ["HOST" ], "host1.net:27017,host2.net:27017" )
59
54
self .assertEqual (settings_dict ["PORT" ], None )
60
55
61
56
def test_conn_max_age (self ):
62
- settings_dict = parse_uri ("mongodb://localhost/myDatabase " , conn_max_age = 600 )
57
+ settings_dict = parse_uri ("mongodb://localhost" , conn_max_age = 600 )
63
58
self .assertEqual (settings_dict ["CONN_MAX_AGE" ], 600 )
64
59
65
60
def test_test_kwarg (self ):
66
- settings_dict = parse_uri ("mongodb://localhost/myDatabase " , test = {"NAME" : "test_db" })
61
+ settings_dict = parse_uri ("mongodb://localhost" , test = {"NAME" : "test_db" })
67
62
self .assertEqual (settings_dict ["TEST" ]["NAME" ], "test_db" )
68
63
69
64
def test_invalid_credentials (self ):
70
- with self .assertRaises (pymongo .errors .InvalidURI ):
71
- parse_uri ("mongodb://:@localhost/myDatabase" )
72
-
73
- def test_no_prefix (self ):
74
- with self .assertRaises (pymongo .errors .InvalidURI ):
75
- parse_uri ("cluster0.example.mongodb.net/myDatabase" )
65
+ msg = "The empty string is not valid username."
66
+ with self .assertRaisesMessage (pymongo .errors .InvalidURI , msg ):
67
+ parse_uri ("mongodb://:@localhost" )
76
68
77
- def test_hosts_without_ports (self ):
78
- settings_dict = parse_uri ("mongodb://host1.net,host2.net/myDatabase" )
79
- self .assertEqual (settings_dict ["HOST" ], "host1.net:27017,host2.net:27017" )
80
- self .assertEqual (settings_dict ["PORT" ], None )
69
+ def test_no_scheme (self ):
70
+ with self .assertRaisesMessage (pymongo .errors .InvalidURI , "Invalid URI scheme" ):
71
+ parse_uri ("cluster0.example.mongodb.net" )
0 commit comments