<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . test import TestCase <EOL> import floppyforms <EOL> from djadmin2 . forms import floppify_widget , floppify_form , modelform_factory <EOL> from . . models import Post <EOL> class ModelFormFactoryTest ( TestCase ) : <EOL> def test_modelform_factory ( self ) : <EOL> form_class = modelform_factory ( Post , exclude = [ ] ) <EOL> self . assertTrue ( form_class ) <EOL> field = form_class . base_fields [ '<STR_LIT:title>' ] <EOL> self . assertTrue ( isinstance ( field . widget , floppyforms . TextInput ) ) <EOL> class GetFloppyformWidgetTest ( TestCase ) : <EOL> def assertExpectWidget ( self , instance , new_class_ , <EOL> equal_attributes = None , new_attributes = None ) : <EOL> new_instance = floppify_widget ( instance ) <EOL> self . assertEqual ( new_instance . __class__ , new_class_ ) <EOL> if equal_attributes : <EOL> for attribute in equal_attributes : <EOL> self . assertTrue ( <EOL> hasattr ( instance , attribute ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( attribute , instance ) ) <EOL> self . assertTrue ( <EOL> hasattr ( new_instance , attribute ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( attribute , new_instance ) ) <EOL> old_attr = getattr ( instance , attribute ) <EOL> new_attr = getattr ( new_instance , attribute ) <EOL> self . assertEqual ( old_attr , new_attr , <EOL> '<STR_LIT>' % <EOL> ( old_attr , new_attr ) ) <EOL> if new_attributes : <EOL> for attribute , value in new_attributes . items ( ) : <EOL> self . assertTrue ( <EOL> hasattr ( new_instance , attribute ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( attribute , new_instance ) ) <EOL> new_attr = getattr ( new_instance , attribute ) <EOL> self . assertEqual ( new_attr , value , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( new_attr , value ) ) <EOL> def test_created_widget_doesnt_leak_attributes_into_original_widget ( self ) : <EOL> widget = forms . TextInput ( ) <EOL> widget . is_required = True <EOL> widget . attrs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> new_widget = floppify_widget ( widget ) <EOL> self . assertFalse ( widget . __dict__ is new_widget . __dict__ ) <EOL> new_widget . is_required = False <EOL> self . assertEqual ( widget . is_required , True ) <EOL> new_widget . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertEqual ( widget . attrs [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_copy_attribute_is_required ( self ) : <EOL> widget = forms . TextInput ( ) <EOL> widget . is_required = True <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . TextInput , <EOL> equal_attributes = [ '<STR_LIT>' ] ) <EOL> def test_input_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . Input ( ) , <EOL> floppyforms . widgets . Input ) <EOL> widget = forms . widgets . Input ( ) <EOL> widget . input_type = '<STR_LIT:email>' <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . Input , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_textinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . TextInput ( ) , <EOL> floppyforms . widgets . TextInput , <EOL> [ '<STR_LIT>' ] , <EOL> { '<STR_LIT>' : '<STR_LIT:text>' } ) <EOL> def test_passwordinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . PasswordInput ( ) , <EOL> floppyforms . widgets . PasswordInput , <EOL> [ '<STR_LIT>' ] , <EOL> { '<STR_LIT>' : '<STR_LIT:password>' } ) <EOL> def test_hiddeninput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . HiddenInput ( ) , <EOL> floppyforms . widgets . HiddenInput ) <EOL> widget = forms . widgets . HiddenInput ( ) <EOL> widget . is_hidden = False <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . HiddenInput , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_multiplehiddeninput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . MultipleHiddenInput ( ) , <EOL> floppyforms . widgets . MultipleHiddenInput ) <EOL> widget = forms . widgets . MultipleHiddenInput ( choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:yes>' , '<STR_LIT>' ) , <EOL> ) ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . MultipleHiddenInput , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_fileinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . FileInput ( ) , <EOL> floppyforms . widgets . FileInput ) <EOL> widget = forms . widgets . FileInput ( ) <EOL> widget . needs_multipart_form = False <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . FileInput , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_clearablefileinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . ClearableFileInput ( ) , <EOL> floppyforms . widgets . ClearableFileInput ) <EOL> widget = forms . widgets . ClearableFileInput ( ) <EOL> widget . initial_text = '<STR_LIT>' <EOL> widget . input_text = '<STR_LIT>' <EOL> widget . clear_checkbox_label = '<STR_LIT>' <EOL> widget . template_with_initial = '<STR_LIT>' <EOL> widget . template_with_clear = '<STR_LIT>' <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . ClearableFileInput , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_textarea_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . Textarea ( ) , <EOL> floppyforms . widgets . Textarea ) <EOL> def test_dateinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . DateInput ( ) , <EOL> floppyforms . DateInput ) <EOL> widget = forms . widgets . DateInput ( format = '<STR_LIT>' ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . DateInput , <EOL> [ '<STR_LIT>' ] , <EOL> { '<STR_LIT>' : '<STR_LIT:date>' } ) <EOL> def test_datetimeinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . DateTimeInput ( ) , <EOL> floppyforms . widgets . DateTimeInput ) <EOL> widget = forms . widgets . DateTimeInput ( format = '<STR_LIT>' ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . DateTimeInput , <EOL> [ '<STR_LIT>' ] , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_timeinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . TimeInput ( ) , <EOL> floppyforms . widgets . TimeInput ) <EOL> widget = forms . widgets . TimeInput ( format = '<STR_LIT>' ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . TimeInput , <EOL> [ '<STR_LIT>' ] , <EOL> { '<STR_LIT>' : '<STR_LIT:time>' } ) <EOL> def test_checkboxinput_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . CheckboxInput ( ) , <EOL> floppyforms . widgets . CheckboxInput ) <EOL> check_test = lambda v : False <EOL> widget = forms . widgets . CheckboxInput ( check_test = check_test ) <EOL> new_widget = floppify_widget ( widget ) <EOL> self . assertEqual ( widget . check_test , new_widget . check_test ) <EOL> self . assertTrue ( new_widget . check_test is check_test ) <EOL> def test_select_widget ( self ) : <EOL> choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> self . assertExpectWidget ( <EOL> forms . widgets . Select ( ) , <EOL> floppyforms . widgets . Select ) <EOL> widget = forms . widgets . Select ( choices = choices ) <EOL> widget . allow_multiple_selected = True <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . Select , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> def test_nullbooleanselect_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . NullBooleanSelect ( ) , <EOL> floppyforms . widgets . NullBooleanSelect , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> widget = forms . widgets . NullBooleanSelect ( ) <EOL> widget . choices = list ( widget . choices ) <EOL> value , label = widget . choices [ <NUM_LIT:0> ] <EOL> widget . choices [ <NUM_LIT:0> ] = value , '<STR_LIT>' <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . NullBooleanSelect , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> def test_selectmultiple_widget ( self ) : <EOL> choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> self . assertExpectWidget ( <EOL> forms . widgets . SelectMultiple ( ) , <EOL> floppyforms . widgets . SelectMultiple ) <EOL> widget = forms . widgets . SelectMultiple ( choices = choices ) <EOL> widget . allow_multiple_selected = False <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . SelectMultiple , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> def test_radioselect_widget ( self ) : <EOL> choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> self . assertExpectWidget ( <EOL> forms . widgets . RadioSelect ( ) , <EOL> floppyforms . widgets . RadioSelect ) <EOL> self . assertExpectWidget ( <EOL> forms . widgets . RadioSelect ( choices = choices ) , <EOL> floppyforms . widgets . RadioSelect , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> widget = forms . widgets . RadioSelect ( renderer = '<STR_LIT>' ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> forms . widgets . RadioSelect ) <EOL> def test_checkboxselectmultiple_widget ( self ) : <EOL> choices = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> self . assertExpectWidget ( <EOL> forms . widgets . CheckboxSelectMultiple ( ) , <EOL> floppyforms . widgets . CheckboxSelectMultiple ) <EOL> self . assertExpectWidget ( <EOL> forms . widgets . CheckboxSelectMultiple ( choices = choices ) , <EOL> floppyforms . widgets . CheckboxSelectMultiple , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) ) <EOL> def test_multi_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . widgets . MultiWidget ( [ ] ) , <EOL> floppyforms . widgets . MultiWidget ) <EOL> text_input = forms . widgets . TextInput ( ) <EOL> widget = forms . widgets . MultiWidget ( [ text_input ] ) <EOL> new_widget = floppify_widget ( widget ) <EOL> self . assertEqual ( widget . widgets , new_widget . widgets ) <EOL> self . assertTrue ( new_widget . widgets [ <NUM_LIT:0> ] is text_input ) <EOL> def test_splitdatetime_widget ( self ) : <EOL> widget = forms . widgets . SplitDateTimeWidget ( ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . SplitDateTimeWidget ) <EOL> widget = forms . widgets . SplitDateTimeWidget ( <EOL> date_format = '<STR_LIT>' , time_format = '<STR_LIT>' ) <EOL> new_widget = floppify_widget ( widget ) <EOL> self . assertTrue ( isinstance ( <EOL> new_widget . widgets [ <NUM_LIT:0> ] , floppyforms . widgets . DateInput ) ) <EOL> self . assertTrue ( isinstance ( <EOL> new_widget . widgets [ <NUM_LIT:1> ] , floppyforms . widgets . TimeInput ) ) <EOL> self . assertEqual ( new_widget . widgets [ <NUM_LIT:0> ] . format , '<STR_LIT>' ) <EOL> self . assertEqual ( new_widget . widgets [ <NUM_LIT:1> ] . format , '<STR_LIT>' ) <EOL> def test_splithiddendatetime_widget ( self ) : <EOL> widget = forms . widgets . SplitHiddenDateTimeWidget ( ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . SplitHiddenDateTimeWidget ) <EOL> widget = forms . widgets . SplitHiddenDateTimeWidget ( <EOL> date_format = '<STR_LIT>' , time_format = '<STR_LIT>' ) <EOL> new_widget = floppify_widget ( widget ) <EOL> self . assertTrue ( isinstance ( <EOL> new_widget . widgets [ <NUM_LIT:0> ] , floppyforms . widgets . DateInput ) ) <EOL> self . assertTrue ( isinstance ( <EOL> new_widget . widgets [ <NUM_LIT:1> ] , floppyforms . widgets . TimeInput ) ) <EOL> self . assertEqual ( new_widget . widgets [ <NUM_LIT:0> ] . format , '<STR_LIT>' ) <EOL> self . assertEqual ( new_widget . widgets [ <NUM_LIT:0> ] . is_hidden , True ) <EOL> self . assertEqual ( new_widget . widgets [ <NUM_LIT:1> ] . format , '<STR_LIT>' ) <EOL> self . assertEqual ( new_widget . widgets [ <NUM_LIT:1> ] . is_hidden , True ) <EOL> def test_selectdate_widget ( self ) : <EOL> self . assertExpectWidget ( <EOL> forms . extras . widgets . SelectDateWidget ( ) , <EOL> floppyforms . widgets . SelectDateWidget ) <EOL> widget = forms . extras . widgets . SelectDateWidget ( <EOL> attrs = { '<STR_LIT>' : '<STR_LIT:value>' } , <EOL> years = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . assertExpectWidget ( <EOL> widget , <EOL> floppyforms . widgets . SelectDateWidget , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> class ModelFormTest ( TestCase ) : <EOL> def test_custom_base_form ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> pass <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> form = form_class ( ) <EOL> self . assertTrue ( isinstance ( <EOL> form . fields [ '<STR_LIT:title>' ] . widget , <EOL> floppyforms . widgets . TextInput ) ) <EOL> def test_declared_fields ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> subtitle = forms . CharField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> self . assertTrue ( isinstance ( <EOL> form_class . base_fields [ '<STR_LIT>' ] . widget , <EOL> floppyforms . widgets . TextInput ) ) <EOL> self . assertTrue ( isinstance ( <EOL> form_class . declared_fields [ '<STR_LIT>' ] . widget , <EOL> floppyforms . widgets . TextInput ) ) <EOL> self . assertTrue ( isinstance ( <EOL> form_class . base_fields [ '<STR_LIT:title>' ] . widget , <EOL> floppyforms . widgets . TextInput ) ) <EOL> def test_additional_form_fields ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> subtitle = forms . CharField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> form = form_class ( ) <EOL> self . assertTrue ( isinstance ( <EOL> form . fields [ '<STR_LIT>' ] . widget , <EOL> floppyforms . widgets . TextInput ) ) <EOL> def test_subclassing_forms ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> subtitle = forms . CharField ( ) <EOL> class Meta : <EOL> model = Post <EOL> exclude = [ ] <EOL> class ChildForm ( MyForm ) : <EOL> created = forms . DateField ( ) <EOL> form_class = modelform_factory ( model = Post , form = ChildForm , exclude = [ ] ) <EOL> form = form_class ( ) <EOL> self . assertTrue ( isinstance ( <EOL> form . fields [ '<STR_LIT:title>' ] . widget , <EOL> floppyforms . widgets . TextInput ) ) <EOL> self . assertTrue ( isinstance ( <EOL> form . fields [ '<STR_LIT>' ] . widget , <EOL> floppyforms . widgets . TextInput ) ) <EOL> self . assertTrue ( isinstance ( <EOL> form . fields [ '<STR_LIT>' ] . widget , <EOL> floppyforms . widgets . DateInput ) ) <EOL> class FieldWidgetTest ( TestCase ) : <EOL> def test_dont_overwrite_none_default_widget ( self ) : <EOL> email_input = forms . widgets . Textarea ( ) <EOL> class MyForm ( forms . ModelForm ) : <EOL> email = forms . EmailField ( widget = email_input ) <EOL> class Meta : <EOL> model = Post <EOL> exclude = [ ] <EOL> form_class = floppify_form ( MyForm ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT:email>' ] . widget <EOL> self . assertFalse ( isinstance ( widget , floppyforms . widgets . EmailInput ) ) <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . Textarea ) ) <EOL> def test_float_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> float = forms . FloatField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT:float>' ] . widget <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . NumberInput ) ) <EOL> self . assertEqual ( widget . input_type , '<STR_LIT>' ) <EOL> def test_decimal_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> decimal = forms . DecimalField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT>' ] . widget <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . NumberInput ) ) <EOL> self . assertEqual ( widget . input_type , '<STR_LIT>' ) <EOL> def test_integer_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> integer = forms . IntegerField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT>' ] . widget <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . NumberInput ) ) <EOL> self . assertEqual ( widget . input_type , '<STR_LIT>' ) <EOL> def test_email_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> email = forms . EmailField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT:email>' ] . widget <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . EmailInput ) ) <EOL> self . assertEqual ( widget . input_type , '<STR_LIT:email>' ) <EOL> def test_url_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> url = forms . URLField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT:url>' ] . widget <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . URLInput ) ) <EOL> self . assertEqual ( widget . input_type , '<STR_LIT:url>' ) <EOL> def test_slug_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> slug = forms . SlugField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT>' ] . widget <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . SlugInput ) ) <EOL> self . assertEqual ( widget . input_type , '<STR_LIT:text>' ) <EOL> def test_ipaddress_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> ipaddress = forms . IPAddressField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT>' ] . widget <EOL> self . assertTrue ( isinstance ( widget , floppyforms . widgets . IPAddressInput ) ) <EOL> self . assertEqual ( widget . input_type , '<STR_LIT:text>' ) <EOL> def test_splitdatetime_field ( self ) : <EOL> class MyForm ( forms . ModelForm ) : <EOL> splitdatetime = forms . SplitDateTimeField ( ) <EOL> form_class = modelform_factory ( model = Post , form = MyForm , exclude = [ ] ) <EOL> widget = form_class ( ) . fields [ '<STR_LIT>' ] . widget <EOL> self . assertTrue ( isinstance ( <EOL> widget , floppyforms . widgets . SplitDateTimeWidget ) ) <EOL> self . assertTrue ( isinstance ( <EOL> widget . widgets [ <NUM_LIT:0> ] , floppyforms . widgets . DateInput ) ) <EOL> self . assertTrue ( isinstance ( <EOL> widget . widgets [ <NUM_LIT:1> ] , floppyforms . widgets . TimeInput ) ) </s>
