Python源码示例:rest.DateField()
示例1
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
示例2
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
示例3
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
示例4
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
示例5
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)
示例6
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
示例7
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)
示例8
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')
示例9
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
示例10
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
示例11
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
示例12
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
示例13
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
示例14
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
示例15
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')
示例16
def test_should_date_convert_date():
assert_conversion(serializers.DateField, graphene.types.datetime.Date)