From 2df0fd8a673cd9980c2b305dd6d5a74a8da77b34 Mon Sep 17 00:00:00 2001 From: Bobbe <34186858+30350n@users.noreply.github.com> Date: Sat, 24 Feb 2024 04:27:47 +0100 Subject: [PATCH] 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 --- .../plugin/base/integration/ScheduleMixin.py | 7 ++++-- .../integration/test_scheduled_task.py | 22 +++++++++---------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/InvenTree/plugin/base/integration/ScheduleMixin.py b/InvenTree/plugin/base/integration/ScheduleMixin.py index 3b06c88315..201f0ea5cd 100644 --- a/InvenTree/plugin/base/integration/ScheduleMixin.py +++ b/InvenTree/plugin/base/integration/ScheduleMixin.py @@ -50,8 +50,8 @@ class ScheduleMixin: def __init__(self): """Register mixin.""" super().__init__() - self.scheduled_tasks = self.get_scheduled_tasks() - self.validate_scheduled_tasks() + + self.scheduled_tasks = [] self.add_mixin('schedule', 'has_scheduled_tasks', __class__) @@ -156,6 +156,9 @@ class ScheduleMixin: def register_tasks(self): """Register the tasks with the database.""" + self.scheduled_tasks = self.get_scheduled_tasks() + self.validate_scheduled_tasks() + try: from django_q.models import Schedule diff --git a/InvenTree/plugin/samples/integration/test_scheduled_task.py b/InvenTree/plugin/samples/integration/test_scheduled_task.py index 11521a8252..548c7c4ddc 100644 --- a/InvenTree/plugin/samples/integration/test_scheduled_task.py +++ b/InvenTree/plugin/samples/integration/test_scheduled_task.py @@ -21,6 +21,11 @@ class ExampleScheduledTaskPluginTests(TestCase): # check that the built-in function is running 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 self.assertEqual( 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.') self.assertEqual(len(scheduled_plugin_tasks), 3) @@ -88,7 +88,7 @@ class ScheduledTaskPluginTests(TestCase): pass with self.assertRaises(MixinImplementationError): - NoSchedules() + NoSchedules().register_tasks() class WrongFuncSchedules(Base): """Plugin with broken functions. @@ -102,7 +102,7 @@ class ScheduledTaskPluginTests(TestCase): pass # pragma: no cover with self.assertRaises(MixinImplementationError): - WrongFuncSchedules() + WrongFuncSchedules().register_tasks() class WrongFuncSchedules1(WrongFuncSchedules): """Plugin with broken functions. @@ -113,7 +113,7 @@ class ScheduledTaskPluginTests(TestCase): SCHEDULED_TASKS = {'test': {'func': 'test', 'minutes': 30}} with self.assertRaises(MixinImplementationError): - WrongFuncSchedules1() + WrongFuncSchedules1().register_tasks() class WrongFuncSchedules2(WrongFuncSchedules): """Plugin with broken functions. @@ -124,7 +124,7 @@ class ScheduledTaskPluginTests(TestCase): SCHEDULED_TASKS = {'test': {'func': 'test', 'minutes': 30}} with self.assertRaises(MixinImplementationError): - WrongFuncSchedules2() + WrongFuncSchedules2().register_tasks() class WrongFuncSchedules3(WrongFuncSchedules): """Plugin with broken functions. @@ -137,7 +137,7 @@ class ScheduledTaskPluginTests(TestCase): } with self.assertRaises(MixinImplementationError): - WrongFuncSchedules3() + WrongFuncSchedules3().register_tasks() class WrongFuncSchedules4(WrongFuncSchedules): """Plugin with broken functions. @@ -148,4 +148,4 @@ class ScheduledTaskPluginTests(TestCase): SCHEDULED_TASKS = {'test': {'func': 'test', 'schedule': 'I'}} with self.assertRaises(MixinImplementationError): - WrongFuncSchedules4() + WrongFuncSchedules4().register_tasks()