Add test to show that ManyToMany relation fails in Python 3 when the field attributes null==True and blank==True are set.

This commit is contained in:
Norman Jäckel 2014-09-02 21:02:00 +02:00
parent b40525d8e6
commit 6fdd2d9a7b
2 changed files with 34 additions and 1 deletions

View File

@ -55,6 +55,10 @@ class ManyToManyModel(RESTFrameworkModel):
rel = models.ManyToManyField(Anchor, help_text='Some help text.')
class ManyToManyModelTwo(RESTFrameworkModel):
rel = models.ManyToManyField(Anchor, null=True, blank=True, help_text='Some other help text.')
class ReadOnlyManyToManyModel(RESTFrameworkModel):
text = models.CharField(max_length=100, default='anchor')
rel = models.ManyToManyField(Anchor)

View File

@ -10,7 +10,7 @@ from rest_framework import serializers, fields, relations
from tests.models import (
HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel,
DefaultValueModel, ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo,
DefaultValueModel, ManyToManyModel, ManyToManyModelTwo, Person, ReadOnlyManyToManyModel, Photo,
RESTFrameworkModel, ForeignKeySource
)
from tests.models import BasicModelSerializer
@ -757,6 +757,35 @@ class MetadataTests(TestCase):
self.assertTrue(isinstance(serializer.data.fields[field_name], field))
class ManyToManyTwoTests(TestCase):
"""
Tests model with a ManyToMany relationship with field attributes
null==True and blank==True.
"""
def setUp(self):
class ManyToManySerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyModelTwo
self.serializer_class = ManyToManySerializer
# An anchor instance to use for the relationship
self.anchor = Anchor()
self.anchor.save()
def test_create(self):
"""
Create an instance of a model with a ManyToMany relationship.
"""
data = {'rel': [self.anchor.id]}
serializer = self.serializer_class(data=data)
self.assertEqual(serializer.is_valid(), True)
instance = serializer.save()
self.assertEqual(len(ManyToManyModelTwo.objects.all()), 2)
self.assertEqual(instance.pk, 2)
self.assertEqual(list(instance.rel.all()), [self.anchor])
class ManyToManyTests(TestCase):
def setUp(self):
class ManyToManySerializer(serializers.ModelSerializer):