|
| 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"] = "should not be overridden" |
| 45 | + settings["HOST"] = "mongodb://localhost/db" |
| 46 | + self.assertEqual( |
| 47 | + DatabaseWrapper(settings).settings_dict["NAME"], "should not be overridden" |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +class GetConnectionParamsTests(SimpleTestCase): |
17 | 52 | def test_host(self): |
18 | 53 | settings = connection.settings_dict.copy() |
19 | 54 | settings["HOST"] = "host" |
|
0 commit comments