| 116 | |
| 117 | class EditionForm(forms.ModelForm): |
| 118 | author = forms.ModelChoiceField(queryset=Person.objects.all()) |
| 119 | publication = forms.ModelChoiceField(queryset=Publication.objects.all()) |
| 120 | edition = forms.IntegerField() |
| 121 | isbn = forms.CharField(max_length=13) |
| 122 | |
| 123 | class Meta: |
| 124 | model = Edition |
| 125 | |
| 126 | class UniqueErrorsTests(TestCase): |
| 127 | |
| 128 | def setUp(self): |
| 129 | self.author1 = Person.objects.create(name=u'Author #1') |
| 130 | self.author2 = Person.objects.create(name=u'Author #2') |
| 131 | self.pub1 = Publication.objects.create(title='Pub #1', date_published=date(2000, 10, 31)) |
| 132 | self.pub2 = Publication.objects.create(title='Pub #2', date_published=date(2004, 1, 5)) |
| 133 | form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161484100'}) |
| 134 | form.save() |
| 135 | |
| 136 | def test_unique_error_message(self): |
| 137 | form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub2.pk, 'edition': 1, 'isbn': '9783161484100'}) |
| 138 | self.assertEquals(form.errors, {'isbn': [u'Edition with this Isbn already exists.']}) |
| 139 | |
| 140 | def test_unique_together_error_message(self): |
| 141 | form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 2, 'isbn': '9783161489999'}) |
| 142 | self.assertEquals(form.errors, {'__all__': [u'Edition with this Author and Publication already exists.']}) |
| 143 | form = EditionForm(data={'author': self.author2.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161487777'}) |
| 144 | self.assertEquals(form.errors, {'__all__': [u'Edition with this Publication and Edition already exists.']}) |