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

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

    # HG changeset patch
    # Parent 1967de9eb7e9389fa57e3cd61c7f4e1f7d079f8a
    
    diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py
    a b  
    5353True
    5454>>> [form.cleaned_data for form in formset.forms]
    5555[{'votes': 100, 'choice': u'Calexico'}]
     56>>> [form.cleaned_data for form in formset.valid_forms]
     57[{'votes': 100, 'choice': u'Calexico'}]
    5658
    5759If a FormSet was not passed any data, its is_valid method should return False.
    5860>>> formset = ChoiceFormSet()
     
    108110True
    109111>>> [form.cleaned_data for form in formset.forms]
    110112[{'votes': 100, 'choice': u'Calexico'}, {}]
     113>>> [form.cleaned_data for form in formset.valid_forms]
     114[{'votes': 100, 'choice': u'Calexico'}]
    111115
    112116But the second form was blank! Shouldn't we get some errors? No. If we display
    113117a form as blank, it's ok for it to be submitted as blank. If we fill out even
     
    190194True
    191195>>> [form.cleaned_data for form in formset.forms]
    192196[{}, {}, {}]
     197>>> [form.cleaned_data for form in formset.valid_forms]
     198[]
    193199
    194200
    195201We can just fill out one of the forms.
     
    211217True
    212218>>> [form.cleaned_data for form in formset.forms]
    213219[{'votes': 100, 'choice': u'Calexico'}, {}, {}]
     220>>> [form.cleaned_data for form in formset.valid_forms]
     221[{'votes': 100, 'choice': u'Calexico'}]
    214222
    215223
    216224And once again, if we try to partially complete a form, validation will fail.
     
    303311True
    304312>>> [form.cleaned_data for form in formset.forms]
    305313[{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}, {'votes': 900, 'DELETE': True, 'choice': u'Fergie'}, {}]
     314>>> [form.cleaned_data for form in formset.valid_forms]
     315[{'votes': 100, 'DELETE': False, 'choice': u'Calexico'}]
    306316>>> [form.cleaned_data for form in formset.deleted_forms]
    307317[{'votes': 900, 'DELETE': True, 'choice': u'Fergie'}]
    308318
     
    332342Traceback (most recent call last):
    333343...
    334344AttributeError: 'CheckForm' object has no attribute 'cleaned_data'
     345>>> [form.cleaned_data for form in formset.valid_forms]
     346[{'field': 200, 'DELETE': False}]
    335347
    336348If we remove the deletion flag now we will have our validation back.
    337349
     
    404416>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    405417>>> formset.is_valid()
    406418True
     419>>> for form in formset.valid_forms:
     420...    print form.cleaned_data
     421{'votes': 500, 'ORDER': 0, 'choice': u'The Decemberists'}
     422{'votes': 100, 'ORDER': 1, 'choice': u'Calexico'}
     423{'votes': 900, 'ORDER': 2, 'choice': u'Fergie'}
    407424>>> for form in formset.ordered_forms:
    408425...    print form.cleaned_data
    409426{'votes': 500, 'ORDER': 0, 'choice': u'The Decemberists'}
     
    434451>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    435452>>> formset.is_valid()
    436453True
     454>>> for form in formset.valid_forms:
     455...    print form.cleaned_data
     456{'votes': 100, 'ORDER': 1, 'choice': u'Calexico'}
     457{'votes': 900, 'ORDER': 2, 'choice': u'Fergie'}
     458{'votes': 500, 'ORDER': None, 'choice': u'The Decemberists'}
     459{'votes': 50, 'ORDER': None, 'choice': u'Basia Bulat'}
    437460>>> for form in formset.ordered_forms:
    438461...    print form.cleaned_data
    439462{'votes': 100, 'ORDER': 1, 'choice': u'Calexico'}
     
    452475>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    453476>>> formset.is_valid()
    454477True
     478>>> for form in formset.valid_forms:
     479...    print form.cleaned_data
    455480>>> for form in formset.ordered_forms:
    456481...    print form.cleaned_data
    457482
     
    513538>>> formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
    514539>>> formset.is_valid()
    515540True
     541>>> for form in formset.valid_forms:
     542...    print form.cleaned_data
     543{'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': u'The Decemberists'}
     544{'votes': 100, 'DELETE': False, 'ORDER': 1, 'choice': u'Calexico'}
    516545>>> for form in formset.ordered_forms:
    517546...    print form.cleaned_data
    518547{'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': u'The Decemberists'}
     
    546575Traceback (most recent call last):
    547576...
    548577AttributeError: 'Person' object has no attribute 'cleaned_data'
     578>>> [form.cleaned_data for form in p.valid_forms]
     579[{'DELETE': False, 'name': u'John Smith', 'ORDER': None}]
    549580
    550581# FormSet clean hook ##########################################################
    551582
Back to Top