mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
regression test for #1604
This commit is contained in:
parent
4057425be4
commit
739516730e
|
@ -75,6 +75,16 @@ class ActionItem(RESTFrameworkModel):
|
|||
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
|
||||
class Person(RESTFrameworkModel):
|
||||
name = models.CharField(max_length=10)
|
||||
|
|
|
@ -10,7 +10,7 @@ from rest_framework import serializers, fields, relations
|
|||
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
||||
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
|
||||
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, RESTFrameworkModel,
|
||||
ForeignKeySource, ManyToManySource)
|
||||
ForeignKeySource, ManyToManySource, UntouchablePerson, UntouchablePersonLog)
|
||||
from rest_framework.tests.models import BasicModelSerializer
|
||||
import datetime
|
||||
import pickle
|
||||
|
@ -98,6 +98,7 @@ class ActionItemSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = ActionItem
|
||||
|
||||
|
||||
class ActionItemSerializerOptionalFields(serializers.ModelSerializer):
|
||||
"""
|
||||
Intended to test that fields with `required=False` are excluded from validation.
|
||||
|
@ -108,6 +109,20 @@ class ActionItemSerializerOptionalFields(serializers.ModelSerializer):
|
|||
model = ActionItem
|
||||
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 Meta:
|
||||
|
@ -980,6 +995,33 @@ class WritableFieldDefaultValueTests(TestCase):
|
|||
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):
|
||||
|
||||
def setUp(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user