Skip to content

Commit

Permalink
s/parse/parse_uri/
Browse files Browse the repository at this point in the history
  • Loading branch information
aclark4life committed Dec 3, 2024
1 parent 5ebf6a2 commit 79e3058
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions django_mongodb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Check Django compatibility before other imports which may fail if the
# wrong version of Django is installed.
from .utils import check_django_compatability, parse
from .utils import check_django_compatability, parse_uri

check_django_compatability()

Expand All @@ -14,7 +14,7 @@
from .lookups import register_lookups # noqa: E402
from .query import register_nodes # noqa: E402

__all__ = ['parse']
__all__ = ["parse_uri"]

register_aggregates()
register_expressions()
Expand Down
6 changes: 3 additions & 3 deletions django_mongodb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.db.backends.utils import logger
from django.utils.version import get_version_tuple
from pymongo.uri_parser import parse_uri
from pymongo.uri_parser import parse_uri as parse_uri_mongo


def check_django_compatability():
Expand All @@ -26,7 +26,7 @@ def check_django_compatability():
)


def parse(
def parse_uri(
uri, engine=None, conn_max_age=None, conn_health_checks=None, ssl_require=None, test=None
):
"""
Expand All @@ -35,7 +35,7 @@ def parse(
``pymongo.uri_parser.parse_uri()`` function that converts PyMongo's
settings dictionary into a Django database settings dictionary.
"""
uri = parse_uri(uri)
uri = parse_uri_mongo(uri)

host = None
port = None
Expand Down
24 changes: 13 additions & 11 deletions tests/utils_/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class MongoParseURITests(SimpleTestCase):
"""
Test django_mongodb.parse(uri) function
Test django_mongodb.parse_uri(uri) function
"""

def setUp(self):
Expand All @@ -22,14 +22,16 @@ def setUp(self):

@patch("dns.resolver.resolve")
def test_uri(self, mock_resolver):
settings_dict = django_mongodb.parse("mongodb://cluster0.example.mongodb.net/myDatabase")
settings_dict = django_mongodb.parse_uri(
"mongodb://cluster0.example.mongodb.net/myDatabase"
)
self.assertEqual(settings_dict["ENGINE"], "django_mongodb")
self.assertEqual(settings_dict["NAME"], "myDatabase")
self.assertEqual(settings_dict["HOST"], "cluster0.example.mongodb.net")

@patch("dns.resolver.resolve")
def test_srv_uri_with_options(self, mock_resolver):
settings_dict = django_mongodb.parse(URI)
settings_dict = django_mongodb.parse_uri(URI)
self.assertEqual(settings_dict["ENGINE"], "django_mongodb")
self.assertEqual(settings_dict["NAME"], "myDatabase")
self.assertEqual(settings_dict["HOST"], "mongodb+srv://cluster0.example.mongodb.net")
Expand All @@ -41,41 +43,41 @@ def test_srv_uri_with_options(self, mock_resolver):
)

def test_localhost(self):
settings_dict = django_mongodb.parse("mongodb://localhost/myDatabase")
settings_dict = django_mongodb.parse_uri("mongodb://localhost/myDatabase")
self.assertEqual(settings_dict["ENGINE"], "django_mongodb")
self.assertEqual(settings_dict["NAME"], "myDatabase")
self.assertEqual(settings_dict["HOST"], "localhost")

def test_localhost_bad_credentials(self):
with self.assertRaises(pymongo.errors.InvalidURI):
django_mongodb.parse("mongodb://:@localhost/myDatabase")
django_mongodb.parse_uri("mongodb://:@localhost/myDatabase")

@patch("dns.resolver.resolve")
def test_engine_kwarg(self, mock_resolver):
settings_dict = django_mongodb.parse(URI, engine="some_other_engine")
settings_dict = django_mongodb.parse_uri(URI, engine="some_other_engine")
self.assertEqual(settings_dict["ENGINE"], "some_other_engine")

@patch("dns.resolver.resolve")
def test_conn_max_age_kwarg(self, mock_resolver):
settings_dict = django_mongodb.parse(URI, conn_max_age=600)
settings_dict = django_mongodb.parse_uri(URI, conn_max_age=600)
self.assertEqual(settings_dict["CONN_MAX_AGE"], 600)

@patch("dns.resolver.resolve")
def test_conn_health_checks_kwarg(self, mock_resolver):
settings_dict = django_mongodb.parse(URI, conn_health_checks=True)
settings_dict = django_mongodb.parse_uri(URI, conn_health_checks=True)
self.assertEqual(settings_dict["CONN_HEALTH_CHECKS"], True)

@patch("dns.resolver.resolve")
def test_ssl_require_kwarg(self, mock_resolver):
settings_dict = django_mongodb.parse(URI, ssl_require=True)
settings_dict = django_mongodb.parse_uri(URI, ssl_require=True)
self.assertEqual(settings_dict["SSL_REQUIRE"], True)

@patch("dns.resolver.resolve")
def test_test_kwarg(self, mock_resolver):
settings_dict = django_mongodb.parse(URI, test={"NAME": "test_db"})
settings_dict = django_mongodb.parse_uri(URI, test={"NAME": "test_db"})
self.assertEqual(settings_dict["TEST"]["NAME"], "test_db")

@patch("dns.resolver.resolve")
def test_uri_no_prefix(self, mock_resolver):
with self.assertRaises(pymongo.errors.InvalidURI):
django_mongodb.parse("cluster0.example.mongodb.net/myDatabase")
django_mongodb.parse_uri("cluster0.example.mongodb.net/myDatabase")

0 comments on commit 79e3058

Please sign in to comment.