Python rest_framework.serializers.DateField() Examples

The following are 16 code examples of rest_framework.serializers.DateField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework.serializers , or try the search function .
Example #1
Source File: test_listoritemfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_representation_list():
    """
    When given a valid list, the ListOrItemField to_representation method should utilize the list to-native
    logic.
    """
    field = ListOrItemField(child=DateField(format=ISO_8601))
    data = field.to_representation([date.today()])
    assert [date.today().isoformat()] == data 
Example #2
Source File: test_listoritemfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_internal_value_list():
    """
    When given a valid list, the ListOrItemField to_internal_value method should utilize the list
    from-native logic.
    """
    field = ListOrItemField(child=DateField(format=ISO_8601))
    data = field.to_internal_value([date.today().isoformat()])
    assert [date.today()] == data 
Example #3
Source File: test_listoritemfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_representation_item():
    """
    When given a valid item, the ListOrItemField to_representation method should utilize the item to-native
    logic.
    """
    field = ListOrItemField(child=DateField(format=ISO_8601))
    data = field.to_representation(date.today())
    assert date.today().isoformat() == data 
Example #4
Source File: test_listoritemfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_internal_value_item():
    """
    When given a valid item, the ListOrItemField to_internal_value method should utilize the item
    from-native logic.
    """
    field = ListOrItemField(child=DateField(format=ISO_8601))
    data = field.to_internal_value(date.today().isoformat())
    assert date.today() == data 
Example #5
Source File: test_listoritemfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_validate_required_missing():
    """
    When given a None value the ListOrItemField validate method should raise a ValidationError.
    """
    field = ListOrItemField(child=DateField(format=ISO_8601))
    with pytest.raises(ValidationError):
        field.to_internal_value(None) 
Example #6
Source File: test_listfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_representation_with_item_field():
    """
    When a ListField has an item-field, to_representation should return a list of elements resulting from
    the application of the item-field's to_representation method to each element of the input object list.
    """
    field = ListField(child=DateField(format=ISO_8601))
    obj = [date(2000, 1, 1), date(2000, 1, 2)]
    data = field.to_representation(obj)
    assert ["2000-01-01", "2000-01-02"] == data 
Example #7
Source File: test_listfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_missing_required_list():
    """
    When a ListField requires a value, then validate should raise a ValidationError on a missing
    (None) value.
    """
    field = ListField(child=DateField())
    with pytest.raises(ValidationError):
        field.to_internal_value(None) 
Example #8
Source File: test_listfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_validate_non_list():
    """
    When a ListField is given a non-list value, then validate should raise a ValidationError.
    """
    field = ListField(child=DateField())
    with pytest.raises(ValidationError):
        field.to_internal_value('notAList') 
Example #9
Source File: test_listfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_errors_non_list():
    """
    When a ListField is given a non-list value, then there should be one error related to the type
    mismatch.
    """
    field = ListField(child=DateField())
    try:
        field.to_internal_value('notAList')
        assert False, 'Expected ValidationError'
    except ValidationError as e:
        pass 
Example #10
Source File: test_partialdictfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_internal_value_with_included_keys():
    """
    When a PartialDictField has an included_keys, to_internal_value should return a dict of elmenents
    resulting from the application of the value-field's to_internal_value method to values of the input
    data dict that are includeded by included_keys.
    """
    field = PartialDictField(included_keys=['a'], child=DateField())
    data = {"a": "2000-01-01", "b": "2000-01-02"}
    obj = field.to_internal_value(data)
    assert {"a": date(2000, 1, 1)} == obj 
Example #11
Source File: test_partialdictfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_internal_value_with_nonexisting_included_keys():
    """
    When a PartialDictField has an included_keys that includes nonexisting keys, to_internal_value should
    ignore them.
    """
    field = PartialDictField(included_keys=['a', 'c'], child=DateField())
    data = {"a": "2000-01-01", "b": "2000-01-02"}
    obj = field.to_internal_value(data)
    assert {"a": date(2000, 1, 1)} == obj 
Example #12
Source File: test_partialdictfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_to_representation_with_nonexisting_included_keys():
    """
    When a PartialDictField has an included_keys that includes nonexisting keys, to_representation should
    ignore them.
    """
    field = PartialDictField(included_keys=['a', 'c'], 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"} == data 
Example #13
Source File: test_dictfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #14
Source File: test_dictfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #15
Source File: test_dictfield.py    From drf-compound-fields with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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') 
Example #16
Source File: test_field_converter.py    From graphene-django with MIT License 5 votes vote down vote up
def test_should_date_convert_date():
    assert_conversion(serializers.DateField, graphene.types.datetime.Date)