2013-01-12 13:32:53 +04:00
|
|
|
"""
|
2013-01-15 14:51:10 +04:00
|
|
|
General serializer field tests.
|
2013-01-12 13:32:53 +04:00
|
|
|
"""
|
2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2013-05-18 14:27:48 +04:00
|
|
|
from django.utils.datastructures import SortedDict
|
2013-02-15 00:19:51 +04:00
|
|
|
import datetime
|
2013-05-18 20:27:53 +04:00
|
|
|
from rest_framework.fields import (humanize_field, humanize_field_type,
|
|
|
|
humanize_form_fields)
|
2013-05-18 20:10:17 +04:00
|
|
|
from django import forms
|
2013-04-15 14:40:18 +04:00
|
|
|
from decimal import Decimal
|
2013-01-12 13:32:53 +04:00
|
|
|
from django.db import models
|
|
|
|
from django.test import TestCase
|
2013-02-15 00:19:51 +04:00
|
|
|
from django.core import validators
|
2013-01-12 13:32:53 +04:00
|
|
|
from rest_framework import serializers
|
2013-04-15 14:40:18 +04:00
|
|
|
from rest_framework.serializers import Serializer
|
2013-05-18 17:48:36 +04:00
|
|
|
from rest_framework.tests.models import RESTFrameworkModel
|
2013-05-18 20:10:17 +04:00
|
|
|
from rest_framework.fields import Field
|
|
|
|
from collections import namedtuple
|
|
|
|
from uuid import uuid4
|
2013-01-12 13:32:53 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TimestampedModel(models.Model):
|
|
|
|
added = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
|
2013-01-12 13:43:07 +04:00
|
|
|
class CharPrimaryKeyModel(models.Model):
|
|
|
|
id = models.CharField(max_length=20, primary_key=True)
|
|
|
|
|
|
|
|
|
2013-01-12 13:32:53 +04:00
|
|
|
class TimestampedModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = TimestampedModel
|
|
|
|
|
|
|
|
|
2013-01-12 13:43:07 +04:00
|
|
|
class CharPrimaryKeyModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = CharPrimaryKeyModel
|
|
|
|
|
|
|
|
|
2013-02-15 00:19:51 +04:00
|
|
|
class TimeFieldModel(models.Model):
|
|
|
|
clock = models.TimeField()
|
|
|
|
|
|
|
|
|
|
|
|
class TimeFieldModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = TimeFieldModel
|
|
|
|
|
|
|
|
|
|
|
|
class BasicFieldTests(TestCase):
|
2013-01-12 13:32:53 +04:00
|
|
|
def test_auto_now_fields_read_only(self):
|
|
|
|
"""
|
2013-01-15 14:51:10 +04:00
|
|
|
auto_now and auto_now_add fields should be read_only by default.
|
2013-01-12 13:32:53 +04:00
|
|
|
"""
|
|
|
|
serializer = TimestampedModelSerializer()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.fields['added'].read_only, True)
|
2013-01-12 13:43:07 +04:00
|
|
|
|
|
|
|
def test_auto_pk_fields_read_only(self):
|
2013-01-15 14:51:10 +04:00
|
|
|
"""
|
|
|
|
AutoField fields should be read_only by default.
|
|
|
|
"""
|
2013-01-12 13:43:07 +04:00
|
|
|
serializer = TimestampedModelSerializer()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.fields['id'].read_only, True)
|
2013-01-12 13:43:07 +04:00
|
|
|
|
|
|
|
def test_non_auto_pk_fields_not_read_only(self):
|
2013-01-15 14:51:10 +04:00
|
|
|
"""
|
|
|
|
PK fields other than AutoField fields should not be read_only by default.
|
|
|
|
"""
|
2013-01-12 13:43:07 +04:00
|
|
|
serializer = CharPrimaryKeyModelSerializer()
|
2013-03-06 16:19:39 +04:00
|
|
|
self.assertEqual(serializer.fields['id'].read_only, False)
|
2013-02-15 00:19:51 +04:00
|
|
|
|
2013-05-18 14:27:48 +04:00
|
|
|
def test_dict_field_ordering(self):
|
|
|
|
"""
|
|
|
|
Field should preserve dictionary ordering, if it exists.
|
|
|
|
See: https://github.com/tomchristie/django-rest-framework/issues/832
|
|
|
|
"""
|
|
|
|
ret = SortedDict()
|
|
|
|
ret['c'] = 1
|
|
|
|
ret['b'] = 1
|
|
|
|
ret['a'] = 1
|
|
|
|
ret['z'] = 1
|
|
|
|
field = serializers.Field()
|
|
|
|
keys = list(field.to_native(ret).keys())
|
|
|
|
self.assertEqual(keys, ['c', 'b', 'a', 'z'])
|
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
|
|
|
|
class DateFieldTest(TestCase):
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
Tests for the DateFieldTest from_native() and to_native() behavior
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_string(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts default iso input formats.
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
f = serializers.DateField()
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.from_native('1984-07-31')
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual(datetime.date(1984, 7, 31), result_1)
|
2013-02-26 14:09:54 +04:00
|
|
|
|
|
|
|
def test_from_native_datetime_date(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts a datetime.date instance.
|
|
|
|
"""
|
|
|
|
f = serializers.DateField()
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.from_native(datetime.date(1984, 7, 31))
|
|
|
|
|
|
|
|
self.assertEqual(result_1, datetime.date(1984, 7, 31))
|
|
|
|
|
|
|
|
def test_from_native_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts custom input formats.
|
|
|
|
"""
|
|
|
|
f = serializers.DateField(input_formats=['%Y -- %d'])
|
|
|
|
result = f.from_native('1984 -- 31')
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual(datetime.date(1984, 1, 31), result)
|
|
|
|
|
|
|
|
def test_from_native_invalid_default_on_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() don't accept default formats if custom format is preset
|
|
|
|
"""
|
|
|
|
f = serializers.DateField(input_formats=['%Y -- %d'])
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.from_native('1984-07-31')
|
|
|
|
except validators.ValidationError as e:
|
|
|
|
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY -- DD"])
|
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
2013-02-26 14:09:54 +04:00
|
|
|
|
|
|
|
def test_from_native_empty(self):
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on empty param.
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
f = serializers.DateField()
|
|
|
|
result = f.from_native('')
|
|
|
|
|
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
2013-03-01 20:04:30 +04:00
|
|
|
def test_from_native_none(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on None param.
|
|
|
|
"""
|
|
|
|
f = serializers.DateField()
|
|
|
|
result = f.from_native(None)
|
|
|
|
|
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
def test_from_native_invalid_date(self):
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
Make sure from_native() raises a ValidationError on passing an invalid date.
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
f = serializers.DateField()
|
|
|
|
|
|
|
|
try:
|
2013-03-01 16:16:00 +04:00
|
|
|
f.from_native('1984-13-31')
|
2013-02-26 14:09:54 +04:00
|
|
|
except validators.ValidationError as e:
|
2013-03-06 16:19:39 +04:00
|
|
|
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
|
2013-02-26 14:09:54 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_invalid_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() raises a ValidationError on passing an invalid format.
|
|
|
|
"""
|
|
|
|
f = serializers.DateField()
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
try:
|
|
|
|
f.from_native('1984 -- 31')
|
|
|
|
except validators.ValidationError as e:
|
2013-03-06 16:19:39 +04:00
|
|
|
self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"])
|
2013-03-01 16:16:00 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_to_native(self):
|
|
|
|
"""
|
2013-03-21 12:41:54 +04:00
|
|
|
Make sure to_native() returns datetime as default.
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
f = serializers.DateField()
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.to_native(datetime.date(1984, 7, 31))
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-21 12:41:54 +04:00
|
|
|
self.assertEqual(datetime.date(1984, 7, 31), result_1)
|
|
|
|
|
|
|
|
def test_to_native_iso(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() with 'iso-8601' returns iso formated date.
|
|
|
|
"""
|
|
|
|
f = serializers.DateField(format='iso-8601')
|
|
|
|
|
|
|
|
result_1 = f.to_native(datetime.date(1984, 7, 31))
|
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual('1984-07-31', result_1)
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_to_native_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() returns correct custom format.
|
|
|
|
"""
|
2013-03-06 16:19:39 +04:00
|
|
|
f = serializers.DateField(format="%Y - %m.%d")
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.to_native(datetime.date(1984, 7, 31))
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual('1984 - 07.31', result_1)
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-07 03:15:19 +04:00
|
|
|
def test_to_native_none(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on None param.
|
|
|
|
"""
|
|
|
|
f = serializers.DateField(required=False)
|
|
|
|
self.assertEqual(None, f.to_native(None))
|
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
class DateTimeFieldTest(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the DateTimeField from_native() and to_native() behavior
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_string(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts default iso input formats.
|
|
|
|
"""
|
|
|
|
f = serializers.DateTimeField()
|
2013-03-01 19:24:25 +04:00
|
|
|
result_1 = f.from_native('1984-07-31 04:31')
|
|
|
|
result_2 = f.from_native('1984-07-31 04:31:59')
|
|
|
|
result_3 = f.from_native('1984-07-31 04:31:59.000200')
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 19:24:25 +04:00
|
|
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_1)
|
|
|
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_2)
|
|
|
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_3)
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_datetime_datetime(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts a datetime.datetime instance.
|
|
|
|
"""
|
|
|
|
f = serializers.DateTimeField()
|
2013-03-01 19:24:25 +04:00
|
|
|
result_1 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
result_2 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
result_3 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 19:24:25 +04:00
|
|
|
self.assertEqual(result_1, datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
self.assertEqual(result_2, datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
self.assertEqual(result_3, datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts custom input formats.
|
|
|
|
"""
|
|
|
|
f = serializers.DateTimeField(input_formats=['%Y -- %H:%M'])
|
|
|
|
result = f.from_native('1984 -- 04:59')
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual(datetime.datetime(1984, 1, 1, 4, 59), result)
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_invalid_default_on_custom_format(self):
|
2013-02-26 14:09:54 +04:00
|
|
|
"""
|
2013-03-01 16:16:00 +04:00
|
|
|
Make sure from_native() don't accept default formats if custom format is preset
|
2013-02-26 14:09:54 +04:00
|
|
|
"""
|
2013-03-01 16:16:00 +04:00
|
|
|
f = serializers.DateTimeField(input_formats=['%Y -- %H:%M'])
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
try:
|
|
|
|
f.from_native('1984-07-31 04:31:59')
|
|
|
|
except validators.ValidationError as e:
|
2013-03-06 16:19:39 +04:00
|
|
|
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: YYYY -- hh:mm"])
|
2013-03-01 16:16:00 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
2013-02-26 14:09:54 +04:00
|
|
|
|
|
|
|
def test_from_native_empty(self):
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on empty param.
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
f = serializers.DateTimeField()
|
|
|
|
result = f.from_native('')
|
|
|
|
|
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
2013-03-01 20:04:30 +04:00
|
|
|
def test_from_native_none(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on None param.
|
|
|
|
"""
|
|
|
|
f = serializers.DateTimeField()
|
|
|
|
result = f.from_native(None)
|
|
|
|
|
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_invalid_datetime(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() raises a ValidationError on passing an invalid datetime.
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
f = serializers.DateTimeField()
|
|
|
|
|
|
|
|
try:
|
2013-03-01 16:16:00 +04:00
|
|
|
f.from_native('04:61:59')
|
2013-02-26 14:09:54 +04:00
|
|
|
except validators.ValidationError as e:
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
|
2013-03-06 16:19:39 +04:00
|
|
|
"YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]"])
|
2013-02-26 14:09:54 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_from_native_invalid_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() raises a ValidationError on passing an invalid format.
|
|
|
|
"""
|
2013-02-26 14:09:54 +04:00
|
|
|
f = serializers.DateTimeField()
|
|
|
|
|
|
|
|
try:
|
2013-03-01 16:16:00 +04:00
|
|
|
f.from_native('04 -- 31')
|
2013-02-26 14:09:54 +04:00
|
|
|
except validators.ValidationError as e:
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: "
|
2013-03-06 16:19:39 +04:00
|
|
|
"YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]"])
|
2013-02-26 14:09:54 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_to_native(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() returns isoformat as default.
|
|
|
|
"""
|
|
|
|
f = serializers.DateTimeField()
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
|
|
|
result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-21 12:41:54 +04:00
|
|
|
self.assertEqual(datetime.datetime(1984, 7, 31), result_1)
|
|
|
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_2)
|
|
|
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_3)
|
|
|
|
self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_4)
|
|
|
|
|
|
|
|
def test_to_native_iso(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() with format=iso-8601 returns iso formatted datetime.
|
|
|
|
"""
|
|
|
|
f = serializers.DateTimeField(format='iso-8601')
|
|
|
|
|
|
|
|
result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
|
|
|
result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual('1984-07-31T00:00:00', result_1)
|
|
|
|
self.assertEqual('1984-07-31T04:31:00', result_2)
|
|
|
|
self.assertEqual('1984-07-31T04:31:59', result_3)
|
|
|
|
self.assertEqual('1984-07-31T04:31:59.000200', result_4)
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
def test_to_native_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() returns correct custom format.
|
|
|
|
"""
|
2013-03-06 16:19:39 +04:00
|
|
|
f = serializers.DateTimeField(format="%Y - %H:%M")
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.to_native(datetime.datetime(1984, 7, 31))
|
|
|
|
result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31))
|
|
|
|
result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59))
|
|
|
|
result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200))
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual('1984 - 00:00', result_1)
|
|
|
|
self.assertEqual('1984 - 04:31', result_2)
|
|
|
|
self.assertEqual('1984 - 04:31', result_3)
|
|
|
|
self.assertEqual('1984 - 04:31', result_4)
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-07 03:15:19 +04:00
|
|
|
def test_to_native_none(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on None param.
|
|
|
|
"""
|
|
|
|
f = serializers.DateTimeField(required=False)
|
|
|
|
self.assertEqual(None, f.to_native(None))
|
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
class TimeFieldTest(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the TimeField from_native() and to_native() behavior
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_from_native_string(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts default iso input formats.
|
|
|
|
"""
|
2013-02-15 00:19:51 +04:00
|
|
|
f = serializers.TimeField()
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.from_native('04:31')
|
|
|
|
result_2 = f.from_native('04:31:59')
|
|
|
|
result_3 = f.from_native('04:31:59.000200')
|
2013-02-15 00:19:51 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual(datetime.time(4, 31), result_1)
|
|
|
|
self.assertEqual(datetime.time(4, 31, 59), result_2)
|
|
|
|
self.assertEqual(datetime.time(4, 31, 59, 200), result_3)
|
2013-02-15 00:19:51 +04:00
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
def test_from_native_datetime_time(self):
|
2013-02-15 00:19:51 +04:00
|
|
|
"""
|
|
|
|
Make sure from_native() accepts a datetime.time instance.
|
|
|
|
"""
|
|
|
|
f = serializers.TimeField()
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.from_native(datetime.time(4, 31))
|
|
|
|
result_2 = f.from_native(datetime.time(4, 31, 59))
|
|
|
|
result_3 = f.from_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
self.assertEqual(result_1, datetime.time(4, 31))
|
|
|
|
self.assertEqual(result_2, datetime.time(4, 31, 59))
|
|
|
|
self.assertEqual(result_3, datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
def test_from_native_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts custom input formats.
|
|
|
|
"""
|
|
|
|
f = serializers.TimeField(input_formats=['%H -- %M'])
|
|
|
|
result = f.from_native('04 -- 31')
|
|
|
|
|
|
|
|
self.assertEqual(datetime.time(4, 31), result)
|
|
|
|
|
|
|
|
def test_from_native_invalid_default_on_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() don't accept default formats if custom format is preset
|
|
|
|
"""
|
|
|
|
f = serializers.TimeField(input_formats=['%H -- %M'])
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
try:
|
|
|
|
f.from_native('04:31:59')
|
|
|
|
except validators.ValidationError as e:
|
2013-03-06 16:19:39 +04:00
|
|
|
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: hh -- mm"])
|
2013-03-01 16:16:00 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
2013-02-15 00:19:51 +04:00
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
def test_from_native_empty(self):
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on empty param.
|
|
|
|
"""
|
2013-02-15 00:19:51 +04:00
|
|
|
f = serializers.TimeField()
|
|
|
|
result = f.from_native('')
|
2013-02-26 14:09:54 +04:00
|
|
|
|
2013-02-15 00:19:51 +04:00
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
2013-03-01 20:04:30 +04:00
|
|
|
def test_from_native_none(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on None param.
|
|
|
|
"""
|
|
|
|
f = serializers.TimeField()
|
|
|
|
result = f.from_native(None)
|
|
|
|
|
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
2013-02-26 14:09:54 +04:00
|
|
|
def test_from_native_invalid_time(self):
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
Make sure from_native() raises a ValidationError on passing an invalid time.
|
|
|
|
"""
|
2013-02-15 00:19:51 +04:00
|
|
|
f = serializers.TimeField()
|
|
|
|
|
|
|
|
try:
|
2013-03-01 16:16:00 +04:00
|
|
|
f.from_native('04:61:59')
|
2013-02-15 00:19:51 +04:00
|
|
|
except validators.ValidationError as e:
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
|
2013-03-06 16:19:39 +04:00
|
|
|
"hh:mm[:ss[.uuuuuu]]"])
|
2013-02-15 00:19:51 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
2013-03-01 16:16:00 +04:00
|
|
|
|
|
|
|
def test_from_native_invalid_format(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() raises a ValidationError on passing an invalid format.
|
|
|
|
"""
|
|
|
|
f = serializers.TimeField()
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.from_native('04 -- 31')
|
|
|
|
except validators.ValidationError as e:
|
|
|
|
self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: "
|
2013-03-06 16:19:39 +04:00
|
|
|
"hh:mm[:ss[.uuuuuu]]"])
|
2013-03-01 16:16:00 +04:00
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
def test_to_native(self):
|
|
|
|
"""
|
2013-03-21 12:41:54 +04:00
|
|
|
Make sure to_native() returns time object as default.
|
2013-03-01 16:16:00 +04:00
|
|
|
"""
|
|
|
|
f = serializers.TimeField()
|
|
|
|
result_1 = f.to_native(datetime.time(4, 31))
|
|
|
|
result_2 = f.to_native(datetime.time(4, 31, 59))
|
|
|
|
result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
2013-03-21 12:41:54 +04:00
|
|
|
self.assertEqual(datetime.time(4, 31), result_1)
|
|
|
|
self.assertEqual(datetime.time(4, 31, 59), result_2)
|
|
|
|
self.assertEqual(datetime.time(4, 31, 59, 200), result_3)
|
|
|
|
|
|
|
|
def test_to_native_iso(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() with format='iso-8601' returns iso formatted time.
|
|
|
|
"""
|
|
|
|
f = serializers.TimeField(format='iso-8601')
|
|
|
|
result_1 = f.to_native(datetime.time(4, 31))
|
|
|
|
result_2 = f.to_native(datetime.time(4, 31, 59))
|
|
|
|
result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
2013-03-01 16:16:00 +04:00
|
|
|
self.assertEqual('04:31:00', result_1)
|
|
|
|
self.assertEqual('04:31:59', result_2)
|
|
|
|
self.assertEqual('04:31:59.000200', result_3)
|
|
|
|
|
|
|
|
def test_to_native_custom_format(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() returns correct custom format.
|
|
|
|
"""
|
2013-03-06 16:19:39 +04:00
|
|
|
f = serializers.TimeField(format="%H - %S [%f]")
|
2013-03-01 16:16:00 +04:00
|
|
|
result_1 = f.to_native(datetime.time(4, 31))
|
|
|
|
result_2 = f.to_native(datetime.time(4, 31, 59))
|
|
|
|
result_3 = f.to_native(datetime.time(4, 31, 59, 200))
|
|
|
|
|
|
|
|
self.assertEqual('04 - 00 [000000]', result_1)
|
|
|
|
self.assertEqual('04 - 59 [000000]', result_2)
|
2013-03-06 16:19:39 +04:00
|
|
|
self.assertEqual('04 - 59 [000200]', result_3)
|
2013-04-15 14:40:18 +04:00
|
|
|
|
|
|
|
|
|
|
|
class DecimalFieldTest(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the DecimalField from_native() and to_native() behavior
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_from_native_string(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts string values
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField()
|
|
|
|
result_1 = f.from_native('9000')
|
|
|
|
result_2 = f.from_native('1.00000001')
|
|
|
|
|
|
|
|
self.assertEqual(Decimal('9000'), result_1)
|
|
|
|
self.assertEqual(Decimal('1.00000001'), result_2)
|
|
|
|
|
|
|
|
def test_from_native_invalid_string(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() raises ValidationError on passing invalid string
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField()
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.from_native('123.45.6')
|
|
|
|
except validators.ValidationError as e:
|
|
|
|
self.assertEqual(e.messages, ["Enter a number."])
|
|
|
|
else:
|
|
|
|
self.fail("ValidationError was not properly raised")
|
|
|
|
|
|
|
|
def test_from_native_integer(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts integer values
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField()
|
|
|
|
result = f.from_native(9000)
|
|
|
|
|
|
|
|
self.assertEqual(Decimal('9000'), result)
|
|
|
|
|
|
|
|
def test_from_native_float(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() accepts float values
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField()
|
|
|
|
result = f.from_native(1.00000001)
|
|
|
|
|
|
|
|
self.assertEqual(Decimal('1.00000001'), result)
|
|
|
|
|
|
|
|
def test_from_native_empty(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on empty param.
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField()
|
|
|
|
result = f.from_native('')
|
|
|
|
|
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
|
|
|
def test_from_native_none(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on None param.
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField()
|
|
|
|
result = f.from_native(None)
|
|
|
|
|
|
|
|
self.assertEqual(result, None)
|
|
|
|
|
|
|
|
def test_to_native(self):
|
|
|
|
"""
|
|
|
|
Make sure to_native() returns Decimal as string.
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField()
|
|
|
|
|
|
|
|
result_1 = f.to_native(Decimal('9000'))
|
|
|
|
result_2 = f.to_native(Decimal('1.00000001'))
|
|
|
|
|
2013-04-15 17:24:14 +04:00
|
|
|
self.assertEqual(Decimal('9000'), result_1)
|
|
|
|
self.assertEqual(Decimal('1.00000001'), result_2)
|
2013-04-15 14:40:18 +04:00
|
|
|
|
|
|
|
def test_to_native_none(self):
|
|
|
|
"""
|
|
|
|
Make sure from_native() returns None on None param.
|
|
|
|
"""
|
|
|
|
f = serializers.DecimalField(required=False)
|
|
|
|
self.assertEqual(None, f.to_native(None))
|
|
|
|
|
|
|
|
def test_valid_serialization(self):
|
|
|
|
"""
|
|
|
|
Make sure the serializer works correctly
|
|
|
|
"""
|
|
|
|
class DecimalSerializer(Serializer):
|
|
|
|
decimal_field = serializers.DecimalField(max_value=9010,
|
|
|
|
min_value=9000,
|
|
|
|
max_digits=6,
|
|
|
|
decimal_places=2)
|
|
|
|
|
|
|
|
self.assertTrue(DecimalSerializer(data={'decimal_field': '9001'}).is_valid())
|
|
|
|
self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.2'}).is_valid())
|
|
|
|
self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.23'}).is_valid())
|
|
|
|
|
|
|
|
self.assertFalse(DecimalSerializer(data={'decimal_field': '8000'}).is_valid())
|
|
|
|
self.assertFalse(DecimalSerializer(data={'decimal_field': '9900'}).is_valid())
|
|
|
|
self.assertFalse(DecimalSerializer(data={'decimal_field': '9001.234'}).is_valid())
|
|
|
|
|
|
|
|
def test_raise_max_value(self):
|
|
|
|
"""
|
|
|
|
Make sure max_value violations raises ValidationError
|
|
|
|
"""
|
|
|
|
class DecimalSerializer(Serializer):
|
|
|
|
decimal_field = serializers.DecimalField(max_value=100)
|
|
|
|
|
|
|
|
s = DecimalSerializer(data={'decimal_field': '123'})
|
|
|
|
|
|
|
|
self.assertFalse(s.is_valid())
|
2013-04-15 14:55:29 +04:00
|
|
|
self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is less than or equal to 100.']})
|
2013-04-15 14:40:18 +04:00
|
|
|
|
|
|
|
def test_raise_min_value(self):
|
|
|
|
"""
|
|
|
|
Make sure min_value violations raises ValidationError
|
|
|
|
"""
|
|
|
|
class DecimalSerializer(Serializer):
|
|
|
|
decimal_field = serializers.DecimalField(min_value=100)
|
|
|
|
|
|
|
|
s = DecimalSerializer(data={'decimal_field': '99'})
|
|
|
|
|
|
|
|
self.assertFalse(s.is_valid())
|
2013-04-15 14:55:29 +04:00
|
|
|
self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is greater than or equal to 100.']})
|
2013-04-15 14:40:18 +04:00
|
|
|
|
|
|
|
def test_raise_max_digits(self):
|
|
|
|
"""
|
|
|
|
Make sure max_digits violations raises ValidationError
|
|
|
|
"""
|
|
|
|
class DecimalSerializer(Serializer):
|
|
|
|
decimal_field = serializers.DecimalField(max_digits=5)
|
|
|
|
|
|
|
|
s = DecimalSerializer(data={'decimal_field': '123.456'})
|
|
|
|
|
|
|
|
self.assertFalse(s.is_valid())
|
2013-04-15 14:55:29 +04:00
|
|
|
self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 5 digits in total.']})
|
2013-04-15 14:40:18 +04:00
|
|
|
|
|
|
|
def test_raise_max_decimal_places(self):
|
|
|
|
"""
|
|
|
|
Make sure max_decimal_places violations raises ValidationError
|
|
|
|
"""
|
|
|
|
class DecimalSerializer(Serializer):
|
|
|
|
decimal_field = serializers.DecimalField(decimal_places=3)
|
|
|
|
|
|
|
|
s = DecimalSerializer(data={'decimal_field': '123.4567'})
|
|
|
|
|
|
|
|
self.assertFalse(s.is_valid())
|
2013-04-15 14:55:29 +04:00
|
|
|
self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 3 decimal places.']})
|
2013-04-15 14:40:18 +04:00
|
|
|
|
|
|
|
def test_raise_max_whole_digits(self):
|
|
|
|
"""
|
|
|
|
Make sure max_whole_digits violations raises ValidationError
|
|
|
|
"""
|
|
|
|
class DecimalSerializer(Serializer):
|
|
|
|
decimal_field = serializers.DecimalField(max_digits=4, decimal_places=3)
|
|
|
|
|
|
|
|
s = DecimalSerializer(data={'decimal_field': '12345.6'})
|
|
|
|
|
|
|
|
self.assertFalse(s.is_valid())
|
2013-05-18 14:40:25 +04:00
|
|
|
self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 4 digits in total.']})
|
|
|
|
|
|
|
|
|
|
|
|
class ChoiceFieldTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the ChoiceField options generator
|
|
|
|
"""
|
|
|
|
|
|
|
|
SAMPLE_CHOICES = [
|
|
|
|
('red', 'Red'),
|
|
|
|
('green', 'Green'),
|
|
|
|
('blue', 'Blue'),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_choices_required(self):
|
|
|
|
"""
|
|
|
|
Make sure proper choices are rendered if field is required
|
|
|
|
"""
|
|
|
|
f = serializers.ChoiceField(required=True, choices=self.SAMPLE_CHOICES)
|
|
|
|
self.assertEqual(f.choices, self.SAMPLE_CHOICES)
|
|
|
|
|
|
|
|
def test_choices_not_required(self):
|
|
|
|
"""
|
|
|
|
Make sure proper choices (plus blank) are rendered if the field isn't required
|
|
|
|
"""
|
|
|
|
f = serializers.ChoiceField(required=False, choices=self.SAMPLE_CHOICES)
|
|
|
|
self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + self.SAMPLE_CHOICES)
|
2013-05-18 17:48:36 +04:00
|
|
|
|
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
class EmailFieldTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for EmailField attribute values
|
|
|
|
"""
|
|
|
|
|
|
|
|
class EmailFieldModel(RESTFrameworkModel):
|
|
|
|
email_field = models.EmailField(blank=True)
|
|
|
|
|
|
|
|
class EmailFieldWithGivenMaxLengthModel(RESTFrameworkModel):
|
|
|
|
email_field = models.EmailField(max_length=150, blank=True)
|
|
|
|
|
|
|
|
def test_default_model_value(self):
|
|
|
|
class EmailFieldSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = self.EmailFieldModel
|
|
|
|
|
|
|
|
serializer = EmailFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 75)
|
|
|
|
|
|
|
|
def test_given_model_value(self):
|
|
|
|
class EmailFieldSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = self.EmailFieldWithGivenMaxLengthModel
|
|
|
|
|
|
|
|
serializer = EmailFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 150)
|
|
|
|
|
|
|
|
def test_given_serializer_value(self):
|
|
|
|
class EmailFieldSerializer(serializers.ModelSerializer):
|
|
|
|
email_field = serializers.EmailField(source='email_field', max_length=20, required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = self.EmailFieldModel
|
|
|
|
|
|
|
|
serializer = EmailFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 20)
|
|
|
|
|
|
|
|
|
2013-05-18 17:48:36 +04:00
|
|
|
class SlugFieldTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for SlugField attribute values
|
|
|
|
"""
|
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
class SlugFieldModel(RESTFrameworkModel):
|
|
|
|
slug_field = models.SlugField(blank=True)
|
|
|
|
|
|
|
|
class SlugFieldWithGivenMaxLengthModel(RESTFrameworkModel):
|
|
|
|
slug_field = models.SlugField(max_length=84, blank=True)
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
def test_default_model_value(self):
|
2013-05-18 17:48:36 +04:00
|
|
|
class SlugFieldSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
2013-05-18 18:24:54 +04:00
|
|
|
model = self.SlugFieldModel
|
2013-05-18 17:48:36 +04:00
|
|
|
|
|
|
|
serializer = SlugFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 50)
|
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
def test_given_model_value(self):
|
2013-05-18 17:48:36 +04:00
|
|
|
class SlugFieldSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
2013-05-18 18:24:54 +04:00
|
|
|
model = self.SlugFieldWithGivenMaxLengthModel
|
2013-05-18 17:48:36 +04:00
|
|
|
|
|
|
|
serializer = SlugFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 84)
|
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
def test_given_serializer_value(self):
|
|
|
|
class SlugFieldSerializer(serializers.ModelSerializer):
|
|
|
|
slug_field = serializers.SlugField(source='slug_field', max_length=20, required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = self.SlugFieldModel
|
|
|
|
|
|
|
|
serializer = SlugFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 20)
|
|
|
|
|
2013-05-18 17:48:36 +04:00
|
|
|
|
|
|
|
class URLFieldTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for URLField attribute values
|
|
|
|
"""
|
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
class URLFieldModel(RESTFrameworkModel):
|
|
|
|
url_field = models.URLField(blank=True)
|
|
|
|
|
|
|
|
class URLFieldWithGivenMaxLengthModel(RESTFrameworkModel):
|
|
|
|
url_field = models.URLField(max_length=128, blank=True)
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
def test_default_model_value(self):
|
2013-05-18 17:48:36 +04:00
|
|
|
class URLFieldSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
2013-05-18 18:24:54 +04:00
|
|
|
model = self.URLFieldModel
|
2013-05-18 17:48:36 +04:00
|
|
|
|
|
|
|
serializer = URLFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 200)
|
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
def test_given_model_value(self):
|
|
|
|
class URLFieldSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = self.URLFieldWithGivenMaxLengthModel
|
|
|
|
|
|
|
|
serializer = URLFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 128)
|
2013-05-18 17:48:36 +04:00
|
|
|
|
2013-05-18 18:24:54 +04:00
|
|
|
def test_given_serializer_value(self):
|
2013-05-18 17:48:36 +04:00
|
|
|
class URLFieldSerializer(serializers.ModelSerializer):
|
2013-05-18 18:24:54 +04:00
|
|
|
url_field = serializers.URLField(source='url_field', max_length=20, required=False)
|
|
|
|
|
2013-05-18 17:48:36 +04:00
|
|
|
class Meta:
|
2013-05-18 18:24:54 +04:00
|
|
|
model = self.URLFieldWithGivenMaxLengthModel
|
2013-05-18 17:48:36 +04:00
|
|
|
|
|
|
|
serializer = URLFieldSerializer(data={})
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
2013-05-18 18:32:31 +04:00
|
|
|
self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 20)
|
2013-05-18 20:34:21 +04:00
|
|
|
|
|
|
|
|
2013-05-18 20:10:17 +04:00
|
|
|
class HumanizedFieldType(TestCase):
|
|
|
|
def test_standard_type_classes(self):
|
|
|
|
for field_type_name in forms.fields.__all__:
|
|
|
|
field_type = getattr(forms.fields, field_type_name)
|
|
|
|
humanized = humanize_field_type(field_type)
|
|
|
|
self.assert_valid_name(humanized)
|
|
|
|
|
|
|
|
def test_standard_type_names(self):
|
|
|
|
for field_type_name in forms.fields.__all__:
|
|
|
|
humanized = humanize_field_type(field_type_name)
|
|
|
|
self.assert_valid_name(humanized)
|
|
|
|
|
|
|
|
def test_custom_type_name(self):
|
|
|
|
humanized = humanize_field_type('SomeCustomType')
|
|
|
|
self.assertEquals(humanized, u'Some custom type')
|
|
|
|
|
|
|
|
def test_custom_type(self):
|
|
|
|
custom_type = namedtuple('SomeCustomType', [])
|
|
|
|
humanized = humanize_field_type(custom_type)
|
|
|
|
self.assertEquals(humanized, u'Some custom type')
|
|
|
|
|
|
|
|
def assert_valid_name(self, humanized):
|
|
|
|
"""A humanized field name is valid if it's a non-empty
|
|
|
|
unicode.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.assertIsInstance(humanized, unicode)
|
|
|
|
self.assertTrue(humanized)
|
|
|
|
|
|
|
|
|
|
|
|
class HumanizedField(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.required_field = Field()
|
|
|
|
self.required_field.label = uuid4().hex
|
|
|
|
self.required_field.required = True
|
|
|
|
|
|
|
|
self.optional_field = Field()
|
|
|
|
self.optional_field.label = uuid4().hex
|
|
|
|
self.optional_field.required = False
|
|
|
|
|
|
|
|
def test_required(self):
|
|
|
|
self.assertEqual(humanize_field(self.required_field)['required'], True)
|
|
|
|
|
|
|
|
def test_optional(self):
|
|
|
|
self.assertEqual(humanize_field(self.optional_field)['required'],
|
|
|
|
False)
|
|
|
|
|
|
|
|
def test_label(self):
|
|
|
|
for field in (self.required_field, self.optional_field):
|
|
|
|
self.assertEqual(humanize_field(field)['label'], field.label)
|
2013-05-18 20:27:53 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Form(forms.Form):
|
|
|
|
field1 = forms.CharField(max_length=3, label='field one')
|
|
|
|
field2 = forms.CharField(label='field two')
|
|
|
|
|
|
|
|
|
|
|
|
class HumanizedSerializer(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.serializer = TimestampedModelSerializer()
|
|
|
|
|
|
|
|
def test_humanized(self):
|
|
|
|
humanized = humanize_form_fields(Form())
|
|
|
|
self.assertEqual(humanized, {
|
|
|
|
'field1': {
|
|
|
|
u'help_text': u'', u'required': True,
|
|
|
|
u'type': u'Single Character', u'label': 'field one'},
|
|
|
|
'field2': {
|
|
|
|
u'help_text': u'', u'required': True,
|
|
|
|
u'type': u'Single Character', u'label': 'field two'}})
|