Code

Ticket #12304: 12304-only-tests-trunk-r12159.diff

File 12304-only-tests-trunk-r12159.diff, 3.0 KB (added by ramiro, 5 years ago)

The previous patch reduced to only contain the regression tests that demonstrate this was solved with the model-validation branch merge

Line 
1diff -r e891c6dcf1e0 tests/regressiontests/model_forms_regress/models.py
2--- a/tests/regressiontests/model_forms_regress/models.py       Sun Jan 10 13:51:13 2010 -0300
3+++ b/tests/regressiontests/model_forms_regress/models.py       Sun Jan 10 14:18:13 2010 -0300
4@@ -37,3 +37,13 @@
5 
6 class CustomFF(models.Model):
7     f = CustomFileField(upload_to='unused', blank=True)
8+
9+class Edition(models.Model):
10+    author = models.ForeignKey(Person)
11+    publication = models.ForeignKey(Publication)
12+    edition = models.IntegerField()
13+    isbn = models.CharField(max_length=13, unique=True)
14+
15+    class Meta:
16+        unique_together = (('author', 'publication'), ('publication', 'edition'),)
17+
18diff -r e891c6dcf1e0 tests/regressiontests/model_forms_regress/tests.py
19--- a/tests/regressiontests/model_forms_regress/tests.py        Sun Jan 10 13:51:13 2010 -0300
20+++ b/tests/regressiontests/model_forms_regress/tests.py        Sun Jan 10 14:18:13 2010 -0300
21@@ -6,7 +6,7 @@
22 from django.conf import settings
23 from django.test import TestCase
24 
25-from models import Person, Triple, FilePathModel, Article, Publication, CustomFF
26+from models import Person, Triple, FilePathModel, Article, Publication, CustomFF, Edition
27 
28 class ModelMultipleChoiceFieldTests(TestCase):
29 
30@@ -113,3 +113,32 @@
31             date_published=date(1991, 8, 22))
32         f = Form()
33         self.assertEqual(len(f.fields["publications"].choices), 1)
34+
35+class EditionForm(forms.ModelForm):
36+    author = forms.ModelChoiceField(queryset=Person.objects.all())
37+    publication = forms.ModelChoiceField(queryset=Publication.objects.all())
38+    edition = forms.IntegerField()
39+    isbn = forms.CharField(max_length=13)
40+
41+    class Meta:
42+        model = Edition
43+
44+class UniqueErrorsTests(TestCase):
45+
46+    def setUp(self):
47+        self.author1 = Person.objects.create(name=u'Author #1')
48+        self.author2 = Person.objects.create(name=u'Author #2')
49+        self.pub1 = Publication.objects.create(title='Pub #1', date_published=date(2000, 10, 31))
50+        self.pub2 = Publication.objects.create(title='Pub #2', date_published=date(2004, 1, 5))
51+        form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161484100'})
52+        form.save()
53+
54+    def test_unique_error_message(self):
55+        form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub2.pk, 'edition': 1, 'isbn': '9783161484100'})
56+        self.assertEquals(form.errors, {'isbn': [u'Edition with this Isbn already exists.']})
57+
58+    def test_unique_together_error_message(self):
59+        form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 2, 'isbn': '9783161489999'})
60+        self.assertEquals(form.errors, {'__all__': [u'Edition with this Author and Publication already exists.']})
61+        form = EditionForm(data={'author': self.author2.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161487777'})
62+        self.assertEquals(form.errors, {'__all__': [u'Edition with this Publication and Edition already exists.']})