|
| 1 | +from unittest.mock import patch, MagicMock |
| 2 | + |
1 | 3 | from django.contrib.contenttypes.models import ContentType |
2 | 4 | from django.core.exceptions import ObjectDoesNotExist |
3 | 5 | from django.test import TestCase |
4 | 6 |
|
5 | | -from core.models import DataSource, ObjectType |
| 7 | +from core.models import DataSource, Job, ObjectType |
6 | 8 | from core.choices import ObjectChangeActionChoices |
7 | 9 | from dcim.models import Site, Location, Device |
8 | 10 | from netbox.constants import CENSOR_TOKEN, CENSOR_TOKEN_CHANGED |
@@ -200,3 +202,38 @@ def test_with_feature(self): |
200 | 202 | bookmarks_ots = ObjectType.objects.with_feature('bookmarks') |
201 | 203 | self.assertIn(ObjectType.objects.get_by_natural_key('dcim', 'site'), bookmarks_ots) |
202 | 204 | self.assertNotIn(ObjectType.objects.get_by_natural_key('dcim', 'cabletermination'), bookmarks_ots) |
| 205 | + |
| 206 | + |
| 207 | +class JobTest(TestCase): |
| 208 | + |
| 209 | + @patch('core.models.jobs.django_rq.get_queue') |
| 210 | + def test_delete_cancels_job_from_correct_queue(self, mock_get_queue): |
| 211 | + """ |
| 212 | + Test that when a job is deleted, it's canceled from the correct queue. |
| 213 | + """ |
| 214 | + mock_queue = MagicMock() |
| 215 | + mock_rq_job = MagicMock() |
| 216 | + mock_queue.fetch_job.return_value = mock_rq_job |
| 217 | + mock_get_queue.return_value = mock_queue |
| 218 | + |
| 219 | + def dummy_func(**kwargs): |
| 220 | + pass |
| 221 | + |
| 222 | + # Enqueue a job with a custom queue name |
| 223 | + custom_queue = 'my_custom_queue' |
| 224 | + job = Job.enqueue( |
| 225 | + func=dummy_func, |
| 226 | + name='Test Job', |
| 227 | + queue_name=custom_queue |
| 228 | + ) |
| 229 | + |
| 230 | + # Reset mock to clear enqueue call |
| 231 | + mock_get_queue.reset_mock() |
| 232 | + |
| 233 | + # Delete the job |
| 234 | + job.delete() |
| 235 | + |
| 236 | + # Verify the correct queue was used for cancellation |
| 237 | + mock_get_queue.assert_called_with(custom_queue) |
| 238 | + mock_queue.fetch_job.assert_called_with(str(job.job_id)) |
| 239 | + mock_rq_job.cancel.assert_called_once() |
0 commit comments