diff --git a/test_queue_job/tests/test_job.py b/test_queue_job/tests/test_job.py
index e0224ebf3d..9e99a0879b 100644
--- a/test_queue_job/tests/test_job.py
+++ b/test_queue_job/tests/test_job.py
@@ -200,6 +200,49 @@ def test_store_extra_data(self):
stored.invalidate_cache()
self.assertEqual(stored.additional_info, "JUST_TESTING_BUT_FAILED")
+ def test_company_simple(self):
+ company = self.env.ref("base.main_company")
+ eta = datetime.now() + timedelta(hours=5)
+ test_job = Job(
+ self.method,
+ args=("o", "k"),
+ kwargs={"return_context": 1},
+ priority=15,
+ eta=eta,
+ description="My description",
+ )
+ test_job.worker_pid = 99999 # normally set on "set_start"
+ test_job.company_id = company.id
+ test_job.store()
+ job_read = Job.load(self.env, test_job.uuid)
+ self.assertEqual(test_job.func, job_read.func)
+ result_ctx = test_job.func(*tuple(test_job.args), **test_job.kwargs)
+ self.assertEqual(result_ctx.get("allowed_company_ids"), company.ids)
+
+ def test_company_complex(self):
+ company1 = self.env.ref("base.main_company")
+ company2 = company1.create({"name": "Queue job company"})
+ companies = company1 | company2
+ self.env.user.write({"company_ids": [(6, False, companies.ids)]})
+ # Ensure the main company still the first
+ self.assertEqual(self.env.user.company_id, company1)
+ eta = datetime.now() + timedelta(hours=5)
+ test_job = Job(
+ self.method,
+ args=("o", "k"),
+ kwargs={"return_context": 1},
+ priority=15,
+ eta=eta,
+ description="My description",
+ )
+ test_job.worker_pid = 99999 # normally set on "set_start"
+ test_job.company_id = company2.id
+ test_job.store()
+ job_read = Job.load(self.env, test_job.uuid)
+ self.assertEqual(test_job.func, job_read.func)
+ result_ctx = test_job.func(*tuple(test_job.args), **test_job.kwargs)
+ self.assertEqual(result_ctx.get("allowed_company_ids"), company2.ids)
+
def test_read(self):
eta = datetime.now() + timedelta(hours=5)
test_job = Job(
@@ -536,6 +579,56 @@ def test_context_uuid(self):
self.assertTrue(key_present)
self.assertEqual(result["job_uuid"], test_job._uuid)
+ def test_context_custom_keep_context_default(self):
+ """
+ Use with_delay without specify 'keep_context' key.
+ So ensure the default False value to this params.
+ So the context shouldn't be restored with the recordset.
+ """
+ delayable = (
+ self.env["test.queue.job"].with_context(world_origin=42).with_delay()
+ )
+ test_job = delayable.testing_method()
+ self.assertEqual(test_job.db_record().context, "{}")
+ expected_ctx = {
+ "job_uuid": test_job.uuid,
+ "allowed_company_ids": [test_job.company_id],
+ }
+ self.assertEqual(test_job._get_record_context(), expected_ctx)
+
+ def test_context_custom_keep_context_false(self):
+ """
+ Use with_delay without specify by specifying keep_context to False.
+ So the context shouldn't be restored with the recordset.
+ """
+ delayable = (
+ self.env["test.queue.job"]
+ .with_context(world_origin=42)
+ .with_delay(keep_context=False)
+ )
+ test_job = delayable.testing_method()
+ self.assertEqual(test_job.db_record().context, "{}")
+ expected_ctx = {
+ "job_uuid": test_job.uuid,
+ "allowed_company_ids": [test_job.company_id],
+ }
+ self.assertEqual(test_job._get_record_context(), expected_ctx)
+
+ def test_context_custom_keep_context_true(self):
+ """
+ Use with_delay without specify by specifying keep_context to True.
+ So the context should be restored with the recordset.
+ """
+ recordset = self.env["test.queue.job"].with_context(world_origin=42)
+ delayable = recordset.with_delay(keep_context=True)
+ test_job = delayable.testing_method()
+ self.assertEqual(test_job.db_record().context, str(recordset.env.context))
+ expected_ctx = recordset.env.context.copy()
+ expected_ctx.update(
+ {"job_uuid": test_job.uuid, "allowed_company_ids": [test_job.company_id]}
+ )
+ self.assertEqual(test_job._get_record_context(), expected_ctx)
+
def test_override_channel(self):
delayable = self.env["test.queue.job"].with_delay(channel="root.sub.sub")
test_job = delayable.testing_method(return_context=True)