2012-12-12 01:07:25 +04:00
|
|
|
import datetime
|
|
|
|
import pickle
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework import serializers
|
2012-12-16 01:33:08 +04:00
|
|
|
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
2012-11-21 14:07:08 +04:00
|
|
|
BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel,
|
2012-12-13 00:44:55 +04:00
|
|
|
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-10-19 02:45:16 +04:00
|
|
|
class SubComment(object):
|
|
|
|
def __init__(self, sub_comment):
|
|
|
|
self.sub_comment = sub_comment
|
2012-10-26 16:20:30 +04:00
|
|
|
|
2012-10-19 02:45:16 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
class Comment(object):
|
|
|
|
def __init__(self, email, content, created):
|
|
|
|
self.email = email
|
|
|
|
self.content = content
|
|
|
|
self.created = created or datetime.datetime.now()
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return all([getattr(self, attr) == getattr(other, attr)
|
|
|
|
for attr in ('email', 'content', 'created')])
|
2012-10-26 16:20:30 +04:00
|
|
|
|
2012-10-19 02:45:16 +04:00
|
|
|
def get_sub_comment(self):
|
|
|
|
sub_comment = SubComment('And Merry Christmas!')
|
|
|
|
return sub_comment
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class CommentSerializer(serializers.Serializer):
|
|
|
|
email = serializers.EmailField()
|
|
|
|
content = serializers.CharField(max_length=1000)
|
|
|
|
created = serializers.DateTimeField()
|
2012-10-19 02:45:16 +04:00
|
|
|
sub_comment = serializers.Field(source='get_sub_comment.sub_comment')
|
2012-10-26 16:20:30 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
def restore_object(self, data, instance=None):
|
|
|
|
if instance is None:
|
|
|
|
return Comment(**data)
|
|
|
|
for key, val in data.items():
|
|
|
|
setattr(instance, key, val)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2012-11-21 14:07:08 +04:00
|
|
|
class BookSerializer(serializers.ModelSerializer):
|
|
|
|
isbn = serializers.RegexField(regex=r'^[0-9]{13}$', error_messages={'invalid': 'isbn has to be exact 13 numbers'})
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Book
|
|
|
|
|
|
|
|
|
2012-10-17 22:12:34 +04:00
|
|
|
class ActionItemSerializer(serializers.ModelSerializer):
|
2012-11-23 02:50:42 +04:00
|
|
|
|
2012-10-17 22:12:34 +04:00
|
|
|
class Meta:
|
|
|
|
model = ActionItem
|
|
|
|
|
2012-10-26 16:20:30 +04:00
|
|
|
|
2012-10-30 03:03:24 +04:00
|
|
|
class PersonSerializer(serializers.ModelSerializer):
|
2012-11-03 03:32:02 +04:00
|
|
|
info = serializers.Field(source='info')
|
|
|
|
|
2012-10-30 03:03:24 +04:00
|
|
|
class Meta:
|
|
|
|
model = Person
|
2012-11-03 03:32:02 +04:00
|
|
|
fields = ('name', 'age', 'info')
|
2012-11-09 21:01:20 +04:00
|
|
|
read_only_fields = ('age',)
|
2012-10-30 03:03:24 +04:00
|
|
|
|
|
|
|
|
2012-11-23 02:50:42 +04:00
|
|
|
class AlbumsSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Album
|
2012-12-11 02:10:04 +04:00
|
|
|
fields = ['title'] # lists are also valid options
|
2012-11-23 02:50:42 +04:00
|
|
|
|
2012-12-16 01:33:08 +04:00
|
|
|
class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = HasPositiveIntegerAsChoice
|
|
|
|
fields = ['some_integer']
|
|
|
|
|
2012-11-23 02:50:42 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
class BasicTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.comment = Comment(
|
|
|
|
'tom@example.com',
|
|
|
|
'Happy new year!',
|
|
|
|
datetime.datetime(2012, 1, 1)
|
|
|
|
)
|
|
|
|
self.data = {
|
|
|
|
'email': 'tom@example.com',
|
|
|
|
'content': 'Happy new year!',
|
2012-10-19 02:45:16 +04:00
|
|
|
'created': datetime.datetime(2012, 1, 1),
|
|
|
|
'sub_comment': 'This wont change'
|
|
|
|
}
|
|
|
|
self.expected = {
|
|
|
|
'email': 'tom@example.com',
|
|
|
|
'content': 'Happy new year!',
|
|
|
|
'created': datetime.datetime(2012, 1, 1),
|
|
|
|
'sub_comment': 'And Merry Christmas!'
|
2012-09-20 16:06:27 +04:00
|
|
|
}
|
2012-11-03 03:32:02 +04:00
|
|
|
self.person_data = {'name': 'dwight', 'age': 35}
|
|
|
|
self.person = Person(**self.person_data)
|
|
|
|
self.person.save()
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_empty(self):
|
|
|
|
serializer = CommentSerializer()
|
|
|
|
expected = {
|
|
|
|
'email': '',
|
|
|
|
'content': '',
|
2012-10-19 02:45:16 +04:00
|
|
|
'created': None,
|
|
|
|
'sub_comment': ''
|
2012-09-20 16:06:27 +04:00
|
|
|
}
|
|
|
|
self.assertEquals(serializer.data, expected)
|
|
|
|
|
2012-10-03 14:14:47 +04:00
|
|
|
def test_retrieve(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializer(self.comment)
|
2012-10-19 02:45:16 +04:00
|
|
|
self.assertEquals(serializer.data, self.expected)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-10-03 14:14:47 +04:00
|
|
|
def test_create(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializer(data=self.data)
|
2012-09-20 16:06:27 +04:00
|
|
|
expected = self.comment
|
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
self.assertEquals(serializer.object, expected)
|
|
|
|
self.assertFalse(serializer.object is expected)
|
2012-10-19 02:45:16 +04:00
|
|
|
self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!')
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-10-03 14:14:47 +04:00
|
|
|
def test_update(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializer(self.comment, data=self.data)
|
2012-09-20 16:06:27 +04:00
|
|
|
expected = self.comment
|
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
self.assertEquals(serializer.object, expected)
|
|
|
|
self.assertTrue(serializer.object is expected)
|
2012-10-19 02:45:16 +04:00
|
|
|
self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!')
|
2012-11-05 14:53:20 +04:00
|
|
|
|
2012-11-20 23:01:21 +04:00
|
|
|
def test_partial_update(self):
|
|
|
|
msg = 'Merry New Year!'
|
|
|
|
partial_data = {'content': msg}
|
|
|
|
serializer = CommentSerializer(self.comment, data=partial_data)
|
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
|
|
|
serializer = CommentSerializer(self.comment, data=partial_data, partial=True)
|
|
|
|
expected = self.comment
|
|
|
|
self.assertEqual(serializer.is_valid(), True)
|
|
|
|
self.assertEquals(serializer.object, expected)
|
|
|
|
self.assertTrue(serializer.object is expected)
|
|
|
|
self.assertEquals(serializer.data['content'], msg)
|
|
|
|
|
2012-11-03 03:32:08 +04:00
|
|
|
def test_model_fields_as_expected(self):
|
2012-11-09 21:01:20 +04:00
|
|
|
"""
|
|
|
|
Make sure that the fields returned are the same as defined
|
2012-11-03 03:32:08 +04:00
|
|
|
in the Meta data
|
|
|
|
"""
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = PersonSerializer(self.person)
|
2012-11-03 03:32:08 +04:00
|
|
|
self.assertEquals(set(serializer.data.keys()),
|
|
|
|
set(['name', 'age', 'info']))
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-11-03 03:32:02 +04:00
|
|
|
def test_field_with_dictionary(self):
|
2012-11-09 21:01:20 +04:00
|
|
|
"""
|
|
|
|
Make sure that dictionaries from fields are left intact
|
2012-11-03 03:32:02 +04:00
|
|
|
"""
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = PersonSerializer(self.person)
|
2012-11-03 03:32:02 +04:00
|
|
|
expected = self.person_data
|
|
|
|
self.assertEquals(serializer.data['info'], expected)
|
|
|
|
|
2012-11-09 21:01:20 +04:00
|
|
|
def test_read_only_fields(self):
|
|
|
|
"""
|
|
|
|
Attempting to update fields set as read_only should have no effect.
|
|
|
|
"""
|
|
|
|
|
|
|
|
serializer = PersonSerializer(self.person, data={'name': 'dwight', 'age': 99})
|
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(serializer.errors, {})
|
|
|
|
# Assert age is unchanged (35)
|
|
|
|
self.assertEquals(instance.age, self.person_data['age'])
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
class ValidationTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.comment = Comment(
|
|
|
|
'tom@example.com',
|
|
|
|
'Happy new year!',
|
|
|
|
datetime.datetime(2012, 1, 1)
|
|
|
|
)
|
|
|
|
self.data = {
|
|
|
|
'email': 'tom@example.com',
|
|
|
|
'content': 'x' * 1001,
|
|
|
|
'created': datetime.datetime(2012, 1, 1)
|
2012-10-26 16:20:30 +04:00
|
|
|
}
|
2012-11-23 01:36:37 +04:00
|
|
|
self.actionitem = ActionItem(title='Some to do item',
|
2012-10-17 22:12:34 +04:00
|
|
|
)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-10-03 14:14:47 +04:00
|
|
|
def test_create(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializer(data=self.data)
|
2012-09-20 16:06:27 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
|
|
|
self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']})
|
|
|
|
|
2012-10-03 14:14:47 +04:00
|
|
|
def test_update(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializer(self.comment, data=self.data)
|
2012-09-20 16:06:27 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
|
|
|
self.assertEquals(serializer.errors, {'content': [u'Ensure this value has at most 1000 characters (it has 1001).']})
|
|
|
|
|
2012-10-08 20:53:18 +04:00
|
|
|
def test_update_missing_field(self):
|
|
|
|
data = {
|
|
|
|
'content': 'xxx',
|
|
|
|
'created': datetime.datetime(2012, 1, 1)
|
|
|
|
}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializer(self.comment, data=data)
|
2012-10-08 20:53:18 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
|
|
|
self.assertEquals(serializer.errors, {'email': [u'This field is required.']})
|
|
|
|
|
2012-10-17 22:12:34 +04:00
|
|
|
def test_missing_bool_with_default(self):
|
|
|
|
"""Make sure that a boolean value with a 'False' value is not
|
|
|
|
mistaken for not having a default."""
|
|
|
|
data = {
|
2012-10-26 16:20:30 +04:00
|
|
|
'title': 'Some action item',
|
2012-10-17 22:12:34 +04:00
|
|
|
#No 'done' value.
|
|
|
|
}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = ActionItemSerializer(self.actionitem, data=data)
|
2012-10-17 22:12:34 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
self.assertEquals(serializer.errors, {})
|
|
|
|
|
2012-10-24 12:28:10 +04:00
|
|
|
def test_field_validation(self):
|
|
|
|
|
|
|
|
class CommentSerializerWithFieldValidator(CommentSerializer):
|
|
|
|
|
2012-10-24 14:27:01 +04:00
|
|
|
def validate_content(self, attrs, source):
|
2012-10-24 12:28:10 +04:00
|
|
|
value = attrs[source]
|
|
|
|
if "test" not in value:
|
|
|
|
raise serializers.ValidationError("Test not in value")
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'email': 'tom@example.com',
|
|
|
|
'content': 'A test comment',
|
|
|
|
'created': datetime.datetime(2012, 1, 1)
|
|
|
|
}
|
|
|
|
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializerWithFieldValidator(data=data)
|
2012-10-24 12:28:10 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
|
|
|
|
|
|
|
data['content'] = 'This should not validate'
|
|
|
|
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializerWithFieldValidator(data=data)
|
2012-10-24 12:28:10 +04:00
|
|
|
self.assertFalse(serializer.is_valid())
|
|
|
|
self.assertEquals(serializer.errors, {'content': [u'Test not in value']})
|
|
|
|
|
2012-10-24 14:39:17 +04:00
|
|
|
def test_cross_field_validation(self):
|
|
|
|
|
|
|
|
class CommentSerializerWithCrossFieldValidator(CommentSerializer):
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
if attrs["email"] not in attrs["content"]:
|
|
|
|
raise serializers.ValidationError("Email address not in content")
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'email': 'tom@example.com',
|
|
|
|
'content': 'A comment from tom@example.com',
|
|
|
|
'created': datetime.datetime(2012, 1, 1)
|
|
|
|
}
|
|
|
|
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializerWithCrossFieldValidator(data=data)
|
2012-10-24 14:39:17 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
|
|
|
|
|
|
|
data['content'] = 'A comment from foo@bar.com'
|
|
|
|
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = CommentSerializerWithCrossFieldValidator(data=data)
|
2012-10-24 14:39:17 +04:00
|
|
|
self.assertFalse(serializer.is_valid())
|
|
|
|
self.assertEquals(serializer.errors, {'non_field_errors': [u'Email address not in content']})
|
|
|
|
|
2012-10-30 03:03:24 +04:00
|
|
|
def test_null_is_true_fields(self):
|
|
|
|
"""
|
|
|
|
Omitting a value for null-field should validate.
|
|
|
|
"""
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = PersonSerializer(data={'name': 'marko'})
|
2012-10-30 03:03:24 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
self.assertEquals(serializer.errors, {})
|
|
|
|
|
2012-11-17 00:42:04 +04:00
|
|
|
def test_modelserializer_max_length_exceeded(self):
|
|
|
|
data = {
|
|
|
|
'title': 'x' * 201,
|
|
|
|
}
|
|
|
|
serializer = ActionItemSerializer(data=data)
|
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
2012-11-17 01:43:16 +04:00
|
|
|
self.assertEquals(serializer.errors, {'title': [u'Ensure this value has at most 200 characters (it has 201).']})
|
2012-11-17 00:42:04 +04:00
|
|
|
|
2012-11-18 21:14:21 +04:00
|
|
|
def test_default_modelfield_max_length_exceeded(self):
|
|
|
|
data = {
|
|
|
|
'title': 'Testing "info" field...',
|
|
|
|
'info': 'x' * 13,
|
|
|
|
}
|
|
|
|
serializer = ActionItemSerializer(data=data)
|
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
|
|
|
self.assertEquals(serializer.errors, {'info': [u'Ensure this value has at most 12 characters (it has 13).']})
|
|
|
|
|
2012-12-11 02:10:04 +04:00
|
|
|
|
2012-12-16 01:33:08 +04:00
|
|
|
class PositiveIntegerAsChoiceTests(TestCase):
|
|
|
|
def test_positive_integer_in_json_is_correctly_parsed(self):
|
|
|
|
data = {'some_integer':1}
|
|
|
|
serializer = PositiveIntegerAsChoiceSerializer(data=data)
|
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
|
2012-12-11 02:10:04 +04:00
|
|
|
class ModelValidationTests(TestCase):
|
2012-11-23 02:50:42 +04:00
|
|
|
def test_validate_unique(self):
|
|
|
|
"""
|
2012-12-11 02:10:04 +04:00
|
|
|
Just check if serializers.ModelSerializer handles unique checks via .full_clean()
|
2012-11-23 02:50:42 +04:00
|
|
|
"""
|
|
|
|
serializer = AlbumsSerializer(data={'title': 'a'})
|
|
|
|
serializer.is_valid()
|
|
|
|
serializer.save()
|
|
|
|
second_serializer = AlbumsSerializer(data={'title': 'a'})
|
|
|
|
self.assertFalse(second_serializer.is_valid())
|
2012-11-27 02:39:49 +04:00
|
|
|
self.assertEqual(second_serializer.errors, {'title': [u'Album with this Title already exists.']})
|
2012-11-23 02:50:42 +04:00
|
|
|
|
2012-12-18 16:09:58 +04:00
|
|
|
def test_foreign_key_with_partial(self):
|
|
|
|
"""
|
|
|
|
Test ModelSerializer validation with partial=True
|
|
|
|
|
|
|
|
Specifically test foreign key validation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
album = Album(title='test')
|
|
|
|
album.save()
|
|
|
|
|
|
|
|
class PhotoSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Photo
|
|
|
|
|
|
|
|
photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk})
|
|
|
|
self.assertTrue(photo_serializer.is_valid())
|
|
|
|
photo = photo_serializer.save()
|
|
|
|
|
|
|
|
# Updating only the album (foreign key)
|
|
|
|
photo_serializer = PhotoSerializer(instance=photo, data={'album': album.pk}, partial=True)
|
|
|
|
self.assertTrue(photo_serializer.is_valid())
|
|
|
|
self.assertTrue(photo_serializer.save())
|
|
|
|
|
|
|
|
# Updating only the description
|
|
|
|
photo_serializer = PhotoSerializer(instance=photo,
|
|
|
|
data={'description': 'new'},
|
|
|
|
partial=True)
|
|
|
|
|
|
|
|
self.assertTrue(photo_serializer.is_valid())
|
|
|
|
self.assertTrue(photo_serializer.save())
|
|
|
|
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-11-21 14:07:08 +04:00
|
|
|
class RegexValidationTest(TestCase):
|
|
|
|
def test_create_failed(self):
|
|
|
|
serializer = BookSerializer(data={'isbn': '1234567890'})
|
|
|
|
self.assertFalse(serializer.is_valid())
|
|
|
|
self.assertEquals(serializer.errors, {'isbn': [u'isbn has to be exact 13 numbers']})
|
|
|
|
|
|
|
|
serializer = BookSerializer(data={'isbn': '12345678901234'})
|
|
|
|
self.assertFalse(serializer.is_valid())
|
|
|
|
self.assertEquals(serializer.errors, {'isbn': [u'isbn has to be exact 13 numbers']})
|
|
|
|
|
|
|
|
serializer = BookSerializer(data={'isbn': 'abcdefghijklm'})
|
|
|
|
self.assertFalse(serializer.is_valid())
|
|
|
|
self.assertEquals(serializer.errors, {'isbn': [u'isbn has to be exact 13 numbers']})
|
|
|
|
|
|
|
|
def test_create_success(self):
|
|
|
|
serializer = BookSerializer(data={'isbn': '1234567890123'})
|
|
|
|
self.assertTrue(serializer.is_valid())
|
|
|
|
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
class MetadataTests(TestCase):
|
|
|
|
def test_empty(self):
|
|
|
|
serializer = CommentSerializer()
|
|
|
|
expected = {
|
|
|
|
'email': serializers.CharField,
|
|
|
|
'content': serializers.CharField,
|
|
|
|
'created': serializers.DateTimeField
|
|
|
|
}
|
|
|
|
for field_name, field in expected.items():
|
|
|
|
self.assertTrue(isinstance(serializer.data.fields[field_name], field))
|
2012-10-03 13:51:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ManyToManyTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
class ManyToManySerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ManyToManyModel
|
|
|
|
|
|
|
|
self.serializer_class = ManyToManySerializer
|
|
|
|
|
|
|
|
# An anchor instance to use for the relationship
|
|
|
|
self.anchor = Anchor()
|
|
|
|
self.anchor.save()
|
|
|
|
|
|
|
|
# A model instance with a many to many relationship to the anchor
|
|
|
|
self.instance = ManyToManyModel()
|
|
|
|
self.instance.save()
|
|
|
|
self.instance.rel.add(self.anchor)
|
|
|
|
|
|
|
|
# A serialized representation of the model instance
|
|
|
|
self.data = {'id': 1, 'rel': [self.anchor.id]}
|
|
|
|
|
|
|
|
def test_retrieve(self):
|
2012-10-03 14:14:47 +04:00
|
|
|
"""
|
|
|
|
Serialize an instance of a model with a ManyToMany relationship.
|
|
|
|
"""
|
2012-10-03 13:51:38 +04:00
|
|
|
serializer = self.serializer_class(instance=self.instance)
|
|
|
|
expected = self.data
|
|
|
|
self.assertEquals(serializer.data, expected)
|
|
|
|
|
|
|
|
def test_create(self):
|
2012-10-03 14:14:47 +04:00
|
|
|
"""
|
|
|
|
Create an instance of a model with a ManyToMany relationship.
|
|
|
|
"""
|
2012-10-03 13:51:38 +04:00
|
|
|
data = {'rel': [self.anchor.id]}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=data)
|
2012-10-03 13:51:38 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
2012-10-03 14:08:02 +04:00
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(ManyToManyModel.objects.all()), 2)
|
|
|
|
self.assertEquals(instance.pk, 2)
|
|
|
|
self.assertEquals(list(instance.rel.all()), [self.anchor])
|
2012-10-03 14:14:47 +04:00
|
|
|
|
|
|
|
def test_update(self):
|
|
|
|
"""
|
|
|
|
Update an instance of a model with a ManyToMany relationship.
|
|
|
|
"""
|
|
|
|
new_anchor = Anchor()
|
|
|
|
new_anchor.save()
|
|
|
|
data = {'rel': [self.anchor.id, new_anchor.id]}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(self.instance, data=data)
|
2012-10-03 14:14:47 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(ManyToManyModel.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
self.assertEquals(list(instance.rel.all()), [self.anchor, new_anchor])
|
2012-10-03 13:51:38 +04:00
|
|
|
|
2012-10-03 14:50:08 +04:00
|
|
|
def test_create_empty_relationship(self):
|
|
|
|
"""
|
|
|
|
Create an instance of a model with a ManyToMany relationship,
|
|
|
|
containing no items.
|
|
|
|
"""
|
|
|
|
data = {'rel': []}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=data)
|
2012-10-03 14:50:08 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(ManyToManyModel.objects.all()), 2)
|
|
|
|
self.assertEquals(instance.pk, 2)
|
|
|
|
self.assertEquals(list(instance.rel.all()), [])
|
|
|
|
|
|
|
|
def test_update_empty_relationship(self):
|
|
|
|
"""
|
|
|
|
Update an instance of a model with a ManyToMany relationship,
|
|
|
|
containing no items.
|
|
|
|
"""
|
|
|
|
new_anchor = Anchor()
|
|
|
|
new_anchor.save()
|
|
|
|
data = {'rel': []}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(self.instance, data=data)
|
2012-10-03 14:50:08 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(ManyToManyModel.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
self.assertEquals(list(instance.rel.all()), [])
|
|
|
|
|
|
|
|
def test_create_empty_relationship_flat_data(self):
|
|
|
|
"""
|
|
|
|
Create an instance of a model with a ManyToMany relationship,
|
|
|
|
containing no items, using a representation that does not support
|
|
|
|
lists (eg form data).
|
|
|
|
"""
|
|
|
|
data = {'rel': ''}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=data)
|
2012-10-03 14:50:08 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(ManyToManyModel.objects.all()), 2)
|
|
|
|
self.assertEquals(instance.pk, 2)
|
|
|
|
self.assertEquals(list(instance.rel.all()), [])
|
2012-10-26 16:20:30 +04:00
|
|
|
|
|
|
|
|
2012-10-23 01:24:26 +04:00
|
|
|
class ReadOnlyManyToManyTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
class ReadOnlyManyToManySerializer(serializers.ModelSerializer):
|
2012-10-29 00:21:45 +04:00
|
|
|
rel = serializers.ManyRelatedField(read_only=True)
|
2012-10-26 16:20:30 +04:00
|
|
|
|
2012-10-23 01:24:26 +04:00
|
|
|
class Meta:
|
|
|
|
model = ReadOnlyManyToManyModel
|
|
|
|
|
|
|
|
self.serializer_class = ReadOnlyManyToManySerializer
|
|
|
|
|
|
|
|
# An anchor instance to use for the relationship
|
|
|
|
self.anchor = Anchor()
|
|
|
|
self.anchor.save()
|
|
|
|
|
|
|
|
# A model instance with a many to many relationship to the anchor
|
|
|
|
self.instance = ReadOnlyManyToManyModel()
|
|
|
|
self.instance.save()
|
|
|
|
self.instance.rel.add(self.anchor)
|
|
|
|
|
|
|
|
# A serialized representation of the model instance
|
|
|
|
self.data = {'rel': [self.anchor.id], 'id': 1, 'text': 'anchor'}
|
|
|
|
|
|
|
|
def test_update(self):
|
|
|
|
"""
|
|
|
|
Attempt to update an instance of a model with a ManyToMany
|
2012-10-29 00:21:45 +04:00
|
|
|
relationship. Not updated due to read_only=True
|
2012-10-23 01:24:26 +04:00
|
|
|
"""
|
|
|
|
new_anchor = Anchor()
|
|
|
|
new_anchor.save()
|
|
|
|
data = {'rel': [self.anchor.id, new_anchor.id]}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(self.instance, data=data)
|
2012-10-23 01:24:26 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(ReadOnlyManyToManyModel.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
# rel is still as original (1 entry)
|
|
|
|
self.assertEquals(list(instance.rel.all()), [self.anchor])
|
|
|
|
|
|
|
|
def test_update_without_relationship(self):
|
|
|
|
"""
|
|
|
|
Attempt to update an instance of a model where many to ManyToMany
|
2012-10-29 00:21:45 +04:00
|
|
|
relationship is not supplied. Not updated due to read_only=True
|
2012-10-23 01:24:26 +04:00
|
|
|
"""
|
|
|
|
new_anchor = Anchor()
|
|
|
|
new_anchor.save()
|
|
|
|
data = {}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(self.instance, data=data)
|
2012-10-23 01:24:26 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(ReadOnlyManyToManyModel.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
# rel is still as original (1 entry)
|
|
|
|
self.assertEquals(list(instance.rel.all()), [self.anchor])
|
2012-10-03 15:16:30 +04:00
|
|
|
|
2012-10-04 12:29:12 +04:00
|
|
|
|
|
|
|
class DefaultValueTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
class DefaultValueSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = DefaultValueModel
|
|
|
|
|
|
|
|
self.serializer_class = DefaultValueSerializer
|
|
|
|
self.objects = DefaultValueModel.objects
|
|
|
|
|
|
|
|
def test_create_using_default(self):
|
|
|
|
data = {}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=data)
|
2012-10-04 12:29:12 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(self.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
self.assertEquals(instance.text, 'foobar')
|
|
|
|
|
|
|
|
def test_create_overriding_default(self):
|
|
|
|
data = {'text': 'overridden'}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=data)
|
2012-10-04 12:29:12 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(self.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
self.assertEquals(instance.text, 'overridden')
|
|
|
|
|
|
|
|
|
|
|
|
class CallableDefaultValueTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
class CallableDefaultValueSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = CallableDefaultValueModel
|
|
|
|
|
|
|
|
self.serializer_class = CallableDefaultValueSerializer
|
|
|
|
self.objects = CallableDefaultValueModel.objects
|
|
|
|
|
|
|
|
def test_create_using_default(self):
|
|
|
|
data = {}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=data)
|
2012-10-04 12:29:12 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(self.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
self.assertEquals(instance.text, 'foobar')
|
|
|
|
|
|
|
|
def test_create_overriding_default(self):
|
|
|
|
data = {'text': 'overridden'}
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=data)
|
2012-10-04 12:29:12 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
instance = serializer.save()
|
|
|
|
self.assertEquals(len(self.objects.all()), 1)
|
|
|
|
self.assertEquals(instance.pk, 1)
|
|
|
|
self.assertEquals(instance.text, 'overridden')
|
2012-10-22 18:12:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ManyRelatedTests(TestCase):
|
2012-11-20 13:49:54 +04:00
|
|
|
def test_reverse_relations(self):
|
|
|
|
post = BlogPost.objects.create(title="Test blog post")
|
|
|
|
post.blogpostcomment_set.create(text="I hate this blog post")
|
|
|
|
post.blogpostcomment_set.create(text="I love this blog post")
|
2012-10-22 18:12:25 +04:00
|
|
|
|
|
|
|
class BlogPostCommentSerializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
|
|
|
|
class BlogPostSerializer(serializers.Serializer):
|
|
|
|
title = serializers.CharField()
|
|
|
|
comments = BlogPostCommentSerializer(source='blogpostcomment_set')
|
|
|
|
|
2012-11-20 13:49:54 +04:00
|
|
|
serializer = BlogPostSerializer(instance=post)
|
2012-10-22 18:12:25 +04:00
|
|
|
expected = {
|
|
|
|
'title': 'Test blog post',
|
|
|
|
'comments': [
|
|
|
|
{'text': 'I hate this blog post'},
|
|
|
|
{'text': 'I love this blog post'}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-10-30 15:03:03 +04:00
|
|
|
|
2012-11-20 13:41:36 +04:00
|
|
|
def test_callable_source(self):
|
|
|
|
post = BlogPost.objects.create(title="Test blog post")
|
|
|
|
post.blogpostcomment_set.create(text="I love this blog post")
|
|
|
|
|
2012-11-20 13:49:54 +04:00
|
|
|
class BlogPostCommentSerializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
|
|
|
|
class BlogPostSerializer(serializers.Serializer):
|
|
|
|
title = serializers.CharField()
|
|
|
|
first_comment = BlogPostCommentSerializer(source='get_first_comment')
|
|
|
|
|
|
|
|
serializer = BlogPostSerializer(post)
|
2012-11-20 13:41:36 +04:00
|
|
|
|
|
|
|
expected = {
|
|
|
|
'title': 'Test blog post',
|
|
|
|
'first_comment': {'text': 'I love this blog post'}
|
|
|
|
}
|
|
|
|
self.assertEqual(serializer.data, expected)
|
|
|
|
|
2012-10-30 15:03:03 +04:00
|
|
|
|
2012-11-30 03:34:46 +04:00
|
|
|
class RelatedTraversalTest(TestCase):
|
|
|
|
def test_nested_traversal(self):
|
|
|
|
user = Person.objects.create(name="django")
|
|
|
|
post = BlogPost.objects.create(title="Test blog post", writer=user)
|
|
|
|
post.blogpostcomment_set.create(text="I love this blog post")
|
|
|
|
|
|
|
|
from rest_framework.tests.models import BlogPostComment
|
|
|
|
|
|
|
|
class PersonSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
fields = ("name", "age")
|
|
|
|
|
|
|
|
class BlogPostCommentSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BlogPostComment
|
|
|
|
fields = ("text", "post_owner")
|
|
|
|
|
|
|
|
text = serializers.CharField()
|
|
|
|
post_owner = PersonSerializer(source='blog_post.writer')
|
|
|
|
|
|
|
|
class BlogPostSerializer(serializers.Serializer):
|
|
|
|
title = serializers.CharField()
|
|
|
|
comments = BlogPostCommentSerializer(source='blogpostcomment_set')
|
|
|
|
|
|
|
|
serializer = BlogPostSerializer(instance=post)
|
|
|
|
|
|
|
|
expected = {
|
2012-11-30 03:37:21 +04:00
|
|
|
'title': u'Test blog post',
|
2012-11-30 03:34:46 +04:00
|
|
|
'comments': [{
|
2012-11-30 03:37:21 +04:00
|
|
|
'text': u'I love this blog post',
|
2012-11-30 03:34:46 +04:00
|
|
|
'post_owner': {
|
2012-11-30 03:37:21 +04:00
|
|
|
"name": u"django",
|
2012-11-30 03:34:46 +04:00
|
|
|
"age": None
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(serializer.data, expected)
|
|
|
|
|
|
|
|
|
2012-11-19 21:22:17 +04:00
|
|
|
class SerializerMethodFieldTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
class BoopSerializer(serializers.Serializer):
|
|
|
|
beep = serializers.SerializerMethodField('get_beep')
|
|
|
|
boop = serializers.Field()
|
|
|
|
boop_count = serializers.SerializerMethodField('get_boop_count')
|
|
|
|
|
|
|
|
def get_beep(self, obj):
|
|
|
|
return 'hello!'
|
|
|
|
|
|
|
|
def get_boop_count(self, obj):
|
|
|
|
return len(obj.boop)
|
|
|
|
|
|
|
|
self.serializer_class = BoopSerializer
|
|
|
|
|
|
|
|
def test_serializer_method_field(self):
|
|
|
|
|
|
|
|
class MyModel(object):
|
|
|
|
boop = ['a', 'b', 'c']
|
|
|
|
|
|
|
|
source_data = MyModel()
|
|
|
|
|
|
|
|
serializer = self.serializer_class(source_data)
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
'beep': u'hello!',
|
|
|
|
'boop': [u'a', u'b', u'c'],
|
|
|
|
'boop_count': 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(serializer.data, expected)
|
|
|
|
|
2012-10-30 15:03:03 +04:00
|
|
|
|
|
|
|
# Test for issue #324
|
|
|
|
class BlankFieldTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
class BlankFieldModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BlankFieldModel
|
|
|
|
|
|
|
|
class BlankFieldSerializer(serializers.Serializer):
|
|
|
|
title = serializers.CharField(blank=True)
|
|
|
|
|
|
|
|
class NotBlankFieldModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BasicModel
|
|
|
|
|
|
|
|
class NotBlankFieldSerializer(serializers.Serializer):
|
|
|
|
title = serializers.CharField()
|
|
|
|
|
|
|
|
self.model_serializer_class = BlankFieldModelSerializer
|
|
|
|
self.serializer_class = BlankFieldSerializer
|
|
|
|
self.not_blank_model_serializer_class = NotBlankFieldModelSerializer
|
|
|
|
self.not_blank_serializer_class = NotBlankFieldSerializer
|
|
|
|
self.data = {'title': ''}
|
|
|
|
|
|
|
|
def test_create_blank_field(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.serializer_class(data=self.data)
|
2012-10-30 15:03:03 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
|
|
|
|
def test_create_model_blank_field(self):
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.model_serializer_class(data=self.data)
|
2012-10-30 15:03:03 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
|
2012-12-20 03:12:27 +04:00
|
|
|
def test_create_model_null_field(self):
|
|
|
|
serializer = self.model_serializer_class(data={'title': None})
|
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
|
2012-10-30 15:03:03 +04:00
|
|
|
def test_create_not_blank_field(self):
|
|
|
|
"""
|
|
|
|
Test to ensure blank data in a field not marked as blank=True
|
|
|
|
is considered invalid in a non-model serializer
|
|
|
|
"""
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.not_blank_serializer_class(data=self.data)
|
2012-10-30 15:03:03 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
|
|
|
|
|
|
|
def test_create_model_not_blank_field(self):
|
|
|
|
"""
|
|
|
|
Test to ensure blank data in a field not marked as blank=True
|
|
|
|
is considered invalid in a model serializer
|
|
|
|
"""
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = self.not_blank_model_serializer_class(data=self.data)
|
2012-10-30 15:03:03 +04:00
|
|
|
self.assertEquals(serializer.is_valid(), False)
|
2012-12-06 04:43:47 +04:00
|
|
|
|
2012-12-17 18:49:45 +04:00
|
|
|
def test_create_model_null_field(self):
|
|
|
|
serializer = self.model_serializer_class(data={})
|
|
|
|
self.assertEquals(serializer.is_valid(), True)
|
|
|
|
|
2012-12-06 04:43:47 +04:00
|
|
|
|
|
|
|
#test for issue #460
|
|
|
|
class SerializerPickleTests(TestCase):
|
2012-12-06 23:45:50 +04:00
|
|
|
"""
|
|
|
|
Test pickleability of the output of Serializers
|
2012-12-06 04:43:47 +04:00
|
|
|
"""
|
|
|
|
def test_pickle_simple_model_serializer_data(self):
|
2012-12-06 23:45:50 +04:00
|
|
|
"""
|
|
|
|
Test simple serializer
|
2012-12-06 04:43:47 +04:00
|
|
|
"""
|
|
|
|
pickle.dumps(PersonSerializer(Person(name="Methusela", age=969)).data)
|
|
|
|
|
|
|
|
def test_pickle_inner_serializer(self):
|
2012-12-06 23:45:50 +04:00
|
|
|
"""
|
|
|
|
Test pickling a serializer whose resulting .data (a SortedDictWithMetadata) will
|
|
|
|
have unpickleable meta data--in order to make sure metadata doesn't get pulled into the pickle.
|
|
|
|
See DictWithMetadata.__getstate__
|
2012-12-06 04:43:47 +04:00
|
|
|
"""
|
|
|
|
class InnerPersonSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
fields = ('name', 'age')
|
|
|
|
pickle.dumps(InnerPersonSerializer(Person(name="Noah", age=950)).data)
|
2012-12-11 19:14:52 +04:00
|
|
|
|
2012-12-12 01:07:25 +04:00
|
|
|
|
2012-12-11 19:14:52 +04:00
|
|
|
class DepthTest(TestCase):
|
2012-12-12 01:07:25 +04:00
|
|
|
def test_implicit_nesting(self):
|
|
|
|
writer = Person.objects.create(name="django", age=1)
|
|
|
|
post = BlogPost.objects.create(title="Test blog post", writer=writer)
|
|
|
|
|
|
|
|
class BlogPostSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BlogPost
|
|
|
|
depth = 1
|
|
|
|
|
|
|
|
serializer = BlogPostSerializer(instance=post)
|
|
|
|
expected = {'id': 1, 'title': u'Test blog post',
|
|
|
|
'writer': {'id': 1, 'name': u'django', 'age': 1}}
|
|
|
|
|
|
|
|
self.assertEqual(serializer.data, expected)
|
|
|
|
|
|
|
|
def test_explicit_nesting(self):
|
|
|
|
writer = Person.objects.create(name="django", age=1)
|
|
|
|
post = BlogPost.objects.create(title="Test blog post", writer=writer)
|
2012-12-11 19:14:52 +04:00
|
|
|
|
|
|
|
class PersonSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
|
|
|
|
class BlogPostSerializer(serializers.ModelSerializer):
|
2012-12-12 01:07:25 +04:00
|
|
|
writer = PersonSerializer()
|
|
|
|
|
2012-12-11 19:14:52 +04:00
|
|
|
class Meta:
|
|
|
|
model = BlogPost
|
|
|
|
|
|
|
|
serializer = BlogPostSerializer(instance=post)
|
|
|
|
expected = {'id': 1, 'title': u'Test blog post',
|
2012-12-12 01:07:25 +04:00
|
|
|
'writer': {'id': 1, 'name': u'django', 'age': 1}}
|
2012-12-11 19:14:52 +04:00
|
|
|
|
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-13 00:44:55 +04:00
|
|
|
|
|
|
|
|
|
|
|
class NestedSerializerContextTests(TestCase):
|
|
|
|
|
|
|
|
def test_nested_serializer_context(self):
|
|
|
|
"""
|
|
|
|
Regression for #497
|
|
|
|
|
|
|
|
https://github.com/tomchristie/django-rest-framework/issues/497
|
|
|
|
"""
|
|
|
|
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 "success"
|
|
|
|
|
|
|
|
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 "success"
|
|
|
|
|
|
|
|
class AlbumCollection(object):
|
|
|
|
albums = None
|
|
|
|
|
|
|
|
class AlbumCollectionSerializer(serializers.Serializer):
|
|
|
|
albums = AlbumSerializer(source="albums")
|
|
|
|
|
|
|
|
album1 = Album.objects.create(title="album 1")
|
|
|
|
album2 = Album.objects.create(title="album 2")
|
|
|
|
Photo.objects.create(description="Bigfoot", album=album1)
|
|
|
|
Photo.objects.create(description="Unicorn", album=album1)
|
|
|
|
Photo.objects.create(description="Yeti", album=album2)
|
|
|
|
Photo.objects.create(description="Sasquatch", album=album2)
|
|
|
|
album_collection = AlbumCollection()
|
|
|
|
album_collection.albums = [album1, album2]
|
|
|
|
|
|
|
|
# This will raise RuntimeError if context doesn't get passed correctly to the nested Serializers
|
|
|
|
AlbumCollectionSerializer(album_collection, context={'context_item': 'album context'}).data
|