File tree Expand file tree Collapse file tree
pymongosql/sqlalchemy_mongodb Expand file tree Collapse file tree Original file line number Diff line number Diff 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 :
Original file line number Diff line number Diff line change 994. Validating object creation from query results
1010"""
1111
12+ import os
13+
1214import pytest
1315
16+ from tests .conftest import TEST_DB , TEST_URI
17+
1418# SQLAlchemy version compatibility
1519try :
1620 import sqlalchemy
@@ -107,8 +111,32 @@ def __repr__(self):
107111# Pytest fixtures
108112@pytest .fixture
109113def 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
You can’t perform that action at this time.
0 commit comments