Skip to content

Commit 0d96c97

Browse files
committed
Fix test cases which can run on remote server
1 parent 6a934c0 commit 0d96c97

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

pymongosql/sqlalchemy_mongodb/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,20 @@ def register_dialect():
128128
try:
129129
from sqlalchemy.dialects import registry
130130

131-
# Register for standard MongoDB URLs only
131+
# Register for standard MongoDB URLs
132132
registry.register("mongodb", "pymongosql.sqlalchemy_mongodb.sqlalchemy_dialect", "PyMongoSQLDialect")
133-
# Note: mongodb+srv is handled by converting to mongodb in create_connect_args
134-
# SQLAlchemy doesn't support the + character in scheme names directly
133+
134+
# Try to register both SRV forms so SQLAlchemy can resolve SRV-style URLs
135+
# (either 'mongodb+srv' or the dotted 'mongodb.srv' plugin name).
136+
# Some SQLAlchemy versions accept '+' in scheme names; others import
137+
# the dotted plugin name. Attempt both registrations in one block.
138+
try:
139+
registry.register("mongodb+srv", "pymongosql.sqlalchemy_mongodb.sqlalchemy_dialect", "PyMongoSQLDialect")
140+
registry.register("mongodb.srv", "pymongosql.sqlalchemy_mongodb.sqlalchemy_dialect", "PyMongoSQLDialect")
141+
except Exception:
142+
# If registration fails we fall back to handling SRV URIs in
143+
# create_engine_from_mongodb_uri by converting 'mongodb+srv' to 'mongodb'.
144+
pass
135145

136146
return True
137147
except ImportError:

tests/test_sqlalchemy_integration.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
4. Validating object creation from query results
1010
"""
1111

12+
import os
13+
1214
import pytest
1315

16+
from tests.conftest import TEST_DB, TEST_URI
17+
1418
# SQLAlchemy version compatibility
1519
try:
1620
import sqlalchemy
@@ -107,8 +111,32 @@ def __repr__(self):
107111
# Pytest fixtures
108112
@pytest.fixture
109113
def sqlalchemy_engine():
110-
"""Provide a SQLAlchemy engine connected to MongoDB."""
111-
engine = create_engine("mongodb://testuser:testpass@localhost:27017/test_db")
114+
"""Provide a SQLAlchemy engine connected to MongoDB. The URI is taken from environment variables
115+
(PYMONGOSQL_TEST_URI or MONGODB_URI) or falls back to a sensible local default.
116+
"""
117+
uri = os.environ.get("PYMONGOSQL_TEST_URI") or os.environ.get("MONGODB_URI") or TEST_URI
118+
db = os.environ.get("PYMONGOSQL_TEST_DB") or TEST_DB
119+
120+
def _ensure_uri_has_db(uri_value: str, database: str) -> str:
121+
if not database:
122+
return uri_value
123+
idx = uri_value.find("://")
124+
if idx == -1:
125+
return uri_value
126+
rest = uri_value[idx + 3 :]
127+
if "/" in rest:
128+
after = rest.split("/", 1)[1]
129+
if after == "" or after.startswith("?"):
130+
return uri_value.rstrip("/") + "/" + database
131+
return uri_value
132+
return uri_value.rstrip("/") + "/" + database
133+
134+
if uri:
135+
uri_to_use = _ensure_uri_has_db(uri, db)
136+
else:
137+
uri_to_use = "mongodb://testuser:testpass@localhost:27017/test_db"
138+
139+
engine = create_engine(uri_to_use)
112140
yield engine
113141

114142

0 commit comments

Comments
 (0)