Ticket #26142: 26142-test.diff

File 26142-test.diff, 1.2 KB (added by Tim Graham, 8 years ago)
  • tests/model_formsets/tests.py

    diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
    index 0739a6b..50daac7 100644
    a b class ModelFormsetTest(TestCase):  
    427427            '<Author: Walt Whitman>',
    428428        ])
    429429
     430    def test_max_num_prevent_instance_creation(self):
     431        Author.objects.create(name='Charles Baudelaire')
     432        AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=1, extra=0, validate_max=True)
     433        data = {
     434            'form-TOTAL_FORMS': '1',
     435            'form-INITIAL_FORMS': '0', # Would be 1 normally, but pretend the user edited it.
     436            'form-MAX_NUM_FORMS': '0',
     437            'form-0-name': 'Walt Whitman',
     438        }
     439        formset = AuthorFormSet(data, queryset=Author.objects.all())
     440        self.assertTrue(formset.is_valid())
     441        formset.save()
     442        self.assertQuerysetEqual(Author.objects.all(), [
     443            '<Author: Charles Baudelaire>',
     444            '<Author: Walt Whitman>',
     445        ])
     446
    430447    def test_min_num(self):
    431448        # Test the behavior of min_num with model formsets. It should be
    432449        # added to extra.
Back to Top