Skip to content

Commit a2d94e3

Browse files
committed
[IMP] queue_job: perform_enqueued_jobs should filter the context
1 parent a77575e commit a2d94e3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

queue_job/tests/common.py

+8
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def _add_job(self, *args, **kwargs):
256256
if not job.identity_key or all(
257257
j.identity_key != job.identity_key for j in self.enqueued_jobs
258258
):
259+
self._prepare_context(job)
259260
self.enqueued_jobs.append(job)
260261

261262
patcher = mock.patch.object(job, "store")
@@ -274,6 +275,13 @@ def _add_job(self, *args, **kwargs):
274275
)
275276
return job
276277

278+
def _prepare_context(self, job):
279+
# pylint: disable=context-overriden
280+
job_model = job.job_model.with_context({})
281+
field_records = job_model._fields["records"]
282+
# Filter the context to simulate store/load of the job
283+
job.recordset = field_records.convert_to_write(job.recordset, job_model)
284+
277285
def __enter__(self):
278286
return self
279287

test_queue_job/tests/test_delay_mocks.py

+17
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ def test_trap_jobs_perform(self):
268268
self.assertEqual(logs[2].message, "test_trap_jobs_perform graph 3")
269269
self.assertEqual(logs[3].message, "test_trap_jobs_perform graph 1")
270270

271+
def test_trap_jobs_prepare_context(self):
272+
with trap_jobs() as trap:
273+
# pylint: disable=context-overriden
274+
model1 = self.env["test.queue.job"].with_context({"config_key": 42})
275+
# pylint: disable=context-overriden
276+
model2 = self.env["test.queue.job"].with_context(
277+
{"config_key": 42, "lang": "it_IT"}
278+
)
279+
model1.with_delay().testing_method("0", "K", return_context=1)
280+
model2.with_delay().testing_method("0", "K", return_context=1)
281+
282+
[job1, job2] = trap.enqueued_jobs
283+
trap.perform_enqueued_jobs()
284+
285+
self.assertEqual(job1.result, {"job_uuid": mock.ANY})
286+
self.assertEqual(job2.result, {"job_uuid": mock.ANY, "lang": "it_IT"})
287+
271288
def test_mock_with_delay(self):
272289
with mock_with_delay() as (delayable_cls, delayable):
273290
self.env["test.queue.job"].button_that_uses_with_delay()

0 commit comments

Comments
 (0)