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 )
@@ -204,6 +205,34 @@ def test_database_backend_app_missing(self) -> None:
204205 self .assertEqual (len (errors ), 1 )
205206 self .assertIn ("django_tasks.backends.database" , errors [0 ].hint )
206207
208+ def test_priority_range_check (self ) -> None :
209+ with self .assertRaises (IntegrityError ):
210+ DBTaskResult .objects .create (
211+ task_path = "" , backend_name = "default" , priority = - 101 , args_kwargs = {}
212+ )
213+
214+ with self .assertRaises (IntegrityError ):
215+ DBTaskResult .objects .create (
216+ task_path = "" , backend_name = "default" , priority = 101 , args_kwargs = {}
217+ )
218+
219+ # Django accepts the float, but only stores an int
220+ result = DBTaskResult .objects .create (
221+ task_path = "" , backend_name = "default" , priority = 3.1 , args_kwargs = {}
222+ )
223+ result .refresh_from_db ()
224+ self .assertEqual (result .priority , 3 )
225+
226+ DBTaskResult .objects .create (
227+ task_path = "" , backend_name = "default" , priority = 100 , args_kwargs = {}
228+ )
229+ DBTaskResult .objects .create (
230+ task_path = "" , backend_name = "default" , priority = - 100 , args_kwargs = {}
231+ )
232+ DBTaskResult .objects .create (
233+ task_path = "" , backend_name = "default" , priority = 0 , args_kwargs = {}
234+ )
235+
207236
208237@override_settings (
209238 TASKS = {
@@ -426,6 +455,7 @@ def test_run_after_priority(self) -> None:
426455 high_priority_result = test_tasks .noop_task .using (priority = 10 ).enqueue ()
427456
428457 low_priority_result = test_tasks .noop_task .using (priority = 2 ).enqueue ()
458+ lower_priority_result = test_tasks .noop_task .using (priority = - 2 ).enqueue ()
429459
430460 self .assertEqual (
431461 [dbt .task_result for dbt in DBTaskResult .objects .all ()],
@@ -435,6 +465,7 @@ def test_run_after_priority(self) -> None:
435465 low_priority_result ,
436466 far_future_result ,
437467 future_result ,
468+ lower_priority_result ,
438469 ],
439470 )
440471
@@ -443,6 +474,7 @@ def test_run_after_priority(self) -> None:
443474 [
444475 high_priority_result ,
445476 low_priority_result ,
477+ lower_priority_result ,
446478 ],
447479 )
448480
0 commit comments