77
88from django .core .exceptions import SuspiciousOperation
99from django .core .management import call_command , execute_from_command_line
10- from django .test import TestCase , TransactionTestCase , override_settings
10+ from django .db .utils import IntegrityError
11+ from django .test import TransactionTestCase , override_settings
1112from django .urls import reverse
1213from django .utils import timezone
1314
2930 }
3031 }
3132)
32- class DatabaseBackendTestCase (TestCase ):
33+ class DatabaseBackendTestCase (TransactionTestCase ):
3334 def test_using_correct_backend (self ) -> None :
3435 self .assertEqual (default_task_backend , tasks ["default" ])
3536 self .assertIsInstance (tasks ["default" ], DatabaseBackend )
@@ -202,6 +203,32 @@ def test_database_backend_app_missing(self) -> None:
202203 self .assertEqual (len (errors ), 1 )
203204 self .assertIn ("django_tasks.backends.database" , errors [0 ].hint )
204205
206+ def test_priority_range_check (self ) -> None :
207+ with self .assertRaises (IntegrityError ):
208+ DBTaskResult .objects .create (
209+ task_path = "" , backend_name = "default" , priority = - 101 , args_kwargs = {}
210+ )
211+
212+ with self .assertRaises (IntegrityError ):
213+ DBTaskResult .objects .create (
214+ task_path = "" , backend_name = "default" , priority = 101 , args_kwargs = {}
215+ )
216+
217+ with self .assertRaises (IntegrityError ):
218+ DBTaskResult .objects .create (
219+ task_path = "" , backend_name = "default" , priority = 3.1 , args_kwargs = {}
220+ )
221+
222+ DBTaskResult .objects .create (
223+ task_path = "" , backend_name = "default" , priority = 100 , args_kwargs = {}
224+ )
225+ DBTaskResult .objects .create (
226+ task_path = "" , backend_name = "default" , priority = - 100 , args_kwargs = {}
227+ )
228+ DBTaskResult .objects .create (
229+ task_path = "" , backend_name = "default" , priority = 0 , args_kwargs = {}
230+ )
231+
205232
206233@override_settings (
207234 TASKS = {
@@ -402,6 +429,7 @@ def test_run_after_priority(self) -> None:
402429 high_priority_result = test_tasks .noop_task .using (priority = 10 ).enqueue ()
403430
404431 low_priority_result = test_tasks .noop_task .using (priority = 2 ).enqueue ()
432+ lower_priority_result = test_tasks .noop_task .using (priority = - 2 ).enqueue ()
405433
406434 self .assertEqual (
407435 [dbt .task_result for dbt in DBTaskResult .objects .all ()],
@@ -411,6 +439,7 @@ def test_run_after_priority(self) -> None:
411439 low_priority_result ,
412440 far_future_result ,
413441 future_result ,
442+ lower_priority_result ,
414443 ],
415444 )
416445
@@ -419,6 +448,7 @@ def test_run_after_priority(self) -> None:
419448 [
420449 high_priority_result ,
421450 low_priority_result ,
451+ lower_priority_result ,
422452 ],
423453 )
424454
0 commit comments