Skip to content

Commit bc78310

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

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

queue_job/tests/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def _add_job(self, *args, **kwargs):
255255
if not job.identity_key or all(
256256
j.identity_key != job.identity_key for j in self.enqueued_jobs
257257
):
258+
self._prepare_context(job)
258259
self.enqueued_jobs.append(job)
259260

260261
patcher = mock.patch.object(job, "store")
@@ -273,6 +274,12 @@ def _add_job(self, *args, **kwargs):
273274
)
274275
return job
275276

277+
def _prepare_context(self, job):
278+
job_model = job.job_model.with_context({})
279+
field_records = job_model._fields["records"]
280+
# Filter the context to simulate store/load of the job
281+
job.recordset = field_records.convert_to_write(job.recordset, job_model)
282+
276283
def __enter__(self):
277284
return self
278285

test_queue_job/tests/test_delay_mocks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ def test_trap_jobs_perform(self):
270270
self.assertEqual(logs[2].message, "test_trap_jobs_perform graph 3")
271271
self.assertEqual(logs[3].message, "test_trap_jobs_perform graph 1")
272272

273+
def test_trap_jobs_prepare_context(self):
274+
with trap_jobs() as trap:
275+
model1 = self.env["test.queue.job"].with_context({"config_key": 42})
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+
273288
def test_mock_with_delay(self):
274289
with mock_with_delay() as (delayable_cls, delayable):
275290
self.env["test.queue.job"].button_that_uses_with_delay()

0 commit comments

Comments
 (0)