Skip to content

Commit

Permalink
Added as_uri method to MongoDB transport - Fixes #1795 (#1796)
Browse files Browse the repository at this point in the history
* Added as_uri method to MongoDB transport, Added tests

* Make flake8 happy

* Removed unnecessary test
  • Loading branch information
romanukes authored Oct 4, 2023
1 parent 0e445d1 commit 2454aa7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions kombu/transport/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from kombu.utils.encoding import bytes_to_str
from kombu.utils.json import dumps, loads
from kombu.utils.objects import cached_property
from kombu.utils.url import maybe_sanitize_url

from . import virtual
from .base import to_rabbitmq_queue_arguments
Expand Down Expand Up @@ -510,3 +511,15 @@ class Transport(virtual.Transport):

def driver_version(self):
return pymongo.version

def as_uri(self, uri: str, include_password=False, mask='**') -> str:
if not uri:
return 'mongodb://'
if include_password:
return uri

if ',' not in uri:
return maybe_sanitize_url(uri)

uri1, remainder = uri.split(',', 1)
return ','.join([maybe_sanitize_url(uri1), remainder])
15 changes: 15 additions & 0 deletions t/unit/transport/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ def test_custom_host(self):

assert dbname == 'kombu_default'

def test_custom_port(self):
url = 'mongodb://localhost:27018'
channel = _create_mock_connection(url).default_channel
hostname, dbname, options = channel._parse_uri()

assert hostname == 'mongodb://localhost:27018'

def test_replicaset_hosts(self):
url = 'mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=test_rs' # noqa
channel = _create_mock_connection(url).default_channel
hostname, dbname, options = channel._parse_uri()

assert hostname == 'mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=test_rs' # noqa
assert options['replicaset'] == 'test_rs'

def test_custom_database(self):
url = 'mongodb://localhost/dbname'
channel = _create_mock_connection(url).default_channel
Expand Down

0 comments on commit 2454aa7

Please sign in to comment.