<s> import datetime <EOL> from django . test import TestCase <EOL> from django import forms <EOL> from models import Category , Writer , Book , DerivedBook , Post , FlexibleDatePost <EOL> from mforms import ( ProductForm , PriceForm , BookForm , DerivedBookForm , <EOL> ExplicitPKForm , PostForm , DerivedPostForm , CustomWriterForm , <EOL> FlexDatePostForm ) <EOL> class IncompleteCategoryFormWithFields ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> url = forms . CharField ( required = False ) <EOL> class Meta : <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> model = Category <EOL> class IncompleteCategoryFormWithExclude ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> url = forms . CharField ( required = False ) <EOL> class Meta : <EOL> exclude = [ '<STR_LIT:url>' ] <EOL> model = Category <EOL> class ValidationTest ( TestCase ) : <EOL> def test_validates_with_replaced_field_not_specified ( self ) : <EOL> form = IncompleteCategoryFormWithFields ( data = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> def test_validates_with_replaced_field_excluded ( self ) : <EOL> form = IncompleteCategoryFormWithExclude ( data = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> def test_notrequired_overrides_notblank ( self ) : <EOL> form = CustomWriterForm ( { } ) <EOL> assert form . is_valid ( ) <EOL> class UniqueTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . writer = Writer . objects . create ( name = '<STR_LIT>' ) <EOL> def test_simple_unique ( self ) : <EOL> form = ProductForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> obj = form . save ( ) <EOL> form = ProductForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> form = ProductForm ( { '<STR_LIT>' : '<STR_LIT>' } , instance = obj ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_unique_together ( self ) : <EOL> """<STR_LIT>""" <EOL> form = PriceForm ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> form = PriceForm ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> def test_unique_null ( self ) : <EOL> title = '<STR_LIT>' <EOL> form = BookForm ( { '<STR_LIT:title>' : title , '<STR_LIT>' : self . writer . pk } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> form = BookForm ( { '<STR_LIT:title>' : title , '<STR_LIT>' : self . writer . pk } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> form = BookForm ( { '<STR_LIT:title>' : title } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> form = BookForm ( { '<STR_LIT:title>' : title } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_inherited_unique ( self ) : <EOL> title = '<STR_LIT>' <EOL> Book . objects . create ( title = title , author = self . writer , special_id = <NUM_LIT:1> ) <EOL> form = DerivedBookForm ( { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : self . writer . pk , '<STR_LIT>' : u'<STR_LIT:1>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> def test_inherited_unique_together ( self ) : <EOL> title = '<STR_LIT>' <EOL> form = BookForm ( { '<STR_LIT:title>' : title , '<STR_LIT>' : self . writer . pk } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> form = DerivedBookForm ( { '<STR_LIT:title>' : title , '<STR_LIT>' : self . writer . pk , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> def test_abstract_inherited_unique ( self ) : <EOL> title = '<STR_LIT>' <EOL> isbn = '<STR_LIT>' <EOL> dbook = DerivedBook . objects . create ( title = title , author = self . writer , isbn = isbn ) <EOL> form = DerivedBookForm ( { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : self . writer . pk , '<STR_LIT>' : isbn } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> def test_abstract_inherited_unique_together ( self ) : <EOL> title = '<STR_LIT>' <EOL> isbn = '<STR_LIT>' <EOL> dbook = DerivedBook . objects . create ( title = title , author = self . writer , isbn = isbn ) <EOL> form = DerivedBookForm ( { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : self . writer . pk , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : u'<STR_LIT:0>' , '<STR_LIT>' : u'<STR_LIT:0>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> def test_explicitpk_unspecified ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ExplicitPKForm ( { '<STR_LIT:key>' : u'<STR_LIT>' , '<STR_LIT>' : u'<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> def test_explicitpk_unique ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ExplicitPKForm ( { '<STR_LIT:key>' : u'<STR_LIT>' , '<STR_LIT>' : u'<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> form = ExplicitPKForm ( { '<STR_LIT:key>' : u'<STR_LIT>' , '<STR_LIT>' : u'<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT:key>' ] , [ u'<STR_LIT>' ] ) <EOL> def test_unique_for_date ( self ) : <EOL> p = Post . objects . create ( title = "<STR_LIT>" , <EOL> slug = "<STR_LIT>" , subtitle = "<STR_LIT>" , posted = datetime . date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:3> ) ) <EOL> form = PostForm ( { '<STR_LIT:title>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT:title>' ] , [ u'<STR_LIT>' ] ) <EOL> form = PostForm ( { '<STR_LIT:title>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = PostForm ( { '<STR_LIT:title>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = PostForm ( { '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> form = PostForm ( { '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> form = PostForm ( { '<STR_LIT>' : "<STR_LIT>" , "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } , instance = p ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = PostForm ( { '<STR_LIT:title>' : "<STR_LIT>" } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> def test_inherited_unique_for_date ( self ) : <EOL> p = Post . objects . create ( title = "<STR_LIT>" , <EOL> slug = "<STR_LIT>" , subtitle = "<STR_LIT>" , posted = datetime . date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:3> ) ) <EOL> form = DerivedPostForm ( { '<STR_LIT:title>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT:title>' ] , [ u'<STR_LIT>' ] ) <EOL> form = DerivedPostForm ( { '<STR_LIT:title>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = DerivedPostForm ( { '<STR_LIT:title>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = DerivedPostForm ( { '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> form = DerivedPostForm ( { '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ u'<STR_LIT>' ] ) <EOL> form = DerivedPostForm ( { '<STR_LIT>' : "<STR_LIT>" , "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , '<STR_LIT>' : '<STR_LIT>' } , instance = p ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_unique_for_date_with_nullable_date ( self ) : <EOL> p = FlexibleDatePost . objects . create ( title = "<STR_LIT>" , <EOL> slug = "<STR_LIT>" , subtitle = "<STR_LIT>" , posted = datetime . date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:3> ) ) <EOL> form = FlexDatePostForm ( { '<STR_LIT:title>' : "<STR_LIT>" } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = FlexDatePostForm ( { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = FlexDatePostForm ( { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form = FlexDatePostForm ( { '<STR_LIT>' : "<STR_LIT>" , "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } , instance = p ) <EOL> self . assertTrue ( form . is_valid ( ) ) </s>
