|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pymongo |
| 4 | +from django.test import SimpleTestCase |
| 5 | + |
| 6 | +from django_mongodb import parse_uri |
| 7 | + |
| 8 | + |
| 9 | +class ParseURITests(SimpleTestCase): |
| 10 | + def test_simple_uri(self): |
| 11 | + settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/myDatabase") |
| 12 | + self.assertEqual(settings_dict["ENGINE"], "django_mongodb") |
| 13 | + self.assertEqual(settings_dict["NAME"], "myDatabase") |
| 14 | + self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net") |
| 15 | + |
| 16 | + 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") |
| 20 | + |
| 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. |
| 25 | + with patch("dns.resolver.resolve"): |
| 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 | + ) |
| 35 | + |
| 36 | + def test_localhost(self): |
| 37 | + settings_dict = parse_uri("mongodb://localhost") |
| 38 | + self.assertEqual(settings_dict["HOST"], "localhost") |
| 39 | + self.assertEqual(settings_dict["PORT"], 27017) |
| 40 | + |
| 41 | + def test_localhost_with_port(self): |
| 42 | + settings_dict = parse_uri("mongodb://localhost:27018") |
| 43 | + self.assertEqual(settings_dict["HOST"], "localhost") |
| 44 | + self.assertEqual(settings_dict["PORT"], 27018) |
| 45 | + |
| 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") |
| 54 | + self.assertEqual(settings_dict["PORT"], None) |
| 55 | + |
| 56 | + def test_conn_max_age(self): |
| 57 | + settings_dict = parse_uri("mongodb://localhost", conn_max_age=600) |
| 58 | + self.assertEqual(settings_dict["CONN_MAX_AGE"], 600) |
| 59 | + |
| 60 | + def test_test_kwarg(self): |
| 61 | + settings_dict = parse_uri("mongodb://localhost", test={"NAME": "test_db"}) |
| 62 | + self.assertEqual(settings_dict["TEST"], {"NAME": "test_db"}) |
| 63 | + |
| 64 | + def test_invalid_credentials(self): |
| 65 | + msg = "The empty string is not valid username." |
| 66 | + with self.assertRaisesMessage(pymongo.errors.InvalidURI, msg): |
| 67 | + parse_uri("mongodb://:@localhost") |
| 68 | + |
| 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