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({})
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
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_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)
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)
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
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)
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)
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"
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_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_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"
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')
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)
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, {})
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"})
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')
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')
class DictSerializer(serializers.Serializer): emails = DictField(child=serializers.EmailField(), required=False)