mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 19:43:59 +03:00
Added tests for TextField
This commit is contained in:
parent
241be38340
commit
36cc56bc9d
|
@ -381,7 +381,7 @@ class ModelSerializer(Serializer):
|
||||||
models.DateField: DateField,
|
models.DateField: DateField,
|
||||||
models.EmailField: EmailField,
|
models.EmailField: EmailField,
|
||||||
models.CharField: CharField,
|
models.CharField: CharField,
|
||||||
models.TextField: TextField,
|
models.TextField: TextField,
|
||||||
models.CommaSeparatedIntegerField: CharField,
|
models.CommaSeparatedIntegerField: CharField,
|
||||||
models.BooleanField: BooleanField,
|
models.BooleanField: BooleanField,
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,26 @@ class CommentSerializer(serializers.Serializer):
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
class LongText(object):
|
||||||
|
def __init__(self, content):
|
||||||
|
self.content = content
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return all([getattr(self, attr) == getattr(other, attr)
|
||||||
|
for attr in ('content',)])
|
||||||
|
|
||||||
|
|
||||||
|
class LongTextSerializer(serializers.Serializer):
|
||||||
|
content = serializers.TextField()
|
||||||
|
|
||||||
|
def restore_object(self, data, instance=None):
|
||||||
|
if instance is None:
|
||||||
|
return LongText(**data)
|
||||||
|
for key, val in data.items():
|
||||||
|
setattr(instance, key, val)
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class BasicTests(TestCase):
|
class BasicTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.comment = Comment(
|
self.comment = Comment(
|
||||||
|
@ -82,6 +102,7 @@ class ValidationTests(TestCase):
|
||||||
'content': 'x' * 1001,
|
'content': 'x' * 1001,
|
||||||
'created': datetime.datetime(2012, 1, 1)
|
'created': datetime.datetime(2012, 1, 1)
|
||||||
}
|
}
|
||||||
|
self.long_text = LongText('test test test test')
|
||||||
|
|
||||||
def test_create(self):
|
def test_create(self):
|
||||||
serializer = CommentSerializer(self.data)
|
serializer = CommentSerializer(self.data)
|
||||||
|
@ -102,6 +123,14 @@ class ValidationTests(TestCase):
|
||||||
self.assertEquals(serializer.is_valid(), False)
|
self.assertEquals(serializer.is_valid(), False)
|
||||||
self.assertEquals(serializer.errors, {'email': [u'This field is required.']})
|
self.assertEquals(serializer.errors, {'email': [u'This field is required.']})
|
||||||
|
|
||||||
|
def test_update_long_text(self):
|
||||||
|
data = {
|
||||||
|
'content' : 'Lorem ipsum dolor sit amet.'
|
||||||
|
}
|
||||||
|
serializer = LongTextSerializer(data, self.long_text)
|
||||||
|
self.assertEquals(serializer.is_valid(), True)
|
||||||
|
self.assertEquals(data['content'], self.long_text.content)
|
||||||
|
|
||||||
|
|
||||||
class MetadataTests(TestCase):
|
class MetadataTests(TestCase):
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user