Пример #1
0
    def test_init_validation(self):
        """Tests the validation done in __init__"""

        # Try minimal acceptable configuration
        BatchConfigurationV6(do_validate=True)

        # Invalid version
        json_dict = {'version': 'BAD'}
        self.assertRaises(InvalidConfiguration, BatchConfigurationV6, json_dict, True)

        # Valid priority
        json_dict = {'version': '6', 'priority': 500}
        BatchConfigurationV6(configuration=json_dict, do_validate=True)
Пример #2
0
    def test_convert_configuration_to_v6(self):
        """Tests calling convert_configuration_to_v6()"""

        # Try configuration with nothing set
        configuration = BatchConfiguration()
        json = convert_configuration_to_v6(configuration)
        BatchConfigurationV6(configuration=json.get_dict(), do_validate=True)  # Revalidate

        # Try configuration with priority set
        configuration = BatchConfiguration()
        configuration.priority = 100
        json = convert_configuration_to_v6(configuration)
        BatchConfigurationV6(configuration=json.get_dict(), do_validate=True)  # Revalidate
        self.assertEqual(json.get_configuration().priority, configuration.priority)
Пример #3
0
    def test_create_from_json(self):
        """Tests creating a BatchConfiguration from a JSON"""

        # Valid batch configuration
        json_dict = {'version': '6', 'priority': 201}
        json = BatchConfigurationV6(configuration=json_dict, do_validate=True)
        config = json.get_configuration()
        self.assertEqual(config.priority, 201)
Пример #4
0
    def get_configuration(self):
        """Returns the configuration for this batch

        :returns: The configuration for this batch
        :rtype: :class:`batch.configuration.configuration.BatchConfiguration`
        """

        return BatchConfigurationV6(configuration=self.configuration, do_validate=False).get_configuration()
Пример #5
0
    def _post_v6(self, request):
        """The v6 version for validating a new batch

        :param request: the HTTP POST request
        :type request: :class:`rest_framework.request.Request`
        :rtype: :class:`rest_framework.response.Response`
        :returns: the HTTP response to send back to the user
        """

        recipe_type_id = rest_util.parse_int(request, 'recipe_type_id')
        definition_dict = rest_util.parse_dict(request, 'definition')
        configuration_dict = rest_util.parse_dict(request,
                                                  'configuration',
                                                  required=False)

        # Make sure the recipe type exists
        try:
            recipe_type = RecipeType.objects.get(pk=recipe_type_id)
        except RecipeType.DoesNotExist:
            raise BadParameter('Unknown recipe type: %d' % recipe_type_id)

        try:
            definition = BatchDefinitionV6(definition=definition_dict,
                                           do_validate=True).get_definition()
            configuration = BatchConfigurationV6(
                configuration=configuration_dict,
                do_validate=True).get_configuration()
        except InvalidDefinition as ex:
            raise BadParameter(unicode(ex))
        except InvalidConfiguration as ex:
            raise BadParameter(unicode(ex))

        # Validate the batch
        validation = Batch.objects.validate_batch_v6(
            recipe_type, definition, configuration=configuration)
        batch = validation.batch
        recipe_type_serializer = RecipeTypeBaseSerializerV6(batch.recipe_type)
        resp_dict = {
            'is_valid': validation.is_valid,
            'errors': [e.to_dict() for e in validation.errors],
            'warnings': [w.to_dict() for w in validation.warnings],
            'recipes_estimated': definition.estimated_recipes,
            'recipe_type': recipe_type_serializer.data
        }
        if batch.superseded_batch:
            recipe_type_rev_serializer = RecipeTypeRevisionBaseSerializerV6(
                batch.superseded_batch.recipe_type_rev)
            prev_batch_dict = {
                'recipe_type_rev': recipe_type_rev_serializer.data
            }
            resp_dict['prev_batch'] = prev_batch_dict
            if definition.prev_batch_diff:
                diff_v6 = convert_recipe_diff_to_v6_json(
                    definition.prev_batch_diff)
                diff_dict = rest_util.strip_schema_version(diff_v6.get_dict())
                prev_batch_dict['diff'] = diff_dict
        return Response(resp_dict)
Пример #6
0
    def _create_v6(self, request):
        """The v6 version for creating batches

        :param request: the HTTP POST request
        :type request: :class:`rest_framework.request.Request`
        :rtype: :class:`rest_framework.response.Response`
        :returns: the HTTP response to send back to the user
        """

        title = rest_util.parse_string(request, 'title', required=False)
        description = rest_util.parse_string(request,
                                             'description',
                                             required=False)
        recipe_type_id = rest_util.parse_int(request, 'recipe_type_id')
        definition_dict = rest_util.parse_dict(request, 'definition')
        configuration_dict = rest_util.parse_dict(request,
                                                  'configuration',
                                                  required=False)

        # Make sure the recipe type exists
        try:
            recipe_type = RecipeType.objects.get(pk=recipe_type_id)
        except RecipeType.DoesNotExist:
            raise BadParameter('Unknown recipe type: %d' % recipe_type_id)

        # Validate and create the batch
        try:
            definition = BatchDefinitionV6(definition=definition_dict,
                                           do_validate=True).get_definition()
            configuration = BatchConfigurationV6(
                configuration=configuration_dict,
                do_validate=True).get_configuration()
            with transaction.atomic():
                event = TriggerEvent.objects.create_trigger_event(
                    'USER', None, {'user': '******'}, now())
                batch = Batch.objects.create_batch_v6(
                    title,
                    description,
                    recipe_type,
                    event,
                    definition,
                    configuration=configuration)
                CommandMessageManager().send_messages(
                    [create_batch_recipes_message(batch.id)])
        except InvalidDefinition as ex:
            raise BadParameter(unicode(ex))
        except InvalidConfiguration as ex:
            raise BadParameter(unicode(ex))

        # Fetch the full batch with details
        batch = Batch.objects.get_details_v6(batch.id)

        url = reverse('batch_details_view', args=[batch.id], request=request)
        serializer = BatchDetailsSerializerV6(batch)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers={'Location': url})
Пример #7
0
    def test_validate(self):
        """Tests calling BatchConfiguration.validate()"""

        batch = batch_test_utils.create_batch()

        # Valid configuration
        json_dict = {'version': '6', 'priority': 202}
        json = BatchConfigurationV6(configuration=json_dict)
        configuration = json.get_configuration()
        configuration.validate(batch)
Пример #8
0
    def _update_v6(self, request, batch_id):
        """The v6 version for updating a batch

        :param request: the HTTP PATCH request
        :type request: :class:`rest_framework.request.Request`
        :param batch_id: the batch id
        :type batch_id: int
        :rtype: :class:`rest_framework.response.Response`
        :returns: the HTTP response to send back to the user
        """

        title = rest_util.parse_string(request, 'title', required=False)
        description = rest_util.parse_string(request,
                                             'description',
                                             required=False)
        configuration_dict = rest_util.parse_dict(request,
                                                  'configuration',
                                                  required=False)

        try:
            batch = Batch.objects.get(id=batch_id)
        except Batch.DoesNotExist:
            raise Http404()

        # Validate and create the batch
        try:
            configuration = None
            if configuration_dict:
                configuration_json = BatchConfigurationV6(
                    configuration=configuration_dict, do_validate=True)
                configuration = configuration_json.get_configuration()
            Batch.objects.edit_batch_v6(batch,
                                        title=title,
                                        description=description,
                                        configuration=configuration)
        except InvalidConfiguration as ex:
            raise BadParameter('Batch configuration invalid: %s' % unicode(ex))

        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #9
0
    def test_inputmap(self):
        dataset_def = {
            'parameters': {
                'files': [{
                    'media_types': ['image/png'],
                    'required': True,
                    'multiple': False,
                    'name': 'INPUT_IMAGE'
                }],
                'json': []
            }
        }
        the_dataset = data_test_utils.create_dataset(definition=dataset_def)
        workspace = storage_test_utils.create_workspace()
        src_file_a = storage_test_utils.create_file(file_name='input_a.PNG',
                                                    file_type='SOURCE',
                                                    media_type='image/png',
                                                    file_size=10,
                                                    data_type_tags=['type'],
                                                    file_path='the_path',
                                                    workspace=workspace)
        src_file_b = storage_test_utils.create_file(file_name='input_b.PNG',
                                                    file_type='SOURCE',
                                                    media_type='image/png',
                                                    file_size=10,
                                                    data_type_tags=['type'],
                                                    file_path='the_path',
                                                    workspace=workspace)
        data_list = []
        data_dict = {
            'version': '6',
            'files': {
                'FILE_INPUT': [src_file_a.id]
            },
            'json': {}
        }
        data_list.append(DataV6(data=data_dict).get_dict())
        data_dict = {
            'version': '6',
            'files': {
                'FILE_INPUT': [src_file_b.id]
            },
            'json': {}
        }
        data_list.append(DataV6(data=data_dict).get_dict())
        members = data_test_utils.create_dataset_members(dataset=the_dataset,
                                                         data_list=data_list)

        batch_def = BatchDefinition()
        batch_def.dataset = the_dataset.id
        batch = batch_test_utils.create_batch(definition=batch_def)

        json_dict = {
            'version':
            '6',
            'priority':
            100,
            'inputMap': [{
                'input': 'FILE_INPUT',
                'datasetParameter': 'INPUT_IMAGE'
            }]
        }
        json = BatchConfigurationV6(configuration=json_dict)
        configuration = json.get_configuration()
        configuration.validate(batch)

        json_dict = {
            'version': '6',
            'priority': 100,
            'inputMap': [{
                'input': 'FILE_INPUT',
                'datasetParameter': 'FILE_INPUT'
            }]
        }
        json = BatchConfigurationV6(configuration=json_dict)
        configuration = json.get_configuration()
        self.assertRaises(InvalidConfiguration, configuration.validate, batch)