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

File 12304-only-tests-trunk-r12159.diff, 3.0 KB (added by Ramiro Morales, 15 years ago)

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

  • tests/regressiontests/model_forms_regress/models.py

    diff -r e891c6dcf1e0 tests/regressiontests/model_forms_regress/models.py
    a b  
    3737
    3838class CustomFF(models.Model):
    3939    f = CustomFileField(upload_to='unused', blank=True)
     40
     41class Edition(models.Model):
     42    author = models.ForeignKey(Person)
     43    publication = models.ForeignKey(Publication)
     44    edition = models.IntegerField()
     45    isbn = models.CharField(max_length=13, unique=True)
     46
     47    class Meta:
     48        unique_together = (('author', 'publication'), ('publication', 'edition'),)
     49
  • tests/regressiontests/model_forms_regress/tests.py

    diff -r e891c6dcf1e0 tests/regressiontests/model_forms_regress/tests.py
    a b  
    66from django.conf import settings
    77from django.test import TestCase
    88
    9 from models import Person, Triple, FilePathModel, Article, Publication, CustomFF
     9from models import Person, Triple, FilePathModel, Article, Publication, CustomFF, Edition
    1010
    1111class ModelMultipleChoiceFieldTests(TestCase):
    1212
     
    113113            date_published=date(1991, 8, 22))
    114114        f = Form()
    115115        self.assertEqual(len(f.fields["publications"].choices), 1)
     116
     117class 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
     126class 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.']})
Back to Top