2828from django .test import SimpleTestCase , TestCase , TransactionTestCase , override_settings
2929from django .test .testcases import _deferredSkip # type:ignore[attr-defined]
3030from django .utils import timezone
31- from django_tasks import (
32- TaskResultStatus ,
33- default_task_backend ,
34- task_backends ,
35- )
36- from django_tasks .base import Task
37- from django_tasks .exceptions import InvalidTaskError , TaskResultDoesNotExist
38- from django_tasks .signals import task_enqueued
39- from django_tasks .utils import get_random_id
31+ from django .utils .crypto import get_random_string
32+
33+ try :
34+ from django .tasks import (
35+ TaskResultStatus ,
36+ default_task_backend ,
37+ task_backends ,
38+ )
39+ from django .tasks .base import Task
40+ from django .tasks .exceptions import InvalidTask , TaskResultDoesNotExist
41+ from django .tasks .signals import task_enqueued
42+
43+ DUMMY_BACKEND = "django.tasks.backends.dummy.DummyBackend"
44+ LOGGER = "django.tasks"
45+ except ImportError :
46+ from django_tasks import (
47+ TaskResultStatus ,
48+ default_task_backend ,
49+ task_backends ,
50+ )
51+ from django_tasks .base import Task
52+ from django_tasks .exceptions import InvalidTaskError as InvalidTask
53+ from django_tasks .exceptions import TaskResultDoesNotExist
54+ from django_tasks .signals import task_enqueued
55+
56+ DUMMY_BACKEND = "django_tasks.backends.dummy.DummyBackend"
57+ LOGGER = "django_tasks"
58+
4059
4160from django_tasks_db import DatabaseBackend , compat
4261from django_tasks_db .admin import DBTaskResultAdmin
@@ -166,7 +185,7 @@ def test_refresh_result(self) -> None:
166185 started_at = timezone .now (),
167186 finished_at = timezone .now (),
168187 return_value = 42 ,
169- worker_ids = [get_random_id ( )],
188+ worker_ids = [get_random_string ( 32 )],
170189 )
171190
172191 self .assertEqual (result .status , TaskResultStatus .READY )
@@ -197,7 +216,7 @@ async def test_refresh_result_async(self) -> None:
197216 started_at = timezone .now (),
198217 finished_at = timezone .now (),
199218 return_value = 42 ,
200- worker_ids = [get_random_id ( )],
219+ worker_ids = [get_random_string ( 32 )],
201220 )
202221
203222 self .assertEqual (result .status , TaskResultStatus .READY )
@@ -335,7 +354,7 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
335354 self .assertEqual (self .get_task_count_in_new_connection (), 1 )
336355
337356 def test_enqueue_logs (self ) -> None :
338- with self .assertLogs ("django_tasks" , level = "DEBUG" ) as captured_logs :
357+ with self .assertLogs (LOGGER , level = "DEBUG" ) as captured_logs :
339358 result = test_tasks .noop_task .enqueue ()
340359
341360 self .assertEqual (len (captured_logs .output ), 1 )
@@ -421,7 +440,7 @@ def test_validate_on_enqueue(self) -> None:
421440 )
422441
423442 with self .assertRaisesMessage (
424- InvalidTaskError , "Queue 'unknown_queue' is not valid for backend"
443+ InvalidTask , "Queue 'unknown_queue' is not valid for backend"
425444 ):
426445 task_with_custom_queue_name .enqueue ()
427446
@@ -439,7 +458,7 @@ async def test_validate_on_aenqueue(self) -> None:
439458 )
440459
441460 with self .assertRaisesMessage (
442- InvalidTaskError , "Queue 'unknown_queue' is not valid for backend"
461+ InvalidTask , "Queue 'unknown_queue' is not valid for backend"
443462 ):
444463 await task_with_custom_queue_name .aenqueue ()
445464
@@ -511,11 +530,11 @@ def test_postgres_id_function_expression(self) -> None:
511530 "BACKEND" : "django_tasks_db.DatabaseBackend" ,
512531 "QUEUES" : ["default" , "queue-1" ],
513532 },
514- "dummy" : {"BACKEND" : "django_tasks.backends.dummy.DummyBackend" },
533+ "dummy" : {"BACKEND" : DUMMY_BACKEND },
515534 }
516535)
517536class DatabaseBackendWorkerTestCase (TransactionTestCase ):
518- worker_id = get_random_id ( )
537+ worker_id = get_random_string ( 32 )
519538
520539 run_worker = staticmethod (
521540 partial (
@@ -531,7 +550,7 @@ class DatabaseBackendWorkerTestCase(TransactionTestCase):
531550
532551 def tearDown (self ) -> None :
533552 logger = logging .getLogger ("django_tasks_db" )
534- tasks_logger = logging .getLogger ("django_tasks" )
553+ tasks_logger = logging .getLogger (LOGGER )
535554
536555 # Reset the logger after every run, to ensure the correct `stdout` is used
537556 for handler in logger .handlers :
@@ -1240,7 +1259,7 @@ def test_exclusive_transaction(self) -> None:
12401259 "BACKEND" : "django_tasks_db.DatabaseBackend" ,
12411260 "QUEUES" : ["default" , "queue-1" ],
12421261 },
1243- "dummy" : {"BACKEND" : "django_tasks.backends.dummy.DummyBackend" },
1262+ "dummy" : {"BACKEND" : DUMMY_BACKEND },
12441263 }
12451264)
12461265class DatabaseBackendPruneTaskResultsTestCase (TransactionTestCase ):
@@ -1518,7 +1537,7 @@ def start_worker(
15181537 args = []
15191538
15201539 if worker_id is None :
1521- worker_id = get_random_id ( )
1540+ worker_id = get_random_string ( 32 )
15221541
15231542 p = subprocess .Popen (
15241543 [
@@ -1606,7 +1625,7 @@ def test_repeat_ctrl_c(self) -> None:
16061625 result = test_tasks .hang .enqueue ()
16071626 self .assertEqual (DBTaskResult .objects .get (id = result .id ).worker_ids , [])
16081627
1609- worker_id = get_random_id ( )
1628+ worker_id = get_random_string ( 32 )
16101629
16111630 process = self .start_worker (worker_id = worker_id )
16121631
0 commit comments