Exemplo n.º 1
0
 def test_serialization(self):
     '''
     Test that an InputSpec instance serializes to the expected
     dictionary representation
     '''
     i = InputSpecSerializer(self.input_spec)
     self.assertDictEqual(i.data, self.expected_spec_result)
Exemplo n.º 2
0
 def create(self, validated_data):
     '''
     Returns an OperationInput instance from the validated
     data.
     '''
     spec = InputSpecSerializer(data=validated_data['spec']).get_instance()
     return OperationInput(validated_data['description'],
                           validated_data['name'], spec,
                           validated_data['required'])
Exemplo n.º 3
0
    def test_non_native_types(self):
        '''
        The other tests use the children of the Attribute classes to 
        produce their serialized/deserialized representation.

        Here, we test the "other" input spec types, such as those for
        Observations, ObservationSets, etc.
        '''
        types_to_test = {
            'Observation': ObservationInputSpec,
            'ObservationSet': ObservationSetInputSpec,
            'Feature': FeatureInputSpec,
            'FeatureSet': FeatureSetInputSpec
        }
        for t, tt in types_to_test.items():
            spec_dict = {'attribute_type': t}
            x = InputSpecSerializer(data=spec_dict)
            self.assertTrue(x.is_valid())
            i = tt()
            x = InputSpecSerializer(i)
            self.assertEqual(x.data, spec_dict)
Exemplo n.º 4
0
 def test_equality(self):
     '''
     Tests that the equality overload works as expected
     '''
     input_spec_dict1 = {
         'attribute_type': 'BoundedInteger',
         'min': 0,
         'max': 5,
         'default': 3
     }
     input_spec_dict2 = {
         'attribute_type': 'BoundedInteger',
         'min': 0,
         'max': 5,
         'default': 3
     }
     input_spec_dict3 = {
         'attribute_type': 'BoundedInteger',
         'min': 0,
         'max': 5,
         'default': 4  # different default
     }
     input_spec_dict4 = {
         'attribute_type': 'BoundedInteger',
         'min': 0,
         'max': 3,  # different max 
         'default': 3
     }
     input_spec_dict5 = {'attribute_type': 'Integer', 'default': 3}
     i1 = InputSpecSerializer(data=input_spec_dict1).get_instance()
     i2 = InputSpecSerializer(data=input_spec_dict2).get_instance()
     i3 = InputSpecSerializer(data=input_spec_dict3).get_instance()
     i4 = InputSpecSerializer(data=input_spec_dict4).get_instance()
     i5 = InputSpecSerializer(data=input_spec_dict5).get_instance()
     self.assertEqual(i1, i2)
     self.assertNotEqual(i1, i3)
     self.assertNotEqual(i1, i4)
     self.assertNotEqual(i4, i5)
Exemplo n.º 5
0
    def test_list_types_serialization(self):
        spec = StringListInputSpec(default=['abc', 'xyz'])
        i = InputSpecSerializer(spec)
        self.assertDictEqual(i.data, {
            'attribute_type': 'StringList',
            'default': ['abc', 'xyz']
        })

        spec = StringListInputSpec(default=['a b'])
        i = InputSpecSerializer(spec)
        # below, note that the default didn't change to "a_b". The value of "a b" (with a space)
        # is valid, and that's all that gets checked.
        self.assertDictEqual(i.data, {
            'attribute_type': 'StringList',
            'default': ['a b']
        })

        spec = UnrestrictedStringListInputSpec(default=['a?b', 'xyz'])
        i = InputSpecSerializer(spec)
        self.assertDictEqual(i.data, {
            'attribute_type': 'UnrestrictedStringList',
            'default': ['a?b', 'xyz']
        })
Exemplo n.º 6
0
class OperationInputSerializer(serializers.Serializer):

    description = serializers.CharField(max_length=5000, required=True)
    name = serializers.CharField(max_length=100, required=True)
    required = serializers.BooleanField(required=True)
    spec = InputSpecSerializer(required=True)

    def to_representation(self, instance):
        if type(instance) == OperationInput:
            return instance.to_dict()
        else:
            return instance

    def create(self, validated_data):
        '''
        Returns an OperationInput instance from the validated
        data.
        '''
        spec = InputSpecSerializer(data=validated_data['spec']).get_instance()
        return OperationInput(validated_data['description'],
                              validated_data['name'], spec,
                              validated_data['required'])

    def get_instance(self):
        '''
        A more suggestive way to retrieve the OperationInput
        instance from the serializer than `save()`, since
        we are not actually saving OperationInput instances in the
        database.
        '''
        self.is_valid(raise_exception=True)
        return self.create(self.validated_data)

    def is_valid(self, raise_exception=False):
        if hasattr(self, 'initial_data'):
            payload_keys = set(
                self.initial_data.keys())  # all the payload keys
            serializer_fields = set(
                self.fields.keys())  # all the serializer fields
            extra_fields = payload_keys.difference(serializer_fields)
            #filter(lambda key: key not in serializer_fields , payload_keys)
            if len(extra_fields) > 0:
                raise ValidationError('Extra fields ({s}) in payload'.format(
                    s=','.join(extra_fields)))
        return super().is_valid(raise_exception)
Exemplo n.º 7
0
    def test_deserialization(self):
        '''
        Test that a JSON-like representation properly creates an OperationInput
        instance, or issues appropriate errors if malformatted.
        '''
        i = InputSpecSerializer(data=self.input_spec_dict)
        self.assertTrue(i.is_valid())
        ii = i.get_instance()
        self.assertDictEqual(ii.to_dict(), self.expected_spec_result)

        # missing default is ok.
        input_spec_dict = {
            'attribute_type': 'BoundedInteger',
            'min': self.min_val,
            'max': self.max_val
        }
        i = InputSpecSerializer(data=input_spec_dict)
        self.assertTrue(i.is_valid())

        # the attribute_type is not valid
        invalid_input_spec_dict = {
            'attribute_type': 'Some bad type',
            'min': self.min_val,
            'max': self.max_val,
            'default': self.default
        }
        i = InputSpecSerializer(data=invalid_input_spec_dict)
        self.assertFalse(i.is_valid())

        # default is not within the bounds
        invalid_input_spec_dict = {
            'attribute_type': 'BoundedInteger',
            'min': self.min_val,
            'max': self.max_val,
            'default': self.max_val + 1
        }
        i = InputSpecSerializer(data=invalid_input_spec_dict)
        self.assertFalse(i.is_valid())

        # missing 'min' key
        invalid_input_spec_dict = {
            'attribute_type': 'BoundedInteger',
            'max': self.max_val,
            'default': self.default
        }
        i = InputSpecSerializer(data=invalid_input_spec_dict)
        self.assertFalse(i.is_valid())