Changed DepthTest to have depth=2

This commit is contained in:
JC 2013-04-27 13:10:39 -07:00
parent 33a26a76f1
commit 3b0fa3ebaa

View File

@ -3,7 +3,7 @@ from django.utils.datastructures import MultiValueDict
from django.test import TestCase from django.test import TestCase
from rest_framework import serializers from rest_framework import serializers
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel, from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel, BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo) ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo)
import datetime import datetime
import pickle import pickle
@ -767,8 +767,6 @@ class RelatedTraversalTest(TestCase):
post = BlogPost.objects.create(title="Test blog post", writer=user) post = BlogPost.objects.create(title="Test blog post", writer=user)
post.blogpostcomment_set.create(text="I love this blog post") post.blogpostcomment_set.create(text="I love this blog post")
from rest_framework.tests.models import BlogPostComment
class PersonSerializer(serializers.ModelSerializer): class PersonSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Person model = Person
@ -968,23 +966,26 @@ class SerializerPickleTests(TestCase):
class DepthTest(TestCase): class DepthTest(TestCase):
def test_implicit_nesting(self): def test_implicit_nesting(self):
writer = Person.objects.create(name="django", age=1) writer = Person.objects.create(name="django", age=1)
post = BlogPost.objects.create(title="Test blog post", writer=writer) post = BlogPost.objects.create(title="Test blog post", writer=writer)
comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post)
class BlogPostSerializer(serializers.ModelSerializer): class BlogPostCommentSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = BlogPost model = BlogPostComment
depth = 1 depth = 2
serializer = BlogPostSerializer(instance=post) serializer = BlogPostCommentSerializer(instance=comment)
expected = {'id': 1, 'title': 'Test blog post', expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post',
'writer': {'id': 1, 'name': 'django', 'age': 1}} 'writer': {'id': 1, 'name': 'django', 'age': 1}}}
self.assertEqual(serializer.data, expected) self.assertEqual(serializer.data, expected)
def test_explicit_nesting(self): def test_explicit_nesting(self):
writer = Person.objects.create(name="django", age=1) writer = Person.objects.create(name="django", age=1)
post = BlogPost.objects.create(title="Test blog post", writer=writer) post = BlogPost.objects.create(title="Test blog post", writer=writer)
comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post)
class PersonSerializer(serializers.ModelSerializer): class PersonSerializer(serializers.ModelSerializer):
class Meta: class Meta:
@ -996,9 +997,15 @@ class DepthTest(TestCase):
class Meta: class Meta:
model = BlogPost model = BlogPost
serializer = BlogPostSerializer(instance=post) class BlogPostCommentSerializer(serializers.ModelSerializer):
expected = {'id': 1, 'title': 'Test blog post', blog_post = BlogPostSerializer()
'writer': {'id': 1, 'name': 'django', 'age': 1}}
class Meta:
model = BlogPostComment
serializer = BlogPostCommentSerializer(instance=comment)
expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post',
'writer': {'id': 1, 'name': 'django', 'age': 1}}}
self.assertEqual(serializer.data, expected) self.assertEqual(serializer.data, expected)