Ticket #14655: formset-iterable-tests.diff
File formset-iterable-tests.diff, 34.6 KB (added by , 14 years ago) |
---|
-
tests/modeltests/model_formsets/tests.py
diff --git a/tests/modeltests/model_formsets/tests.py b/tests/modeltests/model_formsets/tests.py index 396f1bd..20bd384 100644
a b class ModelFormsetTest(TestCase): 92 92 AuthorFormSet = modelformset_factory(Author, extra=3) 93 93 94 94 formset = AuthorFormSet(queryset=qs) 95 self.assertEqual(len(formset .forms), 3)96 self.assertEqual(formset .forms[0].as_p(),95 self.assertEqual(len(formset), 3) 96 self.assertEqual(formset[0].as_p(), 97 97 '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></p>') 98 self.assertEqual(formset .forms[1].as_p(),98 self.assertEqual(formset[1].as_p(), 99 99 '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /><input type="hidden" name="form-1-id" id="id_form-1-id" /></p>') 100 self.assertEqual(formset .forms[2].as_p(),100 self.assertEqual(formset[2].as_p(), 101 101 '<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>') 102 102 103 103 data = { … … class ModelFormsetTest(TestCase): 130 130 AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=False) 131 131 132 132 formset = AuthorFormSet(queryset=qs) 133 self.assertEqual(len(formset .forms), 3)134 self.assertEqual(formset .forms[0].as_p(),133 self.assertEqual(len(formset), 3) 134 self.assertEqual(formset[0].as_p(), 135 135 '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></p>' % author2.id) 136 self.assertEqual(formset .forms[1].as_p(),136 self.assertEqual(formset[1].as_p(), 137 137 '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" value="Charles Baudelaire" maxlength="100" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" /></p>' % author1.id) 138 self.assertEqual(formset .forms[2].as_p(),138 self.assertEqual(formset[2].as_p(), 139 139 '<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>') 140 140 141 141 data = { … … class ModelFormsetTest(TestCase): 168 168 AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=True) 169 169 170 170 formset = AuthorFormSet(queryset=qs) 171 self.assertEqual(len(formset .forms), 4)172 self.assertEqual(formset .forms[0].as_p(),171 self.assertEqual(len(formset), 4) 172 self.assertEqual(formset[0].as_p(), 173 173 '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /></p>\n' 174 174 '<p><label for="id_form-0-DELETE">Delete:</label> <input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></p>' % author2.id) 175 self.assertEqual(formset .forms[1].as_p(),175 self.assertEqual(formset[1].as_p(), 176 176 '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" value="Charles Baudelaire" maxlength="100" /></p>\n' 177 177 '<p><label for="id_form-1-DELETE">Delete:</label> <input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /><input type="hidden" name="form-1-id" value="%d" id="id_form-1-id" /></p>' % author1.id) 178 self.assertEqual(formset .forms[2].as_p(),178 self.assertEqual(formset[2].as_p(), 179 179 '<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" value="Paul Verlaine" maxlength="100" /></p>\n' 180 180 '<p><label for="id_form-2-DELETE">Delete:</label> <input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE" /><input type="hidden" name="form-2-id" value="%d" id="id_form-2-id" /></p>' % author3.id) 181 self.assertEqual(formset .forms[3].as_p(),181 self.assertEqual(formset[3].as_p(), 182 182 '<p><label for="id_form-3-name">Name:</label> <input id="id_form-3-name" type="text" name="form-3-name" maxlength="100" /></p>\n' 183 183 '<p><label for="id_form-3-DELETE">Delete:</label> <input type="checkbox" name="form-3-DELETE" id="id_form-3-DELETE" /><input type="hidden" name="form-3-id" id="id_form-3-id" /></p>') 184 184 … … class ModelFormsetTest(TestCase): 285 285 286 286 AuthorFormSet = modelformset_factory(Author, max_num=None, extra=3) 287 287 formset = AuthorFormSet(queryset=qs) 288 self.assertEqual(len(formset .forms), 6)288 self.assertEqual(len(formset), 6) 289 289 self.assertEqual(len(formset.extra_forms), 3) 290 290 291 291 AuthorFormSet = modelformset_factory(Author, max_num=4, extra=3) 292 292 formset = AuthorFormSet(queryset=qs) 293 self.assertEqual(len(formset .forms), 4)293 self.assertEqual(len(formset), 4) 294 294 self.assertEqual(len(formset.extra_forms), 1) 295 295 296 296 AuthorFormSet = modelformset_factory(Author, max_num=0, extra=3) 297 297 formset = AuthorFormSet(queryset=qs) 298 self.assertEqual(len(formset .forms), 3)298 self.assertEqual(len(formset), 3) 299 299 self.assertEqual(len(formset.extra_forms), 0) 300 300 301 301 AuthorFormSet = modelformset_factory(Author, max_num=None) … … class ModelFormsetTest(TestCase): 356 356 def test_model_inheritance(self): 357 357 BetterAuthorFormSet = modelformset_factory(BetterAuthor) 358 358 formset = BetterAuthorFormSet() 359 self.assertEqual(len(formset .forms), 1)360 self.assertEqual(formset .forms[0].as_p(),359 self.assertEqual(len(formset), 1) 360 self.assertEqual(formset[0].as_p(), 361 361 '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /></p>\n' 362 362 '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="text" name="form-0-write_speed" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" id="id_form-0-author_ptr" /></p>') 363 363 … … class ModelFormsetTest(TestCase): 379 379 hemingway_id = BetterAuthor.objects.get(name="Ernest Hemingway").pk 380 380 381 381 formset = BetterAuthorFormSet() 382 self.assertEqual(len(formset .forms), 2)383 self.assertEqual(formset .forms[0].as_p(),382 self.assertEqual(len(formset), 2) 383 self.assertEqual(formset[0].as_p(), 384 384 '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Ernest Hemingway" maxlength="100" /></p>\n' 385 385 '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="text" name="form-0-write_speed" value="10" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" value="%d" id="id_form-0-author_ptr" /></p>' % hemingway_id) 386 self.assertEqual(formset .forms[1].as_p(),386 self.assertEqual(formset[1].as_p(), 387 387 '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /></p>\n' 388 388 '<p><label for="id_form-1-write_speed">Write speed:</label> <input type="text" name="form-1-write_speed" id="id_form-1-write_speed" /><input type="hidden" name="form-1-author_ptr" id="id_form-1-author_ptr" /></p>') 389 389 … … class ModelFormsetTest(TestCase): 411 411 author = Author.objects.create(name='Charles Baudelaire') 412 412 413 413 formset = AuthorBooksFormSet(instance=author) 414 self.assertEqual(len(formset .forms), 3)415 self.assertEqual(formset .forms[0].as_p(),414 self.assertEqual(len(formset), 3) 415 self.assertEqual(formset[0].as_p(), 416 416 '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" maxlength="100" /><input type="hidden" name="book_set-0-author" value="%d" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" id="id_book_set-0-id" /></p>' % author.id) 417 self.assertEqual(formset .forms[1].as_p(),417 self.assertEqual(formset[1].as_p(), 418 418 '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="%d" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>' % author.id) 419 self.assertEqual(formset .forms[2].as_p(),419 self.assertEqual(formset[2].as_p(), 420 420 '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="%d" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>' % author.id) 421 421 422 422 data = { … … class ModelFormsetTest(TestCase): 445 445 author = Author.objects.get(name='Charles Baudelaire') 446 446 447 447 formset = AuthorBooksFormSet(instance=author) 448 self.assertEqual(len(formset .forms), 3)449 self.assertEqual(formset .forms[0].as_p(),448 self.assertEqual(len(formset), 3) 449 self.assertEqual(formset[0].as_p(), 450 450 '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-0-author" value="%d" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="%d" id="id_book_set-0-id" /></p>' % (author.id, book1.id)) 451 self.assertEqual(formset .forms[1].as_p(),451 self.assertEqual(formset[1].as_p(), 452 452 '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="%d" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>' % author.id) 453 self.assertEqual(formset .forms[2].as_p(),453 self.assertEqual(formset[2].as_p(), 454 454 '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="%d" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>' % author.id) 455 455 456 456 data = { … … class ModelFormsetTest(TestCase): 509 509 # Test using a custom prefix on an inline formset. 510 510 511 511 formset = AuthorBooksFormSet(prefix="test") 512 self.assertEqual(len(formset .forms), 2)513 self.assertEqual(formset .forms[0].as_p(),512 self.assertEqual(len(formset), 2) 513 self.assertEqual(formset[0].as_p(), 514 514 '<p><label for="id_test-0-title">Title:</label> <input id="id_test-0-title" type="text" name="test-0-title" maxlength="100" /><input type="hidden" name="test-0-author" id="id_test-0-author" /><input type="hidden" name="test-0-id" id="id_test-0-id" /></p>') 515 self.assertEqual(formset .forms[1].as_p(),515 self.assertEqual(formset[1].as_p(), 516 516 '<p><label for="id_test-1-title">Title:</label> <input id="id_test-1-title" type="text" name="test-1-title" maxlength="100" /><input type="hidden" name="test-1-author" id="id_test-1-author" /><input type="hidden" name="test-1-id" id="id_test-1-id" /></p>') 517 517 518 518 def test_inline_formsets_with_custom_pk(self): … … class ModelFormsetTest(TestCase): 523 523 author = Author.objects.create(pk=1, name='Charles Baudelaire') 524 524 525 525 formset = AuthorBooksFormSet2(instance=author) 526 self.assertEqual(len(formset .forms), 1)527 self.assertEqual(formset .forms[0].as_p(),526 self.assertEqual(len(formset), 1) 527 self.assertEqual(formset[0].as_p(), 528 528 '<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label> <input type="text" name="bookwithcustompk_set-0-my_pk" id="id_bookwithcustompk_set-0-my_pk" /></p>\n' 529 529 '<p><label for="id_bookwithcustompk_set-0-title">Title:</label> <input id="id_bookwithcustompk_set-0-title" type="text" name="bookwithcustompk_set-0-title" maxlength="100" /><input type="hidden" name="bookwithcustompk_set-0-author" value="1" id="id_bookwithcustompk_set-0-author" /></p>') 530 530 … … class ModelFormsetTest(TestCase): 555 555 author = Author.objects.create(pk=1, name='Charles Baudelaire') 556 556 557 557 formset = AuthorBooksFormSet3(instance=author) 558 self.assertEqual(len(formset .forms), 1)559 self.assertEqual(formset .forms[0].as_p(),558 self.assertEqual(len(formset), 1) 559 self.assertEqual(formset[0].as_p(), 560 560 '<p><label for="id_alternatebook_set-0-title">Title:</label> <input id="id_alternatebook_set-0-title" type="text" name="alternatebook_set-0-title" maxlength="100" /></p>\n' 561 561 '<p><label for="id_alternatebook_set-0-notes">Notes:</label> <input id="id_alternatebook_set-0-notes" type="text" name="alternatebook_set-0-notes" maxlength="100" /><input type="hidden" name="alternatebook_set-0-author" value="1" id="id_alternatebook_set-0-author" /><input type="hidden" name="alternatebook_set-0-book_ptr" id="id_alternatebook_set-0-book_ptr" /></p>') 562 562 … … class ModelFormsetTest(TestCase): 643 643 644 644 custom_qs = Book.objects.order_by('-title') 645 645 formset = AuthorBooksFormSet(instance=author, queryset=custom_qs) 646 self.assertEqual(len(formset .forms), 5)647 self.assertEqual(formset .forms[0].as_p(),646 self.assertEqual(len(formset), 5) 647 self.assertEqual(formset[0].as_p(), 648 648 '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Paradis Artificiels" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p>') 649 self.assertEqual(formset .forms[1].as_p(),649 self.assertEqual(formset[1].as_p(), 650 650 '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" value="2" id="id_book_set-1-id" /></p>') 651 self.assertEqual(formset .forms[2].as_p(),651 self.assertEqual(formset[2].as_p(), 652 652 '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" value="3" id="id_book_set-2-id" /></p>') 653 self.assertEqual(formset .forms[3].as_p(),653 self.assertEqual(formset[3].as_p(), 654 654 '<p><label for="id_book_set-3-title">Title:</label> <input id="id_book_set-3-title" type="text" name="book_set-3-title" maxlength="100" /><input type="hidden" name="book_set-3-author" value="1" id="id_book_set-3-author" /><input type="hidden" name="book_set-3-id" id="id_book_set-3-id" /></p>') 655 self.assertEqual(formset .forms[4].as_p(),655 self.assertEqual(formset[4].as_p(), 656 656 '<p><label for="id_book_set-4-title">Title:</label> <input id="id_book_set-4-title" type="text" name="book_set-4-title" maxlength="100" /><input type="hidden" name="book_set-4-author" value="1" id="id_book_set-4-author" /><input type="hidden" name="book_set-4-id" id="id_book_set-4-id" /></p>') 657 657 658 658 data = { … … class ModelFormsetTest(TestCase): 673 673 674 674 custom_qs = Book.objects.filter(title__startswith='F') 675 675 formset = AuthorBooksFormSet(instance=author, queryset=custom_qs) 676 self.assertEqual(formset .forms[0].as_p(),676 self.assertEqual(formset[0].as_p(), 677 677 '<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="3" id="id_book_set-0-id" /></p>') 678 self.assertEqual(formset .forms[1].as_p(),678 self.assertEqual(formset[1].as_p(), 679 679 '<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>') 680 self.assertEqual(formset .forms[2].as_p(),680 self.assertEqual(formset[2].as_p(), 681 681 '<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>') 682 682 683 683 data = { … … class ModelFormsetTest(TestCase): 697 697 698 698 CustomPrimaryKeyFormSet = modelformset_factory(CustomPrimaryKey) 699 699 formset = CustomPrimaryKeyFormSet() 700 self.assertEqual(len(formset .forms), 1)701 self.assertEqual(formset .forms[0].as_p(),700 self.assertEqual(len(formset), 1) 701 self.assertEqual(formset[0].as_p(), 702 702 '<p><label for="id_form-0-my_pk">My pk:</label> <input id="id_form-0-my_pk" type="text" name="form-0-my_pk" maxlength="10" /></p>\n' 703 703 '<p><label for="id_form-0-some_field">Some field:</label> <input id="id_form-0-some_field" type="text" name="form-0-some_field" maxlength="100" /></p>') 704 704 … … class ModelFormsetTest(TestCase): 708 708 709 709 FormSet = inlineformset_factory(Place, Owner, extra=2, can_delete=False) 710 710 formset = FormSet(instance=place) 711 self.assertEqual(len(formset .forms), 2)712 self.assertEqual(formset .forms[0].as_p(),711 self.assertEqual(len(formset), 2) 712 self.assertEqual(formset[0].as_p(), 713 713 '<p><label for="id_owner_set-0-name">Name:</label> <input id="id_owner_set-0-name" type="text" name="owner_set-0-name" maxlength="100" /><input type="hidden" name="owner_set-0-place" value="1" id="id_owner_set-0-place" /><input type="hidden" name="owner_set-0-auto_id" id="id_owner_set-0-auto_id" /></p>') 714 self.assertEqual(formset .forms[1].as_p(),714 self.assertEqual(formset[1].as_p(), 715 715 '<p><label for="id_owner_set-1-name">Name:</label> <input id="id_owner_set-1-name" type="text" name="owner_set-1-name" maxlength="100" /><input type="hidden" name="owner_set-1-place" value="1" id="id_owner_set-1-place" /><input type="hidden" name="owner_set-1-auto_id" id="id_owner_set-1-auto_id" /></p>') 716 716 717 717 data = { … … class ModelFormsetTest(TestCase): 732 732 self.assertEqual(owner.place.name, 'Giordanos') 733 733 734 734 formset = FormSet(instance=place) 735 self.assertEqual(len(formset .forms), 3)736 self.assertEqual(formset .forms[0].as_p(),735 self.assertEqual(len(formset), 3) 736 self.assertEqual(formset[0].as_p(), 737 737 '<p><label for="id_owner_set-0-name">Name:</label> <input id="id_owner_set-0-name" type="text" name="owner_set-0-name" value="Joe Perry" maxlength="100" /><input type="hidden" name="owner_set-0-place" value="1" id="id_owner_set-0-place" /><input type="hidden" name="owner_set-0-auto_id" value="1" id="id_owner_set-0-auto_id" /></p>') 738 self.assertEqual(formset .forms[1].as_p(),738 self.assertEqual(formset[1].as_p(), 739 739 '<p><label for="id_owner_set-1-name">Name:</label> <input id="id_owner_set-1-name" type="text" name="owner_set-1-name" maxlength="100" /><input type="hidden" name="owner_set-1-place" value="1" id="id_owner_set-1-place" /><input type="hidden" name="owner_set-1-auto_id" id="id_owner_set-1-auto_id" /></p>') 740 self.assertEqual(formset .forms[2].as_p(),740 self.assertEqual(formset[2].as_p(), 741 741 '<p><label for="id_owner_set-2-name">Name:</label> <input id="id_owner_set-2-name" type="text" name="owner_set-2-name" maxlength="100" /><input type="hidden" name="owner_set-2-place" value="1" id="id_owner_set-2-place" /><input type="hidden" name="owner_set-2-auto_id" id="id_owner_set-2-auto_id" /></p>') 742 742 743 743 data = { … … class ModelFormsetTest(TestCase): 763 763 764 764 FormSet = modelformset_factory(OwnerProfile) 765 765 formset = FormSet() 766 self.assertEqual(formset .forms[0].as_p(),766 self.assertEqual(formset[0].as_p(), 767 767 '<p><label for="id_form-0-owner">Owner:</label> <select name="form-0-owner" id="id_form-0-owner">\n' 768 768 '<option value="" selected="selected">---------</option>\n' 769 769 '<option value="1">Joe Perry at Giordanos</option>\n' … … class ModelFormsetTest(TestCase): 776 776 self.assertEqual(FormSet.max_num, 1) 777 777 778 778 formset = FormSet(instance=owner) 779 self.assertEqual(len(formset .forms), 1)780 self.assertEqual(formset .forms[0].as_p(),779 self.assertEqual(len(formset), 1) 780 self.assertEqual(formset[0].as_p(), 781 781 '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="text" name="ownerprofile-0-age" id="id_ownerprofile-0-age" /><input type="hidden" name="ownerprofile-0-owner" value="1" id="id_ownerprofile-0-owner" /></p>') 782 782 783 783 data = { … … class ModelFormsetTest(TestCase): 796 796 self.assertEqual(profile1.age, 54) 797 797 798 798 formset = FormSet(instance=owner) 799 self.assertEqual(len(formset .forms), 1)800 self.assertEqual(formset .forms[0].as_p(),799 self.assertEqual(len(formset), 1) 800 self.assertEqual(formset[0].as_p(), 801 801 '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="text" name="ownerprofile-0-age" value="54" id="id_ownerprofile-0-age" /><input type="hidden" name="ownerprofile-0-owner" value="1" id="id_ownerprofile-0-owner" /></p>') 802 802 803 803 data = { … … class ModelFormsetTest(TestCase): 824 824 self.assertEqual(FormSet.max_num, 1) 825 825 826 826 formset = FormSet(instance=place) 827 self.assertEqual(len(formset .forms), 1)828 self.assertEqual(formset .forms[0].as_p(),827 self.assertEqual(len(formset), 1) 828 self.assertEqual(formset[0].as_p(), 829 829 '<p><label for="id_location_set-0-lat">Lat:</label> <input id="id_location_set-0-lat" type="text" name="location_set-0-lat" maxlength="100" /></p>\n' 830 830 '<p><label for="id_location_set-0-lon">Lon:</label> <input id="id_location_set-0-lon" type="text" name="location_set-0-lon" maxlength="100" /><input type="hidden" name="location_set-0-place" value="1" id="id_location_set-0-place" /><input type="hidden" name="location_set-0-id" id="id_location_set-0-id" /></p>') 831 831 … … class ModelFormsetTest(TestCase): 946 946 # default. This is required to ensure the value is tested for change correctly 947 947 # when determine what extra forms have changed to save. 948 948 949 self.assertEquals(len(formset .forms), 1) # this formset only has one form950 form = formset .forms[0]949 self.assertEquals(len(formset), 1) # this formset only has one form 950 form = formset[0] 951 951 now = form.fields['date_joined'].initial() 952 952 result = form.as_p() 953 953 result = re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?', '__DATETIME__', result) -
tests/regressiontests/forms/tests/formsets.py
diff --git a/tests/regressiontests/forms/tests/formsets.py b/tests/regressiontests/forms/tests/formsets.py index bf7124f..c806ec8 100644
a b class FormsFormsetTestCase(TestCase): 66 66 67 67 formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 68 68 self.assertTrue(formset.is_valid()) 69 self.assertEqual([form.cleaned_data for form in formset .forms], [{'votes': 100, 'choice': u'Calexico'}])69 self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'choice': u'Calexico'}]) 70 70 71 71 # If a FormSet was not passed any data, its is_valid method should return False. 72 72 formset = ChoiceFormSet() … … class FormsFormsetTestCase(TestCase): 97 97 formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') 98 98 form_output = [] 99 99 100 for form in formset .forms:100 for form in formset: 101 101 form_output.append(form.as_ul()) 102 102 103 103 self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> … … class FormsFormsetTestCase(TestCase): 119 119 120 120 formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 121 121 self.assertTrue(formset.is_valid()) 122 self.assertEqual([form.cleaned_data for form in formset .forms], [{'votes': 100, 'choice': u'Calexico'}, {}])122 self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'choice': u'Calexico'}, {}]) 123 123 124 124 def test_second_form_partially_filled(self): 125 125 # But the second form was blank! Shouldn't we get some errors? No. If we display … … class FormsFormsetTestCase(TestCase): 170 170 formset = ChoiceFormSet(auto_id=False, prefix='choices') 171 171 form_output = [] 172 172 173 for form in formset .forms:173 for form in formset: 174 174 form_output.append(form.as_ul()) 175 175 176 176 self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" /></li> … … class FormsFormsetTestCase(TestCase): 198 198 199 199 formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 200 200 self.assertTrue(formset.is_valid()) 201 self.assertEqual([form.cleaned_data for form in formset .forms], [{}, {}, {}])201 self.assertEqual([form.cleaned_data for form in formset], [{}, {}, {}]) 202 202 203 203 def test_single_form_completed(self): 204 204 # We can just fill out one of the forms. … … class FormsFormsetTestCase(TestCase): 218 218 ChoiceFormSet = formset_factory(Choice, extra=3) 219 219 formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 220 220 self.assertTrue(formset.is_valid()) 221 self.assertEqual([form.cleaned_data for form in formset .forms], [{'votes': 100, 'choice': u'Calexico'}, {}, {}])221 self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'choice': u'Calexico'}, {}, {}]) 222 222 223 223 def test_second_form_partially_filled_2(self): 224 224 # And once again, if we try to partially complete a form, validation will fail. … … class FormsFormsetTestCase(TestCase): 261 261 formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') 262 262 form_output = [] 263 263 264 for form in formset .forms:264 for form in formset: 265 265 form_output.append(form.as_ul()) 266 266 267 267 self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> … … class FormsFormsetTestCase(TestCase): 291 291 formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') 292 292 form_output = [] 293 293 294 for form in formset .forms:294 for form in formset: 295 295 form_output.append(form.as_ul()) 296 296 297 297 self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> … … class FormsFormsetTestCase(TestCase): 324 324 325 325 formset = ChoiceFormSet(data, auto_id=False, prefix='choices') 326 326 self.assertTrue(formset.is_valid()) 327 self.assertEqual([form.cleaned_data for form in formset .forms], [{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}, {'votes': 900, 'DELETE': True, 'choice': u'Fergie'}, {}])327 self.assertEqual([form.cleaned_data for form in formset], [{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}, {'votes': 900, 'DELETE': True, 'choice': u'Fergie'}, {}]) 328 328 self.assertEqual([form.cleaned_data for form in formset.deleted_forms], [{'votes': 900, 'DELETE': True, 'choice': u'Fergie'}]) 329 329 330 330 # If we fill a form with something and then we check the can_delete checkbox for … … class FormsFormsetTestCase(TestCase): 376 376 # FormSets with ordering ###################################################### 377 377 # We can also add ordering ability to a FormSet with an argument to 378 378 # formset_factory. This will add a integer field to each form instance. When 379 # form validation succeeds, [form.cleaned_data for form in formset .forms] will have the data in the correct379 # form validation succeeds, [form.cleaned_data for form in formset] will have the data in the correct 380 380 # order specified by the ordering fields. If a number is duplicated in the set 381 381 # of ordering fields, for instance form 0 and form 3 are both marked as 1, then 382 382 # the form index used as a secondary ordering criteria. In order to put … … class FormsFormsetTestCase(TestCase): 388 388 formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') 389 389 form_output = [] 390 390 391 for form in formset .forms:391 for form in formset: 392 392 form_output.append(form.as_ul()) 393 393 394 394 self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> … … class FormsFormsetTestCase(TestCase): 499 499 formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') 500 500 form_output = [] 501 501 502 for form in formset .forms:502 for form in formset: 503 503 form_output.append(form.as_ul()) 504 504 505 505 self.assertEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> … … class FormsFormsetTestCase(TestCase): 625 625 formset = LimitedFavoriteDrinkFormSet() 626 626 form_output = [] 627 627 628 for form in formset .forms:628 for form in formset: 629 629 form_output.append(str(form)) 630 630 631 631 self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" id="id_form-0-name" /></td></tr> … … class FormsFormsetTestCase(TestCase): 637 637 formset = LimitedFavoriteDrinkFormSet() 638 638 form_output = [] 639 639 640 for form in formset .forms:640 for form in formset: 641 641 form_output.append(str(form)) 642 642 643 643 self.assertEqual('\n'.join(form_output), "") … … class FormsFormsetTestCase(TestCase): 646 646 formset = LimitedFavoriteDrinkFormSet() 647 647 form_output = [] 648 648 649 for form in formset .forms:649 for form in formset: 650 650 form_output.append(str(form)) 651 651 652 652 self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" id="id_form-0-name" /></td></tr> … … class FormsFormsetTestCase(TestCase): 658 658 formset = LimitedFavoriteDrinkFormSet() 659 659 form_output = [] 660 660 661 for form in formset .forms:661 for form in formset: 662 662 form_output.append(str(form)) 663 663 664 664 self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" id="id_form-0-name" /></td></tr>""") … … class FormsFormsetTestCase(TestCase): 677 677 formset = LimitedFavoriteDrinkFormSet(initial=initial) 678 678 form_output = [] 679 679 680 for form in formset .forms:680 for form in formset: 681 681 form_output.append(str(form)) 682 682 683 683 self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" value="Fernet and Coke" id="id_form-0-name" /></td></tr> … … class FormsFormsetTestCase(TestCase): 695 695 formset = LimitedFavoriteDrinkFormSet(initial=initial) 696 696 form_output = [] 697 697 698 for form in formset .forms:698 for form in formset: 699 699 form_output.append(str(form)) 700 700 701 701 self.assertEqual('\n'.join(form_output), "") … … class FormsFormsetTestCase(TestCase): 713 713 formset = LimitedFavoriteDrinkFormSet(initial=initial) 714 714 form_output = [] 715 715 716 for form in formset .forms:716 for form in formset: 717 717 form_output.append(str(form)) 718 718 719 719 self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" value="Gin Tonic" id="id_form-0-name" /></td></tr> … … class FormsFormsetTestCase(TestCase): 728 728 formset = LimitedFavoriteDrinkFormSet(initial=initial) 729 729 form_output = [] 730 730 731 for form in formset .forms:731 for form in formset: 732 732 form_output.append(str(form)) 733 733 734 734 self.assertEqual('\n'.join(form_output), """<tr><th><label for="id_form-0-name">Name:</label></th><td><input type="text" name="form-0-name" value="Gin Tonic" id="id_form-0-name" /></td></tr> … … class FormsFormsetTestCase(TestCase): 762 762 self.assertFalse(formset.is_valid()) 763 763 self.assertEqual(formset.non_form_errors(), [u'You may only specify a drink once.']) 764 764 765 def test_iteration(self): 766 # Regression tests for #16455 -- formset instance iterable 767 extra = 3 768 ChoiceFormset = formset_factory(Choice, extra=extra) 769 formset = ChoiceFormset() 770 771 # confirm iterated formset yields formset.forms 772 forms = list(formset) 773 self.assertEqual(forms, formset.forms) 774 self.assertEqual(len(formset), len(forms)) 775 776 # confirm indexing of formset 777 self.assertEqual(formset[0], forms[0]) 778 with self.assertRaises(IndexError): 779 formset[extra] 780 781 # define formset which renders forms in reverse order 782 class BaseReverseFormSet(BaseFormSet): 783 def __iter__(self): 784 for form in reversed(self.forms): 785 yield form 786 787 ReverseChoiceFormset = formset_factory(Choice, BaseReverseFormSet, extra=extra) 788 reverse_formset = ReverseChoiceFormset() 789 790 # confirm that __iter__ modifies rendering order 791 # compare forms from "reverse" formset with forms from original formset 792 self.assertEqual(str(reverse_formset[0]), str(forms[-1])) 793 self.assertEqual(str(reverse_formset[1]), str(forms[-2])) 794 self.assertEqual(len(reverse_formset), len(forms)) 795 765 796 766 797 data = { 767 798 'choices-TOTAL_FORMS': '1', # the number of forms rendered