From ac51ca4eae3595a6706b4c38a11211cd09575f05 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn=20K=C3=BCnsem=C3=B6ller?=
 <jkuensem@physik.uni-bielefeld.de>
Date: Tue, 4 May 2021 19:22:14 +0200
Subject: [PATCH] TMSS-703: Fix is_triggered test

---
 .../scheduling/lib/constraints/template_constraints_v1.py     | 2 +-
 SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py     | 2 +-
 SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py   | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py b/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py
index 342b727554e..baaca766e6e 100644
--- a/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py
+++ b/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py
@@ -62,7 +62,7 @@ def can_run_within_timewindow(scheduling_unit: models.SchedulingUnitBlueprint, l
 def can_run_after(scheduling_unit: models.SchedulingUnitBlueprint, lower_bound: datetime) -> bool:
     '''Check if the given scheduling_unit can run somewhere after the given lowerbound timestamp depending on the sub's constrains-template/doc.'''
     constraints = scheduling_unit.scheduling_constraints_doc
-    if 'before' in constraints['time']:
+    if 'time' in constraints and 'before' in constraints['time']:
         before = parser.parse(constraints['time']['before'], ignoretz=True)
         return before > lower_bound
 
diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
index 130cf7a6686..4ebfde47aac 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
@@ -517,7 +517,7 @@ class SchedulingUnitBlueprint(RefreshFromDbInvalidatesCachedPropertiesMixin, Tem
             if self.project.can_trigger:
                 from lofar.sas.tmss.services.scheduling.constraints import can_run_after
                 start_time = datetime.datetime.utcnow() + datetime.timedelta(minutes=3)
-                if self.draft.scheduling_constraints_template is None or can_run_after(self, start_time):  # todo: use self.scheduling_constraints_template after TMSS-697 was merged
+                if self.scheduling_constraints_template is None or can_run_after(self, start_time):
                     logger.info('Triggered obs name=%s can run after start_time=%s. The scheduler will pick this up and cancel ongoing observations if necessary.' % (self.name, start_time))
                 else:
                     logger.info('Triggered obs name=%s cannot run after start_time=%s. Adding it for book-keeping, but it will be unschedulable.' % (self.name, start_time))
diff --git a/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py b/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py
index ca34a170f93..3639b0fbe6e 100755
--- a/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py
+++ b/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py
@@ -854,7 +854,8 @@ class SchedulingUnitBlueprintTest(unittest.TestCase):
         # setup
         project = models.Project.objects.create(**Project_test_data(can_trigger=True))
         scheduling_set = models.SchedulingSet.objects.create(**SchedulingSet_test_data(project=project))
-        scheduling_unit_draft = models.SchedulingUnitDraft.objects.create(**SchedulingUnitDraft_test_data(scheduling_set=scheduling_set, is_triggered=True))
+        constraints_template = models.SchedulingConstraintsTemplate.objects.get(name="constraints")
+        scheduling_unit_draft = models.SchedulingUnitDraft.objects.create(**SchedulingUnitDraft_test_data(scheduling_set=scheduling_set, is_triggered=True, scheduling_constraints_template=constraints_template))
         scheduling_unit_blueprint = models.SchedulingUnitBlueprint.objects.create(**SchedulingUnitBlueprint_test_data(draft=scheduling_unit_draft))
 
         # assert
@@ -874,7 +875,6 @@ class SchedulingUnitBlueprintTest(unittest.TestCase):
         self.assertIn('does not allow triggering', str(context.exception))
 
 
-
 class TaskBlueprintTest(unittest.TestCase):
     @classmethod
     def setUpClass(cls) -> None:
-- 
GitLab