2014-09-22 15:25:57 +04:00
|
|
|
from decimal import Decimal
|
|
|
|
from django.utils import timezone
|
|
|
|
from rest_framework import fields
|
|
|
|
import datetime
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2014-09-22 17:54:33 +04:00
|
|
|
def get_items(mapping_or_list_of_two_tuples):
|
|
|
|
# Tests accept either lists of two tuples, or dictionaries.
|
|
|
|
if isinstance(mapping_or_list_of_two_tuples, dict):
|
|
|
|
# {value: expected}
|
|
|
|
return mapping_or_list_of_two_tuples.items()
|
|
|
|
# [(value, expected), ...]
|
|
|
|
return mapping_or_list_of_two_tuples
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class FieldValues:
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
2014-09-22 17:54:33 +04:00
|
|
|
Base class for testing valid and invalid input values.
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
def test_valid_inputs(self):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Ensure that valid values return the expected validated data.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
for input_value, expected_output in get_items(self.valid_inputs):
|
2014-09-22 15:25:57 +04:00
|
|
|
assert self.field.run_validation(input_value) == expected_output
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
def test_invalid_inputs(self):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Ensure that invalid values raise the expected validation error.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
for input_value, expected_failure in get_items(self.invalid_inputs):
|
2014-09-22 15:25:57 +04:00
|
|
|
with pytest.raises(fields.ValidationError) as exc_info:
|
|
|
|
self.field.run_validation(input_value)
|
|
|
|
assert exc_info.value.messages == expected_failure
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
def test_outputs(self):
|
|
|
|
for output_value, expected_output in get_items(self.outputs):
|
|
|
|
assert self.field.to_representation(output_value) == expected_output
|
|
|
|
|
2014-09-22 15:25:57 +04:00
|
|
|
|
2014-09-22 16:26:47 +04:00
|
|
|
# Boolean types...
|
2014-09-22 15:25:57 +04:00
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestBooleanField(FieldValues):
|
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'true': True,
|
|
|
|
'false': False,
|
|
|
|
'1': True,
|
|
|
|
'0': False,
|
|
|
|
1: True,
|
|
|
|
0: False,
|
|
|
|
True: True,
|
|
|
|
False: False,
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'foo': ['`foo` is not a valid boolean.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {
|
|
|
|
'true': True,
|
|
|
|
'false': False,
|
|
|
|
'1': True,
|
|
|
|
'0': False,
|
|
|
|
1: True,
|
|
|
|
0: False,
|
|
|
|
True: True,
|
|
|
|
False: False,
|
|
|
|
'other': True
|
|
|
|
}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.BooleanField()
|
|
|
|
|
|
|
|
|
2014-09-22 16:26:47 +04:00
|
|
|
# String types...
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestCharField(FieldValues):
|
|
|
|
valid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
1: '1',
|
|
|
|
'abc': 'abc'
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'': ['This field may not be blank.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {
|
|
|
|
1: '1',
|
|
|
|
'abc': 'abc'
|
|
|
|
}
|
2014-09-22 16:26:47 +04:00
|
|
|
field = fields.CharField()
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestEmailField(FieldValues):
|
|
|
|
valid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'example@example.com': 'example@example.com',
|
|
|
|
' example@example.com ': 'example@example.com',
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'example.com': ['Enter a valid email address.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 16:26:47 +04:00
|
|
|
field = fields.EmailField()
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestRegexField(FieldValues):
|
|
|
|
valid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'a9': 'a9',
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'A9': ["This value does not match the required pattern."]
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 16:26:47 +04:00
|
|
|
field = fields.RegexField(regex='[a-z][0-9]')
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestSlugField(FieldValues):
|
|
|
|
valid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'slug-99': 'slug-99',
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'slug 99': ["Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 16:26:47 +04:00
|
|
|
field = fields.SlugField()
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestURLField(FieldValues):
|
|
|
|
valid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'http://example.com': 'http://example.com',
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'example.com': ['Enter a valid URL.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 16:26:47 +04:00
|
|
|
field = fields.URLField()
|
|
|
|
|
|
|
|
|
2014-09-22 15:25:57 +04:00
|
|
|
# Number types...
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestIntegerField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `IntegerField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'1': 1,
|
|
|
|
'0': 0,
|
|
|
|
1: 1,
|
|
|
|
0: 0,
|
|
|
|
1.0: 1,
|
|
|
|
0.0: 0
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'abc': ['A valid integer is required.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {
|
|
|
|
'1': 1,
|
|
|
|
'0': 0,
|
|
|
|
1: 1,
|
|
|
|
0: 0,
|
|
|
|
1.0: 1,
|
|
|
|
0.0: 0
|
|
|
|
}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.IntegerField()
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestMinMaxIntegerField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `IntegerField` with min and max limits.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'1': 1,
|
|
|
|
'3': 3,
|
|
|
|
1: 1,
|
|
|
|
3: 3,
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
0: ['Ensure this value is greater than or equal to 1.'],
|
|
|
|
4: ['Ensure this value is less than or equal to 3.'],
|
|
|
|
'0': ['Ensure this value is greater than or equal to 1.'],
|
|
|
|
'4': ['Ensure this value is less than or equal to 3.'],
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.IntegerField(min_value=1, max_value=3)
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestFloatField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `FloatField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'1': 1.0,
|
|
|
|
'0': 0.0,
|
|
|
|
1: 1.0,
|
|
|
|
0: 0.0,
|
|
|
|
1.0: 1.0,
|
|
|
|
0.0: 0.0,
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'abc': ["A valid number is required."]
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {
|
|
|
|
'1': 1.0,
|
|
|
|
'0': 0.0,
|
|
|
|
1: 1.0,
|
|
|
|
0: 0.0,
|
|
|
|
1.0: 1.0,
|
|
|
|
0.0: 0.0,
|
|
|
|
}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.FloatField()
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestMinMaxFloatField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `FloatField` with min and max limits.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'1': 1,
|
|
|
|
'3': 3,
|
|
|
|
1: 1,
|
|
|
|
3: 3,
|
|
|
|
1.0: 1.0,
|
|
|
|
3.0: 3.0,
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
0.9: ['Ensure this value is greater than or equal to 1.'],
|
|
|
|
3.1: ['Ensure this value is less than or equal to 3.'],
|
|
|
|
'0.0': ['Ensure this value is greater than or equal to 1.'],
|
|
|
|
'3.1': ['Ensure this value is less than or equal to 3.'],
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.FloatField(min_value=1, max_value=3)
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestDecimalField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `DecimalField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'12.3': Decimal('12.3'),
|
|
|
|
'0.1': Decimal('0.1'),
|
|
|
|
10: Decimal('10'),
|
|
|
|
0: Decimal('0'),
|
|
|
|
12.3: Decimal('12.3'),
|
|
|
|
0.1: Decimal('0.1'),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = (
|
2014-09-22 17:54:33 +04:00
|
|
|
('abc', ["A valid number is required."]),
|
|
|
|
(Decimal('Nan'), ["A valid number is required."]),
|
|
|
|
(Decimal('Inf'), ["A valid number is required."]),
|
|
|
|
('12.345', ["Ensure that there are no more than 3 digits in total."]),
|
|
|
|
('0.01', ["Ensure that there are no more than 1 decimal places."]),
|
|
|
|
(123, ["Ensure that there are no more than 2 digits before the decimal point."])
|
|
|
|
)
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {
|
|
|
|
'1': '1.0',
|
|
|
|
'0': '0.0',
|
|
|
|
'1.09': '1.1',
|
|
|
|
'0.04': '0.0',
|
|
|
|
1: '1.0',
|
|
|
|
0: '0.0',
|
|
|
|
Decimal('1.0'): '1.0',
|
|
|
|
Decimal('0.0'): '0.0',
|
|
|
|
Decimal('1.09'): '1.1',
|
|
|
|
Decimal('0.04'): '0.0',
|
|
|
|
}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.DecimalField(max_digits=3, decimal_places=1)
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestMinMaxDecimalField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `DecimalField` with min and max limits.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'10.0': 10.0,
|
|
|
|
'20.0': 20.0,
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'9.9': ['Ensure this value is greater than or equal to 10.'],
|
|
|
|
'20.1': ['Ensure this value is less than or equal to 20.'],
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.DecimalField(
|
|
|
|
max_digits=3, decimal_places=1,
|
|
|
|
min_value=10, max_value=20
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestNoStringCoercionDecimalField(FieldValues):
|
|
|
|
"""
|
|
|
|
Output values for `DecimalField` with `coerce_to_string=False`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
1.09: Decimal('1.1'),
|
|
|
|
0.04: Decimal('0.0'),
|
|
|
|
'1.09': Decimal('1.1'),
|
|
|
|
'0.04': Decimal('0.0'),
|
|
|
|
Decimal('1.09'): Decimal('1.1'),
|
|
|
|
Decimal('0.04'): Decimal('0.0'),
|
|
|
|
}
|
|
|
|
field = fields.DecimalField(
|
|
|
|
max_digits=3, decimal_places=1,
|
|
|
|
coerce_to_string=False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-22 15:25:57 +04:00
|
|
|
# Date & time fields...
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestDateField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'2001-01-01': datetime.date(2001, 1, 1),
|
|
|
|
datetime.date(2001, 1, 1): datetime.date(2001, 1, 1),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'abc': ['Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'],
|
|
|
|
'2001-99-99': ['Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'],
|
|
|
|
datetime.datetime(2001, 1, 1, 12, 00): ['Expected a date but got a datetime.'],
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.DateField()
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestCustomInputFormatDateField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateField` with a cutom input format.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'1 Jan 2001': datetime.date(2001, 1, 1),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'2001-01-01': ['Date has wrong format. Use one of these formats instead: DD [Jan-Dec] YYYY']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.DateField(input_formats=['%d %b %Y'])
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestDateTimeField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateTimeField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'2001-01-01 13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
|
|
|
'2001-01-01T13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
|
|
|
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
|
|
|
'2001-01-01T14:00+0100': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
|
|
|
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
|
|
|
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'],
|
|
|
|
'2001-99-99T99:00': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'],
|
|
|
|
datetime.date(2001, 1, 1): ['Expected a datetime but got a date.'],
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.DateTimeField(default_timezone=timezone.UTC())
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestCustomInputFormatDateTimeField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateTimeField` with a cutom input format.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'1:35pm, 1 Jan 2001': datetime.datetime(2001, 1, 1, 13, 35, tzinfo=timezone.UTC()),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'2001-01-01T20:50': ['Datetime has wrong format. Use one of these formats instead: hh:mm[AM|PM], DD [Jan-Dec] YYYY']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.DateTimeField(default_timezone=timezone.UTC(), input_formats=['%I:%M%p, %d %b %Y'])
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestNaiveDateTimeField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateTimeField` with naive datetimes.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): datetime.datetime(2001, 1, 1, 13, 00),
|
|
|
|
'2001-01-01 13:00': datetime.datetime(2001, 1, 1, 13, 00),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.DateTimeField(default_timezone=None)
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestTimeField(FieldValues):
|
2014-09-22 16:26:47 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `TimeField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'13:00': datetime.time(13, 00),
|
|
|
|
datetime.time(13, 00): datetime.time(13, 00),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'abc': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'],
|
|
|
|
'99:99': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'],
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 16:26:47 +04:00
|
|
|
field = fields.TimeField()
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestCustomInputFormatTimeField(FieldValues):
|
2014-09-22 16:26:47 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `TimeField` with a custom input format.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'1:00pm': datetime.time(13, 00),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 16:26:47 +04:00
|
|
|
'13:00': ['Time has wrong format. Use one of these formats instead: hh:mm[AM|PM]'],
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 16:26:47 +04:00
|
|
|
field = fields.TimeField(input_formats=['%I:%M%p'])
|
|
|
|
|
|
|
|
|
2014-09-22 15:25:57 +04:00
|
|
|
# Choice types...
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestChoiceField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `ChoiceField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'poor': 'poor',
|
|
|
|
'medium': 'medium',
|
|
|
|
'good': 'good',
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'awful': ['`awful` is not a valid choice.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
('poor', 'Poor quality'),
|
|
|
|
('medium', 'Medium quality'),
|
|
|
|
('good', 'Good quality'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestChoiceFieldWithType(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for a `Choice` field that uses an integer type,
|
|
|
|
instead of a char type.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'1': 1,
|
|
|
|
3: 3,
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
5: ['`5` is not a valid choice.'],
|
|
|
|
'abc': ['`abc` is not a valid choice.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
(1, 'Poor quality'),
|
|
|
|
(2, 'Medium quality'),
|
|
|
|
(3, 'Good quality'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestChoiceFieldWithListChoices(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for a `Choice` field that uses a flat list for the
|
|
|
|
choices, rather than a list of pairs of (`value`, `description`).
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'poor': 'poor',
|
|
|
|
'medium': 'medium',
|
|
|
|
'good': 'good',
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'awful': ['`awful` is not a valid choice.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.ChoiceField(choices=('poor', 'medium', 'good'))
|
|
|
|
|
|
|
|
|
2014-09-22 18:34:06 +04:00
|
|
|
class TestMultipleChoiceField(FieldValues):
|
2014-09-22 15:25:57 +04:00
|
|
|
"""
|
|
|
|
Valid and invalid values for `MultipleChoiceField`.
|
|
|
|
"""
|
2014-09-22 18:34:06 +04:00
|
|
|
valid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
(): set(),
|
|
|
|
('aircon',): set(['aircon']),
|
|
|
|
('aircon', 'manual'): set(['aircon', 'manual']),
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
invalid_inputs = {
|
2014-09-22 15:25:57 +04:00
|
|
|
'abc': ['Expected a list of items but got type `str`'],
|
|
|
|
('aircon', 'incorrect'): ['`incorrect` is not a valid choice.']
|
|
|
|
}
|
2014-09-22 18:34:06 +04:00
|
|
|
outputs = {}
|
2014-09-22 15:25:57 +04:00
|
|
|
field = fields.MultipleChoiceField(
|
|
|
|
choices=[
|
|
|
|
('aircon', 'AirCon'),
|
|
|
|
('manual', 'Manual drive'),
|
|
|
|
('diesel', 'Diesel'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# """
|
|
|
|
# General serializer field tests.
|
|
|
|
# """
|
|
|
|
# from __future__ import unicode_literals
|
2013-05-19 16:55:46 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# import datetime
|
|
|
|
# import re
|
|
|
|
# from decimal import Decimal
|
|
|
|
# from uuid import uuid4
|
|
|
|
# from django.core import validators
|
|
|
|
# from django.db import models
|
|
|
|
# from django.test import TestCase
|
|
|
|
# from django.utils.datastructures import SortedDict
|
|
|
|
# from rest_framework import serializers
|
|
|
|
# from tests.models import RESTFrameworkModel
|
2013-01-12 13:32:53 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class TimestampedModel(models.Model):
|
|
|
|
# added = models.DateTimeField(auto_now_add=True)
|
|
|
|
# updated = models.DateTimeField(auto_now=True)
|
2013-01-12 13:32:53 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class CharPrimaryKeyModel(models.Model):
|
|
|
|
# id = models.CharField(max_length=20, primary_key=True)
|
2013-01-12 13:43:07 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class TimestampedModelSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = TimestampedModel
|
2013-01-12 13:32:53 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class CharPrimaryKeyModelSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = CharPrimaryKeyModel
|
2013-01-12 13:43:07 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class TimeFieldModel(models.Model):
|
|
|
|
# clock = models.TimeField()
|
2013-02-15 00:19:51 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class TimeFieldModelSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = TimeFieldModel
|
2013-02-15 00:19:51 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# SAMPLE_CHOICES = [
|
|
|
|
# ('red', 'Red'),
|
|
|
|
# ('green', 'Green'),
|
|
|
|
# ('blue', 'Blue'),
|
|
|
|
# ]
|
2013-11-05 20:21:18 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class ChoiceFieldModel(models.Model):
|
|
|
|
# choice = models.CharField(choices=SAMPLE_CHOICES, blank=True, max_length=255)
|
2013-11-05 20:21:18 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class ChoiceFieldModelSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = ChoiceFieldModel
|
|
|
|
|
|
|
|
|
|
|
|
# class ChoiceFieldModelWithNull(models.Model):
|
|
|
|
# choice = models.CharField(choices=SAMPLE_CHOICES, blank=True, null=True, max_length=255)
|
|
|
|
|
|
|
|
|
|
|
|
# class ChoiceFieldModelWithNullSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = ChoiceFieldModelWithNull
|
|
|
|
|
|
|
|
|
|
|
|
# class BasicFieldTests(TestCase):
|
|
|
|
# def test_auto_now_fields_read_only(self):
|
|
|
|
# """
|
|
|
|
# auto_now and auto_now_add fields should be read_only by default.
|
|
|
|
# """
|
|
|
|
# serializer = TimestampedModelSerializer()
|
|
|
|
# self.assertEqual(serializer.fields['added'].read_only, True)
|
|
|
|
|
|
|
|
# def test_auto_pk_fields_read_only(self):
|
|
|
|
# """
|
|
|
|
# AutoField fields should be read_only by default.
|
|
|
|
# """
|
|
|
|
# serializer = TimestampedModelSerializer()
|
|
|
|
# self.assertEqual(serializer.fields['id'].read_only, True)
|
|
|
|
|
|
|
|
# def test_non_auto_pk_fields_not_read_only(self):
|
|
|
|
# """
|
|
|
|
# PK fields other than AutoField fields should not be read_only by default.
|
|
|
|
# """
|
|
|
|
# serializer = CharPrimaryKeyModelSerializer()
|
|
|
|
# self.assertEqual(serializer.fields['id'].read_only, False)
|
|
|
|
|
|
|
|
# def test_dict_field_ordering(self):
|
|
|
|
# """
|
|
|
|
# Field should preserve dictionary ordering, if it exists.
|
|
|
|
# See: https://github.com/tomchristie/django-rest-framework/issues/832
|
|
|
|
# """
|
|
|
|
# ret = SortedDict()
|
|
|
|
# ret['c'] = 1
|
|
|
|
# ret['b'] = 1
|
|
|
|
# ret['a'] = 1
|
|
|
|
# ret['z'] = 1
|
|
|
|
# field = serializers.Field()
|
|
|
|
# keys = list(field.to_native(ret).keys())
|
|
|
|
# self.assertEqual(keys, ['c', 'b', 'a', 'z'])
|
|
|
|
|
|
|
|
# def test_widget_html_attributes(self):
|
|
|
|
# """
|
|
|
|
# Make sure widget_html() renders the correct attributes
|
|
|
|
# """
|
|
|
|
# r = re.compile('(\S+)=["\']?((?:.(?!["\']?\s+(?:\S+)=|[>"\']))+.)["\']?')
|
|
|
|
# form = TimeFieldModelSerializer().data
|
|
|
|
# attributes = r.findall(form.fields['clock'].widget_html())
|
|
|
|
# self.assertIn(('name', 'clock'), attributes)
|
|
|
|
# self.assertIn(('id', 'clock'), attributes)
|
2014-05-06 16:17:51 +04:00
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class DateFieldTest(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for the DateFieldTest from_native() and to_native() behavior
|
|
|
|
# """
|
|
|
|
|
|
|
|
# def test_from_native_string(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts default iso input formats.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField()
|
|
|
|
# result_1 = f.from_native('1984-07-31')
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.date(1984, 7, 31), result_1)
|
|
|
|
|
|
|
|
# def test_from_native_datetime_date(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts a datetime.date instance.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField()
|
|
|
|
# result_1 = f.from_native(datetime.date(1984, 7, 31))
|
|
|
|
|
|
|
|
# self.assertEqual(result_1, datetime.date(1984, 7, 31))
|
|
|
|
|
|
|
|
# def test_from_native_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts custom input formats.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField(input_formats=['%Y -- %d'])
|
|
|
|
# result = f.from_native('1984 -- 31')
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.date(1984, 1, 31), result)
|
|
|
|
|
|
|
|
# def test_from_native_invalid_default_on_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() don't accept default formats if custom format is preset
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField(input_formats=['%Y -- %d'])
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('1984-07-31')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY -- DD"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_from_native_empty(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on empty param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField()
|
|
|
|
# result = f.from_native('')
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_from_native_none(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on None param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField()
|
|
|
|
# result = f.from_native(None)
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_from_native_invalid_date(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() raises a ValidationError on passing an invalid date.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField()
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('1984-13-31')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_from_native_invalid_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() raises a ValidationError on passing an invalid format.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField()
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('1984 -- 31')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_to_native(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() returns datetime as default.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField()
|
|
|
|
|
|
|
|
# result_1 = f.to_native(datetime.date(1984, 7, 31))
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.date(1984, 7, 31), result_1)
|
|
|
|
|
|
|
|
# def test_to_native_iso(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() with 'iso-8601' returns iso formated date.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField(format='iso-8601')
|
|
|
|
|
|
|
|
# result_1 = f.to_native(datetime.date(1984, 7, 31))
|
|
|
|
|
|
|
|
# self.assertEqual('1984-07-31', result_1)
|
|
|
|
|
|
|
|
# def test_to_native_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() returns correct custom format.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField(format="%Y - %m.%d")
|
|
|
|
|
|
|
|
# result_1 = f.to_native(datetime.date(1984, 7, 31))
|
|
|
|
|
|
|
|
# self.assertEqual('1984 - 07.31', result_1)
|
|
|
|
|
|
|
|
# def test_to_native_none(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on None param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateField(required=False)
|
|
|
|
# self.assertEqual(None, f.to_native(None))
|
|
|
|
|
|
|
|
|
|
|
|
# class DateTimeFieldTest(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for the DateTimeField from_native() and to_native() behavior
|
|
|
|
# """
|
|
|
|
|
|
|
|
# def test_from_native_string(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts default iso input formats.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField()
|
|
|
|
# result_1 = f.from_native('1984-07-31 04:31')
|
|
|
|
# result_2 = f.from_native('1984-07-31 04:31:59')
|
|
|
|
# result_3 = f.from_native('1984-07-31 04:31:59.000200')
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_1)
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_2)
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_3)
|
|
|
|
|
|
|
|
# def test_from_native_datetime_datetime(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts a datetime.datetime instance.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField()
|
|
|
|
# result_1 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
# result_2 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
# result_3 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual(result_1, datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
# self.assertEqual(result_2, datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
# self.assertEqual(result_3, datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
|
|
|
|
|
|
|
# def test_from_native_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts custom input formats.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField(input_formats=['%Y -- %H:%M'])
|
|
|
|
# result = f.from_native('1984 -- 04:59')
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 1, 1, 4, 59), result)
|
|
|
|
|
|
|
|
# def test_from_native_invalid_default_on_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() don't accept default formats if custom format is preset
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField(input_formats=['%Y -- %H:%M'])
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('1984-07-31 04:31:59')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: YYYY -- hh:mm"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_from_native_empty(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on empty param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField()
|
|
|
|
# result = f.from_native('')
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_from_native_none(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on None param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField()
|
|
|
|
# result = f.from_native(None)
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_from_native_invalid_datetime(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() raises a ValidationError on passing an invalid datetime.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField()
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('04:61:59')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
|
|
|
|
# "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_from_native_invalid_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() raises a ValidationError on passing an invalid format.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField()
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('04 -- 31')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
|
|
|
|
# "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_to_native(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() returns isoformat as default.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField()
|
|
|
|
|
|
|
|
# result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
|
|
|
# result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
# result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
# result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 7, 31), result_1)
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_2)
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_3)
|
|
|
|
# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_4)
|
|
|
|
|
|
|
|
# def test_to_native_iso(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() with format=iso-8601 returns iso formatted datetime.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField(format='iso-8601')
|
|
|
|
|
|
|
|
# result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
|
|
|
# result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
# result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
# result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual('1984-07-31T00:00:00', result_1)
|
|
|
|
# self.assertEqual('1984-07-31T04:31:00', result_2)
|
|
|
|
# self.assertEqual('1984-07-31T04:31:59', result_3)
|
|
|
|
# self.assertEqual('1984-07-31T04:31:59.000200', result_4)
|
|
|
|
|
|
|
|
# def test_to_native_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() returns correct custom format.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField(format="%Y - %H:%M")
|
|
|
|
|
|
|
|
# result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
|
|
|
# result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
# result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
# result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual('1984 - 00:00', result_1)
|
|
|
|
# self.assertEqual('1984 - 04:31', result_2)
|
|
|
|
# self.assertEqual('1984 - 04:31', result_3)
|
|
|
|
# self.assertEqual('1984 - 04:31', result_4)
|
|
|
|
|
|
|
|
# def test_to_native_none(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on None param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DateTimeField(required=False)
|
|
|
|
# self.assertEqual(None, f.to_native(None))
|
|
|
|
|
|
|
|
|
|
|
|
# class TimeFieldTest(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for the TimeField from_native() and to_native() behavior
|
|
|
|
# """
|
|
|
|
|
|
|
|
# def test_from_native_string(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts default iso input formats.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField()
|
|
|
|
# result_1 = f.from_native('04:31')
|
|
|
|
# result_2 = f.from_native('04:31:59')
|
|
|
|
# result_3 = f.from_native('04:31:59.000200')
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.time(4, 31), result_1)
|
|
|
|
# self.assertEqual(datetime.time(4, 31, 59), result_2)
|
|
|
|
# self.assertEqual(datetime.time(4, 31, 59, 200), result_3)
|
|
|
|
|
|
|
|
# def test_from_native_datetime_time(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts a datetime.time instance.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField()
|
|
|
|
# result_1 = f.from_native(datetime.time(4, 31))
|
|
|
|
# result_2 = f.from_native(datetime.time(4, 31, 59))
|
|
|
|
# result_3 = f.from_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual(result_1, datetime.time(4, 31))
|
|
|
|
# self.assertEqual(result_2, datetime.time(4, 31, 59))
|
|
|
|
# self.assertEqual(result_3, datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
# def test_from_native_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts custom input formats.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField(input_formats=['%H -- %M'])
|
|
|
|
# result = f.from_native('04 -- 31')
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.time(4, 31), result)
|
|
|
|
|
|
|
|
# def test_from_native_invalid_default_on_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() don't accept default formats if custom format is preset
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField(input_formats=['%H -- %M'])
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('04:31:59')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: hh -- mm"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_from_native_empty(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on empty param.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField()
|
|
|
|
# result = f.from_native('')
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_from_native_none(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on None param.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField()
|
|
|
|
# result = f.from_native(None)
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_from_native_invalid_time(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() raises a ValidationError on passing an invalid time.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField()
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('04:61:59')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
|
|
|
|
# "hh:mm[:ss[.uuuuuu]]"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_from_native_invalid_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() raises a ValidationError on passing an invalid format.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField()
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('04 -- 31')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
|
|
|
|
# "hh:mm[:ss[.uuuuuu]]"])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_to_native(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() returns time object as default.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField()
|
|
|
|
# result_1 = f.to_native(datetime.time(4, 31))
|
|
|
|
# result_2 = f.to_native(datetime.time(4, 31, 59))
|
|
|
|
# result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual(datetime.time(4, 31), result_1)
|
|
|
|
# self.assertEqual(datetime.time(4, 31, 59), result_2)
|
|
|
|
# self.assertEqual(datetime.time(4, 31, 59, 200), result_3)
|
|
|
|
|
|
|
|
# def test_to_native_iso(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() with format='iso-8601' returns iso formatted time.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField(format='iso-8601')
|
|
|
|
# result_1 = f.to_native(datetime.time(4, 31))
|
|
|
|
# result_2 = f.to_native(datetime.time(4, 31, 59))
|
|
|
|
# result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual('04:31:00', result_1)
|
|
|
|
# self.assertEqual('04:31:59', result_2)
|
|
|
|
# self.assertEqual('04:31:59.000200', result_3)
|
|
|
|
|
|
|
|
# def test_to_native_custom_format(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() returns correct custom format.
|
|
|
|
# """
|
|
|
|
# f = serializers.TimeField(format="%H - %S [%f]")
|
|
|
|
# result_1 = f.to_native(datetime.time(4, 31))
|
|
|
|
# result_2 = f.to_native(datetime.time(4, 31, 59))
|
|
|
|
# result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
# self.assertEqual('04 - 00 [000000]', result_1)
|
|
|
|
# self.assertEqual('04 - 59 [000000]', result_2)
|
|
|
|
# self.assertEqual('04 - 59 [000200]', result_3)
|
|
|
|
|
|
|
|
|
|
|
|
# class DecimalFieldTest(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for the DecimalField from_native() and to_native() behavior
|
|
|
|
# """
|
|
|
|
|
|
|
|
# def test_from_native_string(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts string values
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField()
|
|
|
|
# result_1 = f.from_native('9000')
|
|
|
|
# result_2 = f.from_native('1.00000001')
|
|
|
|
|
|
|
|
# self.assertEqual(Decimal('9000'), result_1)
|
|
|
|
# self.assertEqual(Decimal('1.00000001'), result_2)
|
|
|
|
|
|
|
|
# def test_from_native_invalid_string(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() raises ValidationError on passing invalid string
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField()
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# f.from_native('123.45.6')
|
|
|
|
# except validators.ValidationError as e:
|
|
|
|
# self.assertEqual(e.messages, ["Enter a number."])
|
|
|
|
# else:
|
|
|
|
# self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
# def test_from_native_integer(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts integer values
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField()
|
|
|
|
# result = f.from_native(9000)
|
|
|
|
|
|
|
|
# self.assertEqual(Decimal('9000'), result)
|
|
|
|
|
|
|
|
# def test_from_native_float(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() accepts float values
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField()
|
|
|
|
# result = f.from_native(1.00000001)
|
|
|
|
|
|
|
|
# self.assertEqual(Decimal('1.00000001'), result)
|
|
|
|
|
|
|
|
# def test_from_native_empty(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on empty param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField()
|
|
|
|
# result = f.from_native('')
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_from_native_none(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on None param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField()
|
|
|
|
# result = f.from_native(None)
|
|
|
|
|
|
|
|
# self.assertEqual(result, None)
|
|
|
|
|
|
|
|
# def test_to_native(self):
|
|
|
|
# """
|
|
|
|
# Make sure to_native() returns Decimal as string.
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField()
|
|
|
|
|
|
|
|
# result_1 = f.to_native(Decimal('9000'))
|
|
|
|
# result_2 = f.to_native(Decimal('1.00000001'))
|
|
|
|
|
|
|
|
# self.assertEqual(Decimal('9000'), result_1)
|
|
|
|
# self.assertEqual(Decimal('1.00000001'), result_2)
|
|
|
|
|
|
|
|
# def test_to_native_none(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns None on None param.
|
|
|
|
# """
|
|
|
|
# f = serializers.DecimalField(required=False)
|
|
|
|
# self.assertEqual(None, f.to_native(None))
|
|
|
|
|
|
|
|
# def test_valid_serialization(self):
|
|
|
|
# """
|
|
|
|
# Make sure the serializer works correctly
|
|
|
|
# """
|
|
|
|
# class DecimalSerializer(serializers.Serializer):
|
|
|
|
# decimal_field = serializers.DecimalField(max_value=9010,
|
|
|
|
# min_value=9000,
|
|
|
|
# max_digits=6,
|
|
|
|
# decimal_places=2)
|
|
|
|
|
|
|
|
# self.assertTrue(DecimalSerializer(data={'decimal_field': '9001'}).is_valid())
|
|
|
|
# self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.2'}).is_valid())
|
|
|
|
# self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.23'}).is_valid())
|
|
|
|
|
|
|
|
# self.assertFalse(DecimalSerializer(data={'decimal_field': '8000'}).is_valid())
|
|
|
|
# self.assertFalse(DecimalSerializer(data={'decimal_field': '9900'}).is_valid())
|
|
|
|
# self.assertFalse(DecimalSerializer(data={'decimal_field': '9001.234'}).is_valid())
|
|
|
|
|
|
|
|
# def test_raise_max_value(self):
|
|
|
|
# """
|
|
|
|
# Make sure max_value violations raises ValidationError
|
|
|
|
# """
|
|
|
|
# class DecimalSerializer(serializers.Serializer):
|
|
|
|
# decimal_field = serializers.DecimalField(max_value=100)
|
|
|
|
|
|
|
|
# s = DecimalSerializer(data={'decimal_field': '123'})
|
|
|
|
|
|
|
|
# self.assertFalse(s.is_valid())
|
|
|
|
# self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is less than or equal to 100.']})
|
|
|
|
|
|
|
|
# def test_raise_min_value(self):
|
|
|
|
# """
|
|
|
|
# Make sure min_value violations raises ValidationError
|
|
|
|
# """
|
|
|
|
# class DecimalSerializer(serializers.Serializer):
|
|
|
|
# decimal_field = serializers.DecimalField(min_value=100)
|
|
|
|
|
|
|
|
# s = DecimalSerializer(data={'decimal_field': '99'})
|
|
|
|
|
|
|
|
# self.assertFalse(s.is_valid())
|
|
|
|
# self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is greater than or equal to 100.']})
|
|
|
|
|
|
|
|
# def test_raise_max_digits(self):
|
|
|
|
# """
|
|
|
|
# Make sure max_digits violations raises ValidationError
|
|
|
|
# """
|
|
|
|
# class DecimalSerializer(serializers.Serializer):
|
|
|
|
# decimal_field = serializers.DecimalField(max_digits=5)
|
|
|
|
|
|
|
|
# s = DecimalSerializer(data={'decimal_field': '123.456'})
|
|
|
|
|
|
|
|
# self.assertFalse(s.is_valid())
|
|
|
|
# self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 5 digits in total.']})
|
|
|
|
|
|
|
|
# def test_raise_max_decimal_places(self):
|
|
|
|
# """
|
|
|
|
# Make sure max_decimal_places violations raises ValidationError
|
|
|
|
# """
|
|
|
|
# class DecimalSerializer(serializers.Serializer):
|
|
|
|
# decimal_field = serializers.DecimalField(decimal_places=3)
|
|
|
|
|
|
|
|
# s = DecimalSerializer(data={'decimal_field': '123.4567'})
|
|
|
|
|
|
|
|
# self.assertFalse(s.is_valid())
|
|
|
|
# self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 3 decimal places.']})
|
|
|
|
|
|
|
|
# def test_raise_max_whole_digits(self):
|
|
|
|
# """
|
|
|
|
# Make sure max_whole_digits violations raises ValidationError
|
|
|
|
# """
|
|
|
|
# class DecimalSerializer(serializers.Serializer):
|
|
|
|
# decimal_field = serializers.DecimalField(max_digits=4, decimal_places=3)
|
|
|
|
|
|
|
|
# s = DecimalSerializer(data={'decimal_field': '12345.6'})
|
|
|
|
|
|
|
|
# self.assertFalse(s.is_valid())
|
|
|
|
# self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 4 digits in total.']})
|
|
|
|
|
|
|
|
|
|
|
|
# class ChoiceFieldTests(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for the ChoiceField options generator
|
|
|
|
# """
|
|
|
|
# def test_choices_required(self):
|
|
|
|
# """
|
|
|
|
# Make sure proper choices are rendered if field is required
|
|
|
|
# """
|
|
|
|
# f = serializers.ChoiceField(required=True, choices=SAMPLE_CHOICES)
|
|
|
|
# self.assertEqual(f.choices, SAMPLE_CHOICES)
|
|
|
|
|
|
|
|
# def test_choices_not_required(self):
|
|
|
|
# """
|
|
|
|
# Make sure proper choices (plus blank) are rendered if the field isn't required
|
|
|
|
# """
|
|
|
|
# f = serializers.ChoiceField(required=False, choices=SAMPLE_CHOICES)
|
|
|
|
# self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES)
|
|
|
|
|
|
|
|
# def test_blank_choice_display(self):
|
|
|
|
# blank = 'No Preference'
|
|
|
|
# f = serializers.ChoiceField(
|
|
|
|
# required=False,
|
|
|
|
# choices=SAMPLE_CHOICES,
|
|
|
|
# blank_display_value=blank,
|
|
|
|
# )
|
|
|
|
# self.assertEqual(f.choices, [('', blank)] + SAMPLE_CHOICES)
|
|
|
|
|
|
|
|
# def test_invalid_choice_model(self):
|
|
|
|
# s = ChoiceFieldModelSerializer(data={'choice': 'wrong_value'})
|
|
|
|
# self.assertFalse(s.is_valid())
|
|
|
|
# self.assertEqual(s.errors, {'choice': ['Select a valid choice. wrong_value is not one of the available choices.']})
|
|
|
|
# self.assertEqual(s.data['choice'], '')
|
|
|
|
|
|
|
|
# def test_empty_choice_model(self):
|
|
|
|
# """
|
|
|
|
# Test that the 'empty' value is correctly passed and used depending on
|
|
|
|
# the 'null' property on the model field.
|
|
|
|
# """
|
|
|
|
# s = ChoiceFieldModelSerializer(data={'choice': ''})
|
|
|
|
# self.assertTrue(s.is_valid())
|
|
|
|
# self.assertEqual(s.data['choice'], '')
|
|
|
|
|
|
|
|
# s = ChoiceFieldModelWithNullSerializer(data={'choice': ''})
|
|
|
|
# self.assertTrue(s.is_valid())
|
|
|
|
# self.assertEqual(s.data['choice'], None)
|
|
|
|
|
|
|
|
# def test_from_native_empty(self):
|
|
|
|
# """
|
|
|
|
# Make sure from_native() returns an empty string on empty param by default.
|
|
|
|
# """
|
|
|
|
# f = serializers.ChoiceField(choices=SAMPLE_CHOICES)
|
|
|
|
# self.assertEqual(f.from_native(''), '')
|
|
|
|
# self.assertEqual(f.from_native(None), '')
|
|
|
|
|
|
|
|
# def test_from_native_empty_override(self):
|
|
|
|
# """
|
|
|
|
# Make sure you can override from_native() behavior regarding empty values.
|
|
|
|
# """
|
|
|
|
# f = serializers.ChoiceField(choices=SAMPLE_CHOICES, empty=None)
|
|
|
|
# self.assertEqual(f.from_native(''), None)
|
|
|
|
# self.assertEqual(f.from_native(None), None)
|
|
|
|
|
|
|
|
# def test_metadata_choices(self):
|
|
|
|
# """
|
|
|
|
# Make sure proper choices are included in the field's metadata.
|
|
|
|
# """
|
|
|
|
# choices = [{'value': v, 'display_name': n} for v, n in SAMPLE_CHOICES]
|
|
|
|
# f = serializers.ChoiceField(choices=SAMPLE_CHOICES)
|
|
|
|
# self.assertEqual(f.metadata()['choices'], choices)
|
|
|
|
|
|
|
|
# def test_metadata_choices_not_required(self):
|
|
|
|
# """
|
|
|
|
# Make sure proper choices are included in the field's metadata.
|
|
|
|
# """
|
|
|
|
# choices = [{'value': v, 'display_name': n}
|
|
|
|
# for v, n in models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES]
|
|
|
|
# f = serializers.ChoiceField(required=False, choices=SAMPLE_CHOICES)
|
|
|
|
# self.assertEqual(f.metadata()['choices'], choices)
|
|
|
|
|
|
|
|
|
|
|
|
# class EmailFieldTests(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for EmailField attribute values
|
|
|
|
# """
|
|
|
|
|
|
|
|
# class EmailFieldModel(RESTFrameworkModel):
|
|
|
|
# email_field = models.EmailField(blank=True)
|
|
|
|
|
|
|
|
# class EmailFieldWithGivenMaxLengthModel(RESTFrameworkModel):
|
|
|
|
# email_field = models.EmailField(max_length=150, blank=True)
|
|
|
|
|
|
|
|
# def test_default_model_value(self):
|
|
|
|
# class EmailFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = self.EmailFieldModel
|
|
|
|
|
|
|
|
# serializer = EmailFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 75)
|
|
|
|
|
|
|
|
# def test_given_model_value(self):
|
|
|
|
# class EmailFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = self.EmailFieldWithGivenMaxLengthModel
|
|
|
|
|
|
|
|
# serializer = EmailFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 150)
|
|
|
|
|
|
|
|
# def test_given_serializer_value(self):
|
|
|
|
# class EmailFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# email_field = serializers.EmailField(source='email_field', max_length=20, required=False)
|
|
|
|
|
|
|
|
# class Meta:
|
|
|
|
# model = self.EmailFieldModel
|
|
|
|
|
|
|
|
# serializer = EmailFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 20)
|
|
|
|
|
|
|
|
|
|
|
|
# class SlugFieldTests(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for SlugField attribute values
|
|
|
|
# """
|
|
|
|
|
|
|
|
# class SlugFieldModel(RESTFrameworkModel):
|
|
|
|
# slug_field = models.SlugField(blank=True)
|
|
|
|
|
|
|
|
# class SlugFieldWithGivenMaxLengthModel(RESTFrameworkModel):
|
|
|
|
# slug_field = models.SlugField(max_length=84, blank=True)
|
|
|
|
|
|
|
|
# def test_default_model_value(self):
|
|
|
|
# class SlugFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = self.SlugFieldModel
|
|
|
|
|
|
|
|
# serializer = SlugFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 50)
|
|
|
|
|
|
|
|
# def test_given_model_value(self):
|
|
|
|
# class SlugFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = self.SlugFieldWithGivenMaxLengthModel
|
|
|
|
|
|
|
|
# serializer = SlugFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 84)
|
|
|
|
|
|
|
|
# def test_given_serializer_value(self):
|
|
|
|
# class SlugFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# slug_field = serializers.SlugField(source='slug_field',
|
|
|
|
# max_length=20, required=False)
|
|
|
|
|
|
|
|
# class Meta:
|
|
|
|
# model = self.SlugFieldModel
|
|
|
|
|
|
|
|
# serializer = SlugFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['slug_field'],
|
|
|
|
# 'max_length'), 20)
|
|
|
|
|
|
|
|
# def test_invalid_slug(self):
|
|
|
|
# """
|
|
|
|
# Make sure an invalid slug raises ValidationError
|
|
|
|
# """
|
|
|
|
# class SlugFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# slug_field = serializers.SlugField(source='slug_field', max_length=20, required=True)
|
2013-05-25 02:44:23 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class Meta:
|
|
|
|
# model = self.SlugFieldModel
|
2013-05-25 02:44:23 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# s = SlugFieldSerializer(data={'slug_field': 'a b'})
|
2013-05-25 02:44:23 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# self.assertEqual(s.is_valid(), False)
|
|
|
|
# self.assertEqual(s.errors, {'slug_field': ["Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]})
|
2013-05-20 16:04:38 +04:00
|
|
|
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class URLFieldTests(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for URLField attribute values.
|
2014-02-11 22:52:32 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# (Includes test for #1210, checking that validators can be overridden.)
|
|
|
|
# """
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class URLFieldModel(RESTFrameworkModel):
|
|
|
|
# url_field = models.URLField(blank=True)
|
2013-05-18 18:24:54 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class URLFieldWithGivenMaxLengthModel(RESTFrameworkModel):
|
|
|
|
# url_field = models.URLField(max_length=128, blank=True)
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_default_model_value(self):
|
|
|
|
# class URLFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = self.URLFieldModel
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# serializer = URLFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['url_field'],
|
|
|
|
# 'max_length'), 200)
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_given_model_value(self):
|
|
|
|
# class URLFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = self.URLFieldWithGivenMaxLengthModel
|
2013-05-18 18:24:54 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# serializer = URLFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['url_field'],
|
|
|
|
# 'max_length'), 128)
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_given_serializer_value(self):
|
|
|
|
# class URLFieldSerializer(serializers.ModelSerializer):
|
|
|
|
# url_field = serializers.URLField(source='url_field',
|
|
|
|
# max_length=20, required=False)
|
2013-05-18 18:24:54 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class Meta:
|
|
|
|
# model = self.URLFieldWithGivenMaxLengthModel
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# serializer = URLFieldSerializer(data={})
|
|
|
|
# self.assertEqual(serializer.is_valid(), True)
|
|
|
|
# self.assertEqual(getattr(serializer.fields['url_field'],
|
|
|
|
# 'max_length'), 20)
|
2013-05-18 20:10:17 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_validators_can_be_overridden(self):
|
|
|
|
# url_field = serializers.URLField(validators=[])
|
|
|
|
# validators = url_field.validators
|
|
|
|
# self.assertEqual([], validators, 'Passing `validators` kwarg should have overridden default validators')
|
2014-02-11 22:52:32 +04:00
|
|
|
|
2013-05-18 20:10:17 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class FieldMetadata(TestCase):
|
|
|
|
# def setUp(self):
|
|
|
|
# self.required_field = serializers.Field()
|
|
|
|
# self.required_field.label = uuid4().hex
|
|
|
|
# self.required_field.required = True
|
2013-05-18 20:10:17 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# self.optional_field = serializers.Field()
|
|
|
|
# self.optional_field.label = uuid4().hex
|
|
|
|
# self.optional_field.required = False
|
2013-05-18 20:10:17 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_required(self):
|
|
|
|
# self.assertEqual(self.required_field.metadata()['required'], True)
|
2013-05-18 20:10:17 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_optional(self):
|
|
|
|
# self.assertEqual(self.optional_field.metadata()['required'], False)
|
2013-05-18 20:10:17 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_label(self):
|
|
|
|
# for field in (self.required_field, self.optional_field):
|
|
|
|
# self.assertEqual(field.metadata()['label'], field.label)
|
2013-06-02 23:12:49 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class FieldCallableDefault(TestCase):
|
|
|
|
# def setUp(self):
|
|
|
|
# self.simple_callable = lambda: 'foo bar'
|
2013-06-02 23:12:49 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_default_can_be_simple_callable(self):
|
|
|
|
# """
|
|
|
|
# Ensure that the 'default' argument can also be a simple callable.
|
|
|
|
# """
|
|
|
|
# field = serializers.WritableField(default=self.simple_callable)
|
|
|
|
# into = {}
|
|
|
|
# field.field_from_native({}, {}, 'field', into)
|
|
|
|
# self.assertEqual(into, {'field': 'foo bar'})
|
2013-06-12 20:36:16 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class CustomIntegerField(TestCase):
|
|
|
|
# """
|
|
|
|
# Test that custom fields apply min_value and max_value constraints
|
|
|
|
# """
|
|
|
|
# def test_custom_fields_can_be_validated_for_value(self):
|
2013-06-12 20:36:16 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class MoneyField(models.PositiveIntegerField):
|
|
|
|
# pass
|
2013-06-12 20:36:16 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class EntryModel(models.Model):
|
|
|
|
# bank = MoneyField(validators=[validators.MaxValueValidator(100)])
|
2013-06-12 20:36:16 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class EntrySerializer(serializers.ModelSerializer):
|
|
|
|
# class Meta:
|
|
|
|
# model = EntryModel
|
2013-06-12 20:36:16 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# entry = EntryModel(bank=1)
|
2013-06-12 20:36:16 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# serializer = EntrySerializer(entry, data={"bank": 11})
|
|
|
|
# self.assertTrue(serializer.is_valid())
|
2013-06-12 20:36:16 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# serializer = EntrySerializer(entry, data={"bank": -1})
|
|
|
|
# self.assertFalse(serializer.is_valid())
|
2013-06-12 20:36:16 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# serializer = EntrySerializer(entry, data={"bank": 101})
|
|
|
|
# self.assertFalse(serializer.is_valid())
|
2013-06-12 20:36:16 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class BooleanField(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for BooleanField
|
|
|
|
# """
|
|
|
|
# def test_boolean_required(self):
|
|
|
|
# class BooleanRequiredSerializer(serializers.Serializer):
|
|
|
|
# bool_field = serializers.BooleanField(required=True)
|
2013-08-16 17:20:49 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# self.assertFalse(BooleanRequiredSerializer(data={}).is_valid())
|
2014-08-16 17:49:31 +04:00
|
|
|
|
|
|
|
|
2014-09-11 16:20:44 +04:00
|
|
|
# class SerializerMethodFieldTest(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for the SerializerMethodField field_to_native() behavior
|
|
|
|
# """
|
|
|
|
# class SerializerTest(serializers.Serializer):
|
|
|
|
# def get_my_test(self, obj):
|
|
|
|
# return obj.my_test[0:5]
|
|
|
|
|
|
|
|
# class ModelCharField(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for CharField
|
|
|
|
# """
|
|
|
|
# def test_none_serializing(self):
|
|
|
|
# class CharFieldSerializer(serializers.Serializer):
|
|
|
|
# char = serializers.CharField(allow_none=True, required=False)
|
|
|
|
# serializer = CharFieldSerializer(data={'char': None})
|
|
|
|
# self.assertTrue(serializer.is_valid())
|
|
|
|
# self.assertIsNone(serializer.object['char'])
|
2013-06-12 20:36:16 +04:00
|
|
|
|
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class SerializerMethodFieldTest(TestCase):
|
|
|
|
# """
|
|
|
|
# Tests for the SerializerMethodField field_to_native() behavior
|
|
|
|
# """
|
|
|
|
# class SerializerTest(serializers.Serializer):
|
|
|
|
# def get_my_test(self, obj):
|
|
|
|
# return obj.my_test[0:5]
|
2014-08-16 17:49:31 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# class Example():
|
|
|
|
# my_test = 'Hey, this is a test !'
|
2014-08-16 17:49:31 +04:00
|
|
|
|
2014-09-02 20:41:23 +04:00
|
|
|
# def test_field_to_native(self):
|
|
|
|
# s = serializers.SerializerMethodField('get_my_test')
|
|
|
|
# s.initialize(self.SerializerTest(), 'name')
|
|
|
|
# result = s.field_to_native(self.Example(), None)
|
|
|
|
# self.assertEqual(result, 'Hey, ')
|