2014-09-22 20:46:02 +04:00
|
|
|
import datetime
|
2022-11-22 08:15:25 +03:00
|
|
|
import math
|
2015-03-03 14:34:06 +03:00
|
|
|
import os
|
2016-09-15 14:44:45 +03:00
|
|
|
import re
|
2015-06-25 23:55:51 +03:00
|
|
|
import uuid
|
2017-11-06 11:55:09 +03:00
|
|
|
from decimal import ROUND_DOWN, ROUND_UP, Decimal
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
import pytest
|
2018-07-06 13:14:31 +03:00
|
|
|
import pytz
|
2018-07-06 11:44:58 +03:00
|
|
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
2015-07-16 13:47:40 +03:00
|
|
|
from django.http import QueryDict
|
2016-07-06 18:07:16 +03:00
|
|
|
from django.test import TestCase, override_settings
|
2022-06-06 14:39:06 +03:00
|
|
|
from django.utils.timezone import activate, deactivate, override
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
|
|
import rest_framework
|
2018-07-06 13:14:31 +03:00
|
|
|
from rest_framework import exceptions, serializers
|
2019-07-01 15:25:47 +03:00
|
|
|
from rest_framework.fields import (
|
|
|
|
BuiltinSignatureError, DjangoImageField, is_simple_callable
|
|
|
|
)
|
2016-10-10 15:03:46 +03:00
|
|
|
|
2022-06-06 14:39:06 +03:00
|
|
|
utc = datetime.timezone.utc
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
# Tests for helper functions.
|
|
|
|
# ---------------------------
|
|
|
|
|
2019-05-06 21:35:58 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
class TestIsSimpleCallable:
|
|
|
|
|
|
|
|
def test_method(self):
|
|
|
|
class Foo:
|
|
|
|
@classmethod
|
|
|
|
def classmethod(cls):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def valid_kwargs(self, param='value'):
|
|
|
|
pass
|
|
|
|
|
2016-10-25 22:47:24 +03:00
|
|
|
def valid_vargs_kwargs(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def invalid(self, param):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert is_simple_callable(Foo.classmethod)
|
|
|
|
|
|
|
|
# unbound methods
|
|
|
|
assert not is_simple_callable(Foo.valid)
|
|
|
|
assert not is_simple_callable(Foo.valid_kwargs)
|
2016-10-25 22:47:24 +03:00
|
|
|
assert not is_simple_callable(Foo.valid_vargs_kwargs)
|
2016-10-10 15:03:46 +03:00
|
|
|
assert not is_simple_callable(Foo.invalid)
|
|
|
|
|
|
|
|
# bound methods
|
|
|
|
assert is_simple_callable(Foo().valid)
|
|
|
|
assert is_simple_callable(Foo().valid_kwargs)
|
2016-10-25 22:47:24 +03:00
|
|
|
assert is_simple_callable(Foo().valid_vargs_kwargs)
|
2016-10-10 15:03:46 +03:00
|
|
|
assert not is_simple_callable(Foo().invalid)
|
|
|
|
|
|
|
|
def test_function(self):
|
|
|
|
def simple():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def valid(param='value', param2='value'):
|
|
|
|
pass
|
|
|
|
|
2016-10-25 22:47:24 +03:00
|
|
|
def valid_vargs_kwargs(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def invalid(param, param2='value'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert is_simple_callable(simple)
|
|
|
|
assert is_simple_callable(valid)
|
2016-10-25 22:47:24 +03:00
|
|
|
assert is_simple_callable(valid_vargs_kwargs)
|
2016-10-10 15:03:46 +03:00
|
|
|
assert not is_simple_callable(invalid)
|
|
|
|
|
2022-06-06 14:54:57 +03:00
|
|
|
@pytest.mark.parametrize('obj', (True, None, "str", b'bytes', 123, 1.23))
|
|
|
|
def test_not_callable(self, obj):
|
|
|
|
assert not is_simple_callable(obj)
|
|
|
|
|
2016-10-25 22:47:24 +03:00
|
|
|
def test_4602_regression(self):
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class ChoiceModel(models.Model):
|
|
|
|
choice_field = models.CharField(
|
|
|
|
max_length=1, default='a',
|
|
|
|
choices=(('a', 'A'), ('b', 'B')),
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = 'tests'
|
|
|
|
|
|
|
|
assert is_simple_callable(ChoiceModel().get_choice_field_display)
|
|
|
|
|
2019-07-01 15:25:47 +03:00
|
|
|
def test_builtin_function(self):
|
|
|
|
# Built-in function signatures are not easily inspectable, so the
|
|
|
|
# current expectation is to just raise a helpful error message.
|
|
|
|
timestamp = datetime.datetime.now()
|
|
|
|
|
|
|
|
with pytest.raises(BuiltinSignatureError) as exc_info:
|
|
|
|
is_simple_callable(timestamp.date)
|
|
|
|
|
|
|
|
assert str(exc_info.value) == (
|
|
|
|
'Built-in function signatures are not inspectable. Wrap the '
|
|
|
|
'function call in a simple, pure Python function.')
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def test_type_annotation(self):
|
|
|
|
# The annotation will otherwise raise a syntax error in python < 3.5
|
2019-04-30 11:44:01 +03:00
|
|
|
locals = {}
|
|
|
|
exec("def valid(param: str='value'): pass", locals)
|
|
|
|
valid = locals['valid']
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
assert is_simple_callable(valid)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2014-09-23 17:15:00 +04:00
|
|
|
# Tests for field keyword arguments and core functionality.
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestEmpty:
|
|
|
|
"""
|
|
|
|
Tests for `required`, `allow_null`, `allow_blank`, `default`.
|
|
|
|
"""
|
2014-09-23 17:15:00 +04:00
|
|
|
def test_required(self):
|
|
|
|
"""
|
|
|
|
By default a field must be included in the input.
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.IntegerField()
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
2014-09-23 17:15:00 +04:00
|
|
|
field.run_validation()
|
2014-10-10 17:16:09 +04:00
|
|
|
assert exc_info.value.detail == ['This field is required.']
|
2014-09-23 17:15:00 +04:00
|
|
|
|
|
|
|
def test_not_required(self):
|
|
|
|
"""
|
|
|
|
If `required=False` then a field may be omitted from the input.
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.IntegerField(required=False)
|
|
|
|
with pytest.raises(serializers.SkipField):
|
2014-09-23 17:15:00 +04:00
|
|
|
field.run_validation()
|
|
|
|
|
|
|
|
def test_disallow_null(self):
|
|
|
|
"""
|
|
|
|
By default `None` is not a valid input.
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.IntegerField()
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
2014-09-23 17:15:00 +04:00
|
|
|
field.run_validation(None)
|
2014-10-10 17:16:09 +04:00
|
|
|
assert exc_info.value.detail == ['This field may not be null.']
|
2014-09-23 17:15:00 +04:00
|
|
|
|
|
|
|
def test_allow_null(self):
|
|
|
|
"""
|
|
|
|
If `allow_null=True` then `None` is a valid input.
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.IntegerField(allow_null=True)
|
2014-09-23 17:15:00 +04:00
|
|
|
output = field.run_validation(None)
|
|
|
|
assert output is None
|
|
|
|
|
|
|
|
def test_disallow_blank(self):
|
|
|
|
"""
|
|
|
|
By default '' is not a valid input.
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.CharField()
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
2014-09-23 17:15:00 +04:00
|
|
|
field.run_validation('')
|
2014-10-10 17:16:09 +04:00
|
|
|
assert exc_info.value.detail == ['This field may not be blank.']
|
2014-09-23 17:15:00 +04:00
|
|
|
|
|
|
|
def test_allow_blank(self):
|
|
|
|
"""
|
|
|
|
If `allow_blank=True` then '' is a valid input.
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.CharField(allow_blank=True)
|
2014-09-23 17:15:00 +04:00
|
|
|
output = field.run_validation('')
|
2014-12-15 14:55:17 +03:00
|
|
|
assert output == ''
|
2014-09-23 17:15:00 +04:00
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
"""
|
|
|
|
If `default` is set, then omitted values get the default input.
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.IntegerField(default=123)
|
2014-09-23 17:15:00 +04:00
|
|
|
output = field.run_validation()
|
2019-04-30 18:53:44 +03:00
|
|
|
assert output == 123
|
2014-09-23 17:15:00 +04:00
|
|
|
|
2014-09-26 13:46:52 +04:00
|
|
|
|
|
|
|
class TestSource:
|
|
|
|
def test_source(self):
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
example_field = serializers.CharField(source='other')
|
|
|
|
serializer = ExampleSerializer(data={'example_field': 'abc'})
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'other': 'abc'}
|
|
|
|
|
2014-09-24 23:53:37 +04:00
|
|
|
def test_redundant_source(self):
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
example_field = serializers.CharField(source='example_field')
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
2014-10-31 19:38:39 +03:00
|
|
|
ExampleSerializer().fields
|
2014-09-24 23:53:37 +04:00
|
|
|
assert str(exc_info.value) == (
|
|
|
|
"It is redundant to specify `source='example_field'` on field "
|
2014-09-25 13:49:25 +04:00
|
|
|
"'CharField' in serializer 'ExampleSerializer', because it is the "
|
|
|
|
"same as the field name. Remove the `source` keyword argument."
|
2014-09-24 23:53:37 +04:00
|
|
|
)
|
|
|
|
|
2015-02-26 19:34:14 +03:00
|
|
|
def test_callable_source(self):
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
example_field = serializers.CharField(source='example_callable')
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class ExampleInstance:
|
2015-02-26 19:34:14 +03:00
|
|
|
def example_callable(self):
|
|
|
|
return 'example callable value'
|
|
|
|
|
|
|
|
serializer = ExampleSerializer(ExampleInstance())
|
|
|
|
assert serializer.data['example_field'] == 'example callable value'
|
|
|
|
|
|
|
|
def test_callable_source_raises(self):
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
example_field = serializers.CharField(source='example_callable', read_only=True)
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class ExampleInstance:
|
2015-02-26 19:34:14 +03:00
|
|
|
def example_callable(self):
|
|
|
|
raise AttributeError('method call failed')
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
|
|
serializer = ExampleSerializer(ExampleInstance())
|
|
|
|
serializer.data.items()
|
|
|
|
|
|
|
|
assert 'method call failed' in str(exc_info.value)
|
|
|
|
|
2019-07-01 15:25:47 +03:00
|
|
|
def test_builtin_callable_source_raises(self):
|
|
|
|
class BuiltinSerializer(serializers.Serializer):
|
|
|
|
date = serializers.ReadOnlyField(source='timestamp.date')
|
|
|
|
|
|
|
|
with pytest.raises(BuiltinSignatureError) as exc_info:
|
|
|
|
BuiltinSerializer({'timestamp': datetime.datetime.now()}).data
|
|
|
|
|
|
|
|
assert str(exc_info.value) == (
|
|
|
|
'Field source for `BuiltinSerializer.date` maps to a built-in '
|
|
|
|
'function type and is invalid. Define a property or method on '
|
|
|
|
'the `dict` instance that wraps the call to the built-in function.')
|
|
|
|
|
2014-09-23 17:15:00 +04:00
|
|
|
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestReadOnly:
|
2022-11-15 15:29:15 +03:00
|
|
|
def setup_method(self):
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestSerializer(serializers.Serializer):
|
2018-03-20 23:09:31 +03:00
|
|
|
read_only = serializers.ReadOnlyField(default="789")
|
2014-10-17 16:23:14 +04:00
|
|
|
writable = serializers.IntegerField()
|
2014-09-26 13:46:52 +04:00
|
|
|
self.Serializer = TestSerializer
|
|
|
|
|
2018-03-20 23:09:31 +03:00
|
|
|
def test_writable_fields(self):
|
|
|
|
"""
|
|
|
|
Read-only fields should not be writable, even with default ()
|
|
|
|
"""
|
|
|
|
serializer = self.Serializer()
|
2019-05-21 17:45:31 +03:00
|
|
|
assert len(list(serializer._writable_fields)) == 1
|
2018-03-20 23:09:31 +03:00
|
|
|
|
2014-09-26 13:46:52 +04:00
|
|
|
def test_validate_read_only(self):
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
Read-only serializers.should not be included in validation.
|
2014-09-26 13:46:52 +04:00
|
|
|
"""
|
|
|
|
data = {'read_only': 123, 'writable': 456}
|
|
|
|
serializer = self.Serializer(data=data)
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'writable': 456}
|
|
|
|
|
|
|
|
def test_serialize_read_only(self):
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
Read-only serializers.should be serialized.
|
2014-09-26 13:46:52 +04:00
|
|
|
"""
|
|
|
|
instance = {'read_only': 123, 'writable': 456}
|
|
|
|
serializer = self.Serializer(instance)
|
|
|
|
assert serializer.data == {'read_only': 123, 'writable': 456}
|
|
|
|
|
|
|
|
|
|
|
|
class TestWriteOnly:
|
2022-11-15 15:29:15 +03:00
|
|
|
def setup_method(self):
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestSerializer(serializers.Serializer):
|
2014-10-17 16:23:14 +04:00
|
|
|
write_only = serializers.IntegerField(write_only=True)
|
|
|
|
readable = serializers.IntegerField()
|
2014-09-26 13:46:52 +04:00
|
|
|
self.Serializer = TestSerializer
|
|
|
|
|
|
|
|
def test_validate_write_only(self):
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
Write-only serializers.should be included in validation.
|
2014-09-26 13:46:52 +04:00
|
|
|
"""
|
|
|
|
data = {'write_only': 123, 'readable': 456}
|
|
|
|
serializer = self.Serializer(data=data)
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'write_only': 123, 'readable': 456}
|
|
|
|
|
|
|
|
def test_serialize_write_only(self):
|
|
|
|
"""
|
2014-10-17 16:23:14 +04:00
|
|
|
Write-only serializers.should not be serialized.
|
2014-09-26 13:46:52 +04:00
|
|
|
"""
|
|
|
|
instance = {'write_only': 123, 'readable': 456}
|
|
|
|
serializer = self.Serializer(instance)
|
|
|
|
assert serializer.data == {'readable': 456}
|
|
|
|
|
|
|
|
|
|
|
|
class TestInitial:
|
2022-11-15 15:29:15 +03:00
|
|
|
def setup_method(self):
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestSerializer(serializers.Serializer):
|
2014-10-17 16:23:14 +04:00
|
|
|
initial_field = serializers.IntegerField(initial=123)
|
|
|
|
blank_field = serializers.IntegerField()
|
2014-09-26 13:46:52 +04:00
|
|
|
self.serializer = TestSerializer()
|
|
|
|
|
|
|
|
def test_initial(self):
|
|
|
|
"""
|
|
|
|
Initial values should be included when serializing a new representation.
|
|
|
|
"""
|
|
|
|
assert self.serializer.data == {
|
|
|
|
'initial_field': 123,
|
|
|
|
'blank_field': None
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-17 12:52:21 +03:00
|
|
|
class TestInitialWithCallable:
|
2022-11-15 15:29:15 +03:00
|
|
|
def setup_method(self):
|
2016-02-17 12:52:21 +03:00
|
|
|
def initial_value():
|
|
|
|
return 123
|
|
|
|
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
initial_field = serializers.IntegerField(initial=initial_value)
|
|
|
|
self.serializer = TestSerializer()
|
|
|
|
|
|
|
|
def test_initial_should_accept_callable(self):
|
|
|
|
"""
|
|
|
|
Follows the default ``Field.initial`` behaviour where they accept a
|
|
|
|
callable to produce the initial value"""
|
|
|
|
assert self.serializer.data == {
|
|
|
|
'initial_field': 123,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestLabel:
|
2022-11-15 15:29:15 +03:00
|
|
|
def setup_method(self):
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestSerializer(serializers.Serializer):
|
2014-10-17 16:23:14 +04:00
|
|
|
labeled = serializers.IntegerField(label='My label')
|
2014-09-26 13:46:52 +04:00
|
|
|
self.serializer = TestSerializer()
|
|
|
|
|
|
|
|
def test_label(self):
|
|
|
|
"""
|
|
|
|
A field's label may be set with the `label` argument.
|
|
|
|
"""
|
|
|
|
fields = self.serializer.fields
|
|
|
|
assert fields['labeled'].label == 'My label'
|
|
|
|
|
|
|
|
|
|
|
|
class TestInvalidErrorKey:
|
2022-11-15 15:29:15 +03:00
|
|
|
def setup_method(self):
|
2014-09-26 13:46:52 +04:00
|
|
|
class ExampleField(serializers.Field):
|
|
|
|
def to_native(self, data):
|
|
|
|
self.fail('incorrect')
|
|
|
|
self.field = ExampleField()
|
|
|
|
|
|
|
|
def test_invalid_error_key(self):
|
|
|
|
"""
|
|
|
|
If a field raises a validation error, but does not have a corresponding
|
|
|
|
error message, then raise an appropriate assertion error.
|
|
|
|
"""
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
|
|
|
self.field.to_native(123)
|
|
|
|
expected = (
|
2014-10-17 16:23:14 +04:00
|
|
|
'ValidationError raised by `ExampleField`, but error key '
|
2014-09-26 13:46:52 +04:00
|
|
|
'`incorrect` does not exist in the `error_messages` dictionary.'
|
|
|
|
)
|
|
|
|
assert str(exc_info.value) == expected
|
|
|
|
|
|
|
|
|
|
|
|
class TestBooleanHTMLInput:
|
2015-08-13 11:56:03 +03:00
|
|
|
def test_empty_html_checkbox(self):
|
|
|
|
"""
|
|
|
|
HTML checkboxes do not send any value, but should be treated
|
|
|
|
as `False` by BooleanField.
|
|
|
|
"""
|
2014-09-26 13:46:52 +04:00
|
|
|
class TestSerializer(serializers.Serializer):
|
2014-10-17 16:23:14 +04:00
|
|
|
archived = serializers.BooleanField()
|
2014-09-26 13:46:52 +04:00
|
|
|
|
2015-08-13 11:56:03 +03:00
|
|
|
serializer = TestSerializer(data=QueryDict(''))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'archived': False}
|
|
|
|
|
|
|
|
def test_empty_html_checkbox_not_required(self):
|
2014-09-26 13:46:52 +04:00
|
|
|
"""
|
|
|
|
HTML checkboxes do not send any value, but should be treated
|
2015-08-13 11:56:03 +03:00
|
|
|
as `False` by BooleanField, even if the field is required=False.
|
2014-09-26 13:46:52 +04:00
|
|
|
"""
|
2015-08-13 11:56:03 +03:00
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
archived = serializers.BooleanField(required=False)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict(''))
|
2014-09-26 13:46:52 +04:00
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'archived': False}
|
|
|
|
|
|
|
|
|
2014-12-20 19:26:51 +03:00
|
|
|
class TestHTMLInput:
|
2015-08-24 12:13:16 +03:00
|
|
|
def test_empty_html_charfield_with_default(self):
|
2014-12-17 18:13:48 +03:00
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.CharField(default='happy')
|
|
|
|
|
2015-07-16 13:47:40 +03:00
|
|
|
serializer = TestSerializer(data=QueryDict(''))
|
2014-12-17 18:13:48 +03:00
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'message': 'happy'}
|
|
|
|
|
2015-08-24 12:13:16 +03:00
|
|
|
def test_empty_html_charfield_without_default(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.CharField(allow_blank=True)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('message='))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'message': ''}
|
|
|
|
|
|
|
|
def test_empty_html_charfield_without_default_not_required(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.CharField(allow_blank=True, required=False)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('message='))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'message': ''}
|
|
|
|
|
2015-07-16 13:47:40 +03:00
|
|
|
def test_empty_html_integerfield(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.IntegerField(default=123)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('message='))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'message': 123}
|
|
|
|
|
|
|
|
def test_empty_html_uuidfield_with_default(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.UUIDField(default=uuid.uuid4)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('message='))
|
|
|
|
assert serializer.is_valid()
|
2018-01-08 12:49:46 +03:00
|
|
|
assert list(serializer.validated_data) == ['message']
|
2015-07-16 13:47:40 +03:00
|
|
|
|
|
|
|
def test_empty_html_uuidfield_with_optional(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.UUIDField(required=False)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('message='))
|
|
|
|
assert serializer.is_valid()
|
2018-01-08 12:49:46 +03:00
|
|
|
assert list(serializer.validated_data) == []
|
2015-07-16 13:47:40 +03:00
|
|
|
|
2014-12-20 19:26:51 +03:00
|
|
|
def test_empty_html_charfield_allow_null(self):
|
2014-12-18 13:36:52 +03:00
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.CharField(allow_null=True)
|
|
|
|
|
2015-07-16 13:47:40 +03:00
|
|
|
serializer = TestSerializer(data=QueryDict('message='))
|
2014-12-18 13:36:52 +03:00
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'message': None}
|
|
|
|
|
2014-12-20 19:26:51 +03:00
|
|
|
def test_empty_html_datefield_allow_null(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
expiry = serializers.DateField(allow_null=True)
|
|
|
|
|
2015-07-16 13:47:40 +03:00
|
|
|
serializer = TestSerializer(data=QueryDict('expiry='))
|
2014-12-20 19:26:51 +03:00
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'expiry': None}
|
|
|
|
|
|
|
|
def test_empty_html_charfield_allow_null_allow_blank(self):
|
2014-12-18 13:36:52 +03:00
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.CharField(allow_null=True, allow_blank=True)
|
|
|
|
|
2015-07-16 13:47:40 +03:00
|
|
|
serializer = TestSerializer(data=QueryDict('message='))
|
2014-12-18 13:36:52 +03:00
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'message': ''}
|
|
|
|
|
2014-12-20 19:26:51 +03:00
|
|
|
def test_empty_html_charfield_required_false(self):
|
2014-12-18 13:36:52 +03:00
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
message = serializers.CharField(required=False)
|
|
|
|
|
2015-07-16 13:47:40 +03:00
|
|
|
serializer = TestSerializer(data=QueryDict(''))
|
2014-12-18 13:36:52 +03:00
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {}
|
|
|
|
|
2015-07-16 17:59:15 +03:00
|
|
|
def test_querydict_list_input(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
scores = serializers.ListField(child=serializers.IntegerField())
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('scores=1&scores=3'))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'scores': [1, 3]}
|
|
|
|
|
2015-08-07 13:41:56 +03:00
|
|
|
def test_querydict_list_input_only_one_input(self):
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
scores = serializers.ListField(child=serializers.IntegerField())
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('scores=1&'))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'scores': [1]}
|
|
|
|
|
2018-04-20 16:11:52 +03:00
|
|
|
def test_querydict_list_input_no_values_uses_default(self):
|
|
|
|
"""
|
|
|
|
When there are no values passed in, and default is set
|
|
|
|
The field should return the default value
|
|
|
|
"""
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
a = serializers.IntegerField(required=True)
|
|
|
|
scores = serializers.ListField(default=lambda: [1, 3])
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('a=1&'))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'a': 1, 'scores': [1, 3]}
|
|
|
|
|
|
|
|
def test_querydict_list_input_supports_indexed_keys(self):
|
|
|
|
"""
|
|
|
|
When data is passed in the format `scores[0]=1&scores[1]=3`
|
|
|
|
The field should return the correct list, ignoring the default
|
|
|
|
"""
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
scores = serializers.ListField(default=lambda: [1, 3])
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict("scores[0]=5&scores[1]=6"))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'scores': ['5', '6']}
|
|
|
|
|
|
|
|
def test_querydict_list_input_no_values_no_default_and_not_required(self):
|
|
|
|
"""
|
|
|
|
When there are no keys passed, there is no default, and required=False
|
|
|
|
The field should be skipped
|
|
|
|
"""
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
scores = serializers.ListField(required=False)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict(''))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {}
|
|
|
|
|
|
|
|
def test_querydict_list_input_posts_key_but_no_values(self):
|
|
|
|
"""
|
|
|
|
When there are no keys passed, there is no default, and required=False
|
|
|
|
The field should return an array of 1 item, blank
|
|
|
|
"""
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
scores = serializers.ListField(required=False)
|
|
|
|
|
|
|
|
serializer = TestSerializer(data=QueryDict('scores=&'))
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'scores': ['']}
|
|
|
|
|
2014-12-17 18:13:48 +03:00
|
|
|
|
2014-10-28 19:21:49 +03:00
|
|
|
class TestCreateOnlyDefault:
|
2022-11-15 15:29:15 +03:00
|
|
|
def setup_method(self):
|
2014-10-28 19:21:49 +03:00
|
|
|
default = serializers.CreateOnlyDefault('2001-01-01')
|
|
|
|
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
published = serializers.HiddenField(default=default)
|
|
|
|
text = serializers.CharField()
|
|
|
|
self.Serializer = TestSerializer
|
|
|
|
|
|
|
|
def test_create_only_default_is_provided(self):
|
|
|
|
serializer = self.Serializer(data={'text': 'example'})
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {
|
|
|
|
'text': 'example', 'published': '2001-01-01'
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_create_only_default_is_not_provided_on_update(self):
|
|
|
|
instance = {
|
|
|
|
'text': 'example', 'published': '2001-01-01'
|
|
|
|
}
|
|
|
|
serializer = self.Serializer(instance, data={'text': 'example'})
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {
|
|
|
|
'text': 'example',
|
|
|
|
}
|
|
|
|
|
2015-02-28 09:14:15 +03:00
|
|
|
def test_create_only_default_callable_sets_context(self):
|
2015-03-01 00:06:47 +03:00
|
|
|
"""
|
Stop calling `set_context`, planned for 3.13 drop (#8589)
Per the deprecation warnings (which have been raised since DRF 3.11),
`set_context()` was planned not to be supported in DRF 3.13. I think we
can safely delete it, in favor of `requires_context`.
From the 3.11 announcement:
> Previous our approach to this was that implementations could include a
> `set_context` method, which would be called prior to validation. However
> this approach had issues with potential race conditions. We have now
> move this approach into a pending deprecation state. It will continue to
> function, but will be escalated to a deprecated state in 3.12, and
> removed entirely in 3.13.
Why keep `RemovedInDRF313Warning` around?
=========================================
It's a bit odd that version 3.13 includes an exception class describing
things which are to be deleted in 3.13, but I've opted to keep the (now
unreferenced) class around, for fear of breaking others' setup.
(For example, if projects have a `filterwarnings` setup meant to
intercept `rest_framework.RemovedInDRF313Warning`, an error will be
thrown due to an unresolvable reference).
2022-08-08 13:18:49 +03:00
|
|
|
CreateOnlyDefault instances with a callable default should set context
|
2015-03-01 00:06:47 +03:00
|
|
|
on the callable if possible
|
|
|
|
"""
|
2015-02-28 09:14:15 +03:00
|
|
|
class TestCallableDefault:
|
2019-12-11 11:44:08 +03:00
|
|
|
requires_context = True
|
2015-02-28 09:14:15 +03:00
|
|
|
|
2019-12-11 11:44:08 +03:00
|
|
|
def __call__(self, field=None):
|
|
|
|
return "success" if field is not None else "failure"
|
2015-02-28 09:14:15 +03:00
|
|
|
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
context_set = serializers.CharField(default=serializers.CreateOnlyDefault(TestCallableDefault()))
|
|
|
|
|
|
|
|
serializer = TestSerializer(data={})
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data['context_set'] == 'success'
|
|
|
|
|
2014-10-28 19:21:49 +03:00
|
|
|
|
2017-08-07 18:52:09 +03:00
|
|
|
class Test5087Regression:
|
|
|
|
def test_parent_binding(self):
|
|
|
|
parent = serializers.Serializer()
|
|
|
|
field = serializers.CharField()
|
|
|
|
|
|
|
|
assert field.root is field
|
|
|
|
field.bind('name', parent)
|
|
|
|
assert field.root is parent
|
|
|
|
|
|
|
|
|
2014-09-23 17:15:00 +04:00
|
|
|
# Tests for field input and output values.
|
|
|
|
# ----------------------------------------
|
|
|
|
|
2014-09-22 20:46:02 +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
|
|
|
|
|
|
|
|
|
|
|
|
class FieldValues:
|
|
|
|
"""
|
|
|
|
Base class for testing valid and invalid input values.
|
|
|
|
"""
|
|
|
|
def test_valid_inputs(self):
|
|
|
|
"""
|
|
|
|
Ensure that valid values return the expected validated data.
|
|
|
|
"""
|
|
|
|
for input_value, expected_output in get_items(self.valid_inputs):
|
2017-09-11 12:18:39 +03:00
|
|
|
assert self.field.run_validation(input_value) == expected_output, \
|
|
|
|
'input value: {}'.format(repr(input_value))
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
def test_invalid_inputs(self):
|
|
|
|
"""
|
|
|
|
Ensure that invalid values raise the expected validation error.
|
|
|
|
"""
|
|
|
|
for input_value, expected_failure in get_items(self.invalid_inputs):
|
2014-10-17 16:23:14 +04:00
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
2014-09-22 20:46:02 +04:00
|
|
|
self.field.run_validation(input_value)
|
2017-09-11 12:18:39 +03:00
|
|
|
assert exc_info.value.detail == expected_failure, \
|
|
|
|
'input value: {}'.format(repr(input_value))
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
def test_outputs(self):
|
|
|
|
for output_value, expected_output in get_items(self.outputs):
|
2017-09-11 12:18:39 +03:00
|
|
|
assert self.field.to_representation(output_value) == expected_output, \
|
|
|
|
'output value: {}'.format(repr(output_value))
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
# Boolean types...
|
|
|
|
|
|
|
|
class TestBooleanField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `BooleanField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'true': True,
|
|
|
|
'false': False,
|
|
|
|
'1': True,
|
|
|
|
'0': False,
|
|
|
|
1: True,
|
|
|
|
0: False,
|
|
|
|
True: True,
|
|
|
|
False: False,
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2018-04-20 17:00:27 +03:00
|
|
|
'foo': ['Must be a valid boolean.'],
|
2014-09-23 17:30:17 +04:00
|
|
|
None: ['This field may not be null.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'true': True,
|
|
|
|
'false': False,
|
|
|
|
'1': True,
|
|
|
|
'0': False,
|
|
|
|
1: True,
|
|
|
|
0: False,
|
|
|
|
True: True,
|
|
|
|
False: False,
|
|
|
|
'other': True
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.BooleanField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
2015-09-12 06:01:18 +03:00
|
|
|
def test_disallow_unhashable_collection_types(self):
|
|
|
|
inputs = (
|
|
|
|
[],
|
|
|
|
{},
|
|
|
|
)
|
2017-09-04 12:11:53 +03:00
|
|
|
field = self.field
|
2015-09-12 06:01:18 +03:00
|
|
|
for input_value in inputs:
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.run_validation(input_value)
|
2020-09-05 11:02:27 +03:00
|
|
|
expected = ['Must be a valid boolean.']
|
2015-09-12 06:01:18 +03:00
|
|
|
assert exc_info.value.detail == expected
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
|
2022-09-21 16:08:12 +03:00
|
|
|
class TestNullableBooleanField(TestBooleanField):
|
2014-09-23 17:30:17 +04:00
|
|
|
"""
|
2022-09-21 16:08:12 +03:00
|
|
|
Valid and invalid values for `BooleanField` when `allow_null=True`.
|
2014-09-23 17:30:17 +04:00
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'true': True,
|
|
|
|
'false': False,
|
|
|
|
'null': None,
|
|
|
|
True: True,
|
|
|
|
False: False,
|
|
|
|
None: None
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2018-04-20 17:00:27 +03:00
|
|
|
'foo': ['Must be a valid boolean.'],
|
2014-09-23 17:30:17 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'true': True,
|
|
|
|
'false': False,
|
|
|
|
'null': None,
|
|
|
|
True: True,
|
|
|
|
False: False,
|
2014-09-25 16:10:33 +04:00
|
|
|
None: None,
|
|
|
|
'other': True
|
2014-09-23 17:30:17 +04:00
|
|
|
}
|
2020-09-08 17:32:27 +03:00
|
|
|
field = serializers.BooleanField(allow_null=True)
|
2014-09-23 17:30:17 +04:00
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
# String types...
|
|
|
|
|
|
|
|
class TestCharField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `CharField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
1: '1',
|
|
|
|
'abc': 'abc'
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2016-08-10 19:22:19 +03:00
|
|
|
(): ['Not a valid string.'],
|
|
|
|
True: ['Not a valid string.'],
|
2014-09-22 20:46:02 +04:00
|
|
|
'': ['This field may not be blank.']
|
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
1: '1',
|
|
|
|
'abc': 'abc'
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.CharField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
2015-02-06 08:09:19 +03:00
|
|
|
def test_trim_whitespace_default(self):
|
|
|
|
field = serializers.CharField()
|
2015-02-06 17:43:43 +03:00
|
|
|
assert field.to_internal_value(' abc ') == 'abc'
|
2015-02-06 08:09:19 +03:00
|
|
|
|
|
|
|
def test_trim_whitespace_disabled(self):
|
|
|
|
field = serializers.CharField(trim_whitespace=False)
|
2015-02-06 17:43:43 +03:00
|
|
|
assert field.to_internal_value(' abc ') == ' abc '
|
2015-02-06 08:09:19 +03:00
|
|
|
|
2015-06-24 15:32:54 +03:00
|
|
|
def test_disallow_blank_with_trim_whitespace(self):
|
|
|
|
field = serializers.CharField(allow_blank=False, trim_whitespace=True)
|
|
|
|
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.run_validation(' ')
|
|
|
|
assert exc_info.value.detail == ['This field may not be blank.']
|
|
|
|
|
2018-10-02 17:54:15 +03:00
|
|
|
def test_null_bytes(self):
|
|
|
|
field = serializers.CharField()
|
|
|
|
|
|
|
|
for value in ('\0', 'foo\0', '\0foo', 'foo\0foo'):
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.run_validation(value)
|
|
|
|
assert exc_info.value.detail == [
|
|
|
|
'Null characters are not allowed.'
|
|
|
|
]
|
|
|
|
|
2020-01-06 17:12:21 +03:00
|
|
|
def test_surrogate_characters(self):
|
|
|
|
field = serializers.CharField()
|
|
|
|
|
|
|
|
for code_point, expected_message in (
|
2020-05-14 10:24:09 +03:00
|
|
|
(0xD800, 'Surrogate characters are not allowed: U+D800.'),
|
|
|
|
(0xDFFF, 'Surrogate characters are not allowed: U+DFFF.'),
|
2020-01-06 17:12:21 +03:00
|
|
|
):
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.run_validation(chr(code_point))
|
|
|
|
assert exc_info.value.detail[0].code == 'surrogate_characters_not_allowed'
|
|
|
|
assert str(exc_info.value.detail[0]) == expected_message
|
|
|
|
|
|
|
|
for code_point in (0xD800 - 1, 0xDFFF + 1):
|
|
|
|
field.run_validation(chr(code_point))
|
|
|
|
|
2019-02-19 18:38:20 +03:00
|
|
|
def test_iterable_validators(self):
|
|
|
|
"""
|
|
|
|
Ensure `validators` parameter is compatible with reasonable iterables.
|
|
|
|
"""
|
|
|
|
value = 'example'
|
|
|
|
|
|
|
|
for validators in ([], (), set()):
|
|
|
|
field = serializers.CharField(validators=validators)
|
|
|
|
field.run_validation(value)
|
|
|
|
|
|
|
|
def raise_exception(value):
|
|
|
|
raise exceptions.ValidationError('Raised error')
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
for validators in ([raise_exception], (raise_exception,), {raise_exception}):
|
2019-02-19 18:38:20 +03:00
|
|
|
field = serializers.CharField(validators=validators)
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.run_validation(value)
|
|
|
|
assert exc_info.value.detail == ['Raised error']
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
class TestEmailField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `EmailField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'example@example.com': 'example@example.com',
|
|
|
|
' example@example.com ': 'example@example.com',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'examplecom': ['Enter a valid email address.']
|
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.EmailField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestRegexField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `RegexField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'a9': 'a9',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'A9': ["This value does not match the required pattern."]
|
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.RegexField(regex='[a-z][0-9]')
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2016-09-15 14:44:45 +03:00
|
|
|
class TestiCompiledRegexField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `RegexField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'a9': 'a9',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'A9': ["This value does not match the required pattern."]
|
|
|
|
}
|
|
|
|
outputs = {}
|
|
|
|
field = serializers.RegexField(regex=re.compile('[a-z][0-9]'))
|
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
class TestSlugField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `SlugField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'slug-99': 'slug-99',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2015-01-07 15:46:23 +03:00
|
|
|
'slug 99': ['Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.SlugField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
2017-06-24 08:11:17 +03:00
|
|
|
def test_allow_unicode_true(self):
|
|
|
|
field = serializers.SlugField(allow_unicode=True)
|
|
|
|
|
|
|
|
validation_error = False
|
|
|
|
try:
|
2019-04-30 18:53:44 +03:00
|
|
|
field.run_validation('slug-99-\u0420')
|
2017-06-24 08:11:17 +03:00
|
|
|
except serializers.ValidationError:
|
|
|
|
validation_error = True
|
|
|
|
|
|
|
|
assert not validation_error
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
class TestURLField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `URLField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'http://example.com': 'http://example.com',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'example.com': ['Enter a valid URL.']
|
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.URLField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2015-01-23 18:24:06 +03:00
|
|
|
class TestUUIDField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `UUIDField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'825d7aeb-05a9-45b5-a5b7-05df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'),
|
2015-03-25 20:56:55 +03:00
|
|
|
'825d7aeb05a945b5a5b705df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'),
|
|
|
|
'urn:uuid:213b7d9b-244f-410d-828c-dabce7a2615d': uuid.UUID('213b7d9b-244f-410d-828c-dabce7a2615d'),
|
|
|
|
284758210125106368185219588917561929842: uuid.UUID('d63a6fb6-88d5-40c7-a91c-9edf73283072')
|
2015-01-23 18:24:06 +03:00
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2018-04-20 17:00:27 +03:00
|
|
|
'825d7aeb-05a9-45b5-a5b7': ['Must be a valid UUID.'],
|
|
|
|
(1, 2, 3): ['Must be a valid UUID.']
|
2015-01-23 18:24:06 +03:00
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'): '825d7aeb-05a9-45b5-a5b7-05df87923cda'
|
|
|
|
}
|
|
|
|
field = serializers.UUIDField()
|
|
|
|
|
2015-03-25 20:56:55 +03:00
|
|
|
def _test_format(self, uuid_format, formatted_uuid_0):
|
|
|
|
field = serializers.UUIDField(format=uuid_format)
|
|
|
|
assert field.to_representation(uuid.UUID(int=0)) == formatted_uuid_0
|
|
|
|
assert field.to_internal_value(formatted_uuid_0) == uuid.UUID(int=0)
|
|
|
|
|
|
|
|
def test_formats(self):
|
|
|
|
self._test_format('int', 0)
|
|
|
|
self._test_format('hex_verbose', '00000000-0000-0000-0000-000000000000')
|
|
|
|
self._test_format('urn', 'urn:uuid:00000000-0000-0000-0000-000000000000')
|
|
|
|
self._test_format('hex', '0' * 32)
|
|
|
|
|
2015-01-23 18:24:06 +03:00
|
|
|
|
2015-02-28 10:11:38 +03:00
|
|
|
class TestIPAddressField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `IPAddressField`
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'127.0.0.1': '127.0.0.1',
|
|
|
|
'192.168.33.255': '192.168.33.255',
|
|
|
|
'2001:0db8:85a3:0042:1000:8a2e:0370:7334': '2001:db8:85a3:42:1000:8a2e:370:7334',
|
|
|
|
'2001:cdba:0:0:0:0:3257:9652': '2001:cdba::3257:9652',
|
|
|
|
'2001:cdba::3257:9652': '2001:cdba::3257:9652'
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'127001': ['Enter a valid IPv4 or IPv6 address.'],
|
|
|
|
'127.122.111.2231': ['Enter a valid IPv4 or IPv6 address.'],
|
|
|
|
'2001:::9652': ['Enter a valid IPv4 or IPv6 address.'],
|
|
|
|
'2001:0db8:85a3:0042:1000:8a2e:0370:73341': ['Enter a valid IPv4 or IPv6 address.'],
|
2016-08-01 18:14:26 +03:00
|
|
|
1000: ['Enter a valid IPv4 or IPv6 address.'],
|
2015-02-28 10:11:38 +03:00
|
|
|
}
|
|
|
|
outputs = {}
|
|
|
|
field = serializers.IPAddressField()
|
|
|
|
|
|
|
|
|
2015-03-22 19:46:16 +03:00
|
|
|
class TestIPv4AddressField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `IPAddressField`
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'127.0.0.1': '127.0.0.1',
|
|
|
|
'192.168.33.255': '192.168.33.255',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'127001': ['Enter a valid IPv4 address.'],
|
|
|
|
'127.122.111.2231': ['Enter a valid IPv4 address.'],
|
|
|
|
}
|
|
|
|
outputs = {}
|
|
|
|
field = serializers.IPAddressField(protocol='IPv4')
|
|
|
|
|
|
|
|
|
|
|
|
class TestIPv6AddressField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `IPAddressField`
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'2001:0db8:85a3:0042:1000:8a2e:0370:7334': '2001:db8:85a3:42:1000:8a2e:370:7334',
|
|
|
|
'2001:cdba:0:0:0:0:3257:9652': '2001:cdba::3257:9652',
|
|
|
|
'2001:cdba::3257:9652': '2001:cdba::3257:9652'
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'2001:::9652': ['Enter a valid IPv4 or IPv6 address.'],
|
|
|
|
'2001:0db8:85a3:0042:1000:8a2e:0370:73341': ['Enter a valid IPv4 or IPv6 address.'],
|
|
|
|
}
|
|
|
|
outputs = {}
|
|
|
|
field = serializers.IPAddressField(protocol='IPv6')
|
|
|
|
|
2015-03-23 02:21:09 +03:00
|
|
|
|
2015-03-03 14:34:06 +03:00
|
|
|
class TestFilePathField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `FilePathField`
|
|
|
|
"""
|
|
|
|
|
|
|
|
valid_inputs = {
|
|
|
|
__file__: __file__,
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'wrong_path': ['"wrong_path" is not a valid path choice.']
|
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
}
|
|
|
|
field = serializers.FilePathField(
|
|
|
|
path=os.path.abspath(os.path.dirname(__file__))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
# Number types...
|
|
|
|
|
|
|
|
class TestIntegerField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `IntegerField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'1': 1,
|
|
|
|
'0': 0,
|
|
|
|
1: 1,
|
|
|
|
0: 0,
|
|
|
|
1.0: 1,
|
2015-04-17 21:38:35 +03:00
|
|
|
0.0: 0,
|
|
|
|
'1.0': 1
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2015-04-17 21:38:35 +03:00
|
|
|
0.5: ['A valid integer is required.'],
|
|
|
|
'abc': ['A valid integer is required.'],
|
|
|
|
'0.5': ['A valid integer is required.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'1': 1,
|
|
|
|
'0': 0,
|
|
|
|
1: 1,
|
|
|
|
0: 0,
|
|
|
|
1.0: 1,
|
|
|
|
0.0: 0
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.IntegerField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestMinMaxIntegerField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `IntegerField` with min and max limits.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'1': 1,
|
|
|
|
'3': 3,
|
|
|
|
1: 1,
|
|
|
|
3: 3,
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
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.'],
|
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.IntegerField(min_value=1, max_value=3)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestFloatField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `FloatField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'1': 1.0,
|
|
|
|
'0': 0.0,
|
|
|
|
1: 1.0,
|
|
|
|
0: 0.0,
|
|
|
|
1.0: 1.0,
|
|
|
|
0.0: 0.0,
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'abc': ["A valid number is required."]
|
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'1': 1.0,
|
|
|
|
'0': 0.0,
|
|
|
|
1: 1.0,
|
|
|
|
0: 0.0,
|
|
|
|
1.0: 1.0,
|
|
|
|
0.0: 0.0,
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.FloatField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestMinMaxFloatField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `FloatField` with min and max limits.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'1': 1,
|
|
|
|
'3': 3,
|
|
|
|
1: 1,
|
|
|
|
3: 3,
|
|
|
|
1.0: 1.0,
|
|
|
|
3.0: 3.0,
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
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.'],
|
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.FloatField(min_value=1, max_value=3)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2022-11-22 08:15:25 +03:00
|
|
|
class TestFloatFieldOverFlowError(TestCase):
|
|
|
|
def test_overflow_error_float_field(self):
|
|
|
|
field = serializers.FloatField()
|
|
|
|
with pytest.raises(serializers.ValidationError) as exec_info:
|
|
|
|
field.to_internal_value(data=math.factorial(171))
|
|
|
|
assert "Integer value too large to convert to float" in str(exec_info.value.detail)
|
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
class TestDecimalField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DecimalField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'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'),
|
2015-08-06 11:51:00 +03:00
|
|
|
'2E+1': Decimal('20'),
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
invalid_inputs = (
|
2021-03-09 13:49:03 +03:00
|
|
|
(None, ["This field may not be null."]),
|
|
|
|
('', ["A valid number is required."]),
|
|
|
|
(' ', ["A valid number is required."]),
|
2014-09-22 20:46:02 +04:00
|
|
|
('abc', ["A valid number is required."]),
|
|
|
|
(Decimal('Nan'), ["A valid number is required."]),
|
2019-10-22 12:06:37 +03:00
|
|
|
(Decimal('Snan'), ["A valid number is required."]),
|
2014-09-22 20:46:02 +04:00
|
|
|
(Decimal('Inf'), ["A valid number is required."]),
|
|
|
|
('12.345', ["Ensure that there are no more than 3 digits in total."]),
|
2015-08-06 11:51:00 +03:00
|
|
|
(200000000000.0, ["Ensure that there are no more than 3 digits in total."]),
|
2014-09-22 20:46:02 +04:00
|
|
|
('0.01', ["Ensure that there are no more than 1 decimal places."]),
|
2015-08-06 11:51:00 +03:00
|
|
|
(123, ["Ensure that there are no more than 2 digits before the decimal point."]),
|
|
|
|
('2E+2', ["Ensure that there are no more than 2 digits before the decimal point."])
|
2014-09-22 20:46:02 +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',
|
2014-09-26 20:06:20 +04:00
|
|
|
Decimal('0.04'): '0.0'
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DecimalField(max_digits=3, decimal_places=1)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2021-03-09 13:49:03 +03:00
|
|
|
class TestAllowNullDecimalField(FieldValues):
|
|
|
|
valid_inputs = {
|
|
|
|
None: None,
|
|
|
|
'': None,
|
|
|
|
' ': None,
|
|
|
|
}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
None: '',
|
|
|
|
}
|
|
|
|
field = serializers.DecimalField(max_digits=3, decimal_places=1, allow_null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class TestAllowNullNoStringCoercionDecimalField(FieldValues):
|
|
|
|
valid_inputs = {
|
|
|
|
None: None,
|
|
|
|
'': None,
|
|
|
|
' ': None,
|
|
|
|
}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
None: None,
|
|
|
|
}
|
|
|
|
field = serializers.DecimalField(max_digits=3, decimal_places=1, allow_null=True, coerce_to_string=False)
|
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
class TestMinMaxDecimalField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DecimalField` with min and max limits.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'10.0': Decimal('10.0'),
|
|
|
|
'20.0': Decimal('20.0'),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'9.9': ['Ensure this value is greater than or equal to 10.'],
|
|
|
|
'20.1': ['Ensure this value is less than or equal to 20.'],
|
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DecimalField(
|
2014-09-22 20:46:02 +04:00
|
|
|
max_digits=3, decimal_places=1,
|
|
|
|
min_value=10, max_value=20
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-01 17:04:44 +03:00
|
|
|
class TestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
|
|
|
|
"""
|
|
|
|
Check that empty string ('', ' ') is acceptable value for the DecimalField
|
|
|
|
if allow_null=True and there are max/min validators
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
None: None,
|
|
|
|
'': None,
|
|
|
|
' ': None,
|
|
|
|
' ': None,
|
|
|
|
5: Decimal('5'),
|
|
|
|
'0': Decimal('0'),
|
|
|
|
'10': Decimal('10'),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
-1: ['Ensure this value is greater than or equal to 0.'],
|
|
|
|
11: ['Ensure this value is less than or equal to 10.'],
|
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
None: '',
|
|
|
|
}
|
|
|
|
field = serializers.DecimalField(max_digits=3, decimal_places=1, allow_null=True, min_value=0, max_value=10)
|
|
|
|
|
|
|
|
|
2016-08-10 16:39:26 +03:00
|
|
|
class TestNoMaxDigitsDecimalField(FieldValues):
|
|
|
|
field = serializers.DecimalField(
|
|
|
|
max_value=100, min_value=0,
|
|
|
|
decimal_places=2, max_digits=None
|
|
|
|
)
|
|
|
|
valid_inputs = {
|
|
|
|
'10': Decimal('10.00')
|
|
|
|
}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {}
|
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +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'),
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DecimalField(
|
2014-09-22 20:46:02 +04:00
|
|
|
max_digits=3, decimal_places=1,
|
|
|
|
coerce_to_string=False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-07-06 18:07:16 +03:00
|
|
|
class TestLocalizedDecimalField(TestCase):
|
2021-09-22 11:02:28 +03:00
|
|
|
@override_settings(LANGUAGE_CODE='pl')
|
2016-07-06 18:07:16 +03:00
|
|
|
def test_to_internal_value(self):
|
|
|
|
field = serializers.DecimalField(max_digits=2, decimal_places=1, localize=True)
|
2016-11-30 15:52:32 +03:00
|
|
|
assert field.to_internal_value('1,1') == Decimal('1.1')
|
2016-07-06 18:07:16 +03:00
|
|
|
|
2021-09-22 11:02:28 +03:00
|
|
|
@override_settings(LANGUAGE_CODE='pl')
|
2016-07-06 18:07:16 +03:00
|
|
|
def test_to_representation(self):
|
|
|
|
field = serializers.DecimalField(max_digits=2, decimal_places=1, localize=True)
|
2016-11-30 15:52:32 +03:00
|
|
|
assert field.to_representation(Decimal('1.1')) == '1,1'
|
2016-07-06 18:07:16 +03:00
|
|
|
|
|
|
|
def test_localize_forces_coerce_to_string(self):
|
|
|
|
field = serializers.DecimalField(max_digits=2, decimal_places=1, coerce_to_string=False, localize=True)
|
2019-04-30 18:53:44 +03:00
|
|
|
assert isinstance(field.to_representation(Decimal('1.1')), str)
|
2016-07-06 18:07:16 +03:00
|
|
|
|
|
|
|
|
2016-08-01 19:15:41 +03:00
|
|
|
class TestQuantizedValueForDecimal(TestCase):
|
|
|
|
def test_int_quantized_value_for_decimal(self):
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=2)
|
|
|
|
value = field.to_internal_value(12).as_tuple()
|
|
|
|
expected_digit_tuple = (0, (1, 2, 0, 0), -2)
|
2016-11-30 15:52:32 +03:00
|
|
|
assert value == expected_digit_tuple
|
2016-08-01 19:15:41 +03:00
|
|
|
|
|
|
|
def test_string_quantized_value_for_decimal(self):
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=2)
|
|
|
|
value = field.to_internal_value('12').as_tuple()
|
|
|
|
expected_digit_tuple = (0, (1, 2, 0, 0), -2)
|
2016-11-30 15:52:32 +03:00
|
|
|
assert value == expected_digit_tuple
|
2016-08-01 19:15:41 +03:00
|
|
|
|
|
|
|
def test_part_precision_string_quantized_value_for_decimal(self):
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=2)
|
|
|
|
value = field.to_internal_value('12.0').as_tuple()
|
|
|
|
expected_digit_tuple = (0, (1, 2, 0, 0), -2)
|
2016-11-30 15:52:32 +03:00
|
|
|
assert value == expected_digit_tuple
|
2016-08-01 19:15:41 +03:00
|
|
|
|
|
|
|
|
2022-11-16 17:31:50 +03:00
|
|
|
class TestNormalizedOutputValueDecimalField(TestCase):
|
|
|
|
"""
|
|
|
|
Test that we get the expected behavior of on DecimalField when normalize=True
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_normalize_output(self):
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=3, normalize_output=True)
|
|
|
|
output = field.to_representation(Decimal('1.000'))
|
|
|
|
assert output == '1'
|
|
|
|
|
|
|
|
def test_non_normalize_output(self):
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=3, normalize_output=False)
|
|
|
|
output = field.to_representation(Decimal('1.000'))
|
|
|
|
assert output == '1.000'
|
|
|
|
|
|
|
|
def test_normalize_coeherce_to_string(self):
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=3, normalize_output=True, coerce_to_string=False)
|
|
|
|
output = field.to_representation(Decimal('1.000'))
|
|
|
|
assert output == Decimal('1')
|
|
|
|
|
|
|
|
|
2016-04-27 19:04:01 +03:00
|
|
|
class TestNoDecimalPlaces(FieldValues):
|
|
|
|
valid_inputs = {
|
|
|
|
'0.12345': Decimal('0.12345'),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'0.1234567': ['Ensure that there are no more than 6 digits in total.']
|
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'1.2345': '1.2345',
|
|
|
|
'0': '0',
|
|
|
|
'1.1': '1.1',
|
|
|
|
}
|
|
|
|
field = serializers.DecimalField(max_digits=6, decimal_places=None)
|
|
|
|
|
|
|
|
|
2017-11-06 11:55:09 +03:00
|
|
|
class TestRoundingDecimalField(TestCase):
|
|
|
|
def test_valid_rounding(self):
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=2, rounding=ROUND_UP)
|
|
|
|
assert field.to_representation(Decimal('1.234')) == '1.24'
|
|
|
|
|
|
|
|
field = serializers.DecimalField(max_digits=4, decimal_places=2, rounding=ROUND_DOWN)
|
|
|
|
assert field.to_representation(Decimal('1.234')) == '1.23'
|
|
|
|
|
|
|
|
def test_invalid_rounding(self):
|
|
|
|
with pytest.raises(AssertionError) as excinfo:
|
|
|
|
serializers.DecimalField(max_digits=1, decimal_places=1, rounding='ROUND_UNKNOWN')
|
|
|
|
assert 'Invalid rounding option' in str(excinfo.value)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
2017-11-06 11:55:09 +03:00
|
|
|
|
|
|
|
# Date & time serializers...
|
2014-09-22 20:46:02 +04:00
|
|
|
class TestDateField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'2001-01-01': datetime.date(2001, 1, 1),
|
|
|
|
datetime.date(2001, 1, 1): datetime.date(2001, 1, 1),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2018-04-14 07:23:31 +03: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.'],
|
|
|
|
'2001-01': ['Date has wrong format. Use one of these formats instead: YYYY-MM-DD.'],
|
|
|
|
'2001': ['Date has wrong format. Use one of these formats instead: YYYY-MM-DD.'],
|
2014-09-22 20:46:02 +04:00
|
|
|
datetime.datetime(2001, 1, 1, 12, 00): ['Expected a date but got a datetime.'],
|
|
|
|
}
|
|
|
|
outputs = {
|
2015-04-24 03:25:22 +03:00
|
|
|
datetime.date(2001, 1, 1): '2001-01-01',
|
|
|
|
'2001-01-01': '2001-01-01',
|
2019-04-30 18:53:44 +03:00
|
|
|
str('2016-01-10'): '2016-01-10',
|
2015-04-24 03:25:22 +03:00
|
|
|
None: None,
|
|
|
|
'': None,
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DateField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomInputFormatDateField(FieldValues):
|
|
|
|
"""
|
2016-08-08 11:32:22 +03:00
|
|
|
Valid and invalid values for `DateField` with a custom input format.
|
2014-09-22 20:46:02 +04:00
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'1 Jan 2001': datetime.date(2001, 1, 1),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2014-12-31 16:14:09 +03:00
|
|
|
'2001-01-01': ['Date has wrong format. Use one of these formats instead: DD [Jan-Dec] YYYY.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DateField(input_formats=['%d %b %Y'])
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomOutputFormatDateField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `DateField` with a custom output format.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
datetime.date(2001, 1, 1): '01 Jan 2001'
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DateField(format='%d %b %Y')
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestNoOutputFormatDateField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `DateField` with no output format.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
datetime.date(2001, 1, 1): datetime.date(2001, 1, 1)
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DateField(format=None)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestDateTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateTimeField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
2017-01-18 17:01:18 +03:00
|
|
|
'2001-01-01 13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
|
|
|
|
'2001-01-01T13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
|
2017-07-10 15:42:02 +03:00
|
|
|
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
|
2017-01-18 17:01:18 +03:00
|
|
|
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
|
|
|
|
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2014-12-31 16:14:09 +03: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].'],
|
2017-07-10 12:14:31 +03:00
|
|
|
'2018-08-16 22:00-24:00': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'],
|
2014-09-22 20:46:02 +04:00
|
|
|
datetime.date(2001, 1, 1): ['Expected a datetime but got a date.'],
|
2017-10-31 12:17:08 +03:00
|
|
|
'9999-12-31T21:59:59.99990-03:00': ['Datetime value out of range.'],
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
2017-09-20 13:15:15 +03:00
|
|
|
datetime.datetime(2001, 1, 1, 13, 00): '2001-01-01T13:00:00Z',
|
2017-01-18 17:01:18 +03:00
|
|
|
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc): '2001-01-01T13:00:00Z',
|
2016-06-14 14:23:39 +03:00
|
|
|
'2001-01-01T00:00:00': '2001-01-01T00:00:00',
|
2019-04-30 18:53:44 +03:00
|
|
|
str('2016-01-10T00:00:00'): '2016-01-10T00:00:00',
|
2015-12-13 22:05:04 +03:00
|
|
|
None: None,
|
|
|
|
'': None,
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
2017-01-18 17:01:18 +03:00
|
|
|
field = serializers.DateTimeField(default_timezone=utc)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomInputFormatDateTimeField(FieldValues):
|
|
|
|
"""
|
2016-08-08 11:32:22 +03:00
|
|
|
Valid and invalid values for `DateTimeField` with a custom input format.
|
2014-09-22 20:46:02 +04:00
|
|
|
"""
|
|
|
|
valid_inputs = {
|
2017-01-18 17:01:18 +03:00
|
|
|
'1:35pm, 1 Jan 2001': datetime.datetime(2001, 1, 1, 13, 35, tzinfo=utc),
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2014-12-31 16:14:09 +03: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 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {}
|
2017-01-18 17:01:18 +03:00
|
|
|
field = serializers.DateTimeField(default_timezone=utc, input_formats=['%I:%M%p, %d %b %Y'])
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomOutputFormatDateTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `DateTimeField` with a custom output format.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
datetime.datetime(2001, 1, 1, 13, 00): '01:00PM, 01 Jan 2001',
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DateTimeField(format='%I:%M%p, %d %b %Y')
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestNoOutputFormatDateTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `DateTimeField` with no output format.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00),
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DateTimeField(format=None)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestNaiveDateTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateTimeField` with naive datetimes.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
2017-01-18 17:01:18 +03:00
|
|
|
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc): datetime.datetime(2001, 1, 1, 13, 00),
|
2014-09-22 20:46:02 +04:00
|
|
|
'2001-01-01 13:00': datetime.datetime(2001, 1, 1, 13, 00),
|
|
|
|
}
|
|
|
|
invalid_inputs = {}
|
2017-09-20 13:15:15 +03:00
|
|
|
outputs = {
|
|
|
|
datetime.datetime(2001, 1, 1, 13, 00): '2001-01-01T13:00:00',
|
|
|
|
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc): '2001-01-01T13:00:00',
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.DateTimeField(default_timezone=None)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2017-09-20 13:15:15 +03:00
|
|
|
class TestTZWithDateTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DateTimeField` when not using UTC as the timezone.
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
|
|
|
# use class setup method, as class-level attribute will still be evaluated even if test is skipped
|
|
|
|
kolkata = pytz.timezone('Asia/Kolkata')
|
|
|
|
|
|
|
|
cls.valid_inputs = {
|
|
|
|
'2016-12-19T10:00:00': kolkata.localize(datetime.datetime(2016, 12, 19, 10)),
|
|
|
|
'2016-12-19T10:00:00+05:30': kolkata.localize(datetime.datetime(2016, 12, 19, 10)),
|
|
|
|
datetime.datetime(2016, 12, 19, 10): kolkata.localize(datetime.datetime(2016, 12, 19, 10)),
|
|
|
|
}
|
|
|
|
cls.invalid_inputs = {}
|
|
|
|
cls.outputs = {
|
|
|
|
datetime.datetime(2016, 12, 19, 10): '2016-12-19T10:00:00+05:30',
|
|
|
|
datetime.datetime(2016, 12, 19, 4, 30, tzinfo=utc): '2016-12-19T10:00:00+05:30',
|
|
|
|
}
|
|
|
|
cls.field = serializers.DateTimeField(default_timezone=kolkata)
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(TIME_ZONE='UTC', USE_TZ=True)
|
|
|
|
class TestDefaultTZDateTimeField(TestCase):
|
|
|
|
"""
|
|
|
|
Test the current/default timezone handling in `DateTimeField`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
|
|
|
cls.field = serializers.DateTimeField()
|
|
|
|
cls.kolkata = pytz.timezone('Asia/Kolkata')
|
|
|
|
|
2021-09-22 11:07:00 +03:00
|
|
|
def assertUTC(self, tzinfo):
|
|
|
|
"""
|
|
|
|
Check UTC for datetime.timezone, ZoneInfo, and pytz tzinfo instances.
|
|
|
|
"""
|
|
|
|
assert (
|
|
|
|
tzinfo is utc or
|
|
|
|
(getattr(tzinfo, "key", None) or getattr(tzinfo, "zone", None)) == "UTC"
|
|
|
|
)
|
|
|
|
|
2017-09-20 13:15:15 +03:00
|
|
|
def test_default_timezone(self):
|
2021-09-22 11:07:00 +03:00
|
|
|
self.assertUTC(self.field.default_timezone())
|
2017-09-20 13:15:15 +03:00
|
|
|
|
|
|
|
def test_current_timezone(self):
|
2021-09-22 11:07:00 +03:00
|
|
|
self.assertUTC(self.field.default_timezone())
|
2017-09-20 13:15:15 +03:00
|
|
|
activate(self.kolkata)
|
|
|
|
assert self.field.default_timezone() == self.kolkata
|
|
|
|
deactivate()
|
2021-09-22 11:07:00 +03:00
|
|
|
self.assertUTC(self.field.default_timezone())
|
2017-09-20 13:15:15 +03:00
|
|
|
|
|
|
|
|
2018-02-16 18:47:49 +03:00
|
|
|
@pytest.mark.skipif(pytz is None, reason='pytz not installed')
|
|
|
|
@override_settings(TIME_ZONE='UTC', USE_TZ=True)
|
|
|
|
class TestCustomTimezoneForDateTimeField(TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
|
|
|
cls.kolkata = pytz.timezone('Asia/Kolkata')
|
|
|
|
cls.date_format = '%d/%m/%Y %H:%M'
|
|
|
|
|
|
|
|
def test_should_render_date_time_in_default_timezone(self):
|
|
|
|
field = serializers.DateTimeField(default_timezone=self.kolkata, format=self.date_format)
|
|
|
|
dt = datetime.datetime(2018, 2, 8, 14, 15, 16, tzinfo=pytz.utc)
|
|
|
|
|
|
|
|
with override(self.kolkata):
|
|
|
|
rendered_date = field.to_representation(dt)
|
|
|
|
|
|
|
|
rendered_date_in_timezone = dt.astimezone(self.kolkata).strftime(self.date_format)
|
|
|
|
|
|
|
|
assert rendered_date == rendered_date_in_timezone
|
|
|
|
|
|
|
|
|
2017-03-16 06:45:41 +03:00
|
|
|
class TestNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Invalid values for `DateTimeField` with datetime in DST shift (non-existing or ambiguous) and timezone with DST.
|
|
|
|
Timezone America/New_York has DST shift from 2017-03-12T02:00:00 to 2017-03-12T03:00:00 and
|
|
|
|
from 2017-11-05T02:00:00 to 2017-11-05T01:00:00 in 2017.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {
|
2017-03-21 01:47:25 +03:00
|
|
|
'2017-03-12T02:30:00': ['Invalid datetime for the timezone "America/New_York".'],
|
|
|
|
'2017-11-05T01:30:00': ['Invalid datetime for the timezone "America/New_York".']
|
2017-03-16 06:45:41 +03:00
|
|
|
}
|
|
|
|
outputs = {}
|
2017-03-22 07:01:07 +03:00
|
|
|
|
2021-04-16 19:27:22 +03:00
|
|
|
class MockTimezone(pytz.BaseTzInfo):
|
2017-03-22 07:01:07 +03:00
|
|
|
@staticmethod
|
|
|
|
def localize(value, is_dst):
|
2018-07-06 13:14:31 +03:00
|
|
|
raise pytz.InvalidTimeError()
|
2017-03-22 07:01:07 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'America/New_York'
|
|
|
|
|
|
|
|
field = serializers.DateTimeField(default_timezone=MockTimezone())
|
2017-03-16 06:45:41 +03:00
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
class TestTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `TimeField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'13:00': datetime.time(13, 00),
|
|
|
|
datetime.time(13, 00): datetime.time(13, 00),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2014-12-31 16:14:09 +03: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 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
2016-05-10 13:56:36 +03:00
|
|
|
datetime.time(13, 0): '13:00:00',
|
|
|
|
datetime.time(0, 0): '00:00:00',
|
2016-01-11 14:16:26 +03:00
|
|
|
'00:00:00': '00:00:00',
|
2015-12-14 02:09:56 +03:00
|
|
|
None: None,
|
|
|
|
'': None,
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.TimeField()
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomInputFormatTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `TimeField` with a custom input format.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'1:00pm': datetime.time(13, 00),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2014-12-31 16:14:09 +03:00
|
|
|
'13:00': ['Time has wrong format. Use one of these formats instead: hh:mm[AM|PM].'],
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.TimeField(input_formats=['%I:%M%p'])
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomOutputFormatTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `TimeField` with a custom output format.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
datetime.time(13, 00): '01:00PM'
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.TimeField(format='%I:%M%p')
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestNoOutputFormatTimeField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `TimeField` with a no output format.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {
|
|
|
|
datetime.time(13, 00): datetime.time(13, 00)
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.TimeField(format=None)
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2018-04-24 10:24:05 +03:00
|
|
|
class TestMinMaxDurationField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DurationField` with min and max limits.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'3 08:32:01.000123': datetime.timedelta(days=3, hours=8, minutes=32, seconds=1, microseconds=123),
|
|
|
|
86401: datetime.timedelta(days=1, seconds=1),
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
3600: ['Ensure this value is greater than or equal to 1 day, 0:00:00.'],
|
|
|
|
'4 08:32:01.000123': ['Ensure this value is less than or equal to 4 days, 0:00:00.'],
|
|
|
|
'3600': ['Ensure this value is greater than or equal to 1 day, 0:00:00.'],
|
|
|
|
}
|
|
|
|
outputs = {}
|
|
|
|
field = serializers.DurationField(min_value=datetime.timedelta(days=1), max_value=datetime.timedelta(days=4))
|
|
|
|
|
|
|
|
|
2015-06-01 19:20:53 +03:00
|
|
|
class TestDurationField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `DurationField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'13': datetime.timedelta(seconds=13),
|
|
|
|
'3 08:32:01.000123': datetime.timedelta(days=3, hours=8, minutes=32, seconds=1, microseconds=123),
|
|
|
|
'08:01': datetime.timedelta(minutes=8, seconds=1),
|
|
|
|
datetime.timedelta(days=3, hours=8, minutes=32, seconds=1, microseconds=123): datetime.timedelta(days=3, hours=8, minutes=32, seconds=1, microseconds=123),
|
2015-09-03 02:47:50 +03:00
|
|
|
3600: datetime.timedelta(hours=1),
|
2022-11-24 14:27:45 +03:00
|
|
|
'-999999999 00': datetime.timedelta(days=-999999999),
|
|
|
|
'999999999 00': datetime.timedelta(days=999999999),
|
2015-06-01 19:20:53 +03:00
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'abc': ['Duration has wrong format. Use one of these formats instead: [DD] [HH:[MM:]]ss[.uuuuuu].'],
|
|
|
|
'3 08:32 01.123': ['Duration has wrong format. Use one of these formats instead: [DD] [HH:[MM:]]ss[.uuuuuu].'],
|
2022-11-24 14:27:45 +03:00
|
|
|
'-1000000000 00': ['The number of days must be between -999999999 and 999999999.'],
|
|
|
|
'1000000000 00': ['The number of days must be between -999999999 and 999999999.'],
|
2015-06-01 19:20:53 +03:00
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
datetime.timedelta(days=3, hours=8, minutes=32, seconds=1, microseconds=123): '3 08:32:01.000123',
|
|
|
|
}
|
2016-02-18 22:35:45 +03:00
|
|
|
field = serializers.DurationField()
|
2015-06-01 19:20:53 +03:00
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
# Choice types...
|
|
|
|
|
|
|
|
class TestChoiceField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `ChoiceField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'poor': 'poor',
|
|
|
|
'medium': 'medium',
|
|
|
|
'good': 'good',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2015-01-08 20:16:47 +03:00
|
|
|
'amazing': ['"amazing" is not a valid choice.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
2014-11-19 12:31:26 +03:00
|
|
|
'good': 'good',
|
2015-05-15 11:59:10 +03:00
|
|
|
'': '',
|
|
|
|
'amazing': 'amazing',
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.ChoiceField(
|
2014-09-22 20:46:02 +04:00
|
|
|
choices=[
|
|
|
|
('poor', 'Poor quality'),
|
|
|
|
('medium', 'Medium quality'),
|
|
|
|
('good', 'Good quality'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2014-12-09 16:25:06 +03:00
|
|
|
def test_allow_blank(self):
|
|
|
|
"""
|
|
|
|
If `allow_blank=True` then '' is a valid input.
|
|
|
|
"""
|
|
|
|
field = serializers.ChoiceField(
|
|
|
|
allow_blank=True,
|
|
|
|
choices=[
|
|
|
|
('poor', 'Poor quality'),
|
|
|
|
('medium', 'Medium quality'),
|
|
|
|
('good', 'Good quality'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
output = field.run_validation('')
|
2014-12-15 14:55:17 +03:00
|
|
|
assert output == ''
|
2014-12-09 16:25:06 +03:00
|
|
|
|
2015-07-27 15:51:03 +03:00
|
|
|
def test_allow_null(self):
|
|
|
|
"""
|
|
|
|
If `allow_null=True` then '' on HTML forms is treated as None.
|
|
|
|
"""
|
|
|
|
field = serializers.ChoiceField(
|
|
|
|
allow_null=True,
|
|
|
|
choices=[
|
|
|
|
1, 2, 3
|
|
|
|
]
|
|
|
|
)
|
|
|
|
field.field_name = 'example'
|
|
|
|
value = field.get_value(QueryDict('example='))
|
|
|
|
assert value is None
|
|
|
|
output = field.run_validation(None)
|
|
|
|
assert output is None
|
|
|
|
|
2015-08-06 14:18:09 +03:00
|
|
|
def test_iter_options(self):
|
|
|
|
"""
|
|
|
|
iter_options() should return a list of options and option groups.
|
|
|
|
"""
|
|
|
|
field = serializers.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
('Numbers', ['integer', 'float']),
|
2015-08-06 14:30:26 +03:00
|
|
|
('Strings', ['text', 'email', 'url']),
|
|
|
|
'boolean'
|
2015-08-06 14:18:09 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
items = list(field.iter_options())
|
|
|
|
|
|
|
|
assert items[0].start_option_group
|
|
|
|
assert items[0].label == 'Numbers'
|
|
|
|
assert items[1].value == 'integer'
|
|
|
|
assert items[2].value == 'float'
|
|
|
|
assert items[3].end_option_group
|
|
|
|
|
|
|
|
assert items[4].start_option_group
|
|
|
|
assert items[4].label == 'Strings'
|
|
|
|
assert items[5].value == 'text'
|
|
|
|
assert items[6].value == 'email'
|
|
|
|
assert items[7].value == 'url'
|
|
|
|
assert items[8].end_option_group
|
|
|
|
|
2015-08-06 14:30:26 +03:00
|
|
|
assert items[9].value == 'boolean'
|
|
|
|
|
2017-09-20 12:33:50 +03:00
|
|
|
def test_edit_choices(self):
|
|
|
|
field = serializers.ChoiceField(
|
|
|
|
allow_null=True,
|
|
|
|
choices=[
|
|
|
|
1, 2,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
field.choices = [1]
|
2019-04-30 18:53:44 +03:00
|
|
|
assert field.run_validation(1) == 1
|
2017-09-20 12:33:50 +03:00
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.run_validation(2)
|
|
|
|
assert exc_info.value.detail == ['"2" is not a valid choice.']
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
class TestChoiceFieldWithType(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for a `Choice` field that uses an integer type,
|
|
|
|
instead of a char type.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'1': 1,
|
|
|
|
3: 3,
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2015-01-08 20:16:47 +03:00
|
|
|
5: ['"5" is not a valid choice.'],
|
|
|
|
'abc': ['"abc" is not a valid choice.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'1': 1,
|
|
|
|
1: 1
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.ChoiceField(
|
2014-09-22 20:46:02 +04:00
|
|
|
choices=[
|
|
|
|
(1, 'Poor quality'),
|
|
|
|
(2, 'Medium quality'),
|
|
|
|
(3, 'Good quality'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestChoiceFieldWithListChoices(FieldValues):
|
|
|
|
"""
|
|
|
|
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`).
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'poor': 'poor',
|
|
|
|
'medium': 'medium',
|
|
|
|
'good': 'good',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2015-01-08 20:16:47 +03:00
|
|
|
'awful': ['"awful" is not a valid choice.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'good': 'good'
|
|
|
|
}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.ChoiceField(choices=('poor', 'medium', 'good'))
|
2014-09-22 20:46:02 +04:00
|
|
|
|
|
|
|
|
2015-07-03 14:16:51 +03:00
|
|
|
class TestChoiceFieldWithGroupedChoices(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for a `Choice` field that uses a grouped list for the
|
|
|
|
choices, rather than a list of pairs of (`value`, `description`).
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'poor': 'poor',
|
|
|
|
'medium': 'medium',
|
|
|
|
'good': 'good',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'awful': ['"awful" is not a valid choice.']
|
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'good': 'good'
|
|
|
|
}
|
|
|
|
field = serializers.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
(
|
|
|
|
'Category',
|
|
|
|
(
|
|
|
|
('poor', 'Poor quality'),
|
|
|
|
('medium', 'Medium quality'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
('good', 'Good quality'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestChoiceFieldWithMixedChoices(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for a `Choice` field that uses a single paired or
|
|
|
|
grouped.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
'poor': 'poor',
|
|
|
|
'medium': 'medium',
|
|
|
|
'good': 'good',
|
|
|
|
}
|
|
|
|
invalid_inputs = {
|
|
|
|
'awful': ['"awful" is not a valid choice.']
|
|
|
|
}
|
|
|
|
outputs = {
|
|
|
|
'good': 'good'
|
|
|
|
}
|
|
|
|
field = serializers.ChoiceField(
|
|
|
|
choices=[
|
|
|
|
(
|
|
|
|
'Category',
|
|
|
|
(
|
|
|
|
('poor', 'Poor quality'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'medium',
|
|
|
|
('good', 'Good quality'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-22 20:46:02 +04:00
|
|
|
class TestMultipleChoiceField(FieldValues):
|
|
|
|
"""
|
|
|
|
Valid and invalid values for `MultipleChoiceField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
(): set(),
|
2017-11-06 12:03:01 +03:00
|
|
|
('aircon',): {'aircon'},
|
|
|
|
('aircon', 'manual'): {'aircon', 'manual'},
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
invalid_inputs = {
|
2015-01-08 20:16:47 +03:00
|
|
|
'abc': ['Expected a list of items but got type "str".'],
|
|
|
|
('aircon', 'incorrect'): ['"incorrect" is not a valid choice.']
|
2014-09-22 20:46:02 +04:00
|
|
|
}
|
|
|
|
outputs = [
|
2017-11-06 12:03:01 +03:00
|
|
|
(['aircon', 'manual', 'incorrect'], {'aircon', 'manual', 'incorrect'})
|
2014-09-22 20:46:02 +04:00
|
|
|
]
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.MultipleChoiceField(
|
2014-09-22 20:46:02 +04:00
|
|
|
choices=[
|
|
|
|
('aircon', 'AirCon'),
|
|
|
|
('manual', 'Manual drive'),
|
|
|
|
('diesel', 'Diesel'),
|
|
|
|
]
|
|
|
|
)
|
2014-09-25 15:09:12 +04:00
|
|
|
|
2015-06-01 18:13:12 +03:00
|
|
|
def test_against_partial_and_full_updates(self):
|
2015-06-01 18:04:05 +03:00
|
|
|
field = serializers.MultipleChoiceField(choices=(('a', 'a'), ('b', 'b')))
|
2015-06-01 18:13:12 +03:00
|
|
|
field.partial = False
|
2022-06-08 16:37:46 +03:00
|
|
|
assert field.get_value(QueryDict('')) == []
|
2015-06-01 18:13:12 +03:00
|
|
|
field.partial = True
|
2022-06-08 16:37:46 +03:00
|
|
|
assert field.get_value(QueryDict('')) == rest_framework.fields.empty
|
2015-06-01 18:04:05 +03:00
|
|
|
|
2014-09-25 15:09:12 +04:00
|
|
|
|
2015-07-16 15:51:15 +03:00
|
|
|
class TestEmptyMultipleChoiceField(FieldValues):
|
|
|
|
"""
|
|
|
|
Invalid values for `MultipleChoiceField(allow_empty=False)`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {
|
|
|
|
}
|
|
|
|
invalid_inputs = (
|
|
|
|
([], ['This selection may not be empty.']),
|
|
|
|
)
|
|
|
|
outputs = [
|
|
|
|
]
|
|
|
|
field = serializers.MultipleChoiceField(
|
|
|
|
choices=[
|
|
|
|
('consistency', 'Consistency'),
|
|
|
|
('availability', 'Availability'),
|
|
|
|
('partition', 'Partition tolerance'),
|
|
|
|
],
|
|
|
|
allow_empty=False
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-10-17 16:23:14 +04:00
|
|
|
# File serializers...
|
2014-09-26 20:06:20 +04:00
|
|
|
|
|
|
|
class MockFile:
|
|
|
|
def __init__(self, name='', size=0, url=''):
|
|
|
|
self.name = name
|
|
|
|
self.size = size
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return (
|
|
|
|
isinstance(other, MockFile) and
|
|
|
|
self.name == other.name and
|
|
|
|
self.size == other.size and
|
|
|
|
self.url == other.url
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestFileField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `FileField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
(MockFile(name='example', size=10), MockFile(name='example', size=10))
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
|
|
|
('invalid', ['The submitted data was not a file. Check the encoding type on the form.']),
|
|
|
|
(MockFile(name='example.txt', size=0), ['The submitted file is empty.']),
|
|
|
|
(MockFile(name='', size=10), ['No filename could be determined.']),
|
|
|
|
(MockFile(name='x' * 100, size=10), ['Ensure this filename has at most 10 characters (it has 100).'])
|
|
|
|
]
|
|
|
|
outputs = [
|
2014-10-08 19:59:52 +04:00
|
|
|
(MockFile(name='example.txt', url='/example.txt'), '/example.txt'),
|
|
|
|
('', None)
|
2014-09-26 20:06:20 +04:00
|
|
|
]
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.FileField(max_length=10)
|
2014-09-26 20:06:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestFieldFieldWithName(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `FileField` with a filename output instead of URLs.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = [
|
|
|
|
(MockFile(name='example.txt', url='/example.txt'), 'example.txt')
|
|
|
|
]
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.FileField(use_url=False)
|
2014-09-26 20:06:20 +04:00
|
|
|
|
|
|
|
|
2017-11-06 13:14:37 +03:00
|
|
|
def ext_validator(value):
|
|
|
|
if not value.name.endswith('.png'):
|
|
|
|
raise serializers.ValidationError('File extension is not allowed. Allowed extensions is png.')
|
|
|
|
|
|
|
|
|
2014-09-26 20:06:20 +04:00
|
|
|
# Stub out mock Django `forms.ImageField` class so we don't *actually*
|
|
|
|
# call into it's regular validation, or require PIL for testing.
|
2017-11-06 13:14:37 +03:00
|
|
|
class PassImageValidation(DjangoImageField):
|
|
|
|
default_validators = [ext_validator]
|
|
|
|
|
2014-09-26 20:06:20 +04:00
|
|
|
def to_python(self, value):
|
2017-11-06 13:14:37 +03:00
|
|
|
return value
|
2014-09-26 20:06:20 +04:00
|
|
|
|
|
|
|
|
2017-11-06 13:14:37 +03:00
|
|
|
class FailImageValidation(PassImageValidation):
|
2014-09-26 20:06:20 +04:00
|
|
|
def to_python(self, value):
|
2017-11-06 13:14:37 +03:00
|
|
|
if value.name == 'badimage.png':
|
|
|
|
raise serializers.ValidationError(self.error_messages['invalid_image'])
|
2014-09-26 20:06:20 +04:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
class TestInvalidImageField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for an invalid `ImageField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = [
|
2017-11-06 13:14:37 +03:00
|
|
|
(MockFile(name='badimage.png', size=10), ['Upload a valid image. The file you uploaded was either not an image or a corrupted image.']),
|
|
|
|
(MockFile(name='goodimage.html', size=10), ['File extension is not allowed. Allowed extensions is png.'])
|
2014-09-26 20:06:20 +04:00
|
|
|
]
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.ImageField(_DjangoImageField=FailImageValidation)
|
2014-09-26 20:06:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestValidImageField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for an valid `ImageField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
2017-11-06 13:14:37 +03:00
|
|
|
(MockFile(name='example.png', size=10), MockFile(name='example.png', size=10))
|
2014-09-26 20:06:20 +04:00
|
|
|
]
|
|
|
|
invalid_inputs = {}
|
|
|
|
outputs = {}
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.ImageField(_DjangoImageField=PassImageValidation)
|
2014-09-26 20:06:20 +04:00
|
|
|
|
|
|
|
|
2014-10-17 16:23:14 +04:00
|
|
|
# Composite serializers...
|
2014-09-26 20:06:20 +04:00
|
|
|
|
2014-09-26 16:08:20 +04:00
|
|
|
class TestListField(FieldValues):
|
|
|
|
"""
|
2015-01-23 19:27:23 +03:00
|
|
|
Values for `ListField` with IntegerField as child.
|
2014-09-26 16:08:20 +04:00
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
([1, 2, 3], [1, 2, 3]),
|
2015-07-16 15:51:15 +03:00
|
|
|
(['1', '2', '3'], [1, 2, 3]),
|
|
|
|
([], [])
|
2014-09-26 16:08:20 +04:00
|
|
|
]
|
|
|
|
invalid_inputs = [
|
2015-01-08 20:16:47 +03:00
|
|
|
('not a list', ['Expected a list of items but got type "str".']),
|
2018-01-02 12:45:59 +03:00
|
|
|
([1, 2, 'error', 'error'], {2: ['A valid integer is required.'], 3: ['A valid integer is required.']}),
|
2015-11-13 18:17:09 +03:00
|
|
|
({'one': 'two'}, ['Expected a list of items but got type "dict".'])
|
2014-09-26 16:08:20 +04:00
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
([1, 2, 3], [1, 2, 3]),
|
|
|
|
(['1', '2', '3'], [1, 2, 3])
|
|
|
|
]
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.ListField(child=serializers.IntegerField())
|
2014-09-26 16:08:20 +04:00
|
|
|
|
2015-08-19 18:38:31 +03:00
|
|
|
def test_no_source_on_child(self):
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
|
|
|
serializers.ListField(child=serializers.IntegerField(source='other'))
|
|
|
|
|
|
|
|
assert str(exc_info.value) == (
|
|
|
|
"The `source` argument is not meaningful when applied to a `child=` field. "
|
|
|
|
"Remove `source=` from the field declaration."
|
|
|
|
)
|
|
|
|
|
2015-11-13 18:17:09 +03:00
|
|
|
def test_collection_types_are_invalid_input(self):
|
|
|
|
field = serializers.ListField(child=serializers.CharField())
|
|
|
|
input_value = ({'one': 'two'})
|
|
|
|
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.to_internal_value(input_value)
|
2015-11-13 18:41:57 +03:00
|
|
|
assert exc_info.value.detail == ['Expected a list of items but got type "dict".']
|
2015-11-13 18:17:09 +03:00
|
|
|
|
2021-08-06 12:14:52 +03:00
|
|
|
def test_constructor_misuse_raises(self):
|
|
|
|
# Test that `ListField` can only be instantiated with keyword arguments
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
serializers.ListField(serializers.CharField())
|
|
|
|
|
2014-09-26 16:08:20 +04:00
|
|
|
|
2018-01-02 12:45:59 +03:00
|
|
|
class TestNestedListField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for nested `ListField` with IntegerField as child.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
([[1, 2], [3]], [[1, 2], [3]]),
|
|
|
|
([[]], [[]])
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
|
|
|
(['not a list'], {0: ['Expected a list of items but got type "str".']}),
|
|
|
|
([[1, 2, 'error'], ['error']], {0: {2: ['A valid integer is required.']}, 1: {0: ['A valid integer is required.']}}),
|
|
|
|
([{'one': 'two'}], {0: ['Expected a list of items but got type "dict".']})
|
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
([[1, 2], [3]], [[1, 2], [3]]),
|
|
|
|
]
|
|
|
|
field = serializers.ListField(child=serializers.ListField(child=serializers.IntegerField()))
|
|
|
|
|
|
|
|
|
2015-07-16 15:51:15 +03:00
|
|
|
class TestEmptyListField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `ListField` with allow_empty=False flag.
|
|
|
|
"""
|
|
|
|
valid_inputs = {}
|
|
|
|
invalid_inputs = [
|
|
|
|
([], ['This list may not be empty.'])
|
|
|
|
]
|
|
|
|
outputs = {}
|
|
|
|
field = serializers.ListField(child=serializers.IntegerField(), allow_empty=False)
|
|
|
|
|
|
|
|
|
2017-02-06 12:36:03 +03:00
|
|
|
class TestListFieldLengthLimit(FieldValues):
|
|
|
|
valid_inputs = ()
|
|
|
|
invalid_inputs = [
|
|
|
|
((0, 1), ['Ensure this field has at least 3 elements.']),
|
|
|
|
((0, 1, 2, 3, 4, 5), ['Ensure this field has no more than 4 elements.']),
|
|
|
|
]
|
|
|
|
outputs = ()
|
|
|
|
field = serializers.ListField(child=serializers.IntegerField(), min_length=3, max_length=4)
|
|
|
|
|
|
|
|
|
2015-01-23 19:27:23 +03:00
|
|
|
class TestUnvalidatedListField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `ListField` with no `child` argument.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
([1, '2', True, [4, 5, 6]], [1, '2', True, [4, 5, 6]]),
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
2015-01-30 17:00:25 +03:00
|
|
|
('not a list', ['Expected a list of items but got type "str".']),
|
2015-01-23 19:27:23 +03:00
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
([1, '2', True, [4, 5, 6]], [1, '2', True, [4, 5, 6]]),
|
|
|
|
]
|
|
|
|
field = serializers.ListField()
|
|
|
|
|
|
|
|
|
|
|
|
class TestDictField(FieldValues):
|
|
|
|
"""
|
2018-01-02 12:45:59 +03:00
|
|
|
Values for `DictField` with CharField as child.
|
2015-01-23 19:27:23 +03:00
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
({'a': 1, 'b': '2', 3: 3}, {'a': '1', 'b': '2', '3': '3'}),
|
2019-05-09 11:18:20 +03:00
|
|
|
({}, {}),
|
2015-01-23 19:27:23 +03:00
|
|
|
]
|
|
|
|
invalid_inputs = [
|
2018-01-02 12:45:59 +03:00
|
|
|
({'a': 1, 'b': None, 'c': None}, {'b': ['This field may not be null.'], 'c': ['This field may not be null.']}),
|
2015-01-30 17:00:25 +03:00
|
|
|
('not a dict', ['Expected a dictionary of items but got type "str".']),
|
2015-01-23 19:27:23 +03:00
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
({'a': 1, 'b': '2', 3: 3}, {'a': '1', 'b': '2', '3': '3'}),
|
|
|
|
]
|
|
|
|
field = serializers.DictField(child=serializers.CharField())
|
|
|
|
|
2015-08-19 18:38:31 +03:00
|
|
|
def test_no_source_on_child(self):
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
|
|
|
serializers.DictField(child=serializers.CharField(source='other'))
|
|
|
|
|
|
|
|
assert str(exc_info.value) == (
|
|
|
|
"The `source` argument is not meaningful when applied to a `child=` field. "
|
|
|
|
"Remove `source=` from the field declaration."
|
|
|
|
)
|
|
|
|
|
2016-08-02 16:14:36 +03:00
|
|
|
def test_allow_null(self):
|
|
|
|
"""
|
|
|
|
If `allow_null=True` then `None` is a valid input.
|
|
|
|
"""
|
|
|
|
field = serializers.DictField(allow_null=True)
|
|
|
|
output = field.run_validation(None)
|
|
|
|
assert output is None
|
|
|
|
|
2019-05-09 11:18:20 +03:00
|
|
|
def test_allow_empty_disallowed(self):
|
|
|
|
"""
|
|
|
|
If allow_empty is False then an empty dict is not a valid input.
|
|
|
|
"""
|
|
|
|
field = serializers.DictField(allow_empty=False)
|
|
|
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
|
|
|
field.run_validation({})
|
|
|
|
|
|
|
|
assert exc_info.value.detail == ['This dictionary may not be empty.']
|
|
|
|
|
2016-08-02 16:14:36 +03:00
|
|
|
|
2018-01-02 12:45:59 +03:00
|
|
|
class TestNestedDictField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for nested `DictField` with CharField as child.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
({0: {'a': 1, 'b': '2'}, 1: {3: 3}}, {'0': {'a': '1', 'b': '2'}, '1': {'3': '3'}}),
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
|
|
|
({0: {'a': 1, 'b': None}, 1: {'c': None}}, {'0': {'b': ['This field may not be null.']}, '1': {'c': ['This field may not be null.']}}),
|
|
|
|
({0: 'not a dict'}, {'0': ['Expected a dictionary of items but got type "str".']}),
|
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
({0: {'a': 1, 'b': '2'}, 1: {3: 3}}, {'0': {'a': '1', 'b': '2'}, '1': {'3': '3'}}),
|
|
|
|
]
|
|
|
|
field = serializers.DictField(child=serializers.DictField(child=serializers.CharField()))
|
|
|
|
|
|
|
|
|
2016-08-02 16:14:36 +03:00
|
|
|
class TestDictFieldWithNullChild(FieldValues):
|
|
|
|
"""
|
2018-01-02 12:45:59 +03:00
|
|
|
Values for `DictField` with allow_null CharField as child.
|
2016-08-02 16:14:36 +03:00
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
({'a': None, 'b': '2', 3: 3}, {'a': None, 'b': '2', '3': '3'}),
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
({'a': None, 'b': '2', 3: 3}, {'a': None, 'b': '2', '3': '3'}),
|
|
|
|
]
|
|
|
|
field = serializers.DictField(child=serializers.CharField(allow_null=True))
|
|
|
|
|
2015-01-23 19:27:23 +03:00
|
|
|
|
|
|
|
class TestUnvalidatedDictField(FieldValues):
|
|
|
|
"""
|
2018-01-02 12:45:59 +03:00
|
|
|
Values for `DictField` with no `child` argument.
|
2015-01-23 19:27:23 +03:00
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
({'a': 1, 'b': [4, 5, 6], 1: 123}, {'a': 1, 'b': [4, 5, 6], '1': 123}),
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
2015-01-30 17:00:25 +03:00
|
|
|
('not a dict', ['Expected a dictionary of items but got type "str".']),
|
2015-01-23 19:27:23 +03:00
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
({'a': 1, 'b': [4, 5, 6]}, {'a': 1, 'b': [4, 5, 6]}),
|
|
|
|
]
|
|
|
|
field = serializers.DictField()
|
|
|
|
|
|
|
|
|
2018-01-15 17:52:30 +03:00
|
|
|
class TestHStoreField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `ListField` with CharField as child.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
({'a': 1, 'b': '2', 3: 3}, {'a': '1', 'b': '2', '3': '3'}),
|
|
|
|
({'a': 1, 'b': None}, {'a': '1', 'b': None}),
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
|
|
|
('not a dict', ['Expected a dictionary of items but got type "str".']),
|
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
({'a': 1, 'b': '2', 3: 3}, {'a': '1', 'b': '2', '3': '3'}),
|
|
|
|
]
|
|
|
|
field = serializers.HStoreField()
|
|
|
|
|
|
|
|
def test_child_is_charfield(self):
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
|
|
|
serializers.HStoreField(child=serializers.IntegerField())
|
|
|
|
|
|
|
|
assert str(exc_info.value) == (
|
|
|
|
"The `child` argument must be an instance of `CharField`, "
|
|
|
|
"as the hstore extension stores values as strings."
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_no_source_on_child(self):
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
|
|
|
serializers.HStoreField(child=serializers.CharField(source='other'))
|
|
|
|
|
|
|
|
assert str(exc_info.value) == (
|
|
|
|
"The `source` argument is not meaningful when applied to a `child=` field. "
|
|
|
|
"Remove `source=` from the field declaration."
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_allow_null(self):
|
|
|
|
"""
|
|
|
|
If `allow_null=True` then `None` is a valid input.
|
|
|
|
"""
|
|
|
|
field = serializers.HStoreField(allow_null=True)
|
|
|
|
output = field.run_validation(None)
|
|
|
|
assert output is None
|
|
|
|
|
|
|
|
|
2015-09-28 19:25:52 +03:00
|
|
|
class TestJSONField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `JSONField`.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
|
|
|
({
|
|
|
|
'a': 1,
|
|
|
|
'b': ['some', 'list', True, 1.23],
|
|
|
|
'3': None
|
|
|
|
}, {
|
|
|
|
'a': 1,
|
|
|
|
'b': ['some', 'list', True, 1.23],
|
|
|
|
'3': None
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
|
|
|
({'a': set()}, ['Value must be valid JSON.']),
|
2017-07-07 19:46:17 +03:00
|
|
|
({'a': float('inf')}, ['Value must be valid JSON.']),
|
2015-09-28 19:25:52 +03:00
|
|
|
]
|
|
|
|
outputs = [
|
|
|
|
({
|
|
|
|
'a': 1,
|
|
|
|
'b': ['some', 'list', True, 1.23],
|
|
|
|
'3': 3
|
|
|
|
}, {
|
|
|
|
'a': 1,
|
|
|
|
'b': ['some', 'list', True, 1.23],
|
|
|
|
'3': 3
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
field = serializers.JSONField()
|
|
|
|
|
2016-10-12 16:04:10 +03:00
|
|
|
def test_html_input_as_json_string(self):
|
|
|
|
"""
|
|
|
|
HTML inputs should be treated as a serialized JSON string.
|
|
|
|
"""
|
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
config = serializers.JSONField()
|
|
|
|
|
|
|
|
data = QueryDict(mutable=True)
|
|
|
|
data.update({'config': '{"a":1}'})
|
|
|
|
serializer = TestSerializer(data=data)
|
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == {'config': {"a": 1}}
|
|
|
|
|
2015-09-28 19:25:52 +03:00
|
|
|
|
|
|
|
class TestBinaryJSONField(FieldValues):
|
|
|
|
"""
|
|
|
|
Values for `JSONField` with binary=True.
|
|
|
|
"""
|
|
|
|
valid_inputs = [
|
2015-09-28 19:32:36 +03:00
|
|
|
(b'{"a": 1, "3": null, "b": ["some", "list", true, 1.23]}', {
|
2015-09-28 19:25:52 +03:00
|
|
|
'a': 1,
|
|
|
|
'b': ['some', 'list', True, 1.23],
|
|
|
|
'3': None
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
invalid_inputs = [
|
|
|
|
('{"a": "unterminated string}', ['Value must be valid JSON.']),
|
|
|
|
]
|
|
|
|
outputs = [
|
2015-09-28 19:41:09 +03:00
|
|
|
(['some', 'list', True, 1.23], b'["some", "list", true, 1.23]'),
|
2015-09-28 19:25:52 +03:00
|
|
|
]
|
|
|
|
field = serializers.JSONField(binary=True)
|
|
|
|
|
|
|
|
|
2019-07-02 05:14:45 +03:00
|
|
|
# Tests for FileField.
|
|
|
|
# --------------------
|
2014-09-29 17:12:26 +04:00
|
|
|
|
|
|
|
class MockRequest:
|
|
|
|
def build_absolute_uri(self, value):
|
|
|
|
return 'http://example.com' + value
|
|
|
|
|
|
|
|
|
|
|
|
class TestFileFieldContext:
|
|
|
|
def test_fully_qualified_when_request_in_context(self):
|
2014-10-17 16:23:14 +04:00
|
|
|
field = serializers.FileField(max_length=10)
|
2014-09-29 17:12:26 +04:00
|
|
|
field._context = {'request': MockRequest()}
|
|
|
|
obj = MockFile(name='example.txt', url='/example.txt')
|
|
|
|
value = field.to_representation(obj)
|
|
|
|
assert value == 'http://example.com/example.txt'
|
|
|
|
|
|
|
|
|
2014-09-25 15:09:12 +04:00
|
|
|
# Tests for SerializerMethodField.
|
|
|
|
# --------------------------------
|
|
|
|
|
|
|
|
class TestSerializerMethodField:
|
|
|
|
def test_serializer_method_field(self):
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
example_field = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
def get_example_field(self, obj):
|
|
|
|
return 'ran get_example_field(%d)' % obj['example_field']
|
|
|
|
|
|
|
|
serializer = ExampleSerializer({'example_field': 123})
|
|
|
|
assert serializer.data == {
|
|
|
|
'example_field': 'ran get_example_field(123)'
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_redundant_method_name(self):
|
2019-07-01 15:22:04 +03:00
|
|
|
# Prior to v3.10, redundant method names were not allowed.
|
|
|
|
# This restriction has since been removed.
|
2014-09-25 15:09:12 +04:00
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
example_field = serializers.SerializerMethodField('get_example_field')
|
|
|
|
|
2019-07-01 15:22:04 +03:00
|
|
|
field = ExampleSerializer().fields['example_field']
|
|
|
|
assert field.method_name == 'get_example_field'
|
2018-07-06 11:44:58 +03:00
|
|
|
|
|
|
|
|
2019-07-02 05:14:45 +03:00
|
|
|
# Tests for ModelField.
|
|
|
|
# ---------------------
|
|
|
|
|
|
|
|
class TestModelField:
|
|
|
|
def test_max_length_init(self):
|
|
|
|
field = serializers.ModelField(None)
|
|
|
|
assert len(field.validators) == 0
|
|
|
|
|
|
|
|
field = serializers.ModelField(None, max_length=10)
|
|
|
|
assert len(field.validators) == 1
|
|
|
|
|
|
|
|
|
|
|
|
# Tests for validation errors
|
|
|
|
# ---------------------------
|
|
|
|
|
2018-07-06 11:44:58 +03:00
|
|
|
class TestValidationErrorCode:
|
|
|
|
@pytest.mark.parametrize('use_list', (False, True))
|
|
|
|
def test_validationerror_code_with_msg(self, use_list):
|
|
|
|
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
password = serializers.CharField()
|
|
|
|
|
|
|
|
def validate_password(self, obj):
|
2019-07-01 15:28:16 +03:00
|
|
|
err = DjangoValidationError(
|
|
|
|
'exc_msg %s', code='exc_code', params=('exc_param',),
|
|
|
|
)
|
2018-07-06 11:44:58 +03:00
|
|
|
if use_list:
|
|
|
|
err = DjangoValidationError([err])
|
|
|
|
raise err
|
|
|
|
|
|
|
|
serializer = ExampleSerializer(data={'password': 123})
|
|
|
|
serializer.is_valid()
|
2019-07-01 15:28:16 +03:00
|
|
|
assert serializer.errors == {'password': ['exc_msg exc_param']}
|
|
|
|
assert serializer.errors['password'][0].code == 'exc_code'
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('use_list', (False, True))
|
|
|
|
def test_validationerror_code_with_msg_including_percent(self, use_list):
|
|
|
|
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
password = serializers.CharField()
|
|
|
|
|
|
|
|
def validate_password(self, obj):
|
|
|
|
err = DjangoValidationError('exc_msg with %', code='exc_code')
|
|
|
|
if use_list:
|
|
|
|
err = DjangoValidationError([err])
|
|
|
|
raise err
|
|
|
|
|
|
|
|
serializer = ExampleSerializer(data={'password': 123})
|
|
|
|
serializer.is_valid()
|
|
|
|
assert serializer.errors == {'password': ['exc_msg with %']}
|
2018-07-06 11:44:58 +03:00
|
|
|
assert serializer.errors['password'][0].code == 'exc_code'
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('code', (None, 'exc_code',))
|
|
|
|
@pytest.mark.parametrize('use_list', (False, True))
|
|
|
|
def test_validationerror_code_with_dict(self, use_list, code):
|
|
|
|
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
|
|
|
|
def validate(self, obj):
|
|
|
|
if code is None:
|
|
|
|
err = DjangoValidationError({
|
|
|
|
'email': 'email error',
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
err = DjangoValidationError({
|
|
|
|
'email': DjangoValidationError(
|
|
|
|
'email error',
|
|
|
|
code=code),
|
|
|
|
})
|
|
|
|
if use_list:
|
|
|
|
err = DjangoValidationError([err])
|
|
|
|
raise err
|
|
|
|
|
|
|
|
serializer = ExampleSerializer(data={})
|
|
|
|
serializer.is_valid()
|
|
|
|
expected_code = code if code else 'invalid'
|
|
|
|
if use_list:
|
|
|
|
assert serializer.errors == {
|
|
|
|
'non_field_errors': [
|
|
|
|
exceptions.ErrorDetail(
|
|
|
|
string='email error',
|
|
|
|
code=expected_code
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
assert serializer.errors == {
|
|
|
|
'email': ['email error'],
|
|
|
|
}
|
|
|
|
assert serializer.errors['email'][0].code == expected_code
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('code', (None, 'exc_code',))
|
|
|
|
def test_validationerror_code_with_dict_list_same_code(self, code):
|
|
|
|
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
|
|
|
|
def validate(self, obj):
|
|
|
|
if code is None:
|
|
|
|
raise DjangoValidationError({'email': ['email error 1',
|
|
|
|
'email error 2']})
|
|
|
|
raise DjangoValidationError({'email': [
|
|
|
|
DjangoValidationError('email error 1', code=code),
|
|
|
|
DjangoValidationError('email error 2', code=code),
|
|
|
|
]})
|
|
|
|
|
|
|
|
serializer = ExampleSerializer(data={})
|
|
|
|
serializer.is_valid()
|
|
|
|
expected_code = code if code else 'invalid'
|
|
|
|
assert serializer.errors == {
|
|
|
|
'email': [
|
|
|
|
exceptions.ErrorDetail(
|
|
|
|
string='email error 1',
|
|
|
|
code=expected_code
|
|
|
|
),
|
|
|
|
exceptions.ErrorDetail(
|
|
|
|
string='email error 2',
|
|
|
|
code=expected_code
|
|
|
|
),
|
|
|
|
]
|
|
|
|
}
|