<s> from django . forms import Form , CharField , IntegerField , ValidationError , DateField , formsets <EOL> from django . forms . formsets import formset_factory , BaseFormSet <EOL> from django . utils . unittest import TestCase <EOL> class Choice ( Form ) : <EOL> choice = CharField ( ) <EOL> votes = IntegerField ( ) <EOL> ChoiceFormSet = formset_factory ( Choice ) <EOL> class FavoriteDrinkForm ( Form ) : <EOL> name = CharField ( ) <EOL> class BaseFavoriteDrinksFormSet ( BaseFormSet ) : <EOL> def clean ( self ) : <EOL> seen_drinks = [ ] <EOL> for drink in self . cleaned_data : <EOL> if drink [ '<STR_LIT:name>' ] in seen_drinks : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> seen_drinks . append ( drink [ '<STR_LIT:name>' ] ) <EOL> class EmptyFsetWontValidate ( BaseFormSet ) : <EOL> def clean ( self ) : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> FavoriteDrinksFormSet = formset_factory ( FavoriteDrinkForm , <EOL> formset = BaseFavoriteDrinksFormSet , extra = <NUM_LIT:3> ) <EOL> class FormsFormsetTestCase ( TestCase ) : <EOL> def test_basic_formset ( self ) : <EOL> formset = ChoiceFormSet ( auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertEqual ( str ( formset ) , """<STR_LIT>""" ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertEqual ( [ form . cleaned_data for form in formset . forms ] , [ { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : u'<STR_LIT>' } ] ) <EOL> formset = ChoiceFormSet ( ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> def test_formset_validation ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> self . assertEqual ( formset . errors , [ { '<STR_LIT>' : [ u'<STR_LIT>' ] } ] ) <EOL> def test_formset_initial_data ( self ) : <EOL> initial = [ { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:100> } ] <EOL> formset = ChoiceFormSet ( initial = initial , auto_id = False , prefix = '<STR_LIT>' ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( form . as_ul ( ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertEqual ( [ form . cleaned_data for form in formset . forms ] , [ { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : u'<STR_LIT>' } , { } ] ) <EOL> def test_second_form_partially_filled ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> self . assertEqual ( formset . errors , [ { } , { '<STR_LIT>' : [ u'<STR_LIT>' ] } ] ) <EOL> def test_delete_prefilled_data ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> self . assertEqual ( formset . errors , [ { '<STR_LIT>' : [ u'<STR_LIT>' ] , '<STR_LIT>' : [ u'<STR_LIT>' ] } , { } ] ) <EOL> def test_displaying_more_than_one_blank_form ( self ) : <EOL> ChoiceFormSet = formset_factory ( Choice , extra = <NUM_LIT:3> ) <EOL> formset = ChoiceFormSet ( auto_id = False , prefix = '<STR_LIT>' ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( form . as_ul ( ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertEqual ( [ form . cleaned_data for form in formset . forms ] , [ { } , { } , { } ] ) <EOL> def test_single_form_completed ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ChoiceFormSet = formset_factory ( Choice , extra = <NUM_LIT:3> ) <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertEqual ( [ form . cleaned_data for form in formset . forms ] , [ { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : u'<STR_LIT>' } , { } , { } ] ) <EOL> def test_second_form_partially_filled_2 ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ChoiceFormSet = formset_factory ( Choice , extra = <NUM_LIT:3> ) <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> self . assertEqual ( formset . errors , [ { } , { '<STR_LIT>' : [ u'<STR_LIT>' ] } , { } ] ) <EOL> def test_more_initial_data ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> initial = [ { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:100> } ] <EOL> ChoiceFormSet = formset_factory ( Choice , extra = <NUM_LIT:3> ) <EOL> formset = ChoiceFormSet ( initial = initial , auto_id = False , prefix = '<STR_LIT>' ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( form . as_ul ( ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> self . assertTrue ( formset . empty_form . empty_permitted ) <EOL> self . assertEqual ( formset . empty_form . as_ul ( ) , """<STR_LIT>""" ) <EOL> def test_formset_with_deletion ( self ) : <EOL> ChoiceFormSet = formset_factory ( Choice , can_delete = True ) <EOL> initial = [ { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:100> } , { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } ] <EOL> formset = ChoiceFormSet ( initial = initial , auto_id = False , prefix = '<STR_LIT>' ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( form . as_ul ( ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertEqual ( [ form . cleaned_data for form in formset . forms ] , [ { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : False , '<STR_LIT>' : u'<STR_LIT>' } , { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : True , '<STR_LIT>' : u'<STR_LIT>' } , { } ] ) <EOL> self . assertEqual ( [ form . cleaned_data for form in formset . deleted_forms ] , [ { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : True , '<STR_LIT>' : u'<STR_LIT>' } ] ) <EOL> class CheckForm ( Form ) : <EOL> field = IntegerField ( min_value = <NUM_LIT:100> ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> CheckFormSet = formset_factory ( CheckForm , can_delete = True ) <EOL> formset = CheckFormSet ( data , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> formset = CheckFormSet ( data , prefix = '<STR_LIT>' ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> class Person ( Form ) : <EOL> name = CharField ( ) <EOL> PeopleForm = formset_factory ( <EOL> form = Person , <EOL> can_delete = True ) <EOL> p = PeopleForm ( <EOL> { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . assertTrue ( p . is_valid ( ) ) <EOL> self . assertEqual ( len ( p . deleted_forms ) , <NUM_LIT:1> ) <EOL> def test_formsets_with_ordering ( self ) : <EOL> ChoiceFormSet = formset_factory ( Choice , can_order = True ) <EOL> initial = [ { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:100> } , { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } ] <EOL> formset = ChoiceFormSet ( initial = initial , auto_id = False , prefix = '<STR_LIT>' ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( form . as_ul ( ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> form_output = [ ] <EOL> for form in formset . ordered_forms : <EOL> form_output . append ( form . cleaned_data ) <EOL> self . assertEqual ( form_output , [ <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> ] ) <EOL> def test_empty_ordered_fields ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:4>' , <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ChoiceFormSet = formset_factory ( Choice , can_order = True ) <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> form_output = [ ] <EOL> for form in formset . ordered_forms : <EOL> form_output . append ( form . cleaned_data ) <EOL> self . assertEqual ( form_output , [ <EOL> { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : None , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:50> , '<STR_LIT>' : None , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> ] ) <EOL> def test_ordering_blank_fieldsets ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> } <EOL> ChoiceFormSet = formset_factory ( Choice , can_order = True ) <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> form_output = [ ] <EOL> for form in formset . ordered_forms : <EOL> form_output . append ( form . cleaned_data ) <EOL> self . assertEqual ( form_output , [ ] ) <EOL> def test_formset_with_ordering_and_deletion ( self ) : <EOL> ChoiceFormSet = formset_factory ( Choice , can_order = True , can_delete = True ) <EOL> initial = [ <EOL> { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:100> } , <EOL> { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , <EOL> { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , <EOL> ] <EOL> formset = ChoiceFormSet ( initial = initial , auto_id = False , prefix = '<STR_LIT>' ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( form . as_ul ( ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:4>' , <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> form_output = [ ] <EOL> for form in formset . ordered_forms : <EOL> form_output . append ( form . cleaned_data ) <EOL> self . assertEqual ( form_output , [ <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : False , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : False , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : u'<STR_LIT>' } , <EOL> ] ) <EOL> self . assertEqual ( [ form . cleaned_data for form in formset . deleted_forms ] , [ { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : True , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : u'<STR_LIT>' } ] ) <EOL> def test_invalid_deleted_form_with_ordering ( self ) : <EOL> class Person ( Form ) : <EOL> name = CharField ( ) <EOL> PeopleForm = formset_factory ( form = Person , can_delete = True , can_order = True ) <EOL> p = PeopleForm ( { <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } ) <EOL> self . assertTrue ( p . is_valid ( ) ) <EOL> self . assertEqual ( p . ordered_forms , [ ] ) <EOL> def test_clean_hook ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = FavoriteDrinksFormSet ( data , prefix = '<STR_LIT>' ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> for error in formset . non_form_errors ( ) : <EOL> self . assertEqual ( str ( error ) , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = FavoriteDrinksFormSet ( data , prefix = '<STR_LIT>' ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertEqual ( formset . non_form_errors ( ) , [ ] ) <EOL> def test_limiting_max_forms ( self ) : <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:3> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:3> , max_num = <NUM_LIT:0> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , "<STR_LIT>" ) <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:5> , max_num = <NUM_LIT:2> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:1> , max_num = <NUM_LIT:2> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> def test_max_num_with_initial_data ( self ) : <EOL> initial = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> ] <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:1> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( initial = initial ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> def test_max_num_zero ( self ) : <EOL> initial = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> ] <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:1> , max_num = <NUM_LIT:0> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( initial = initial ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , "<STR_LIT>" ) <EOL> def test_more_initial_than_max_num ( self ) : <EOL> initial = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> ] <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:1> , max_num = <NUM_LIT:2> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( initial = initial ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> initial = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> ] <EOL> LimitedFavoriteDrinkFormSet = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:3> , max_num = <NUM_LIT:2> ) <EOL> formset = LimitedFavoriteDrinkFormSet ( initial = initial ) <EOL> form_output = [ ] <EOL> for form in formset . forms : <EOL> form_output . append ( str ( form ) ) <EOL> self . assertEqual ( '<STR_LIT:\n>' . join ( form_output ) , """<STR_LIT>""" ) <EOL> def test_regression_6926 ( self ) : <EOL> formset = FavoriteDrinksFormSet ( ) <EOL> self . assertEqual ( formset . management_form . prefix , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> } <EOL> formset = FavoriteDrinksFormSet ( data = data ) <EOL> self . assertEqual ( formset . management_form . prefix , '<STR_LIT>' ) <EOL> formset = FavoriteDrinksFormSet ( initial = { } ) <EOL> self . assertEqual ( formset . management_form . prefix , '<STR_LIT>' ) <EOL> def test_regression_12878 ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = FavoriteDrinksFormSet ( data , prefix = '<STR_LIT>' ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> self . assertEqual ( formset . non_form_errors ( ) , [ u'<STR_LIT>' ] ) <EOL> def test_formset_iteration ( self ) : <EOL> ChoiceFormset = formset_factory ( Choice , extra = <NUM_LIT:3> ) <EOL> formset = ChoiceFormset ( ) <EOL> forms = list ( formset ) <EOL> self . assertEqual ( forms , formset . forms ) <EOL> self . assertEqual ( len ( formset ) , len ( forms ) ) <EOL> self . assertEqual ( formset [ <NUM_LIT:0> ] , forms [ <NUM_LIT:0> ] ) <EOL> try : <EOL> formset [ <NUM_LIT:3> ] <EOL> self . fail ( '<STR_LIT>' ) <EOL> except IndexError : <EOL> pass <EOL> class BaseReverseFormSet ( BaseFormSet ) : <EOL> def __iter__ ( self ) : <EOL> for form in reversed ( self . forms ) : <EOL> yield form <EOL> ReverseChoiceFormset = formset_factory ( Choice , BaseReverseFormSet , extra = <NUM_LIT:3> ) <EOL> reverse_formset = ReverseChoiceFormset ( ) <EOL> self . assertEqual ( str ( reverse_formset [ <NUM_LIT:0> ] ) , str ( forms [ - <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( str ( reverse_formset [ <NUM_LIT:1> ] ) , str ( forms [ - <NUM_LIT:2> ] ) ) <EOL> self . assertEqual ( len ( reverse_formset ) , len ( forms ) ) <EOL> def test_hard_limit_on_instantiated_forms ( self ) : <EOL> """<STR_LIT>""" <EOL> _old_DEFAULT_MAX_NUM = formsets . DEFAULT_MAX_NUM <EOL> try : <EOL> formsets . DEFAULT_MAX_NUM = <NUM_LIT:3> <EOL> ChoiceFormSet = formset_factory ( Choice ) <EOL> formset = ChoiceFormSet ( <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT:4>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:4>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> } , <EOL> prefix = '<STR_LIT>' , <EOL> ) <EOL> self . assertEqual ( len ( formset . forms ) , <NUM_LIT:3> ) <EOL> finally : <EOL> formsets . DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM <EOL> def test_increase_hard_limit ( self ) : <EOL> """<STR_LIT>""" <EOL> _old_DEFAULT_MAX_NUM = formsets . DEFAULT_MAX_NUM <EOL> try : <EOL> formsets . DEFAULT_MAX_NUM = <NUM_LIT:3> <EOL> ChoiceFormSet = formset_factory ( Choice , max_num = <NUM_LIT:4> ) <EOL> formset = ChoiceFormSet ( <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT:4>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:4>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> } , <EOL> prefix = '<STR_LIT>' , <EOL> ) <EOL> self . assertEqual ( len ( formset . forms ) , <NUM_LIT:4> ) <EOL> finally : <EOL> formsets . DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:100>' , <EOL> } <EOL> class Choice ( Form ) : <EOL> choice = CharField ( ) <EOL> votes = IntegerField ( ) <EOL> ChoiceFormSet = formset_factory ( Choice ) <EOL> class FormsetAsFooTests ( TestCase ) : <EOL> def test_as_table ( self ) : <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertEqual ( formset . as_table ( ) , """<STR_LIT>""" ) <EOL> def test_as_p ( self ) : <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertEqual ( formset . as_p ( ) , """<STR_LIT>""" ) <EOL> def test_as_ul ( self ) : <EOL> formset = ChoiceFormSet ( data , auto_id = False , prefix = '<STR_LIT>' ) <EOL> self . assertEqual ( formset . as_ul ( ) , """<STR_LIT>""" ) <EOL> class ArticleForm ( Form ) : <EOL> title = CharField ( ) <EOL> pub_date = DateField ( ) <EOL> ArticleFormSet = formset_factory ( ArticleForm ) <EOL> class TestIsBoundBehavior ( TestCase ) : <EOL> def test_no_data_raises_validation_error ( self ) : <EOL> self . assertRaises ( ValidationError , ArticleFormSet , { } ) <EOL> def test_with_management_data_attrs_work_fine ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : u'<STR_LIT:1>' , <EOL> '<STR_LIT>' : u'<STR_LIT:0>' , <EOL> } <EOL> formset = ArticleFormSet ( data ) <EOL> self . assertEqual ( <NUM_LIT:0> , formset . initial_form_count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , formset . total_form_count ( ) ) <EOL> self . assertTrue ( formset . is_bound ) <EOL> self . assertTrue ( formset . forms [ <NUM_LIT:0> ] . is_bound ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertTrue ( formset . forms [ <NUM_LIT:0> ] . is_valid ( ) ) <EOL> self . assertEqual ( [ { } ] , formset . cleaned_data ) <EOL> def test_form_errors_are_cought_by_formset ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : u'<STR_LIT:2>' , <EOL> '<STR_LIT>' : u'<STR_LIT:0>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> } <EOL> formset = ArticleFormSet ( data ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> self . assertEqual ( [ { } , { '<STR_LIT>' : [ u'<STR_LIT>' ] } ] , formset . errors ) <EOL> def test_empty_forms_are_unbound ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : u'<STR_LIT:1>' , <EOL> '<STR_LIT>' : u'<STR_LIT:0>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> } <EOL> unbound_formset = ArticleFormSet ( ) <EOL> bound_formset = ArticleFormSet ( data ) <EOL> empty_forms = [ ] <EOL> empty_forms . append ( unbound_formset . empty_form ) <EOL> empty_forms . append ( bound_formset . empty_form ) <EOL> self . assertFalse ( empty_forms [ <NUM_LIT:0> ] . is_bound ) <EOL> self . assertFalse ( empty_forms [ <NUM_LIT:1> ] . is_bound ) <EOL> self . assertEqual ( empty_forms [ <NUM_LIT:0> ] . as_p ( ) , empty_forms [ <NUM_LIT:1> ] . as_p ( ) ) <EOL> class TestEmptyFormSet ( TestCase ) : <EOL> "<STR_LIT>" <EOL> def test_empty_formset_is_valid ( self ) : <EOL> EmptyFsetWontValidateFormset = formset_factory ( FavoriteDrinkForm , extra = <NUM_LIT:0> , formset = EmptyFsetWontValidate ) <EOL> formset = EmptyFsetWontValidateFormset ( data = { '<STR_LIT>' : '<STR_LIT:0>' , '<STR_LIT>' : '<STR_LIT:0>' } , prefix = "<STR_LIT>" ) <EOL> formset2 = EmptyFsetWontValidateFormset ( data = { '<STR_LIT>' : '<STR_LIT:0>' , '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT>' : '<STR_LIT>' } , prefix = "<STR_LIT>" ) <EOL> self . assertFalse ( formset . is_valid ( ) ) <EOL> self . assertFalse ( formset2 . is_valid ( ) ) </s>
