Skip to content

Commit

Permalink
[IMP] queue_jobs: perform_enqueued_jobs should filter the context
Browse files Browse the repository at this point in the history
  • Loading branch information
florentx committed Jan 17, 2025
1 parent 42a5497 commit 765b151
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions queue_job/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def _add_job(self, *args, **kwargs):
if not job.identity_key or all(
j.identity_key != job.identity_key for j in self.enqueued_jobs
):
self._prepare_context(job)
self.enqueued_jobs.append(job)

patcher = mock.patch.object(job, "store")
Expand All @@ -274,6 +275,12 @@ def _add_job(self, *args, **kwargs):
)
return job

def _prepare_context(self, job):
job_model = job.job_model.with_context({})
field_records = job_model._fields["records"]
# Filter the context to simulate store/load of the job
job.recordset = field_records.convert_to_write(job.recordset, job_model)

def __enter__(self):
return self

Expand Down
15 changes: 15 additions & 0 deletions test_queue_job/tests/test_delay_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ def test_trap_jobs_perform(self):
self.assertEqual(logs[2].message, "test_trap_jobs_perform graph 3")
self.assertEqual(logs[3].message, "test_trap_jobs_perform graph 1")

def test_trap_jobs_prepare_context(self):
with trap_jobs() as trap:
model1 = self.env["test.queue.job"].with_context({"config_key": 42})
model2 = self.env["test.queue.job"].with_context(
{"config_key": 42, "lang": "it_IT"}
)
model1.with_delay().testing_method("0", "K", return_context=1)
model2.with_delay().testing_method("0", "K", return_context=1)

[job1, job2] = trap.enqueued_jobs
trap.perform_enqueued_jobs()

self.assertEqual(job1.result, {"job_uuid": mock.ANY})
self.assertEqual(job2.result, {"job_uuid": mock.ANY, "lang": "it_IT"})

def test_mock_with_delay(self):
with mock_with_delay() as (delayable_cls, delayable):
self.env["test.queue.job"].button_that_uses_with_delay()
Expand Down

0 comments on commit 765b151

Please sign in to comment.