diff --git a/SAS/TMSS/test/t_subtask_validation.py b/SAS/TMSS/test/t_subtask_validation.py index 0481e80e22b1493e273dc0ebfb3a4202b5c7068f..f0ecb41339cc01244f22acdbd6b4214bac12555b 100755 --- a/SAS/TMSS/test/t_subtask_validation.py +++ b/SAS/TMSS/test/t_subtask_validation.py @@ -47,8 +47,7 @@ import requests class SubtaskValidationTest(unittest.TestCase): @staticmethod def create_subtask_template(schema): - subtask_template_data = SubtaskTemplate_test_data() - subtask_template_data['schema'] = schema + subtask_template_data = SubtaskTemplate_test_data(schema=schema) return models.SubtaskTemplate.objects.create(**subtask_template_data) def test_validate_simple_string_schema_with_valid_specification(self): @@ -113,7 +112,7 @@ class SubtaskValidationTest(unittest.TestCase): models.Subtask.objects.create(**subtask_data) self.assertTrue('invalid json' in str(context.exception).lower()) - def test_validate_correlator_schema(self): + def test_validate_correlator_schema_with_valid_specification(self): # fetch correlator_schema for Dupplo UC1 which should be in the initially populated database subtask_template = models.SubtaskTemplate.objects.get(name='correlator schema') self.assertIsNotNone(subtask_template) @@ -144,6 +143,65 @@ class SubtaskValidationTest(unittest.TestCase): models.Subtask.objects.create(**subtask_data) self.assertTrue('-10' in str(context.exception).lower()) + def test_validate_simple_string_schema_with_valid_specification_via_rest(self): + template = rest_data_creator.SubtaskTemplate(schema='{"type": "string"}') + schema_url = rest_data_creator.post_data_and_get_url(template, '/subtask_template/') + + specifications_doc = '"a random string"' + subtask_test_data = rest_data_creator.Subtask(specifications_template_url=schema_url, specifications_doc=specifications_doc) + + # POST and GET a new item and assert correctness + r_dict = POST_and_assert_expected_response(self, BASE_URL + '/subtask/', subtask_test_data, 201, subtask_test_data) + url = r_dict['url'] + GET_and_assert_expected_response(self, url, 200, subtask_test_data) + + def test_validate_simple_string_schema_with_invalid_specification_via_rest(self): + template = rest_data_creator.SubtaskTemplate(schema='{"type": "string"}') + schema_url = rest_data_creator.post_data_and_get_url(template, '/subtask_template/') + + specifications_doc = 42 # not a string, so not compliant with schema + subtask_test_data = rest_data_creator.Subtask(specifications_template_url=schema_url, specifications_doc=specifications_doc) + + # POST and GET a new item and assert correctness + response_content = POST_and_assert_expected_response(self, BASE_URL + '/subtask/', subtask_test_data, 500, {}) + + self.assertTrue("SpecificationException at /api/subtask/" in response_content) + self.assertTrue("42 is not of type 'string'" in response_content) + + def test_validate_correlator_schema_with_valid_specification_via_rest(self): + # fetch correlator_schema for Dupplo UC1 which should be in the initially populated database + response = requests.get(BASE_URL+"/subtask_template/", {"format": "json", "name": "correlator schema"}, auth=AUTH) + self.assertEqual(200, response.status_code) + json_response = response.json() + self.assertEqual(1, json_response.get('count')) + + template = json_response['results'][0] + schema_url = template['url'] + + specifications_doc = '''{ "duration": 10, + "calibrator": { "pointing": {} } + }''' + subtask_test_data = rest_data_creator.Subtask(specifications_template_url=schema_url, specifications_doc=specifications_doc) + + # POST and GET a new item and assert correctness + r_dict = POST_and_assert_expected_response(self, BASE_URL + '/subtask/', subtask_test_data, 201, subtask_test_data) + url = r_dict['url'] + GET_and_assert_expected_response(self, url, 200, subtask_test_data) + + def test_validate_correlator_schema_with_invalid_specification_via_rest(self): + # fetch correlator_schema for Dupplo UC1 which should be in the initially populated database + response = requests.get(BASE_URL+"/subtask_template/", {"format": "json", "name": "correlator schema"}, auth=AUTH) + self.assertEqual(200, response.status_code) + json_response = response.json() + self.assertEqual(1, json_response.get('count')) + + template = json_response['results'][0] + schema_url = template['url'] + + specifications_doc = "bogus spec" + subtask_test_data = rest_data_creator.Subtask(specifications_template_url=schema_url, specifications_doc=specifications_doc) + + POST_and_assert_expected_response(self, BASE_URL + '/subtask/', subtask_test_data, 500, {}) if __name__ == "__main__": os.environ['TZ'] = 'UTC' diff --git a/SAS/TMSS/test/tmss_test_data_django_models.py b/SAS/TMSS/test/tmss_test_data_django_models.py index 56e97911f32d3f96846fb4fb0a38be3e2136b2b3..5b415a9c864db9d270c2643ef111599bf2661532 100644 --- a/SAS/TMSS/test/tmss_test_data_django_models.py +++ b/SAS/TMSS/test/tmss_test_data_django_models.py @@ -167,12 +167,15 @@ def TaskRelationBlueprint_test_data(): "consumer": models.TaskBlueprint.objects.create(**TaskBlueprint_test_data())} -def SubtaskTemplate_test_data(): +def SubtaskTemplate_test_data(schema=None): + if schema is None: + schema = {} + return {"type": models.SubtaskType.objects.get(value='copy'), "name": "observation", "description": 'My one observation', "version": 'v0.314159265359', - "schema": {"mykey": "my value"}, + "schema": schema, "realtime": True, "queue": False, "tags": ["TMSS", "TESTING"]} diff --git a/SAS/TMSS/test/tmss_test_data_rest.py b/SAS/TMSS/test/tmss_test_data_rest.py index 5cd3a6133665fc24dad8f937c13486ce6fde834c..7e747a1230f596ac9672f19ec920fdebf02cfc89 100644 --- a/SAS/TMSS/test/tmss_test_data_rest.py +++ b/SAS/TMSS/test/tmss_test_data_rest.py @@ -258,12 +258,15 @@ class TMSSRESTTestDataCreator(): "producer": producer_url, "consumer": consumer_url} - def SubtaskTemplate(self, name="subtask1"): + def SubtaskTemplate(self, name="subtask1", schema=None): + if schema is None: + schema = {} + return {"type": self.django_api_url + '/subtask_type/copy/', "name": name, "description": 'My one observation', "version": 'v0.314159265359', - "schema": {"mykey": "my value"}, + "schema": schema, "realtime": True, "queue": False, "tags": ["TMSS", "TESTING"]} @@ -296,7 +299,7 @@ class TMSSRESTTestDataCreator(): "location": "upstairs", "tags": ['tmss', 'testing']} - def Subtask(self, cluster_url=None, task_blueprint_url=None, specifications_template_url=None): + def Subtask(self, cluster_url=None, task_blueprint_url=None, specifications_template_url=None, specifications_doc=None): if cluster_url is None: cluster_url = self.post_data_and_get_url(self.Cluster(), '/cluster/') @@ -305,11 +308,14 @@ class TMSSRESTTestDataCreator(): if specifications_template_url is None: specifications_template_url = self.post_data_and_get_url(self.SubtaskTemplate(), '/subtask_template/') - + + if specifications_doc is None: + specifications_doc = "{}" + return {"start_time": datetime.utcnow().isoformat(), "stop_time": datetime.utcnow().isoformat(), "state": self.django_api_url + '/subtask_state/scheduling/', - "specifications_doc": "{}", + "specifications_doc": specifications_doc, "task_blueprint": task_blueprint_url, "specifications_template": specifications_template_url, "tags": ["TMSS", "TESTING"],