<s> from __future__ import absolute_import , unicode_literals <EOL> from django . forms . models import inlineformset_factory <EOL> from django . test import TestCase , skipUnlessDBFeature <EOL> from django . utils import six <EOL> from . models import Poet , Poem , School , Parent , Child <EOL> class DeletionTests ( TestCase ) : <EOL> def test_deletion ( self ) : <EOL> PoemFormSet = inlineformset_factory ( Poet , Poem , can_delete = True , fields = "<STR_LIT>" ) <EOL> poet = Poet . objects . create ( name = '<STR_LIT:test>' ) <EOL> poem = poet . poem_set . create ( name = '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : str ( poem . pk ) , <EOL> '<STR_LIT>' : str ( poet . pk ) , <EOL> '<STR_LIT>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> formset = PoemFormSet ( data , instance = poet ) <EOL> formset . save ( ) <EOL> self . assertTrue ( formset . is_valid ( ) ) <EOL> self . assertEqual ( Poem . objects . count ( ) , <NUM_LIT:0> ) <EOL> def test_add_form_deletion_when_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> PoemFormSet = inlineformset_factory ( Poet , Poem , can_delete = True , fields = "<STR_LIT>" ) <EOL> poet = Poet . objects . create ( name = '<STR_LIT:test>' ) <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:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:x>' * <NUM_LIT:1000> , <EOL> } <EOL> formset = PoemFormSet ( data , instance = poet ) <EOL> self . assertEqual ( formset . is_valid ( ) , False ) <EOL> self . assertEqual ( Poem . objects . count ( ) , <NUM_LIT:0> ) <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> formset = PoemFormSet ( data , instance = poet ) <EOL> self . assertEqual ( formset . is_valid ( ) , True ) <EOL> formset . save ( ) <EOL> self . assertEqual ( Poem . objects . count ( ) , <NUM_LIT:0> ) <EOL> def test_change_form_deletion_when_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> PoemFormSet = inlineformset_factory ( Poet , Poem , can_delete = True , fields = "<STR_LIT>" ) <EOL> poet = Poet . objects . create ( name = '<STR_LIT:test>' ) <EOL> poem = poet . poem_set . create ( name = '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : six . text_type ( poem . id ) , <EOL> '<STR_LIT>' : six . text_type ( poem . id ) , <EOL> '<STR_LIT>' : '<STR_LIT:x>' * <NUM_LIT:1000> , <EOL> } <EOL> formset = PoemFormSet ( data , instance = poet ) <EOL> self . assertEqual ( formset . is_valid ( ) , False ) <EOL> self . assertEqual ( Poem . objects . count ( ) , <NUM_LIT:1> ) <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> formset = PoemFormSet ( data , instance = poet ) <EOL> self . assertEqual ( formset . is_valid ( ) , True ) <EOL> formset . save ( ) <EOL> self . assertEqual ( Poem . objects . count ( ) , <NUM_LIT:0> ) <EOL> def test_save_new ( self ) : <EOL> """<STR_LIT>""" <EOL> ChildFormSet = inlineformset_factory ( School , Child , exclude = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> school = School . objects . create ( name = '<STR_LIT:test>' ) <EOL> mother = Parent . objects . create ( name = '<STR_LIT>' ) <EOL> father = Parent . objects . create ( name = '<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> } <EOL> formset = ChildFormSet ( data , instance = school ) <EOL> self . assertEqual ( formset . is_valid ( ) , True ) <EOL> objects = formset . save ( commit = False ) <EOL> for obj in objects : <EOL> obj . mother = mother <EOL> obj . father = father <EOL> obj . save ( ) <EOL> self . assertEqual ( school . child_set . count ( ) , <NUM_LIT:1> ) <EOL> class InlineFormsetFactoryTest ( TestCase ) : <EOL> def test_inline_formset_factory ( self ) : <EOL> """<STR_LIT>""" <EOL> inlineformset_factory ( Parent , Child , fk_name = '<STR_LIT>' , fields = "<STR_LIT>" ) <EOL> inlineformset_factory ( Parent , Child , fk_name = '<STR_LIT>' , fields = "<STR_LIT>" ) <EOL> def test_exception_on_unspecified_foreign_key ( self ) : <EOL> """<STR_LIT>""" <EOL> six . assertRaisesRegex ( self , Exception , <EOL> "<STR_LIT>" , <EOL> inlineformset_factory , Parent , Child <EOL> ) <EOL> def test_fk_name_not_foreign_key_field_from_child ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( Exception , <EOL> "<STR_LIT>" , <EOL> inlineformset_factory , Parent , Child , fk_name = '<STR_LIT>' <EOL> ) <EOL> def test_non_foreign_key_field ( self ) : <EOL> """<STR_LIT>""" <EOL> six . assertRaisesRegex ( self , Exception , <EOL> "<STR_LIT>" , <EOL> inlineformset_factory , Parent , Child , fk_name = '<STR_LIT:test>' <EOL> ) <EOL> def test_any_iterable_allowed_as_argument_to_exclude ( self ) : <EOL> inlineformset_factory ( <EOL> Parent , Child , exclude = [ '<STR_LIT>' ] , fk_name = '<STR_LIT>' <EOL> ) <EOL> inlineformset_factory ( <EOL> Parent , Child , exclude = ( '<STR_LIT>' , ) , fk_name = '<STR_LIT>' <EOL> ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_zero_primary_key ( self ) : <EOL> poet = Poet . objects . create ( id = <NUM_LIT:0> , name = '<STR_LIT:test>' ) <EOL> poem = poet . poem_set . create ( name = '<STR_LIT>' ) <EOL> PoemFormSet = inlineformset_factory ( Poet , Poem , fields = "<STR_LIT>" , extra = <NUM_LIT:0> ) <EOL> formset = PoemFormSet ( None , instance = poet ) <EOL> self . assertEqual ( len ( formset . forms ) , <NUM_LIT:1> ) </s>
