Skip to content

Commit 34ace3c

Browse files
committed
Make tests compatible with pymongo >= 4.14
mongodb/mongo-python-driver#2413 caused some test regressions here. This isn't currently a problem for the upstream test suite since it pins pymongo==4.10.1 via kombu, but we're running into it in Debian where we've already upgraded pymongo for other reasons. kombu already tried to upgrade pymongo but had to revert due to these test regressions (see celery/kombu#2384 and celery#9938). One of the test fixes (relating to `mongodb_backend_settings`) illustrates an incompatibility where I couldn't figure out a reasonable way to avoid passing it through to Celery users, so I added a note to the documentation about it. It may also be worth including a brief mention of it in the release notes. Using the canonical case for the option in question should work with both old and new versions of pymongo.
1 parent 2aebae5 commit 34ace3c

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

docs/userguide/configuration.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,12 @@ This is a dict supporting the following keys:
12081208
constructor. See the :mod:`pymongo` docs to see a list of arguments
12091209
supported.
12101210

1211+
.. note::
1212+
1213+
With pymongo>=4.14, options are case-sensitive when they were previously
1214+
case-insensitive. See :class:`~pymongo.mongo_client.MongoClient` to
1215+
determine the correct case.
1216+
12111217
.. _example-mongodb-result-config:
12121218

12131219
Example configuration

t/unit/backends/test_mongodb.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,21 @@ def test_init_with_settings(self):
130130
'celerydatabase?replicaSet=rs0')
131131
mb = MongoBackend(app=self.app, url=uri)
132132
assert mb.mongo_host == MONGODB_BACKEND_HOST
133-
assert mb.options == dict(
134-
mb._prepare_client_options(),
135-
replicaset='rs0',
136-
)
133+
if 'replicaSet' in mb.options: # pragma: no cover # pymongo >= 4.14
134+
replicaset_option = 'replicaSet'
135+
else: # pragma: no cover # pymongo < 4.14
136+
replicaset_option = 'replicaset'
137+
assert mb.options == {
138+
**mb._prepare_client_options(),
139+
replicaset_option: 'rs0',
140+
}
137141
assert mb.user == CELERY_USER
138142
assert mb.password == CELERY_PASSWORD
139143
assert mb.database_name == CELERY_DATABASE
140144

141145
# same uri, change some parameters in backend settings
142146
self.app.conf.mongodb_backend_settings = {
143-
'replicaset': 'rs1',
147+
replicaset_option: 'rs1',
144148
'user': 'backenduser',
145149
'database': 'another_db',
146150
'options': {
@@ -149,11 +153,11 @@ def test_init_with_settings(self):
149153
}
150154
mb = MongoBackend(app=self.app, url=uri)
151155
assert mb.mongo_host == MONGODB_BACKEND_HOST
152-
assert mb.options == dict(
153-
mb._prepare_client_options(),
154-
replicaset='rs1',
155-
socketKeepAlive=True,
156-
)
156+
assert mb.options == {
157+
**mb._prepare_client_options(),
158+
replicaset_option: 'rs1',
159+
'socketKeepAlive': True,
160+
}
157161
assert mb.user == 'backenduser'
158162
assert mb.password == CELERY_PASSWORD
159163
assert mb.database_name == 'another_db'
@@ -222,11 +226,15 @@ def test_init_mongodb_dnspython2_pymongo4_seedlist(self):
222226

223227
with patch('dns.resolver.resolve', side_effect=resolver):
224228
mb = self.perform_seedlist_assertions()
225-
assert mb.options == dict(
226-
mb._prepare_client_options(),
227-
replicaset='rs0',
228-
tls=True
229-
)
229+
if 'replicaSet' in mb.options: # pragma: no cover # pymongo >= 4.14
230+
replicaset_option = 'replicaSet'
231+
else: # pragma: no cover # pymongo < 4.14
232+
replicaset_option = 'replicaset'
233+
assert mb.options == {
234+
**mb._prepare_client_options(),
235+
replicaset_option: 'rs0',
236+
'tls': True,
237+
}
230238

231239
def perform_seedlist_assertions(self):
232240
mb = MongoBackend(app=self.app, url=MONGODB_SEEDLIST_URI)
@@ -299,12 +307,15 @@ def test_get_connection_with_authmechanism(self):
299307
mb = MongoBackend(app=self.app, url=uri)
300308
mock_Connection.return_value = sentinel.connection
301309
connection = mb._get_connection()
310+
if 'authMechanism' in mb.options: # pragma: no cover # pymongo >= 4.14
311+
authmechanism_option = 'authMechanism'
312+
else: # pragma: no cover # pymongo < 4.14
313+
authmechanism_option = 'authmechanism'
302314
mock_Connection.assert_called_once_with(
303315
host=['localhost:27017'],
304316
username=CELERY_USER,
305317
password=CELERY_PASSWORD,
306-
authmechanism='SCRAM-SHA-256',
307-
**mb._prepare_client_options()
318+
**{**mb._prepare_client_options(), authmechanism_option: 'SCRAM-SHA-256'}
308319
)
309320
assert sentinel.connection == connection
310321

@@ -319,10 +330,13 @@ def test_get_connection_with_authmechanism_no_username(self):
319330
'SCRAM-SHA-256 requires a username.')
320331
with pytest.raises(ConfigurationError):
321332
mb._get_connection()
333+
if 'authMechanism' in mb.options: # pragma: no cover # pymongo >= 4.14
334+
authmechanism_option = 'authMechanism'
335+
else: # pragma: no cover # pymongo < 4.14
336+
authmechanism_option = 'authmechanism'
322337
mock_Connection.assert_called_once_with(
323338
host=['localhost:27017'],
324-
authmechanism='SCRAM-SHA-256',
325-
**mb._prepare_client_options()
339+
**{**mb._prepare_client_options(), authmechanism_option: 'SCRAM-SHA-256'}
326340
)
327341

328342
@patch('celery.backends.mongodb.MongoBackend._get_connection')

0 commit comments

Comments
 (0)