Exemple #1
0
    def get_batch_definition(self):
        """Returns the definition for this batch

        :returns: The definition for this batch
        :rtype: :class:`batch.configuration.definition.batch_definition.BatchDefinition`
        """

        return BatchDefinition(self.definition)
    def test_minimum(self):
        """Tests the bare minimum schema definition"""

        definition = {
            'version': '1.0',
        }

        # No exception means success
        BatchDefinition(definition)
    def test_trigger_default(self):
        """Tests defining the current recipe type trigger should be used."""

        definition = {
            'version': '1.0',
            'trigger_rule': True,
        }

        # No exception means success
        BatchDefinition(definition)
    def test_priority(self):
        """Tests defining override priority."""

        definition = {
            'version': '1.0',
            'priority': 1111,
        }

        # No exception means success
        BatchDefinition(definition)
    def test_all_job(self):
        """Tests defining all jobs"""

        definition = {
            'version': '1.0',
            'all_jobs': True,
        }

        # No exception means success
        BatchDefinition(definition)
Exemple #6
0
    def create(self, request):
        """Creates a new batch and returns a link to the detail URL

        :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')
        title = rest_util.parse_string(request, 'title', required=False)
        description = rest_util.parse_string(request, 'description', 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: %i' % recipe_type_id)

        # Validate the batch definition
        definition_dict = rest_util.parse_dict(request, 'definition')
        definition = None
        try:
            if definition_dict:
                definition = BatchDefinition(definition_dict)
                definition.validate(recipe_type)
        except InvalidDefinition as ex:
            raise BadParameter('Batch definition invalid: %s' % unicode(ex))

        # Create the batch
        batch = Batch.objects.create_batch(recipe_type, definition, title=title, description=description)

        # Fetch the full batch with details
        try:
            batch = Batch.objects.get_details(batch.id)
        except Batch.DoesNotExist:
            raise Http404

        url = reverse('batch_details_view', args=[batch.id], request=request)
        serializer = BatchDetailsSerializer(batch)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=dict(location=url))
    def test_job_names(self):
        """Tests defining a list of job names"""

        definition = {
            'version': '1.0',
            'job_names': [
                'job1',
                'job2',
            ],
        }

        # No exception means success
        BatchDefinition(definition)
    def test_date_range_ended(self):
        """Tests defining a date range with only an end date"""

        definition = {
            'version': '1.0',
            'date_range': {
                'ended': '2016-12-31T00:00:00.000Z',
            },
        }

        # No exception means success
        batch_def = BatchDefinition(definition)
        self.assertEqual(batch_def.ended,
                         datetime.datetime(2016, 12, 31, tzinfo=utc))
Exemple #9
0
    def post(self, request):
        """Validates a new batch and returns any warnings discovered

        :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')

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

        # Validate the batch definition
        definition_dict = rest_util.parse_dict(request, 'definition')
        definition = None
        warnings = []
        try:
            if definition_dict:
                definition = BatchDefinition(definition_dict)
                warnings = definition.validate(recipe_type)
        except InvalidDefinition as ex:
            raise BadParameter('Batch definition invalid: %s' % unicode(ex))

        # Get a rough estimate of how many recipes/files will be affected
        old_recipes = Batch.objects.get_matched_recipes(recipe_type, definition)
        old_files = Batch.objects.get_matched_files(recipe_type, definition)

        return Response({
            'recipe_count': old_recipes.count(),
            'file_count': old_files.count(),
            'warnings': warnings,
        })
    def test_trigger_custom(self):
        """Tests defining a custom trigger to use."""

        definition = {
            'version': '1.0',
            'trigger_rule': {
                'condition': {
                    'media_type': 'text/plain',
                    'data_types': ['foo', 'bar'],
                },
                'data': {
                    'input_data_name': 'my_file',
                    'workspace_name': 'my_workspace',
                },
            },
        }

        # No exception means success
        BatchDefinition(definition)
Exemple #11
0
def create_batch(recipe_type=None,
                 definition=None,
                 title=None,
                 description=None,
                 status=None,
                 recipe_count=0):
    """Creates a batch model for unit testing

    :returns: The batch model
    :rtype: :class:`batch.models.Batch`
    """

    if not recipe_type:
        recipe_type = recipe_test_utils.create_recipe_type()
    if not definition:
        definition = {}
    if not isinstance(definition, BatchDefinition):
        definition = BatchDefinition(definition)
    if not title:
        global BATCH_TITLE_COUNTER
        title = 'Test Batch Title %i' % BATCH_TITLE_COUNTER
        BATCH_TITLE_COUNTER += 1
    if not description:
        global BATCH_DESCRIPTION_COUNTER
        description = 'Test Batch Description %i' % BATCH_DESCRIPTION_COUNTER
        BATCH_DESCRIPTION_COUNTER += 1

    for i in range(recipe_count):
        recipe_test_utils.create_recipe(recipe_type=recipe_type)

    batch = Batch.objects.create_batch(recipe_type=recipe_type,
                                       definition=definition,
                                       title=title,
                                       description=description)
    if status:
        batch.status = status
        batch.save()
    return batch