regression test for #1604

This commit is contained in:
Scott White 2014-08-08 13:12:41 -04:00
parent 4057425be4
commit 739516730e
2 changed files with 53 additions and 1 deletions

View File

@ -75,6 +75,16 @@ class ActionItem(RESTFrameworkModel):
info = CustomField(default='---', max_length=12) info = CustomField(default='---', max_length=12)
# Models for testing #1604 regression
class UntouchablePerson(models.Model):
name = models.CharField(max_length=64)
class UntouchablePersonLog(models.Model):
person = models.ForeignKey(UntouchablePerson, editable=False)
date = models.DateField(null=False)
# Models for reverse relations # Models for reverse relations
class Person(RESTFrameworkModel): class Person(RESTFrameworkModel):
name = models.CharField(max_length=10) name = models.CharField(max_length=10)

View File

@ -10,7 +10,7 @@ from rest_framework import serializers, fields, relations
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel, from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel, BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, RESTFrameworkModel, ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, RESTFrameworkModel,
ForeignKeySource, ManyToManySource) ForeignKeySource, ManyToManySource, UntouchablePerson, UntouchablePersonLog)
from rest_framework.tests.models import BasicModelSerializer from rest_framework.tests.models import BasicModelSerializer
import datetime import datetime
import pickle import pickle
@ -98,6 +98,7 @@ class ActionItemSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ActionItem model = ActionItem
class ActionItemSerializerOptionalFields(serializers.ModelSerializer): class ActionItemSerializerOptionalFields(serializers.ModelSerializer):
""" """
Intended to test that fields with `required=False` are excluded from validation. Intended to test that fields with `required=False` are excluded from validation.
@ -108,6 +109,20 @@ class ActionItemSerializerOptionalFields(serializers.ModelSerializer):
model = ActionItem model = ActionItem
fields = ('title',) fields = ('title',)
class UntouchablePersonLogSerializerExcludedFields(serializers.ModelSerializer):
"""
Intended to test that fields with `editable=False` and that are included
in Meta.exclude are actually excluded from validation.
Tests regression for #1604
"""
class Meta:
model = UntouchablePersonLog
fields = ('date',)
exclude = ('person',)
class ActionItemSerializerCustomRestore(serializers.ModelSerializer): class ActionItemSerializerCustomRestore(serializers.ModelSerializer):
class Meta: class Meta:
@ -980,6 +995,33 @@ class WritableFieldDefaultValueTests(TestCase):
self.assertEqual(got, expected) self.assertEqual(got, expected)
class ExcludedFieldTests(TestCase):
def setUp(self):
self.serializer_class = UntouchablePersonLogSerializerExcludedFields
# An UntouchablePerson instance to use for the relationship
self.person = UntouchablePerson(name='MC Hammer')
self.person.save()
# A log entry with non-editable relationship to the person
self.instance = UntouchablePersonLog(
person=self.person,
date=datetime.date(1990, 1, 13)
)
self.instance.save()
def test_excluded_noneditable_field_is_excluded(self):
# test for #1604
data = {
'date': datetime.date(1990, 1, 13)
}
serializer = UntouchablePersonLogSerializerExcludedFields(
instance=self.instance
)
self.assertEqual(serializer.data, data)
class RelatedFieldDefaultValueTests(WritableFieldDefaultValueTests): class RelatedFieldDefaultValueTests(WritableFieldDefaultValueTests):
def setUp(self): def setUp(self):