added tests

This commit is contained in:
Stephan Groß 2012-11-21 11:07:08 +01:00
parent 86484668f6
commit ed713d0354
2 changed files with 31 additions and 1 deletions

View File

@ -154,3 +154,7 @@ class BlankFieldModel(RESTFrameworkModel):
# Model for issue #380 # Model for issue #380
class OptionalRelationModel(RESTFrameworkModel): class OptionalRelationModel(RESTFrameworkModel):
other = models.ForeignKey('OptionalRelationModel', blank=True, null=True) other = models.ForeignKey('OptionalRelationModel', blank=True, null=True)
# Model for RegexField
class Book(RESTFrameworkModel):
isbn = models.CharField(max_length=13)

View File

@ -2,7 +2,7 @@ import datetime
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 (ActionItem, Anchor, BasicModel, from rest_framework.tests.models import (ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, CallableDefaultValueModel, DefaultValueModel, BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel,
ManyToManyModel, Person, ReadOnlyManyToManyModel) ManyToManyModel, Person, ReadOnlyManyToManyModel)
@ -40,6 +40,13 @@ class CommentSerializer(serializers.Serializer):
return instance 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 ActionItemSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ActionItem model = ActionItem
@ -240,6 +247,25 @@ class ValidationTests(TestCase):
self.assertEquals(serializer.errors, {}) 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): class MetadataTests(TestCase):
def test_empty(self): def test_empty(self):
serializer = CommentSerializer() serializer = CommentSerializer()