2010-02-23 07:06:09 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
from django import forms
|
|
|
|
from models import Category
|
|
|
|
|
|
|
|
|
2010-02-26 01:18:27 +08:00
|
|
|
class IncompleteCategoryFormWithFields(forms.ModelForm):
|
2010-02-23 07:06:09 +08:00
|
|
|
"""
|
|
|
|
A form that replaces the model's url field with a custom one. This should
|
|
|
|
prevent the model field's validation from being called.
|
|
|
|
"""
|
|
|
|
url = forms.CharField(required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
fields = ('name', 'slug')
|
|
|
|
model = Category
|
|
|
|
|
2010-02-26 01:18:27 +08:00
|
|
|
class IncompleteCategoryFormWithExclude(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form that replaces the model's url field with a custom one. This should
|
|
|
|
prevent the model field's validation from being called.
|
|
|
|
"""
|
|
|
|
url = forms.CharField(required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
exclude = ['url']
|
|
|
|
model = Category
|
|
|
|
|
|
|
|
|
2010-02-23 07:06:09 +08:00
|
|
|
class ValidationTest(TestCase):
|
2010-02-26 01:18:27 +08:00
|
|
|
def test_validates_with_replaced_field_not_specified(self):
|
|
|
|
form = IncompleteCategoryFormWithFields(data={'name': 'some name', 'slug': 'some-slug'})
|
|
|
|
assert form.is_valid()
|
|
|
|
|
|
|
|
def test_validates_with_replaced_field_excluded(self):
|
|
|
|
form = IncompleteCategoryFormWithExclude(data={'name': 'some name', 'slug': 'some-slug'})
|
2010-02-23 07:06:09 +08:00
|
|
|
assert form.is_valid()
|
|
|
|
|