|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from odoo.tests.common import TransactionCase |
| 6 | + |
| 7 | +_logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class TestQueueJobResId(TransactionCase): |
| 11 | + """Test automatic population of res_id in queue jobs.""" |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + super().setUpClass() |
| 16 | + # Set context to avoid job queue delay for testing |
| 17 | + cls.env = cls.env( |
| 18 | + context=dict( |
| 19 | + cls.env.context, |
| 20 | + test_queue_job_no_delay=True, |
| 21 | + ) |
| 22 | + ) |
| 23 | + |
| 24 | + def test_01_single_record_job_creation(self): |
| 25 | + """Test that res_id is populated when a single record creates a job.""" |
| 26 | + # Create a test partner record |
| 27 | + partner = self.env["res.partner"].create( |
| 28 | + { |
| 29 | + "name": "Test Partner for Queue Job", |
| 30 | + "email": "test@example.com", |
| 31 | + } |
| 32 | + ) |
| 33 | + |
| 34 | + # Create a method that can be delayed (using existing method) |
| 35 | + # We'll use a simple method that exists on res.partner |
| 36 | + job = partner.with_delay().write({"phone": "1234567890"}) |
| 37 | + |
| 38 | + # Verify job was created |
| 39 | + self.assertTrue(job, "Job should be created") |
| 40 | + |
| 41 | + # Get the queue.job record |
| 42 | + queue_job = self.env["queue.job"].search([("uuid", "=", job.uuid)], limit=1) |
| 43 | + |
| 44 | + # Verify res_id is populated correctly |
| 45 | + self.assertEqual( |
| 46 | + queue_job.res_id, |
| 47 | + partner.id, |
| 48 | + "res_id should be set to the partner ID", |
| 49 | + ) |
| 50 | + self.assertEqual( |
| 51 | + queue_job.res_model, |
| 52 | + "res.partner", |
| 53 | + "res_model should be set to res.partner", |
| 54 | + ) |
| 55 | + self.assertEqual( |
| 56 | + queue_job.model_name, |
| 57 | + "res.partner", |
| 58 | + "model_name should be res.partner", |
| 59 | + ) |
| 60 | + |
| 61 | + def test_02_multiple_records_job_creation(self): |
| 62 | + """Test that res_id is populated when multiple records create a job.""" |
| 63 | + # Create multiple test partner records |
| 64 | + partners = self.env["res.partner"].create( |
| 65 | + [ |
| 66 | + {"name": "Test Partner 1", "email": "test1@example.com"}, |
| 67 | + {"name": "Test Partner 2", "email": "test2@example.com"}, |
| 68 | + {"name": "Test Partner 3", "email": "test3@example.com"}, |
| 69 | + ] |
| 70 | + ) |
| 71 | + |
| 72 | + # Create a job with multiple records |
| 73 | + job = partners.with_delay().write({"phone": "9876543210"}) |
| 74 | + |
| 75 | + # Verify job was created |
| 76 | + self.assertTrue(job, "Job should be created") |
| 77 | + |
| 78 | + # Get the queue.job record |
| 79 | + queue_job = self.env["queue.job"].search([("uuid", "=", job.uuid)], limit=1) |
| 80 | + |
| 81 | + # Verify res_id is populated with first record's ID |
| 82 | + self.assertEqual( |
| 83 | + queue_job.res_id, |
| 84 | + partners[0].id, |
| 85 | + "res_id should be set to the first partner ID", |
| 86 | + ) |
| 87 | + self.assertEqual( |
| 88 | + queue_job.res_model, |
| 89 | + "res.partner", |
| 90 | + "res_model should be set to res.partner", |
| 91 | + ) |
| 92 | + |
| 93 | + def test_03_res_id_field_indexed(self): |
| 94 | + """Test that res_id field is indexed for performance.""" |
| 95 | + # Get the field information |
| 96 | + field = self.env["queue.job"]._fields.get("res_id") |
| 97 | + |
| 98 | + # Verify field exists and is indexed |
| 99 | + self.assertTrue(field, "res_id field should exist") |
| 100 | + self.assertTrue(field.index, "res_id field should be indexed") |
| 101 | + |
| 102 | + def test_04_res_model_field_indexed(self): |
| 103 | + """Test that res_model field is indexed for performance.""" |
| 104 | + # Get the field information |
| 105 | + field = self.env["queue.job"]._fields.get("res_model") |
| 106 | + |
| 107 | + # Verify field exists and is indexed |
| 108 | + self.assertTrue(field, "res_model field should exist") |
| 109 | + self.assertTrue(field.index, "res_model field should be indexed") |
| 110 | + |
| 111 | + def test_05_search_jobs_by_res_id(self): |
| 112 | + """Test searching for jobs by res_id.""" |
| 113 | + # Create a test partner record |
| 114 | + partner = self.env["res.partner"].create( |
| 115 | + { |
| 116 | + "name": "Test Partner for Search", |
| 117 | + "email": "search@example.com", |
| 118 | + } |
| 119 | + ) |
| 120 | + |
| 121 | + # Create multiple jobs for the same partner |
| 122 | + job1 = partner.with_delay().write({"phone": "1111111111"}) |
| 123 | + job2 = partner.with_delay().write({"mobile": "2222222222"}) |
| 124 | + |
| 125 | + # Search for jobs by res_id |
| 126 | + jobs = self.env["queue.job"].search( |
| 127 | + [ |
| 128 | + ("res_id", "=", partner.id), |
| 129 | + ("res_model", "=", "res.partner"), |
| 130 | + ] |
| 131 | + ) |
| 132 | + |
| 133 | + # Verify we found at least the jobs we created |
| 134 | + job_uuids = jobs.mapped("uuid") |
| 135 | + self.assertIn( |
| 136 | + job1.uuid, |
| 137 | + job_uuids, |
| 138 | + "Should find first job by res_id", |
| 139 | + ) |
| 140 | + self.assertIn( |
| 141 | + job2.uuid, |
| 142 | + job_uuids, |
| 143 | + "Should find second job by res_id", |
| 144 | + ) |
0 commit comments