Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[18.0][IMP] queue_job: perform_enqueued_jobs should filter the context #743

Open
wants to merge 2 commits into
base: 18.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions queue_job/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,9 @@ allow-list.

The default allow-list is ("tz", "lang", "allowed_company_ids",
"force_company", "active_test"). It can be customized in
``Base._job_prepare_context_before_enqueue_keys``. **Bypass jobs on
running Odoo**
``Base._job_prepare_context_before_enqueue_keys``.

**Bypass jobs on running Odoo**

When you are developing (ie: connector modules) you might want to bypass
the queue job and run your code immediately.
Expand Down
8 changes: 8 additions & 0 deletions queue_job/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,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 @@ -273,6 +274,13 @@ def _add_job(self, *args, **kwargs):
)
return job

def _prepare_context(self, job):
# pylint: disable=context-overridden
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
22 changes: 22 additions & 0 deletions test_queue_job/tests/test_delay_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,28 @@ 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):
florentx marked this conversation as resolved.
Show resolved Hide resolved
"""Context is transferred to the job according to an allow-list.

Default allow-list is:
("tz", "lang", "allowed_company_ids", "force_company", "active_test")
It can be customized in ``Base._job_prepare_context_before_enqueue_keys``.
"""
# pylint: disable=context-overridden
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