11from unittest .mock import patch
22
33import pymongo
4+ from django .core .exceptions import ImproperlyConfigured
45from django .test import SimpleTestCase
56
67from django_mongodb_backend import parse_uri
@@ -14,9 +15,12 @@ def test_simple_uri(self):
1415 self .assertEqual (settings_dict ["HOST" ], "cluster0.example.mongodb.net" )
1516
1617 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" )
2024
2125 def test_srv_uri_with_options (self ):
2226 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):
3438 )
3539
3640 def test_localhost (self ):
37- settings_dict = parse_uri ("mongodb://localhost" )
41+ settings_dict = parse_uri ("mongodb://localhost/db " )
3842 self .assertEqual (settings_dict ["HOST" ], "localhost" )
3943 self .assertEqual (settings_dict ["PORT" ], 27017 )
4044
4145 def test_localhost_with_port (self ):
42- settings_dict = parse_uri ("mongodb://localhost:27018" )
46+ settings_dict = parse_uri ("mongodb://localhost:27018/db " )
4347 self .assertEqual (settings_dict ["HOST" ], "localhost" )
4448 self .assertEqual (settings_dict ["PORT" ], 27018 )
4549
4650 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 " )
4852 self .assertEqual (settings_dict ["HOST" ], "localhost:27017,localhost:27018" )
4953 self .assertEqual (settings_dict ["PORT" ], None )
5054
5155 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 " )
5357 self .assertEqual (settings_dict ["HOST" ], "host1.net:27017,host2.net:27017" )
5458 self .assertEqual (settings_dict ["PORT" ], None )
5559
5660 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 )
5862 self .assertEqual (settings_dict ["CONN_MAX_AGE" ], 600 )
5963
6064 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" })
6266 self .assertEqual (settings_dict ["TEST" ], {"NAME" : "test_db" })
6367
6468 def test_invalid_credentials (self ):
0 commit comments