Ticket #13878: 03-formset-refactoring-valid-forms-tests.diff

File 03-formset-refactoring-valid-forms-tests.diff, 4.1 KB (added by Petr Marhoun <petr.marhoun@…>, 14 years ago)
  • tests/regressiontests/forms/formsets.py

    diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py
    a b  
    4949True
    5050>>> [form.cleaned_data for form in formset.forms]
    5151[{'votes': 100, 'choice': u'Calexico'}]
     52>>> [form.cleaned_data for form in formset.valid_forms]
     53[{'votes': 100, 'choice': u'Calexico'}]
    5254
    5355If a FormSet was not passed any data, its is_valid method should return False.
    5456>>> formset = ChoiceFormSet()
     
    104106True
    105107>>> [form.cleaned_data for form in formset.forms]
    106108[{'votes': 100, 'choice': u'Calexico'}, {}]
     109>>> [form.cleaned_data for form in formset.valid_forms]
     110[{'votes': 100, 'choice': u'Calexico'}]
    107111
    108112But the second form was blank! Shouldn't we get some errors? No. If we display
    109113a form as blank, it's ok for it to be submitted as blank. If we fill out even
     
    186190True
    187191>>> [form.cleaned_data for form in formset.forms]
    188192[{}, {}, {}]
     193>>> [form.cleaned_data for form in formset.valid_forms]
     194[]
    189195
    190196
    191197We can just fill out one of the forms.
     
    207213True
    208214>>> [form.cleaned_data for form in formset.forms]
    209215[{'votes': 100, 'choice': u'Calexico'}, {}, {}]
     216>>> [form.cleaned_data for form in formset.valid_forms]
     217[{'votes': 100, 'choice': u'Calexico'}]
    210218
    211219
    212220And once again, if we try to partially complete a form, validation will fail.
     
    299307True
    300308>>> [form.cleaned_data for form in formset.forms]
    301309[{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}, {'votes': 900, 'DELETE': True, 'choice': u'Fergie'}, {}]
     310>>> [form.cleaned_data for form in formset.valid_forms]
     311[{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}]
    302312>>> [form.cleaned_data for form in formset.deleted_forms]
    303313[{'votes': 900, 'DELETE': True, 'choice': u'Fergie'}]
    304314
     
    328338Traceback (most recent call last):
    329339...
    330340AttributeError: 'CheckForm' object has no attribute 'cleaned_data'
     341>>> [form.cleaned_data for form in formset.valid_forms]
     342[{'field': 200, 'DELETE': False}]
    331343
    332344If we remove the deletion flag now we will have our validation back.
    333345
     
    400412>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    401413>>> formset.is_valid()
    402414True
     415>>> for form in formset.valid_forms:
     416...    print form.cleaned_data
     417{'votes': 500, 'ORDER': 0, 'choice': u'The Decemberists'}
     418{'votes': 100, 'ORDER': 1, 'choice': u'Calexico'}
     419{'votes': 900, 'ORDER': 2, 'choice': u'Fergie'}
    403420>>> for form in formset.ordered_forms:
    404421...    print form.cleaned_data
    405422{'votes': 500, 'ORDER': 0, 'choice': u'The Decemberists'}
     
    430447>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    431448>>> formset.is_valid()
    432449True
     450>>> for form in formset.valid_forms:
     451...    print form.cleaned_data
     452{'votes': 100, 'ORDER': 1, 'choice': u'Calexico'}
     453{'votes': 900, 'ORDER': 2, 'choice': u'Fergie'}
     454{'votes': 500, 'ORDER': None, 'choice': u'The Decemberists'}
     455{'votes': 50, 'ORDER': None, 'choice': u'Basia Bulat'}
    433456>>> for form in formset.ordered_forms:
    434457...    print form.cleaned_data
    435458{'votes': 100, 'ORDER': 1, 'choice': u'Calexico'}
     
    448471>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    449472>>> formset.is_valid()
    450473True
     474>>> for form in formset.valid_forms:
     475...    print form.cleaned_data
    451476>>> for form in formset.ordered_forms:
    452477...    print form.cleaned_data
    453478
     
    509534>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    510535>>> formset.is_valid()
    511536True
     537>>> for form in formset.valid_forms:
     538...    print form.cleaned_data
     539{'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': u'The Decemberists'}
     540{'votes': 100, 'DELETE': False, 'ORDER': 1, 'choice': u'Calexico'}
    512541>>> for form in formset.ordered_forms:
    513542...    print form.cleaned_data
    514543{'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': u'The Decemberists'}
     
    542571Traceback (most recent call last):
    543572...
    544573AttributeError: 'Person' object has no attribute 'cleaned_data'
     574>>> [form.cleaned_data for form in p.valid_forms]
     575[{'DELETE': False, 'name': u'John Smith', 'ORDER': None}]
    545576
    546577# FormSet clean hook ##########################################################
    547578
Back to Top