From 13e64a66a24323a13112ac5d81b7c09b61d664d8 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Tue, 11 Dec 2012 22:24:25 -0700 Subject: [PATCH 1/3] added failing 2-deep nested serializer context test --- rest_framework/tests/serializer.py | 47 +++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 50a5f5a48..b8140896e 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -4,7 +4,7 @@ from django.test import TestCase from rest_framework import serializers from rest_framework.tests.models import (Album, ActionItem, Anchor, BasicModel, BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel, - ManyToManyModel, Person, ReadOnlyManyToManyModel) + ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo) class SubComment(object): @@ -764,3 +764,48 @@ class DepthTest(TestCase): 'writer': {'id': 1, 'name': u'django', 'age': 1}} self.assertEqual(serializer.data, expected) + + +class NestedSerializerContextTests(TestCase): + + def test_nested_serializer_context(self): + class AlbumCollection(object): + albums = None + + class PhotoSerializer(serializers.ModelSerializer): + class Meta: + model = Photo + fields = ("description", "callable") + + callable = serializers.SerializerMethodField('_callable') + + def _callable(self, instance): + if not 'context_item' in self.context: + raise RuntimeError("context isn't getting passed into 2nd level nested serializer") + return "callable in AlbumSerializer" + + class AlbumSerializer(serializers.ModelSerializer): + class Meta: + model = Album + fields = ("photo_set", "callable") + + photo_set = PhotoSerializer(source="photo_set") + callable = serializers.SerializerMethodField("_callable") + + def _callable(self, instance): + if not 'context_item' in self.context: + raise RuntimeError("context isn't getting passed into 1st level nested serializer") + return "callable in AlbumSerializer" + + class AlbumCollectionSerializer(serializers.Serializer): + albums = AlbumSerializer(source="albums", ) + album1 = Album.objects.create(title="album 1") + album1.photo_set.add(Photo(description="Dog"), Photo(description="Cat")) + album2 = Album.objects.create(title="album 2") + album2.photo_set.add(Photo(description="Yeti"), Photo(description="Sasquatch"), Photo(description="Bigfoot")) + album_collection = AlbumCollection() + album_collection.albums = [album1, album2] + + data = AlbumCollectionSerializer(album_collection, context={'context_item': 'album context'}).data + + From 8bbfa90ff498f531273067a9f3b72e77d902c677 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Tue, 11 Dec 2012 22:42:18 -0700 Subject: [PATCH 2/3] fixed typo --- rest_framework/tests/serializer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index b8140896e..76b37e4f3 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -782,7 +782,7 @@ class NestedSerializerContextTests(TestCase): def _callable(self, instance): if not 'context_item' in self.context: raise RuntimeError("context isn't getting passed into 2nd level nested serializer") - return "callable in AlbumSerializer" + return "success" class AlbumSerializer(serializers.ModelSerializer): class Meta: @@ -795,7 +795,7 @@ class NestedSerializerContextTests(TestCase): def _callable(self, instance): if not 'context_item' in self.context: raise RuntimeError("context isn't getting passed into 1st level nested serializer") - return "callable in AlbumSerializer" + return "success" class AlbumCollectionSerializer(serializers.Serializer): albums = AlbumSerializer(source="albums", ) From 043f4a5a4abdec5cef2e568728a406e56ccbea26 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Tue, 11 Dec 2012 22:47:50 -0700 Subject: [PATCH 3/3] made things prettier --- rest_framework/tests/serializer.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 76b37e4f3..0c11c07b8 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -769,8 +769,6 @@ class DepthTest(TestCase): class NestedSerializerContextTests(TestCase): def test_nested_serializer_context(self): - class AlbumCollection(object): - albums = None class PhotoSerializer(serializers.ModelSerializer): class Meta: @@ -797,15 +795,24 @@ class NestedSerializerContextTests(TestCase): raise RuntimeError("context isn't getting passed into 1st level nested serializer") return "success" + class AlbumCollection(object): + albums = None + class AlbumCollectionSerializer(serializers.Serializer): - albums = AlbumSerializer(source="albums", ) + albums = AlbumSerializer(source="albums") + album1 = Album.objects.create(title="album 1") - album1.photo_set.add(Photo(description="Dog"), Photo(description="Cat")) + album1.photo_set.add( + Photo.objects.create(description="Bigfoot"), + Photo.objects.create(description="Unicorn")) album2 = Album.objects.create(title="album 2") - album2.photo_set.add(Photo(description="Yeti"), Photo(description="Sasquatch"), Photo(description="Bigfoot")) + album2.photo_set.add( + Photo.objects.create(description="Yeti"), + Photo.objects.create(description="Sasquatch")) album_collection = AlbumCollection() album_collection.albums = [album1, album2] + #the test. (will raise RuntimeError if context doesn't get passed correctly to the nested Serializers) data = AlbumCollectionSerializer(album_collection, context={'context_item': 'album context'}).data