Skip to content

Commit ff701bd

Browse files
authored
Merge pull request #2787 from bagerard/2644_add_read_preference_tags_set
Add support for readPreferenceTags in connection parameters #2644
2 parents 7b41658 + 9d8a326 commit ff701bd

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Development
1010
- Fix for uuidRepresentation not read when provided in URI #2741
1111
- Add tests against MongoDB 6.0 and MongoDB 7.0 in the pipeline
1212
- Fix validate() not being called when inheritance is used in EmbeddedDocument and validate is overriden #2784
13+
- Add support for readPreferenceTags in connection parameters #2644
1314

1415
Changes in 0.27.0
1516
=================

mongoengine/connection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,16 @@ def _get_connection_settings(
164164
preference.name.lower() == read_pf_mode
165165
or preference.mode == read_pf_mode
166166
):
167-
conn_settings["read_preference"] = preference
167+
ReadPrefClass = preference.__class__
168168
break
169+
170+
if "readpreferencetags" in uri_options:
171+
conn_settings["read_preference"] = ReadPrefClass(
172+
tag_sets=uri_options["readpreferencetags"]
173+
)
174+
else:
175+
conn_settings["read_preference"] = ReadPrefClass()
176+
169177
if "authmechanismproperties" in uri_options:
170178
conn_settings["authmechanismproperties"] = uri_options[
171179
"authmechanismproperties"

tests/test_connection.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
InvalidOperation,
1212
OperationFailure,
1313
)
14+
from pymongo.read_preferences import Secondary
1415

1516
import mongoengine.connection
1617
from mongoengine import (
@@ -24,6 +25,7 @@
2425
from mongoengine.connection import (
2526
DEFAULT_DATABASE_NAME,
2627
ConnectionFailure,
28+
_get_connection_settings,
2729
disconnect,
2830
get_connection,
2931
get_db,
@@ -180,6 +182,35 @@ def test_connect_fails_if_similar_connection_settings_arent_defined_the_same_way
180182
with pytest.raises(ConnectionFailure):
181183
connect(host="mongodb://localhost:27017/%s" % db_name, alias=db_alias)
182184

185+
def test___get_connection_settings(self):
186+
funky_host = "mongodb://root:12345678@1.1.1.1:27017,2.2.2.2:27017,3.3.3.3:27017/db_api?replicaSet=s0&readPreference=secondary&uuidRepresentation=javaLegacy&readPreferenceTags=region:us-west-2,usage:api"
187+
settings = _get_connection_settings(host=funky_host)
188+
189+
if PYMONGO_VERSION < (4,):
190+
read_pref = Secondary(
191+
tag_sets=[{"region": "us-west-2", "usage": "api"}],
192+
max_staleness=-1,
193+
)
194+
else:
195+
read_pref = Secondary(
196+
tag_sets=[{"region": "us-west-2", "usage": "api"}],
197+
max_staleness=-1,
198+
hedge=None,
199+
)
200+
assert settings == {
201+
"authentication_mechanism": None,
202+
"authentication_source": None,
203+
"authmechanismproperties": None,
204+
"host": [funky_host],
205+
"name": "db_api",
206+
"password": "12345678",
207+
"port": 27017,
208+
"read_preference": read_pref,
209+
"replicaSet": "s0",
210+
"username": "root",
211+
"uuidrepresentation": "javaLegacy",
212+
}
213+
183214
def test_connect_passes_silently_connect_multiple_times_with_same_config(self):
184215
# test default connection to `test`
185216
connect()

0 commit comments

Comments
 (0)