diff --git a/queue_job/tests/common.py b/queue_job/tests/common.py index 1bc976939e..88fc779e69 100644 --- a/queue_job/tests/common.py +++ b/queue_job/tests/common.py @@ -111,7 +111,8 @@ def __eq__(self, other): if not isinstance(other, JobCall): return NotImplemented return ( - self.method.__func__ == other.method.__func__ + self.method.__self__ == other.method.__self__ + and self.method.__func__ == other.method.__func__ and self.args == other.args and self.kwargs == other.kwargs and self.properties == other.properties @@ -280,7 +281,8 @@ def _filtered_enqueued_jobs(self, job_method): enqueued_jobs = [ job for job in self.enqueued_jobs - if job.func.__func__ == job_method.__func__ + if job.func.__self__ == job_method.__self__ + and job.func.__func__ == job_method.__func__ ] return enqueued_jobs @@ -293,7 +295,7 @@ def _format_job_call(self, call): ", ".join("%s=%s" % (key, value) for key, value in call.kwargs.items()) ) return "<%s>.%s(%s) with properties (%s)" % ( - call.method.__self__.__class__._name, + call.method.__self__, call.method.__name__, ", ".join(method_all_args), ", ".join("%s=%s" % (key, value) for key, value in call.properties.items()), diff --git a/test_queue_job/tests/test_delay_mocks.py b/test_queue_job/tests/test_delay_mocks.py index 6736b919b0..5c778a7d35 100644 --- a/test_queue_job/tests/test_delay_mocks.py +++ b/test_queue_job/tests/test_delay_mocks.py @@ -13,7 +13,7 @@ class TestDelayMocks(common.TransactionCase): - def test_trap_jobs_on_with_delay(self): + def test_trap_jobs_on_with_delay_model(self): with trap_jobs() as trap: self.env["test.queue.job"].button_that_uses_with_delay() trap.assert_jobs_count(1) @@ -33,6 +33,133 @@ def test_trap_jobs_on_with_delay(self): ), ) + def test_trap_jobs_on_with_delay_recordset(self): + recordset = self.env["test.queue.job"].create({"name": "test"}) + with trap_jobs() as trap: + recordset.button_that_uses_with_delay() + trap.assert_jobs_count(1) + trap.assert_jobs_count(1, only=recordset.testing_method) + + trap.assert_enqueued_job( + recordset.testing_method, + args=(1,), + kwargs={"foo": 2}, + properties=dict( + channel="root.test", + description="Test", + eta=15, + identity_key=identity_exact, + max_retries=1, + priority=15, + ), + ) + + def test_trap_jobs_on_with_delay_recordset_no_properties(self): + """Verify that trap_jobs() can omit properties""" + recordset = self.env["test.queue.job"].create({"name": "test"}) + with trap_jobs() as trap: + recordset.button_that_uses_with_delay() + + trap.assert_enqueued_job( + recordset.testing_method, + args=(1,), + kwargs={"foo": 2}, + ) + + def test_trap_jobs_on_with_delay_recordset_partial_properties(self): + """Verify that trap_jobs() can check partially properties""" + recordset = self.env["test.queue.job"].create({"name": "test"}) + with trap_jobs() as trap: + recordset.button_that_uses_with_delay() + + trap.assert_enqueued_job( + recordset.testing_method, + args=(1,), + kwargs={"foo": 2}, + properties=dict( + description="Test", + eta=15, + ), + ) + + def test_trap_jobs_on_with_delay_assert_model_count_mismatch(self): + recordset = self.env["test.queue.job"].create({"name": "test"}) + with trap_jobs() as trap: + self.env["test.queue.job"].button_that_uses_with_delay() + trap.assert_jobs_count(1) + with self.assertRaises(AssertionError, msg="0 != 1"): + trap.assert_jobs_count(1, only=recordset.testing_method) + + def test_trap_jobs_on_with_delay_assert_recordset_count_mismatch(self): + recordset = self.env["test.queue.job"].create({"name": "test"}) + with trap_jobs() as trap: + recordset.button_that_uses_with_delay() + trap.assert_jobs_count(1) + with self.assertRaises(AssertionError, msg="0 != 1"): + trap.assert_jobs_count( + 1, only=self.env["test.queue.job"].testing_method + ) + + def test_trap_jobs_on_with_delay_assert_model_enqueued_mismatch(self): + recordset = self.env["test.queue.job"].create({"name": "test"}) + with trap_jobs() as trap: + recordset.button_that_uses_with_delay() + trap.assert_jobs_count(1) + message = ( + r"Job \.testing_method\(1, foo=2\) with " + r"properties \(channel=root\.test, description=Test, eta=15, " + "identity_key=, " + "max_retries=1, priority=15\\) was not enqueued\\.\n" + "Actual enqueued jobs:\n" + r" \* .testing_method\(1, foo=2\) with properties " + r"\(priority=15, max_retries=1, eta=15, description=Test, channel=root.test, " + r"identity_key=\)" + ) % (recordset.id,) + with self.assertRaisesRegex(AssertionError, message): + trap.assert_enqueued_job( + self.env["test.queue.job"].testing_method, + args=(1,), + kwargs={"foo": 2}, + properties=dict( + channel="root.test", + description="Test", + eta=15, + identity_key=identity_exact, + max_retries=1, + priority=15, + ), + ) + + def test_trap_jobs_on_with_delay_assert_recordset_enqueued_mismatch(self): + recordset = self.env["test.queue.job"].create({"name": "test"}) + with trap_jobs() as trap: + self.env["test.queue.job"].button_that_uses_with_delay() + trap.assert_jobs_count(1) + message = ( + r"Job \.testing_method\(1, foo=2\) with " + r"properties \(channel=root\.test, description=Test, eta=15, " + "identity_key=, " + "max_retries=1, priority=15\\) was not enqueued\\.\n" + "Actual enqueued jobs:\n" + r" \* .testing_method\(1, foo=2\) with properties " + r"\(priority=15, max_retries=1, eta=15, description=Test, channel=root.test, " + r"identity_key=\)" + ) % (recordset.id,) + with self.assertRaisesRegex(AssertionError, message): + trap.assert_enqueued_job( + recordset.testing_method, + args=(1,), + kwargs={"foo": 2}, + properties=dict( + channel="root.test", + description="Test", + eta=15, + identity_key=identity_exact, + max_retries=1, + priority=15, + ), + ) + def test_trap_jobs_on_graph(self): with trap_jobs() as trap: self.env["test.queue.job"].button_that_uses_delayable_chain()