def test_validate_non_dict():
    """
    When a DictField is given a non-dict value, then validate should raise a ValidationError.
    """
    field = DictField(child=DateField())
    with pytest.raises(ValidationError):
        field.to_internal_value('notADict')
Exemplo n.º 2
0
def test_validate_non_dict():
    """
    When a DictField is given a non-dict value, then validate should raise a ValidationError.
    """
    field = DictField(child=DateField())
    with pytest.raises(ValidationError):
        field.to_internal_value('notADict')
def test_missing_required_dict():
    """
    When a DictField requires a value, then validate should raise a ValidationError on a missing
    (None) value.
    """
    field = DictField()
    with pytest.raises(ValidationError):
        field.validate(None)
def test_validate_empty_dict():
    """
    When a DictField requires a value, then validate should raise a ValidationError on an empty
    value.
    """
    field = DictField()
    with pytest.raises(ValidationError):
        field.validate({})
Exemplo n.º 5
0
def test_validate_elements_invalid():
    """
    When a DictField is given a dict containing values that are invalid for the value-field, then
    validate should raise a ValidationError.
    """
    field = DictField(child=CharField(max_length=5))
    with pytest.raises(ValidationError):
        field.to_internal_value({"a": "012345", "b": "012345"})
def test_validate_elements_invalid():
    """
    When a DictField is given a dict containing values that are invalid for the value-field, then
    validate should raise a ValidationError.
    """
    field = DictField(child=CharField(max_length=5))
    with pytest.raises(ValidationError):
        field.to_internal_value({"a": "012345", "b": "012345"})
def test_to_native_no_value_field():
    """
    When a DictField has no value-field, to_native should return the data it was given
    un-processed.
    """
    field = DictField()
    obj = {"a": 1, "b": 2}
    data = field.to_native(obj)
    assert obj == data
Exemplo n.º 8
0
def test_to_representation_with_child():
    """
    When a DictField has an value-field, to_representation should return a dict of elements resulting from
    the application of the value-field's to_representation method to each value of the input object dict.
    """
    field = DictField(child=DateField(format=ISO_8601))
    obj = {"a": date(2000, 1, 1), "b": date(2000, 1, 2)}
    data = field.to_representation(obj)
    assert {"a": "2000-01-01", "b": "2000-01-02"} == data
def test_to_representation_with_child():
    """
    When a DictField has an value-field, to_representation should return a dict of elements resulting from
    the application of the value-field's to_representation method to each value of the input object dict.
    """
    field = DictField(child=DateField(format=ISO_8601))
    obj = {"a": date(2000, 1, 1), "b": date(2000, 1, 2)}
    data = field.to_representation(obj)
    assert {"a": "2000-01-01", "b": "2000-01-02"} == data
Exemplo n.º 10
0
 def test_to_native_no_value_field(self):
     """
     When a DictField has no value-field, to_native should return the data it was given
     un-processed.
     """
     field = DictField()
     obj = {"a": 1, "b": 2}
     data = field.to_native(obj)
     self.assertEqual(obj, data)
Exemplo n.º 11
0
 def test_from_native_no_value_field(self):
     """
     When a DictField has no value-field, from_native should return the data it was given
     un-processed.
     """
     field = DictField()
     data = {"a": 1, "b": 2}
     obj = field.from_native(data)
     self.assertEqual(data, obj)
Exemplo n.º 12
0
def test_from_native_no_value_field():
    """
    When a DictField has no value-field, from_native should return the data it was given
    un-processed.
    """
    field = DictField()
    data = {"a": 1, "b": 2}
    obj = field.from_native(data)
    assert data == obj
Exemplo n.º 13
0
 def test_to_native_with_value_field(self):
     """
     When a DictField has an value-field, to_native should return a dict of elements resulting
     from the application of the value-field's to_native method to each value of the input
     object dict.
     """
     field = DictField(DateField(format=ISO_8601))
     obj = {"a": date(2000, 1, 1), "b": date(2000, 1, 2)}
     data = field.to_native(obj)
     self.assertEqual({"a": "2000-01-01", "b": "2000-01-02"}, data)
Exemplo n.º 14
0
 def test_from_native_with_value_field(self):
     """
     When a DictField has an value-field, from_native should return a dict of elements resulting
     from the application of the value-field's from_native method to each value of the input
     data dict.
     """
     field = DictField(DateField())
     data = {"a": "2000-01-01", "b": "2000-01-02"}
     obj = field.from_native(data)
     self.assertEqual({"a": date(2000, 1, 1), "b": date(2000, 1, 2)}, obj)
Exemplo n.º 15
0
def test_validate_elements_valid():
    """
    When a DictField is given a dict whose values are valid for the value-field, then validate
    should not raise a ValidationError.
    """
    field = DictField(child=CharField(max_length=5))
    try:
        field.to_internal_value({"a": "a", "b": "b", "c": "c"})
    except ValidationError:
        assert False, "ValidationError was raised"
Exemplo n.º 16
0
 def test_validate_elements_valid(self):
     """
     When a DictField is given a dict whose values are valid for the value-field, then validate
     will not raise a ValidationError.
     """
     field = DictField(CharField(max_length=5))
     try:
         field.validate({"a": "a", "b": "b", "c": "c"})
     except ValidationError:
         self.fail("ValidationError was raised")
def test_validate_elements_valid():
    """
    When a DictField is given a dict whose values are valid for the value-field, then validate
    should not raise a ValidationError.
    """
    field = DictField(child=CharField(max_length=5))
    try:
        field.to_internal_value({"a": "a", "b": "b", "c": "c"})
    except ValidationError:
        assert False, "ValidationError was raised"
Exemplo n.º 18
0
def test_to_internal_value_with_child():
    """
    When a DictField has an value-field, to_internal_value should return a dict of elements resulting
    from the application of the value-field's to_internal_value method to each value of the input
    data dict.
    """
    field = DictField(child=DateField())
    data = {"a": "2000-01-01", "b": "2000-01-02"}
    obj = field.to_internal_value(data)
    assert {"a": date(2000, 1, 1), "b": date(2000, 1, 2)} == obj
def test_to_internal_value_with_child():
    """
    When a DictField has an value-field, to_internal_value should return a dict of elements resulting
    from the application of the value-field's to_internal_value method to each value of the input
    data dict.
    """
    field = DictField(child=DateField())
    data = {"a": "2000-01-01", "b": "2000-01-02"}
    obj = field.to_internal_value(data)
    assert {"a": date(2000, 1, 1), "b": date(2000, 1, 2)} == obj
Exemplo n.º 20
0
def test_missing_not_required_dict():
    """
    When a DictField do not require a value, then validate should not raise a
    ValidationError on a missing (None) value.
    """
    field = DictField(required=False)

    try:
       field.validate(None)
    except ValidationError:
       assert False, "ValidationError was raised"
Exemplo n.º 21
0
    def __init__(self, *args, **kwargs):
        super(ILPSimpleGeoSerializer, self).__init__(*args, **kwargs)

        if 'context' in kwargs:
            request = kwargs['context']['request']
            geometry = request.GET.get('geometry', 'no')
            simplify = request.GET.get('simplify', 'yes')

            if geometry == 'yes' and simplify == 'no':
                self.fields['geometry'] = DictField(source='get_geometry')

            if geometry == 'yes' and simplify == 'yes':
                self.fields['geometry'] = \
                    DictField(source='get_simple_geometry')
Exemplo n.º 22
0
 def test_missing_required_dict(self):
     """
     When a DictField requires a value, then validate will raise a ValidationError on a missing
     (None) value.
     """
     field = DictField()
     self.assertRaises(ValidationError, field.validate, None)
Exemplo n.º 23
0
 def test_validate_empty_dict(self):
     """
     When a DictField requires a value, then validate will raise a ValidationError on an empty
     value.
     """
     field = DictField()
     self.assertRaises(ValidationError, field.validate, {})
Exemplo n.º 24
0
 def test_validate_elements_invalid(self):
     """
     When a DictField is given a dict containing values that are invalid for the value-field,
     then validate will raise a ValidationError.
     """
     field = DictField(CharField(max_length=5))
     self.assertRaises(ValidationError, field.validate, {"a": "012345", "b": "012345"})
Exemplo n.º 25
0
 def __init__(self, *args, **kwargs):
     super(KLPSerializer, self).__init__(*args, **kwargs)
     if 'context' in kwargs:
         request = kwargs['context']['request']
         geometry = request.GET.get('geometry', 'no')
         # add geometry to fields if geometry=yes in query params
         if geometry == 'yes':
             self.fields['geometry'] = DictField(source='get_geometry')
Exemplo n.º 26
0
 def test_validate_non_dict(self):
     """
     When a DictField is given a non-dict value, then validate will raise a ValidationError.
     """
     field = DictField()
     self.assertRaises(ValidationError, field.validate, 'notADict')
Exemplo n.º 27
0
class DictSerializer(serializers.Serializer):
    emails = DictField(child=serializers.EmailField(), required=False)