|
11 | 11 | InvalidOperation, |
12 | 12 | OperationFailure, |
13 | 13 | ) |
| 14 | +from pymongo.read_preferences import Secondary |
14 | 15 |
|
15 | 16 | import mongoengine.connection |
16 | 17 | from mongoengine import ( |
|
24 | 25 | from mongoengine.connection import ( |
25 | 26 | DEFAULT_DATABASE_NAME, |
26 | 27 | ConnectionFailure, |
| 28 | + _get_connection_settings, |
27 | 29 | disconnect, |
28 | 30 | get_connection, |
29 | 31 | get_db, |
@@ -180,6 +182,35 @@ def test_connect_fails_if_similar_connection_settings_arent_defined_the_same_way |
180 | 182 | with pytest.raises(ConnectionFailure): |
181 | 183 | connect(host="mongodb://localhost:27017/%s" % db_name, alias=db_alias) |
182 | 184 |
|
| 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 | + |
183 | 214 | def test_connect_passes_silently_connect_multiple_times_with_same_config(self): |
184 | 215 | # test default connection to `test` |
185 | 216 | connect() |
|
0 commit comments