From 1683e1619a6d9a9de237db7f1edfd240689327c1 Mon Sep 17 00:00:00 2001 From: Roy de Goei <goei@astron.nl> Date: Wed, 17 Mar 2021 19:29:45 +0100 Subject: [PATCH] TMSS-682: Update after review --- .../tmss/tmssapp/viewsets/specification.py | 1 - SAS/TMSS/backend/test/t_reservations.py | 43 +++++++++++++------ SAS/TMSS/client/lib/populate.py | 39 +++++++++-------- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py index e34f22304e4..83da9d6c7d8 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py @@ -249,7 +249,6 @@ class ReservationStrategyTemplateViewSet(LOFARViewSet): project=project, specifications_template=strategy_template.reservation_template, specifications_doc=reservation_template_spec, - # Do we need to add to the model? reservation_strategy_template=strategy_template, start_time=start_time, duration=duration) # TODO change to stop_time when merge TMSS-668 diff --git a/SAS/TMSS/backend/test/t_reservations.py b/SAS/TMSS/backend/test/t_reservations.py index 80a07ad1b03..ae8a41b8a47 100755 --- a/SAS/TMSS/backend/test/t_reservations.py +++ b/SAS/TMSS/backend/test/t_reservations.py @@ -47,11 +47,12 @@ rest_data_creator = TMSSRESTTestDataCreator(BASE_URL, AUTH) from lofar.sas.tmss.tmss.tmssapp import models from lofar.sas.tmss.tmss.exceptions import SchemaValidationException +from django.core.exceptions import ValidationError class CreationFromReservationStrategyTemplate(unittest.TestCase): """ - Check that reservations can be created from strategy template + Test that reservations can be created from strategy template """ def test_create_reservation_ok(self): @@ -84,26 +85,40 @@ class CreationFromReservationStrategyTemplate(unittest.TestCase): self.assertEqual(response['name'], "reservation") self.assertEqual(response['specifications_doc'], reservation_spec) - def test_create_reservation_exception(self): """ - Check that reservations from the reservation strategy results SchemaValidationException due to wrong + Check that reservations from the reservation strategy results in an Exception due to wrong station assignment """ strategy_template = models.ReservationStrategyTemplate.objects.get(name="Regular station maintenance") + strategy_template.template['resources']['stations'] = ['CS999'] + # using ValidationError seem not to work? + with self.assertRaises(Exception) as context: + strategy_template.save() + self.assertIn('is not one of', str(context.exception)) + self.assertIn('Failed validating', str(context.exception)) - reservation_spec = add_defaults_to_json_object_for_schema(strategy_template.template, - strategy_template.reservation_template.schema) + +class ReservationTest(unittest.TestCase): + """ + Check the Reservation model + TODO: more testcases to be added + """ + + def test_create_reservation_validation_error(self): + """ + Check that creating reservation with results in SchemaValidationException due to wrong station assignment + """ + reservation_template = models.ReservationTemplate.objects.get(pk=1) + reservation_spec = get_default_json_object_for_schema(reservation_template.schema) reservation_spec['resources']['stations'] = ['CS999'] with self.assertRaises(SchemaValidationException) as context: - reservation = models.Reservation.objects.create(name=strategy_template.name, - description="Unittest with %s" % strategy_template.description, - project=None, - specifications_template=strategy_template.reservation_template, - specifications_doc=reservation_spec, - start_time=datetime.now(), - duration=None) # TODO change to stop_time when merge TMSS-668 + models.Reservation.objects.create(name="Test Reservation", + description="Unittest", + project=None, + specifications_template=reservation_template, + specifications_doc=reservation_spec, + start_time=datetime.now(), + duration=None) # TODO change to stop_time when merge TMSS-668 self.assertIn('is not one of', str(context.exception)) - - diff --git a/SAS/TMSS/client/lib/populate.py b/SAS/TMSS/client/lib/populate.py index 9ee870bed2f..ca80ea0fc4e 100644 --- a/SAS/TMSS/client/lib/populate.py +++ b/SAS/TMSS/client/lib/populate.py @@ -84,12 +84,17 @@ def populate_schemas(schema_dir: str=None, templates_filename: str=None): # store the prepared template for upload if template_name == 'scheduling_unit_observing_strategy_template': + template["strategy_template_name"] = template_name # so the 'strategy_template' name + template["template_name"] = "scheduling_unit_template" observing_strategy_templates.append(template) elif template_name == 'reservation_strategy_template': + template["strategy_template_name"] = template_name + template["template_name"] = "reservation_template" reservation_strategy_templates.append(template) else: templates_dict[json_schema_id] = template + # helper functions for uploading def upload_template(template: dict): logger.info("Uploading template with name='%s' version='%s'", template['name'], template['version']) @@ -106,22 +111,18 @@ def populate_schemas(schema_dir: str=None, templates_filename: str=None): template = templates_dict.pop(id) upload_template(template) - # helper functions for uploading observing_strategy_templates - def upload_observing_strategy_templates(template: dict): - scheduling_unit_templates = client.get_path_as_json_object('scheduling_unit_template?name=' + template.get('scheduling_unit_template_name') + '&version=' + template.get('scheduling_unit_template_version')) - scheduling_unit_template = scheduling_unit_templates[0] - template['scheduling_unit_template'] = scheduling_unit_template['url'] - logger.info("Uploading observation strategy with name='%s' version='%s'", template['name'], template['version']) - client.post_template(template_path='scheduling_unit_observing_strategy_template', **template) - - # helper functions for uploading reservation_strategy_templates - def upload_reservation_strategy_templates(template: dict): - scheduling_unit_templates = client.get_path_as_json_object('reservation_template?name=' + template.get('reservation_template_name') + '&version=' + template.get('reservation_template_version')) - scheduling_unit_template = scheduling_unit_templates[0] - template['reservation_template'] = scheduling_unit_template['url'] - logger.info("Uploading reservation strategy with name='%s' version='%s'", template['name'], - template['version']) - client.post_template(template_path='reservation_strategy_template', **template) + def upload_strategy_templates(template: dict): + """ + Helper function for uploading strategy_templates + Use template["strategy_template_name"] for the name of the 'strategy_template' to be uploaded + Use template["template_name"] for the name of the template (used for validation) + """ + tn = template.get('template_name') + response_templates = client.get_path_as_json_object(tn+'?name=' + template.get(tn+'_name') + '&version=' + template.get(tn+'_version')) + template[tn] = response_templates[0]['url'] + logger.info("Uploading strategy with name='%s' version='%s'", template['name'], template['version']) + client.post_template(template_path=template.get('strategy_template_name'), **template) + # first, upload all dependent templates for ref in all_references: @@ -130,15 +131,15 @@ def populate_schemas(schema_dir: str=None, templates_filename: str=None): # then, upload the remaining templates in parallel rest_templates = [template for template in templates_dict.values()] with ThreadPoolExecutor() as executor: - executor.map(upload_template, rest_templates) + executor.map(upload_template, rest_templates) # the reservation_strategy_templates with ThreadPoolExecutor() as executor: - executor.map(upload_reservation_strategy_templates, reservation_strategy_templates) + executor.map(upload_strategy_templates, reservation_strategy_templates) # and finally, the observing_strategy_templates with ThreadPoolExecutor() as executor: - executor.map(upload_observing_strategy_templates, observing_strategy_templates) + executor.map(upload_strategy_templates, observing_strategy_templates) scheduling_constraints_templates = client.get_path_as_json_object('scheduling_constraints_template') if scheduling_constraints_templates: -- GitLab