8000 add MongoDB srv support by stenczelt · Pull Request #115 · libAtoms/abcd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add MongoDB srv support #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions abcd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ def from_url(cls, url, **kwargs):
from abcd.backends.atoms_pymongo import MongoDatabase

return MongoDatabase(db_name=db, **conn_settings, **kwargs)
elif r.scheme == "mongodb+srv":
db = r.path.split("/")[1] if r.path else None
db = db if db else "abcd"
from abcd.backends.atoms_pymongo import MongoDatabase

return MongoDatabase(db_name=db, host=r.geturl(), uri_mode=True, **kwargs)
elif r.scheme == "http" or r.scheme == "https":
raise NotImplementedError("http not yet supported! soon...")
elif r.scheme == "ssh":
Expand Down
18 changes: 11 additions & 7 deletions abcd/backends/atoms_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def __init__(
username=None,
password=None,
authSource="admin",
uri_mode=False,
**kwargs
):
super().__init__()
Expand All @@ -181,13 +182,16 @@ def __init__(
)
)

self.client = MongoClient(
host=host,
port=port,
username=username,
password=password,
authSource=authSource,
)
if uri_mode:
self.client = MongoClient(host=host, authSource=authSource)
else:
self.client = MongoClient(
host=host,
port=port,
username=username,
password=password,
authSource=authSource,
)

try:
info = self.client.server_info() # Forces a call.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lark = "^1.1.9"
mongomock = "^4.1.2"
pytest = "^8.2.2"
pytest-cov = "^5.0.0"
pytest-mock = "^3.14.0"

[tool.poetry.extras]
tests = ["mongomock", "pytest", "pytest-cov"]
Expand Down
29 changes: 29 additions & 0 deletions tests/test_mongodb_srv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Tests for supporting `mongodb+srv://` URIs"""

from pytest import fixture

from abcd import ABCD


@fixture
def mongo_srv(mocker):
# mongomock does not pick up what we need, so this is a bespoke mocker
mock_client = mocker.MagicMock()
mocker.patch("abcd.backends.atoms_pymongo.MongoClient", mock_client)
return mock_client


def test_init_mongodb_srv(mongo_srv):
# client can be created with a mongodb+srv:// URI

# apparently mongomock breaks if this import is outside
from abcd.backends.atoms_pymongo import MongoDatabase

# regression test
uri = "mongodb+srv://user:pass@democluster.randomstr.mongodb.net/?key=value"

# create the client
abcd = ABCD.from_url(uri)

assert isinstance(abcd, MongoDatabase)
mongo_srv.assert_called_once_with(host=uri, authSource="admin")
0