PageRenderTime 56ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/modeltests/model_forms/tests.py

https://code.google.com/p/mango-py/
Python | 199 lines | 196 code | 2 blank | 1 comment | 11 complexity | 97e3706a13cf8941e0e4ce1cb5d127af MD5 | raw file
  1import datetime
  2from django.test import TestCase
  3from django import forms
  4from models import Category, Writer, Book, DerivedBook, Post, FlexibleDatePost
  5from mforms import (ProductForm, PriceForm, BookForm, DerivedBookForm,
  6                   ExplicitPKForm, PostForm, DerivedPostForm, CustomWriterForm,
  7                   FlexDatePostForm)
  8
  9
 10class IncompleteCategoryFormWithFields(forms.ModelForm):
 11    """
 12    A form that replaces the model's url field with a custom one. This should
 13    prevent the model field's validation from being called.
 14    """
 15    url = forms.CharField(required=False)
 16
 17    class Meta:
 18        fields = ('name', 'slug')
 19        model = Category
 20
 21class IncompleteCategoryFormWithExclude(forms.ModelForm):
 22    """
 23    A form that replaces the model's url field with a custom one. This should
 24    prevent the model field's validation from being called.
 25    """
 26    url = forms.CharField(required=False)
 27
 28    class Meta:
 29        exclude = ['url']
 30        model = Category
 31
 32
 33class ValidationTest(TestCase):
 34    def test_validates_with_replaced_field_not_specified(self):
 35        form = IncompleteCategoryFormWithFields(data={'name': 'some name', 'slug': 'some-slug'})
 36        assert form.is_valid()
 37
 38    def test_validates_with_replaced_field_excluded(self):
 39        form = IncompleteCategoryFormWithExclude(data={'name': 'some name', 'slug': 'some-slug'})
 40        assert form.is_valid()
 41
 42    def test_notrequired_overrides_notblank(self):
 43        form = CustomWriterForm({})
 44        assert form.is_valid()
 45
 46# unique/unique_together validation
 47class UniqueTest(TestCase):
 48    def setUp(self):
 49        self.writer = Writer.objects.create(name='Mike Royko')
 50
 51    def test_simple_unique(self):
 52        form = ProductForm({'slug': 'teddy-bear-blue'})
 53        self.assertTrue(form.is_valid())
 54        obj = form.save()
 55        form = ProductForm({'slug': 'teddy-bear-blue'})
 56        self.assertEqual(len(form.errors), 1)
 57        self.assertEqual(form.errors['slug'], [u'Product with this Slug already exists.'])
 58        form = ProductForm({'slug': 'teddy-bear-blue'}, instance=obj)
 59        self.assertTrue(form.is_valid())
 60
 61    def test_unique_together(self):
 62        """ModelForm test of unique_together constraint"""
 63        form = PriceForm({'price': '6.00', 'quantity': '1'})
 64        self.assertTrue(form.is_valid())
 65        form.save()
 66        form = PriceForm({'price': '6.00', 'quantity': '1'})
 67        self.assertFalse(form.is_valid())
 68        self.assertEqual(len(form.errors), 1)
 69        self.assertEqual(form.errors['__all__'], [u'Price with this Price and Quantity already exists.'])
 70
 71    def test_unique_null(self):
 72        title = 'I May Be Wrong But I Doubt It'
 73        form = BookForm({'title': title, 'author': self.writer.pk})
 74        self.assertTrue(form.is_valid())
 75        form.save()
 76        form = BookForm({'title': title, 'author': self.writer.pk})
 77        self.assertFalse(form.is_valid())
 78        self.assertEqual(len(form.errors), 1)
 79        self.assertEqual(form.errors['__all__'], [u'Book with this Title and Author already exists.'])
 80        form = BookForm({'title': title})
 81        self.assertTrue(form.is_valid())
 82        form.save()
 83        form = BookForm({'title': title})
 84        self.assertTrue(form.is_valid())
 85
 86    def test_inherited_unique(self):
 87        title = 'Boss'
 88        Book.objects.create(title=title, author=self.writer, special_id=1)
 89        form = DerivedBookForm({'title': 'Other', 'author': self.writer.pk, 'special_id': u'1', 'isbn': '12345'})
 90        self.assertFalse(form.is_valid())
 91        self.assertEqual(len(form.errors), 1)
 92        self.assertEqual(form.errors['special_id'], [u'Book with this Special id already exists.'])
 93
 94    def test_inherited_unique_together(self):
 95        title = 'Boss'
 96        form = BookForm({'title': title, 'author': self.writer.pk})
 97        self.assertTrue(form.is_valid())
 98        form.save()
 99        form = DerivedBookForm({'title': title, 'author': self.writer.pk, 'isbn': '12345'})
100        self.assertFalse(form.is_valid())
101        self.assertEqual(len(form.errors), 1)
102        self.assertEqual(form.errors['__all__'], [u'Book with this Title and Author already exists.'])
103
104    def test_abstract_inherited_unique(self):
105        title = 'Boss'
106        isbn = '12345'
107        dbook = DerivedBook.objects.create(title=title, author=self.writer, isbn=isbn)
108        form = DerivedBookForm({'title': 'Other', 'author': self.writer.pk, 'isbn': isbn})
109        self.assertFalse(form.is_valid())
110        self.assertEqual(len(form.errors), 1)
111        self.assertEqual(form.errors['isbn'], [u'Derived book with this Isbn already exists.'])
112
113    def test_abstract_inherited_unique_together(self):
114        title = 'Boss'
115        isbn = '12345'
116        dbook = DerivedBook.objects.create(title=title, author=self.writer, isbn=isbn)
117        form = DerivedBookForm({'title': 'Other', 'author': self.writer.pk, 'isbn': '9876', 'suffix1': u'0', 'suffix2': u'0'})
118        self.assertFalse(form.is_valid())
119        self.assertEqual(len(form.errors), 1)
120        self.assertEqual(form.errors['__all__'], [u'Derived book with this Suffix1 and Suffix2 already exists.'])
121
122    def test_explicitpk_unspecified(self):
123        """Test for primary_key being in the form and failing validation."""
124        form = ExplicitPKForm({'key': u'', 'desc': u'' })
125        self.assertFalse(form.is_valid())
126
127    def test_explicitpk_unique(self):
128        """Ensure keys and blank character strings are tested for uniqueness."""
129        form = ExplicitPKForm({'key': u'key1', 'desc': u''})
130        self.assertTrue(form.is_valid())
131        form.save()
132        form = ExplicitPKForm({'key': u'key1', 'desc': u''})
133        self.assertFalse(form.is_valid())
134        self.assertEqual(len(form.errors), 3)
135        self.assertEqual(form.errors['__all__'], [u'Explicit pk with this Key and Desc already exists.'])
136        self.assertEqual(form.errors['desc'], [u'Explicit pk with this Desc already exists.'])
137        self.assertEqual(form.errors['key'], [u'Explicit pk with this Key already exists.'])
138
139    def test_unique_for_date(self):
140        p = Post.objects.create(title="Django 1.0 is released",
141            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
142        form = PostForm({'title': "Django 1.0 is released", 'posted': '2008-09-03'})
143        self.assertFalse(form.is_valid())
144        self.assertEqual(len(form.errors), 1)
145        self.assertEqual(form.errors['title'], [u'Title must be unique for Posted date.'])
146        form = PostForm({'title': "Work on Django 1.1 begins", 'posted': '2008-09-03'})
147        self.assertTrue(form.is_valid())
148        form = PostForm({'title': "Django 1.0 is released", 'posted': '2008-09-04'})
149        self.assertTrue(form.is_valid())
150        form = PostForm({'slug': "Django 1.0", 'posted': '2008-01-01'})
151        self.assertFalse(form.is_valid())
152        self.assertEqual(len(form.errors), 1)
153        self.assertEqual(form.errors['slug'], [u'Slug must be unique for Posted year.'])
154        form = PostForm({'subtitle': "Finally", 'posted': '2008-09-30'})
155        self.assertFalse(form.is_valid())
156        self.assertEqual(form.errors['subtitle'], [u'Subtitle must be unique for Posted month.'])
157        form = PostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
158            "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
159        self.assertTrue(form.is_valid())
160        form = PostForm({'title': "Django 1.0 is released"})
161        self.assertFalse(form.is_valid())
162        self.assertEqual(len(form.errors), 1)
163        self.assertEqual(form.errors['posted'], [u'This field is required.'])
164
165    def test_inherited_unique_for_date(self):
166        p = Post.objects.create(title="Django 1.0 is released",
167            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
168        form = DerivedPostForm({'title': "Django 1.0 is released", 'posted': '2008-09-03'})
169        self.assertFalse(form.is_valid())
170        self.assertEqual(len(form.errors), 1)
171        self.assertEqual(form.errors['title'], [u'Title must be unique for Posted date.'])
172        form = DerivedPostForm({'title': "Work on Django 1.1 begins", 'posted': '2008-09-03'})
173        self.assertTrue(form.is_valid())
174        form = DerivedPostForm({'title': "Django 1.0 is released", 'posted': '2008-09-04'})
175        self.assertTrue(form.is_valid())
176        form = DerivedPostForm({'slug': "Django 1.0", 'posted': '2008-01-01'})
177        self.assertFalse(form.is_valid())
178        self.assertEqual(len(form.errors), 1)
179        self.assertEqual(form.errors['slug'], [u'Slug must be unique for Posted year.'])
180        form = DerivedPostForm({'subtitle': "Finally", 'posted': '2008-09-30'})
181        self.assertFalse(form.is_valid())
182        self.assertEqual(form.errors['subtitle'], [u'Subtitle must be unique for Posted month.'])
183        form = DerivedPostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
184            "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
185        self.assertTrue(form.is_valid())
186
187    def test_unique_for_date_with_nullable_date(self):
188        p = FlexibleDatePost.objects.create(title="Django 1.0 is released",
189            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
190
191        form = FlexDatePostForm({'title': "Django 1.0 is released"})
192        self.assertTrue(form.is_valid())
193        form = FlexDatePostForm({'slug': "Django 1.0"})
194        self.assertTrue(form.is_valid())
195        form = FlexDatePostForm({'subtitle': "Finally"})
196        self.assertTrue(form.is_valid())
197        form = FlexDatePostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
198            "slug": "Django 1.0"}, instance=p)
199        self.assertTrue(form.is_valid())