mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
added tests
This commit is contained in:
parent
86484668f6
commit
ed713d0354
|
@ -154,3 +154,7 @@ class BlankFieldModel(RESTFrameworkModel):
|
|||
# Model for issue #380
|
||||
class OptionalRelationModel(RESTFrameworkModel):
|
||||
other = models.ForeignKey('OptionalRelationModel', blank=True, null=True)
|
||||
|
||||
# Model for RegexField
|
||||
class Book(RESTFrameworkModel):
|
||||
isbn = models.CharField(max_length=13)
|
|
@ -2,7 +2,7 @@ import datetime
|
|||
from django.test import TestCase
|
||||
from rest_framework import serializers
|
||||
from rest_framework.tests.models import (ActionItem, Anchor, BasicModel,
|
||||
BlankFieldModel, BlogPost, CallableDefaultValueModel, DefaultValueModel,
|
||||
BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel,
|
||||
ManyToManyModel, Person, ReadOnlyManyToManyModel)
|
||||
|
||||
|
||||
|
@ -40,6 +40,13 @@ class CommentSerializer(serializers.Serializer):
|
|||
return instance
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
class ActionItemSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ActionItem
|
||||
|
@ -240,6 +247,25 @@ class ValidationTests(TestCase):
|
|||
self.assertEquals(serializer.errors, {})
|
||||
|
||||
|
||||
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())
|
||||
|
||||
|
||||
class MetadataTests(TestCase):
|
||||
def test_empty(self):
|
||||
serializer = CommentSerializer()
|
||||
|
|
Loading…
Reference in New Issue
Block a user