Skip to content

Commit

Permalink
[IMP] test_queue_job: add test for error_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Jan 14, 2025
1 parent ed582a1 commit c035a4c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions test_queue_job/data/queue_job_function_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<field name="model_id" ref="test_queue_job.model_test_queue_job" />
<field name="method">testing_method</field>
<field name="related_action" eval='{"func_name": "testing_related_method"}' />
<field name="error_handler" eval='{"func_name": "testing_error_handler"}' />
</record>
<record
id="job_function_test_queue_job_job_with_retry_pattern"
Expand Down
3 changes: 3 additions & 0 deletions test_queue_job/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def testing_related__url(self, **kwargs):
"url": kwargs["url"].format(subject=subject),
}

def testing_error_handler(self, **kwargs):
return None


class ModelTestQueueJob(models.Model):

Expand Down
25 changes: 25 additions & 0 deletions test_queue_job/tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)

import hashlib
import logging
from datetime import datetime, timedelta
from unittest import mock
from unittest.mock import patch

import odoo.tests.common as common

from odoo.addons.queue_job import identity_exact
from odoo.addons.queue_job.controllers.main import RunJobController
from odoo.addons.queue_job.delay import DelayableGraph
from odoo.addons.queue_job.exception import (
FailedJobError,
Expand All @@ -24,9 +27,12 @@
WAIT_DEPENDENCIES,
Job,
)
from odoo.addons.queue_job.tests.common import trap_jobs

from .common import JobCommonCase

_logger = logging.getLogger(__name__)


class TestJobsOnTestingMethod(JobCommonCase):
"""Test Job"""
Expand Down Expand Up @@ -341,6 +347,25 @@ def test_job_identity_key_func_exact(self):
job1 = Job.load(self.env, test_job_1.uuid)
self.assertEqual(job1.identity_key, expected_key)

def test_failed_job_perform(self):
with trap_jobs() as trap:
model = self.env["test.queue.job"]
job = model.with_delay(priority=1, max_retries=1).testing_method()
trap.assert_jobs_count(1)
with patch.object(type(job), "perform", side_effect=IOError), patch(
"odoo.sql_db.Cursor.commit", return_value=None
): # avoid odoo.sql_db: bad query: ROLLBACK TO SAVEPOINT test_0
controller = RunJobController()
try:
controller._try_perform_job(self.env, job)
with patch(
"odoo.addons.test_queue_job.models.test_models.QueueJob"
".testing_error_handler"
) as patched:
patched.assert_called_once()

Check warning on line 365 in test_queue_job/tests/test_job.py

View check run for this annotation

Codecov / codecov/patch

test_queue_job/tests/test_job.py#L365

Added line #L365 was not covered by tests
except Exception:
_logger.info("Job fails")


class TestJobs(JobCommonCase):
"""Test jobs on other methods or with different job configuration"""
Expand Down

0 comments on commit c035a4c

Please sign in to comment.