|
| 1 | +from unittest.mock import patch |
| 2 | + |
1 | 3 | from django.core.exceptions import ImproperlyConfigured
|
2 | 4 | from django.db import connection
|
3 | 5 | from django.db.backends.signals import connection_created
|
|
6 | 8 | from django_mongodb_backend.base import DatabaseWrapper
|
7 | 9 |
|
8 | 10 |
|
9 |
| -class GetConnectionParamsTests(SimpleTestCase): |
| 11 | +class DatabaseWrapperTests(SimpleTestCase): |
10 | 12 | def test_database_name_empty(self):
|
11 | 13 | settings = connection.settings_dict.copy()
|
12 | 14 | settings["NAME"] = ""
|
13 | 15 | msg = 'settings.DATABASES is missing the "NAME" value.'
|
14 | 16 | with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
15 |
| - DatabaseWrapper(settings).get_connection_params() |
| 17 | + DatabaseWrapper(settings) |
| 18 | + |
| 19 | + def test_database_name_empty_and_host_does_not_contain_database(self): |
| 20 | + settings = connection.settings_dict.copy() |
| 21 | + settings["NAME"] = "" |
| 22 | + settings["HOST"] = "mongodb://localhost" |
| 23 | + msg = 'settings.DATABASES is missing the "NAME" value.' |
| 24 | + with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 25 | + DatabaseWrapper(settings) |
| 26 | + |
| 27 | + def test_database_name_parsed_from_host(self): |
| 28 | + settings = connection.settings_dict.copy() |
| 29 | + settings["NAME"] = "" |
| 30 | + settings["HOST"] = "mongodb://localhost/db" |
| 31 | + self.assertEqual(DatabaseWrapper(settings).settings_dict["NAME"], "db") |
16 | 32 |
|
| 33 | + def test_database_name_parsed_from_srv_host(self): |
| 34 | + settings = connection.settings_dict.copy() |
| 35 | + settings["NAME"] = "" |
| 36 | + settings["HOST"] = "mongodb+srv://localhost/db" |
| 37 | + # patch() prevents a crash when PyMongo attempts to resolve the |
| 38 | + # nonexistent SRV record. |
| 39 | + with patch("dns.resolver.resolve"): |
| 40 | + self.assertEqual(DatabaseWrapper(settings).settings_dict["NAME"], "db") |
| 41 | + |
| 42 | + def test_database_name_not_overridden_by_host(self): |
| 43 | + settings = connection.settings_dict.copy() |
| 44 | + settings["NAME"] = "not overridden" |
| 45 | + settings["HOST"] = "mongodb://localhost/db" |
| 46 | + self.assertEqual(DatabaseWrapper(settings).settings_dict["NAME"], "not overridden") |
| 47 | + |
| 48 | + |
| 49 | +class GetConnectionParamsTests(SimpleTestCase): |
17 | 50 | def test_host(self):
|
18 | 51 | settings = connection.settings_dict.copy()
|
19 | 52 | settings["HOST"] = "host"
|
@@ -56,6 +89,22 @@ def test_options(self):
|
56 | 89 | params = DatabaseWrapper(settings).get_connection_params()
|
57 | 90 | self.assertEqual(params["extra"], "option")
|
58 | 91 |
|
| 92 | + def test_unspecified_settings_omitted(self): |
| 93 | + settings = connection.settings_dict.copy() |
| 94 | + # django.db.utils.ConnectionHandler sets unspecified values to an empty |
| 95 | + # string. |
| 96 | + settings.update( |
| 97 | + { |
| 98 | + "USER": "", |
| 99 | + "PASSWORD": "", |
| 100 | + "PORT": "", |
| 101 | + } |
| 102 | + ) |
| 103 | + params = DatabaseWrapper(settings).get_connection_params() |
| 104 | + self.assertNotIn("username", params) |
| 105 | + self.assertNotIn("password", params) |
| 106 | + self.assertNotIn("port", params) |
| 107 | + |
59 | 108 |
|
60 | 109 | class DatabaseWrapperConnectionTests(TestCase):
|
61 | 110 | def test_set_autocommit(self):
|
|
0 commit comments