Ticket #18376: current_behaviour_test.diff

File current_behaviour_test.diff, 4.7 KB (added by Joeri Bekker, 12 years ago)

Test showing the current behaviour, elaborating on ticket starter's issue.

  • tests/modeltests/model_formsets/models.py

    diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py
    index ae15244..abef770 100644
    a b class BookWithOptionalAltEditor(models.Model):  
    6262        return self.title
    6363
    6464@python_2_unicode_compatible
     65class BookWithOptionalISBN(models.Model):
     66    author = models.ForeignKey(Author)
     67    title = models.CharField(max_length=100)
     68    isbn = models.CharField(max_length=100, null=True, blank=True, unique=True)
     69    priority = models.IntegerField(null=True, blank=True, unique=True)
     70
     71    def __str__(self):
     72        return self.title
     73
     74@python_2_unicode_compatible
    6575class AlternateBook(Book):
    6676    notes = models.CharField(max_length=100)
    6777
  • tests/modeltests/model_formsets/tests.py

    diff --git a/tests/modeltests/model_formsets/tests.py b/tests/modeltests/model_formsets/tests.py
    index 037163b..951f22f 100644
    a b from django.test import TestCase, skipUnlessDBFeature  
    1313from django.utils import six
    1414
    1515from .models import (Author, BetterAuthor, Book, BookWithCustomPK,
    16     BookWithOptionalAltEditor, AlternateBook, AuthorMeeting, CustomPrimaryKey,
    17     Place, Owner, Location, OwnerProfile, Restaurant, Product, Price,
    18     MexicanRestaurant, ClassyMexicanRestaurant, Repository, Revision,
    19     Person, Membership, Team, Player, Poet, Poem, Post)
     16    BookWithOptionalAltEditor, BookWithOptionalISBN, AlternateBook,
     17    AuthorMeeting, CustomPrimaryKey, Place, Owner, Location, OwnerProfile,
     18    Restaurant, Product, Price, MexicanRestaurant, ClassyMexicanRestaurant,
     19    Repository, Revision, Person, Membership, Team, Player, Poet, Poem, Post)
    2020
    2121
    2222class DeletionTests(TestCase):
    class ModelFormsetTest(TestCase):  
    639639        self.assertEqual(book2.author_id, 1)
    640640        self.assertEqual(book2.title, 'Les Fleurs du Mal')
    641641
     642    def test_inline_formsets_with_nullable_unique(self):
     643        """
     644        Fields that have null=True and have None should be excluded from unique
     645        checks. However, you cannot pass in None values via HTML rendered
     646        forms. "Most" to_python methods will make it an empty string instead.
     647        """
     648
     649        AuthorBooksFormSet = inlineformset_factory(Author, BookWithOptionalISBN, can_delete=False, extra=2)
     650        author = Author.objects.create(pk=1, name='Charles Baudelaire')
     651
     652        # Empty unique/nullable isbn (CharField)
     653        data = {
     654            'bookwithoptionalisbn_set-TOTAL_FORMS': '2', # the number of forms rendered
     655            'bookwithoptionalisbn_set-INITIAL_FORMS': '0', # the number of forms with initial data
     656            'bookwithoptionalisbn_set-MAX_NUM_FORMS': '', # the max number of forms
     657            'bookwithoptionalisbn_set-0-author': '1',
     658            'bookwithoptionalisbn_set-0-priority': '1',
     659            'bookwithoptionalisbn_set-0-title': 'Het gouden ei',
     660            'bookwithoptionalisbn_set-1-author': '1',
     661            'bookwithoptionalisbn_set-1-priority': '2',
     662            'bookwithoptionalisbn_set-1-title': 'Fokke en Sukke',
     663            }
     664        formset = AuthorBooksFormSet(data, instance=author)
     665        self.assertFalse(formset.is_valid(), formset.errors)
     666
     667        # Empty unique/nullable priority (IntegerField)
     668        data = {
     669            'bookwithoptionalisbn_set-TOTAL_FORMS': '2', # the number of forms rendered
     670            'bookwithoptionalisbn_set-INITIAL_FORMS': '0', # the number of forms with initial data
     671            'bookwithoptionalisbn_set-MAX_NUM_FORMS': '', # the max number of forms
     672            'bookwithoptionalisbn_set-0-author': '1',
     673            'bookwithoptionalisbn_set-0-isbn': '1234',
     674            'bookwithoptionalisbn_set-0-title': 'Het gouden ei',
     675            'bookwithoptionalisbn_set-1-author': '1',
     676            'bookwithoptionalisbn_set-1-isbn': '5678',
     677            'bookwithoptionalisbn_set-1-title': 'Fokke en Sukke',
     678            }
     679        formset = AuthorBooksFormSet(data, instance=author)
     680        self.assertTrue(formset.is_valid(), formset.errors)
     681
     682        saved = formset.save()
     683        self.assertEqual(len(saved), 2)
     684        book1, book2 = saved
     685        self.assertEqual(book1.author_id, 1)
     686        self.assertEqual(book1.title, 'Het gouden ei')
     687        self.assertEqual(book1.priority, None)
     688        self.assertEqual(book2.author_id, 1)
     689        self.assertEqual(book2.title, 'Fokke en Sukke')
     690        self.assertEqual(book2.priority, None)
     691
    642692    def test_inline_formsets_with_custom_save_method(self):
    643693        AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2)
    644694        author = Author.objects.create(pk=1, name='Charles Baudelaire')
Back to Top