2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 19:46:46 +00:00

Move get_scheduled_tasks call into register_tasks function (#6556)

* Move get_scheduled_tasks call into register_tasks function

* Adjust scheduled_tasks tests

* Set scheduled_tasks default, Fix test_function
This commit is contained in:
Bobbe 2024-02-24 04:27:47 +01:00 committed by GitHub
parent d689b95963
commit 2df0fd8a67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 13 deletions

View File

@ -50,8 +50,8 @@ class ScheduleMixin:
def __init__(self): def __init__(self):
"""Register mixin.""" """Register mixin."""
super().__init__() super().__init__()
self.scheduled_tasks = self.get_scheduled_tasks()
self.validate_scheduled_tasks() self.scheduled_tasks = []
self.add_mixin('schedule', 'has_scheduled_tasks', __class__) self.add_mixin('schedule', 'has_scheduled_tasks', __class__)
@ -156,6 +156,9 @@ class ScheduleMixin:
def register_tasks(self): def register_tasks(self):
"""Register the tasks with the database.""" """Register the tasks with the database."""
self.scheduled_tasks = self.get_scheduled_tasks()
self.validate_scheduled_tasks()
try: try:
from django_q.models import Schedule from django_q.models import Schedule

View File

@ -21,6 +21,11 @@ class ExampleScheduledTaskPluginTests(TestCase):
# check that the built-in function is running # check that the built-in function is running
self.assertEqual(plg.member_func(), False) self.assertEqual(plg.member_func(), False)
# register
plg.register_tasks()
# check that schedule was registers
from django_q.models import Schedule
# check that the tasks are defined # check that the tasks are defined
self.assertEqual( self.assertEqual(
plg.get_task_names(), plg.get_task_names(),
@ -31,11 +36,6 @@ class ExampleScheduledTaskPluginTests(TestCase):
], ],
) )
# register
plg.register_tasks()
# check that schedule was registers
from django_q.models import Schedule
scheduled_plugin_tasks = Schedule.objects.filter(name__istartswith='plugin.') scheduled_plugin_tasks = Schedule.objects.filter(name__istartswith='plugin.')
self.assertEqual(len(scheduled_plugin_tasks), 3) self.assertEqual(len(scheduled_plugin_tasks), 3)
@ -88,7 +88,7 @@ class ScheduledTaskPluginTests(TestCase):
pass pass
with self.assertRaises(MixinImplementationError): with self.assertRaises(MixinImplementationError):
NoSchedules() NoSchedules().register_tasks()
class WrongFuncSchedules(Base): class WrongFuncSchedules(Base):
"""Plugin with broken functions. """Plugin with broken functions.
@ -102,7 +102,7 @@ class ScheduledTaskPluginTests(TestCase):
pass # pragma: no cover pass # pragma: no cover
with self.assertRaises(MixinImplementationError): with self.assertRaises(MixinImplementationError):
WrongFuncSchedules() WrongFuncSchedules().register_tasks()
class WrongFuncSchedules1(WrongFuncSchedules): class WrongFuncSchedules1(WrongFuncSchedules):
"""Plugin with broken functions. """Plugin with broken functions.
@ -113,7 +113,7 @@ class ScheduledTaskPluginTests(TestCase):
SCHEDULED_TASKS = {'test': {'func': 'test', 'minutes': 30}} SCHEDULED_TASKS = {'test': {'func': 'test', 'minutes': 30}}
with self.assertRaises(MixinImplementationError): with self.assertRaises(MixinImplementationError):
WrongFuncSchedules1() WrongFuncSchedules1().register_tasks()
class WrongFuncSchedules2(WrongFuncSchedules): class WrongFuncSchedules2(WrongFuncSchedules):
"""Plugin with broken functions. """Plugin with broken functions.
@ -124,7 +124,7 @@ class ScheduledTaskPluginTests(TestCase):
SCHEDULED_TASKS = {'test': {'func': 'test', 'minutes': 30}} SCHEDULED_TASKS = {'test': {'func': 'test', 'minutes': 30}}
with self.assertRaises(MixinImplementationError): with self.assertRaises(MixinImplementationError):
WrongFuncSchedules2() WrongFuncSchedules2().register_tasks()
class WrongFuncSchedules3(WrongFuncSchedules): class WrongFuncSchedules3(WrongFuncSchedules):
"""Plugin with broken functions. """Plugin with broken functions.
@ -137,7 +137,7 @@ class ScheduledTaskPluginTests(TestCase):
} }
with self.assertRaises(MixinImplementationError): with self.assertRaises(MixinImplementationError):
WrongFuncSchedules3() WrongFuncSchedules3().register_tasks()
class WrongFuncSchedules4(WrongFuncSchedules): class WrongFuncSchedules4(WrongFuncSchedules):
"""Plugin with broken functions. """Plugin with broken functions.
@ -148,4 +148,4 @@ class ScheduledTaskPluginTests(TestCase):
SCHEDULED_TASKS = {'test': {'func': 'test', 'schedule': 'I'}} SCHEDULED_TASKS = {'test': {'func': 'test', 'schedule': 'I'}}
with self.assertRaises(MixinImplementationError): with self.assertRaises(MixinImplementationError):
WrongFuncSchedules4() WrongFuncSchedules4().register_tasks()