<s> from __future__ import absolute_import , unicode_literals <EOL> import datetime <EOL> import os <EOL> from decimal import Decimal <EOL> from django import forms <EOL> from django . core . files . uploadedfile import SimpleUploadedFile <EOL> from django . core . validators import ValidationError <EOL> from django . db import connection <EOL> from django . db . models . query import EmptyQuerySet <EOL> from django . forms . models import model_to_dict <EOL> from django . utils . _os import upath <EOL> from django . utils . unittest import skipUnless <EOL> from django . test import TestCase <EOL> from django . utils import six <EOL> from . models import ( Article , ArticleStatus , BetterWriter , BigInt , Book , <EOL> Category , CommaSeparatedInteger , CustomFieldForExclusionModel , DerivedBook , <EOL> DerivedPost , ExplicitPK , FlexibleDatePost , ImprovedArticle , <EOL> ImprovedArticleWithParentLink , Inventory , Post , Price , <EOL> Product , TextFile , Writer , WriterProfile , Colour , ColourfulItem , <EOL> test_images ) <EOL> if test_images : <EOL> from . models import ImageFile , OptionalImageFile <EOL> class ImageFileForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = ImageFile <EOL> class OptionalImageFileForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = OptionalImageFile <EOL> class ProductForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Product <EOL> class PriceForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Price <EOL> class BookForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Book <EOL> class DerivedBookForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = DerivedBook <EOL> class ExplicitPKForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = ExplicitPK <EOL> fields = ( '<STR_LIT:key>' , '<STR_LIT>' , ) <EOL> class PostForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Post <EOL> class DerivedPostForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = DerivedPost <EOL> class CustomWriterForm ( forms . ModelForm ) : <EOL> name = forms . CharField ( required = False ) <EOL> class Meta : <EOL> model = Writer <EOL> class FlexDatePostForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = FlexibleDatePost <EOL> class BaseCategoryForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Category <EOL> class ArticleForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Article <EOL> class ArticleForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Article <EOL> class PartialArticleForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Article <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class RoykoForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Writer <EOL> class TestArticleForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Article <EOL> class PartialArticleFormWithSlug ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Article <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ArticleStatusForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = ArticleStatus <EOL> class InventoryForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Inventory <EOL> class SelectInventoryForm ( forms . Form ) : <EOL> items = forms . ModelMultipleChoiceField ( Inventory . objects . all ( ) , to_field_name = '<STR_LIT>' ) <EOL> class CustomFieldForExclusionForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = CustomFieldForExclusionModel <EOL> fields = [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> class ShortCategory ( forms . ModelForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT:5> ) <EOL> slug = forms . CharField ( max_length = <NUM_LIT:5> ) <EOL> url = forms . CharField ( max_length = <NUM_LIT:3> ) <EOL> class ImprovedArticleForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = ImprovedArticle <EOL> class ImprovedArticleWithParentLinkForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = ImprovedArticleWithParentLink <EOL> class BetterWriterForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = BetterWriter <EOL> class WriterProfileForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = WriterProfile <EOL> class TextFileForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = TextFile <EOL> class BigIntForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = BigInt <EOL> class ModelFormWithMedia ( forms . ModelForm ) : <EOL> class Media : <EOL> js = ( '<STR_LIT>' , ) <EOL> css = { <EOL> '<STR_LIT:all>' : ( '<STR_LIT>' , ) <EOL> } <EOL> class Meta : <EOL> model = TextFile <EOL> class CommaSeparatedIntegerForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = CommaSeparatedInteger <EOL> class PriceFormWithoutQuantity ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Price <EOL> exclude = ( '<STR_LIT>' , ) <EOL> class ColourfulItemForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = ColourfulItem <EOL> class ModelFormBaseTest ( TestCase ) : <EOL> def test_base_form ( self ) : <EOL> self . assertEqual ( list ( BaseCategoryForm . base_fields ) , <EOL> [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:url>' ] ) <EOL> def test_extra_fields ( self ) : <EOL> class ExtraFields ( BaseCategoryForm ) : <EOL> some_extra_field = forms . BooleanField ( ) <EOL> self . assertEqual ( list ( ExtraFields . base_fields ) , <EOL> [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:url>' , '<STR_LIT>' ] ) <EOL> def test_replace_field ( self ) : <EOL> class ReplaceField ( forms . ModelForm ) : <EOL> url = forms . BooleanField ( ) <EOL> class Meta : <EOL> model = Category <EOL> self . assertTrue ( isinstance ( ReplaceField . base_fields [ '<STR_LIT:url>' ] , <EOL> forms . fields . BooleanField ) ) <EOL> def test_override_field ( self ) : <EOL> class WriterForm ( forms . ModelForm ) : <EOL> book = forms . CharField ( required = False ) <EOL> class Meta : <EOL> model = Writer <EOL> wf = WriterForm ( { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( wf . is_valid ( ) ) <EOL> def test_limit_fields ( self ) : <EOL> class LimitFields ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Category <EOL> fields = [ '<STR_LIT:url>' ] <EOL> self . assertEqual ( list ( LimitFields . base_fields ) , <EOL> [ '<STR_LIT:url>' ] ) <EOL> def test_exclude_fields ( self ) : <EOL> class ExcludeFields ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Category <EOL> exclude = [ '<STR_LIT:url>' ] <EOL> self . assertEqual ( list ( ExcludeFields . base_fields ) , <EOL> [ '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> def test_confused_form ( self ) : <EOL> class ConfusedForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = Category <EOL> fields = [ '<STR_LIT:name>' , '<STR_LIT:url>' ] <EOL> exclude = [ '<STR_LIT:url>' ] <EOL> self . assertEqual ( list ( ConfusedForm . base_fields ) , <EOL> [ '<STR_LIT:name>' ] ) <EOL> def test_mixmodel_form ( self ) : <EOL> class MixModelForm ( BaseCategoryForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = Article <EOL> self . assertEqual ( <EOL> list ( MixModelForm . base_fields ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' ] <EOL> ) <EOL> def test_article_form ( self ) : <EOL> self . assertEqual ( <EOL> list ( ArticleForm . base_fields ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' ] <EOL> ) <EOL> def test_bad_form ( self ) : <EOL> class BadForm ( ArticleForm , BaseCategoryForm ) : <EOL> pass <EOL> self . assertEqual ( <EOL> list ( BadForm . base_fields ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' ] <EOL> ) <EOL> def test_subcategory_form ( self ) : <EOL> class SubCategoryForm ( BaseCategoryForm ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> self . assertEqual ( list ( SubCategoryForm . base_fields ) , <EOL> [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:url>' ] ) <EOL> def test_subclassmeta_form ( self ) : <EOL> class SomeCategoryForm ( forms . ModelForm ) : <EOL> checkbox = forms . BooleanField ( ) <EOL> class Meta : <EOL> model = Category <EOL> class SubclassMeta ( SomeCategoryForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( SomeCategoryForm . Meta ) : <EOL> exclude = [ '<STR_LIT:url>' ] <EOL> self . assertHTMLEqual ( <EOL> str ( SubclassMeta ( ) ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> def test_orderfields_form ( self ) : <EOL> class OrderFields ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Category <EOL> fields = [ '<STR_LIT:url>' , '<STR_LIT:name>' ] <EOL> self . assertEqual ( list ( OrderFields . base_fields ) , <EOL> [ '<STR_LIT:url>' , '<STR_LIT:name>' ] ) <EOL> self . assertHTMLEqual ( <EOL> str ( OrderFields ( ) ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> def test_orderfields2_form ( self ) : <EOL> class OrderFields2 ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Category <EOL> fields = [ '<STR_LIT>' , '<STR_LIT:url>' , '<STR_LIT:name>' ] <EOL> exclude = [ '<STR_LIT:url>' ] <EOL> self . assertEqual ( list ( OrderFields2 . base_fields ) , <EOL> [ '<STR_LIT>' , '<STR_LIT:name>' ] ) <EOL> class TestWidgetForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Category <EOL> fields = [ '<STR_LIT:name>' , '<STR_LIT:url>' , '<STR_LIT>' ] <EOL> widgets = { <EOL> '<STR_LIT:name>' : forms . Textarea , <EOL> '<STR_LIT:url>' : forms . TextInput ( attrs = { '<STR_LIT:class>' : '<STR_LIT:url>' } ) <EOL> } <EOL> class TestWidgets ( TestCase ) : <EOL> def test_base_widgets ( self ) : <EOL> frm = TestWidgetForm ( ) <EOL> self . assertHTMLEqual ( <EOL> str ( frm [ '<STR_LIT:name>' ] ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertHTMLEqual ( <EOL> str ( frm [ '<STR_LIT:url>' ] ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertHTMLEqual ( <EOL> str ( frm [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' <EOL> ) <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>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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>' : '<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>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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 ( { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . writer . pk , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT:0>' <EOL> } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_explicitpk_unspecified ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ExplicitPKForm ( { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> def test_explicitpk_unique ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ExplicitPKForm ( { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> form = ExplicitPKForm ( { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( form . errors ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT:key>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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>' ] , [ '<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 ( ) ) <EOL> class ModelToDictTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_model_to_dict_many_to_many ( self ) : <EOL> categories = [ <EOL> Category ( name = '<STR_LIT>' , slug = '<STR_LIT>' , url = '<STR_LIT>' ) , <EOL> Category ( name = '<STR_LIT>' , slug = '<STR_LIT>' , url = '<STR_LIT>' ) , <EOL> Category ( name = '<STR_LIT>' , slug = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> ] <EOL> for c in categories : <EOL> c . save ( ) <EOL> writer = Writer ( name = '<STR_LIT>' ) <EOL> writer . save ( ) <EOL> art = Article ( <EOL> headline = '<STR_LIT>' , <EOL> slug = '<STR_LIT>' , <EOL> pub_date = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:4> ) , <EOL> writer = writer , <EOL> article = '<STR_LIT>' <EOL> ) <EOL> art . save ( ) <EOL> for c in categories : <EOL> art . categories . add ( c ) <EOL> art . save ( ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> d = model_to_dict ( art ) <EOL> for c in categories : <EOL> self . assertIn ( c . pk , d [ '<STR_LIT>' ] ) <EOL> self . assertIsInstance ( d [ '<STR_LIT>' ] , list ) <EOL> class OldFormForXTests ( TestCase ) : <EOL> def test_base_form ( self ) : <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:0> ) <EOL> f = BaseCategoryForm ( ) <EOL> self . assertHTMLEqual ( <EOL> str ( f ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertHTMLEqual ( <EOL> str ( f . as_ul ( ) ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> self . assertHTMLEqual ( <EOL> str ( f [ "<STR_LIT:name>" ] ) , <EOL> """<STR_LIT>""" ) <EOL> def test_auto_id ( self ) : <EOL> f = BaseCategoryForm ( auto_id = False ) <EOL> self . assertHTMLEqual ( <EOL> str ( f . as_ul ( ) ) , <EOL> """<STR_LIT>""" <EOL> ) <EOL> def test_with_data ( self ) : <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:0> ) <EOL> f = BaseCategoryForm ( { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( f . is_valid ( ) ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:url>' ] , '<STR_LIT>' ) <EOL> c1 = f . save ( ) <EOL> self . assertEqual ( c1 , Category . objects . all ( ) [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( c1 . name , "<STR_LIT>" ) <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:1> ) <EOL> f = BaseCategoryForm ( { '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT:test>' } ) <EOL> self . assertTrue ( f . is_valid ( ) ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:name>' ] , "<STR_LIT>" ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:url>' ] , '<STR_LIT:test>' ) <EOL> c2 = f . save ( ) <EOL> self . assertEqual ( c2 , Category . objects . get ( pk = c2 . pk ) ) <EOL> self . assertEqual ( c2 . name , "<STR_LIT>" ) <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:2> ) <EOL> f = BaseCategoryForm ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:url>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:url>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> c3 = f . save ( commit = False ) <EOL> self . assertEqual ( c3 . name , "<STR_LIT>" ) <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:2> ) <EOL> c3 . save ( ) <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:3> ) <EOL> f = BaseCategoryForm ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:url>' : '<STR_LIT:foo>' } ) <EOL> self . assertEqual ( f . errors [ '<STR_LIT:name>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( f . errors [ '<STR_LIT>' ] , [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( f . cleaned_data , { '<STR_LIT:url>' : '<STR_LIT:foo>' } ) <EOL> with self . assertRaises ( ValueError ) : <EOL> f . save ( ) <EOL> f = BaseCategoryForm ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:url>' : '<STR_LIT:foo>' } ) <EOL> with self . assertRaises ( ValueError ) : <EOL> f . save ( ) <EOL> w_royko = Writer ( name = '<STR_LIT>' ) <EOL> w_royko . save ( ) <EOL> w_woodward = Writer ( name = '<STR_LIT>' ) <EOL> w_woodward . save ( ) <EOL> f = ArticleForm ( auto_id = False ) <EOL> self . assertHTMLEqual ( six . text_type ( f ) , '''<STR_LIT>''' % ( w_woodward . pk , w_royko . pk , c1 . pk , c2 . pk , c3 . pk ) ) <EOL> f = PartialArticleForm ( auto_id = False ) <EOL> self . assertHTMLEqual ( six . text_type ( f ) , '''<STR_LIT>''' ) <EOL> w = Writer . objects . get ( name = '<STR_LIT>' ) <EOL> f = RoykoForm ( auto_id = False , instance = w ) <EOL> self . assertHTMLEqual ( six . text_type ( f ) , '''<STR_LIT>''' ) <EOL> art = Article ( <EOL> headline = '<STR_LIT>' , <EOL> slug = '<STR_LIT>' , <EOL> pub_date = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:4> ) , <EOL> writer = w , <EOL> article = '<STR_LIT>' <EOL> ) <EOL> art . save ( ) <EOL> art_id_1 = art . id <EOL> self . assertEqual ( art_id_1 is not None , True ) <EOL> f = TestArticleForm ( auto_id = False , instance = art ) <EOL> self . assertHTMLEqual ( f . as_ul ( ) , '''<STR_LIT>''' % ( w_woodward . pk , w_royko . pk , c1 . pk , c2 . pk , c3 . pk ) ) <EOL> f = TestArticleForm ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : six . text_type ( w_royko . pk ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , instance = art ) <EOL> self . assertEqual ( f . errors , { } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> test_art = f . save ( ) <EOL> self . assertEqual ( test_art . id == art_id_1 , True ) <EOL> test_art = Article . objects . get ( id = art_id_1 ) <EOL> self . assertEqual ( test_art . headline , '<STR_LIT>' ) <EOL> f = PartialArticleFormWithSlug ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , auto_id = False , instance = art ) <EOL> self . assertHTMLEqual ( f . as_ul ( ) , '''<STR_LIT>''' ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> new_art = f . save ( ) <EOL> self . assertEqual ( new_art . id == art_id_1 , True ) <EOL> new_art = Article . objects . get ( id = art_id_1 ) <EOL> self . assertEqual ( new_art . headline , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( new_art . categories . all ( ) , [ ] ) <EOL> new_art . categories . add ( Category . objects . get ( name = '<STR_LIT>' ) ) <EOL> self . assertQuerysetEqual ( new_art . categories . all ( ) , [ "<STR_LIT>" ] ) <EOL> f = TestArticleForm ( auto_id = False , instance = new_art ) <EOL> self . assertHTMLEqual ( f . as_ul ( ) , '''<STR_LIT>''' % ( w_woodward . pk , w_royko . pk , c1 . pk , c2 . pk , c3 . pk ) ) <EOL> f = TestArticleForm ( <EOL> auto_id = False , <EOL> initial = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ str ( c1 . id ) , str ( c2 . id ) ] <EOL> } ) <EOL> self . assertHTMLEqual ( f . as_ul ( ) , '''<STR_LIT>''' % ( w_woodward . pk , w_royko . pk , c1 . pk , c2 . pk , c3 . pk ) ) <EOL> f = TestArticleForm ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : six . text_type ( w_royko . pk ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ six . text_type ( c1 . id ) , six . text_type ( c2 . id ) ] <EOL> } , instance = new_art ) <EOL> new_art = f . save ( ) <EOL> self . assertEqual ( new_art . id == art_id_1 , True ) <EOL> new_art = Article . objects . get ( id = art_id_1 ) <EOL> self . assertQuerysetEqual ( new_art . categories . order_by ( '<STR_LIT:name>' ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> f = TestArticleForm ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : six . text_type ( w_royko . pk ) , '<STR_LIT>' : '<STR_LIT>' } , instance = new_art ) <EOL> new_art = f . save ( ) <EOL> self . assertEqual ( new_art . id == art_id_1 , True ) <EOL> new_art = Article . objects . get ( id = art_id_1 ) <EOL> self . assertQuerysetEqual ( new_art . categories . all ( ) , [ ] ) <EOL> f = ArticleForm ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : six . text_type ( w_royko . pk ) , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : [ six . text_type ( c1 . id ) , six . text_type ( c2 . id ) ] } ) <EOL> new_art = f . save ( ) <EOL> art_id_2 = new_art . id <EOL> self . assertEqual ( art_id_2 not in ( None , art_id_1 ) , True ) <EOL> new_art = Article . objects . get ( id = art_id_2 ) <EOL> self . assertQuerysetEqual ( new_art . categories . order_by ( '<STR_LIT:name>' ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> f = ArticleForm ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : six . text_type ( w_royko . pk ) , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> new_art = f . save ( ) <EOL> art_id_3 = new_art . id <EOL> self . assertEqual ( art_id_3 not in ( None , art_id_1 , art_id_2 ) , True ) <EOL> new_art = Article . objects . get ( id = art_id_3 ) <EOL> self . assertQuerysetEqual ( new_art . categories . all ( ) , [ ] ) <EOL> f = ArticleForm ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : six . text_type ( w_royko . pk ) , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : [ six . text_type ( c1 . id ) , six . text_type ( c2 . id ) ] } ) <EOL> new_art = f . save ( commit = False ) <EOL> new_art . save ( ) <EOL> art_id_4 = new_art . id <EOL> self . assertEqual ( art_id_4 not in ( None , art_id_1 , art_id_2 , art_id_3 ) , True ) <EOL> new_art = Article . objects . get ( id = art_id_4 ) <EOL> self . assertQuerysetEqual ( new_art . categories . all ( ) , [ ] ) <EOL> f . save_m2m ( ) <EOL> self . assertQuerysetEqual ( new_art . categories . order_by ( '<STR_LIT:name>' ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> cat = Category . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( cat . name , "<STR_LIT>" ) <EOL> self . assertEqual ( cat . id == c3 . id , True ) <EOL> form = ShortCategory ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:url>' : '<STR_LIT>' } , instance = cat ) <EOL> self . assertEqual ( form . save ( ) . name , '<STR_LIT>' ) <EOL> self . assertEqual ( Category . objects . get ( id = c3 . id ) . name , '<STR_LIT>' ) <EOL> f = ArticleForm ( auto_id = False ) <EOL> self . assertHTMLEqual ( f . as_ul ( ) , '''<STR_LIT>''' % ( w_woodward . pk , w_royko . pk , c1 . pk , c2 . pk , c3 . pk ) ) <EOL> c4 = Category . objects . create ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> self . assertEqual ( c4 . name , '<STR_LIT>' ) <EOL> w_bernstein = Writer . objects . create ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( w_bernstein . name , '<STR_LIT>' ) <EOL> self . assertHTMLEqual ( f . as_ul ( ) , '''<STR_LIT>''' % ( w_woodward . pk , w_bernstein . pk , w_royko . pk , c1 . pk , c2 . pk , c3 . pk , c4 . pk ) ) <EOL> f = forms . ModelChoiceField ( Category . objects . all ( ) ) <EOL> self . assertEqual ( list ( f . choices ) , [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( c1 . pk , '<STR_LIT>' ) , <EOL> ( c2 . pk , "<STR_LIT>" ) , <EOL> ( c3 . pk , '<STR_LIT>' ) , <EOL> ( c4 . pk , '<STR_LIT>' ) ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( '<STR_LIT>' ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( None ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( <NUM_LIT:0> ) <EOL> self . assertEqual ( f . clean ( c3 . id ) . name , '<STR_LIT>' ) <EOL> self . assertEqual ( f . clean ( c2 . id ) . name , "<STR_LIT>" ) <EOL> c5 = Category . objects . create ( name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> self . assertEqual ( c5 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( f . clean ( c5 . id ) . name , '<STR_LIT>' ) <EOL> Category . objects . get ( url = '<STR_LIT>' ) . delete ( ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( c5 . id ) <EOL> f = forms . ModelChoiceField ( Category . objects . filter ( pk = c1 . id ) , required = False ) <EOL> self . assertEqual ( f . clean ( '<STR_LIT>' ) , None ) <EOL> f . clean ( '<STR_LIT>' ) <EOL> self . assertEqual ( f . clean ( str ( c1 . id ) ) . name , "<STR_LIT>" ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( '<STR_LIT:100>' ) <EOL> f . queryset = Category . objects . exclude ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( list ( f . choices ) , [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( c1 . pk , '<STR_LIT>' ) , <EOL> ( c2 . pk , "<STR_LIT>" ) , <EOL> ( c3 . pk , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( f . clean ( c3 . id ) . name , '<STR_LIT>' ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( c4 . id ) <EOL> gen_one = list ( f . choices ) <EOL> gen_two = f . choices <EOL> self . assertEqual ( gen_one [ <NUM_LIT:2> ] , ( c2 . pk , "<STR_LIT>" ) ) <EOL> self . assertEqual ( list ( gen_two ) , [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( c1 . pk , '<STR_LIT>' ) , <EOL> ( c2 . pk , "<STR_LIT>" ) , <EOL> ( c3 . pk , '<STR_LIT>' ) ] ) <EOL> f . queryset = Category . objects . all ( ) <EOL> f . label_from_instance = lambda obj : "<STR_LIT>" + str ( obj ) <EOL> self . assertEqual ( list ( f . choices ) , [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( c1 . pk , '<STR_LIT>' ) , <EOL> ( c2 . pk , "<STR_LIT>" ) , <EOL> ( c3 . pk , '<STR_LIT>' ) , <EOL> ( c4 . pk , '<STR_LIT>' ) ] ) <EOL> f = forms . ModelMultipleChoiceField ( Category . objects . all ( ) ) <EOL> self . assertEqual ( list ( f . choices ) , [ <EOL> ( c1 . pk , '<STR_LIT>' ) , <EOL> ( c2 . pk , "<STR_LIT>" ) , <EOL> ( c3 . pk , '<STR_LIT>' ) , <EOL> ( c4 . pk , '<STR_LIT>' ) ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( None ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ ] ) <EOL> self . assertQuerysetEqual ( f . clean ( [ c1 . id ] ) , [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( f . clean ( [ c2 . id ] ) , [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( f . clean ( [ str ( c1 . id ) ] ) , [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( f . clean ( [ str ( c1 . id ) , str ( c2 . id ) ] ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( f . clean ( [ c1 . id , str ( c2 . id ) ] ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( f . clean ( ( c1 . id , str ( c2 . id ) ) ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ '<STR_LIT:100>' ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( '<STR_LIT:hello>' ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ '<STR_LIT>' ] ) <EOL> c6 = Category . objects . create ( id = <NUM_LIT> , name = '<STR_LIT>' , url = '<STR_LIT>' ) <EOL> self . assertEqual ( c6 . name , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( f . clean ( [ c6 . id ] ) , [ "<STR_LIT>" ] ) <EOL> Category . objects . get ( url = '<STR_LIT>' ) . delete ( ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ c6 . id ] ) <EOL> f = forms . ModelMultipleChoiceField ( Category . objects . all ( ) , required = False ) <EOL> self . assertIsInstance ( f . clean ( [ ] ) , EmptyQuerySet ) <EOL> self . assertIsInstance ( f . clean ( ( ) ) , EmptyQuerySet ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ '<STR_LIT>' ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ str ( c3 . id ) , '<STR_LIT>' ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ str ( c1 . id ) , '<STR_LIT>' ] ) <EOL> f . queryset = Category . objects . exclude ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( list ( f . choices ) , [ <EOL> ( c1 . pk , '<STR_LIT>' ) , <EOL> ( c2 . pk , "<STR_LIT>" ) , <EOL> ( c3 . pk , '<STR_LIT>' ) ] ) <EOL> self . assertQuerysetEqual ( f . clean ( [ c3 . id ] ) , [ "<STR_LIT>" ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ c4 . id ] ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . clean ( [ str ( c3 . id ) , str ( c4 . id ) ] ) <EOL> f . queryset = Category . objects . all ( ) <EOL> f . label_from_instance = lambda obj : "<STR_LIT>" + str ( obj ) <EOL> self . assertEqual ( list ( f . choices ) , [ <EOL> ( c1 . pk , '<STR_LIT>' ) , <EOL> ( c2 . pk , "<STR_LIT>" ) , <EOL> ( c3 . pk , '<STR_LIT>' ) , <EOL> ( c4 . pk , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( list ( ImprovedArticleForm . base_fields ) , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( list ( ImprovedArticleWithParentLinkForm . base_fields ) , [ ] ) <EOL> bw = BetterWriter ( name = '<STR_LIT>' , score = <NUM_LIT:10> ) <EOL> bw . save ( ) <EOL> self . assertEqual ( sorted ( model_to_dict ( bw ) ) , <EOL> [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> form = BetterWriterForm ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:12> } ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> bw2 = form . save ( ) <EOL> bw2 . delete ( ) <EOL> form = WriterProfileForm ( ) <EOL> self . assertHTMLEqual ( form . as_p ( ) , '''<STR_LIT>''' % ( w_woodward . pk , w_bernstein . pk , bw . pk , w_royko . pk ) ) <EOL> data = { <EOL> '<STR_LIT>' : six . text_type ( w_woodward . pk ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> form = WriterProfileForm ( data ) <EOL> instance = form . save ( ) <EOL> self . assertEqual ( six . text_type ( instance ) , '<STR_LIT>' ) <EOL> form = WriterProfileForm ( instance = instance ) <EOL> self . assertHTMLEqual ( form . as_p ( ) , '''<STR_LIT>''' % ( w_woodward . pk , w_bernstein . pk , bw . pk , w_royko . pk ) ) <EOL> def test_file_field ( self ) : <EOL> f = TextFileForm ( data = { '<STR_LIT:description>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . is_valid ( ) , False ) <EOL> f = TextFileForm ( data = { '<STR_LIT:description>' : '<STR_LIT>' } , files = { } ) <EOL> self . assertEqual ( f . is_valid ( ) , False ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:file>' : SimpleUploadedFile ( '<STR_LIT>' , b'<STR_LIT>' ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( type ( f . cleaned_data [ '<STR_LIT:file>' ] ) , SimpleUploadedFile ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> instance . file . delete ( ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:file>' : SimpleUploadedFile ( '<STR_LIT>' , b'<STR_LIT>' ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( type ( f . cleaned_data [ '<STR_LIT:file>' ] ) , SimpleUploadedFile ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:file>' : SimpleUploadedFile ( '<STR_LIT>' , b'<STR_LIT>' ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , False ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> instance = instance ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:file>' ] . name , '<STR_LIT>' ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> instance . file . delete ( ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:file>' : SimpleUploadedFile ( '<STR_LIT>' , b'<STR_LIT>' ) } , instance = instance ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> instance . file . delete ( ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:file>' : SimpleUploadedFile ( '<STR_LIT>' , b'<STR_LIT>' ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> instance . file . delete ( ) <EOL> instance . delete ( ) <EOL> f = TextFileForm ( data = { '<STR_LIT:description>' : '<STR_LIT>' } ) <EOL> f . fields [ '<STR_LIT:file>' ] . required = False <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:file>' : SimpleUploadedFile ( '<STR_LIT>' , b'<STR_LIT>' ) } , instance = instance ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> instance = instance ) <EOL> f . fields [ '<STR_LIT:file>' ] . required = False <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . description , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> instance . file . delete ( ) <EOL> instance . delete ( ) <EOL> f = TextFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:file>' : SimpleUploadedFile ( '<STR_LIT>' , b'<STR_LIT>' ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . file . name , '<STR_LIT>' ) <EOL> instance . file . delete ( ) <EOL> instance . delete ( ) <EOL> def test_big_integer_field ( self ) : <EOL> bif = BigIntForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( bif . is_valid ( ) , True ) <EOL> bif = BigIntForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( bif . is_valid ( ) , False ) <EOL> self . assertEqual ( bif . errors , { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> bif = BigIntForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( bif . is_valid ( ) , True ) <EOL> bif = BigIntForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( bif . is_valid ( ) , False ) <EOL> self . assertEqual ( bif . errors , { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> @ skipUnless ( test_images , "<STR_LIT>" ) <EOL> def test_image_field ( self ) : <EOL> with open ( os . path . join ( os . path . dirname ( upath ( __file__ ) ) , "<STR_LIT>" ) , '<STR_LIT:rb>' ) as fp : <EOL> image_data = fp . read ( ) <EOL> with open ( os . path . join ( os . path . dirname ( upath ( __file__ ) ) , "<STR_LIT>" ) , '<STR_LIT:rb>' ) as fp : <EOL> image_data2 = fp . read ( ) <EOL> f = ImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:image>' : SimpleUploadedFile ( '<STR_LIT>' , image_data ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( type ( f . cleaned_data [ '<STR_LIT:image>' ] ) , SimpleUploadedFile ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . width , <NUM_LIT:16> ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:16> ) <EOL> instance . image . delete ( save = False ) <EOL> f = ImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:image>' : SimpleUploadedFile ( '<STR_LIT>' , image_data ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( type ( f . cleaned_data [ '<STR_LIT:image>' ] ) , SimpleUploadedFile ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . width , <NUM_LIT:16> ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:16> ) <EOL> f = ImageFileForm ( data = { '<STR_LIT:description>' : '<STR_LIT>' } , instance = instance ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( f . cleaned_data [ '<STR_LIT:image>' ] . name , '<STR_LIT>' ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:16> ) <EOL> self . assertEqual ( instance . width , <NUM_LIT:16> ) <EOL> instance . image . delete ( save = False ) <EOL> f = ImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:image>' : SimpleUploadedFile ( '<STR_LIT>' , image_data2 ) } , instance = instance ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:32> ) <EOL> self . assertEqual ( instance . width , <NUM_LIT> ) <EOL> instance . image . delete ( save = False ) <EOL> instance . delete ( ) <EOL> f = ImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:image>' : SimpleUploadedFile ( '<STR_LIT>' , image_data2 ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:32> ) <EOL> self . assertEqual ( instance . width , <NUM_LIT> ) <EOL> instance . image . delete ( save = False ) <EOL> instance . delete ( ) <EOL> if connection . features . interprets_empty_strings_as_nulls : <EOL> expected_null_imagefield_repr = '<STR_LIT>' <EOL> else : <EOL> expected_null_imagefield_repr = None <EOL> f = OptionalImageFileForm ( data = { '<STR_LIT:description>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , expected_null_imagefield_repr ) <EOL> self . assertEqual ( instance . width , None ) <EOL> self . assertEqual ( instance . height , None ) <EOL> f = OptionalImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:image>' : SimpleUploadedFile ( '<STR_LIT>' , image_data ) } , instance = instance ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . width , <NUM_LIT:16> ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:16> ) <EOL> f = OptionalImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> instance = instance ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . description , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . width , <NUM_LIT:16> ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:16> ) <EOL> instance . image . delete ( ) <EOL> instance . delete ( ) <EOL> f = OptionalImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' } , <EOL> files = { '<STR_LIT:image>' : SimpleUploadedFile ( '<STR_LIT>' , image_data2 ) } <EOL> ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . width , <NUM_LIT> ) <EOL> self . assertEqual ( instance . height , <NUM_LIT:32> ) <EOL> instance . delete ( ) <EOL> f = ImageFileForm ( <EOL> data = { '<STR_LIT:description>' : '<STR_LIT>' , '<STR_LIT:path>' : '<STR_LIT:foo>' } , <EOL> files = { '<STR_LIT:image>' : SimpleUploadedFile ( '<STR_LIT>' , image_data ) } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> instance = f . save ( ) <EOL> self . assertEqual ( instance . image . name , '<STR_LIT>' ) <EOL> instance . delete ( ) <EOL> def test_media_on_modelform ( self ) : <EOL> f = ModelFormWithMedia ( ) <EOL> self . assertHTMLEqual ( six . text_type ( f . media ) , '''<STR_LIT>''' ) <EOL> f = CommaSeparatedIntegerForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( f . cleaned_data , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> f = CommaSeparatedIntegerForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . errors , { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> f = CommaSeparatedIntegerForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( f . cleaned_data , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> f = CommaSeparatedIntegerForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . errors , { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> f = CommaSeparatedIntegerForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . errors , { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> f = CommaSeparatedIntegerForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( f . cleaned_data , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> f = CommaSeparatedIntegerForm ( { '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> self . assertEqual ( f . is_valid ( ) , True ) <EOL> self . assertEqual ( f . cleaned_data , { '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> form = PriceFormWithoutQuantity ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> price = form . save ( commit = False ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> price . full_clean ( ) <EOL> class Meta : <EOL> model = Price <EOL> fields = ( '<STR_LIT>' , ) <EOL> form = PriceFormWithoutQuantity ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> self . assertEqual ( form . instance . price , Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( form . instance . quantity is None , True ) <EOL> self . assertEqual ( form . instance . pk is None , True ) <EOL> f = ArticleForm ( ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . fields [ '<STR_LIT:status>' ] . clean ( '<STR_LIT>' ) <EOL> f = ArticleStatusForm ( ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> f . fields [ '<STR_LIT:status>' ] . clean ( '<STR_LIT:z>' ) <EOL> def test_foreignkeys_which_use_to_field ( self ) : <EOL> apple = Inventory . objects . create ( barcode = <NUM_LIT> , name = '<STR_LIT>' ) <EOL> pear = Inventory . objects . create ( barcode = <NUM_LIT> , name = '<STR_LIT>' ) <EOL> core = Inventory . objects . create ( barcode = <NUM_LIT> , name = '<STR_LIT>' , parent = apple ) <EOL> field = forms . ModelChoiceField ( Inventory . objects . all ( ) , to_field_name = '<STR_LIT>' ) <EOL> self . assertEqual ( tuple ( field . choices ) , ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) ) ) <EOL> form = InventoryForm ( instance = core ) <EOL> self . assertHTMLEqual ( six . text_type ( form [ '<STR_LIT>' ] ) , '''<STR_LIT>''' ) <EOL> data = model_to_dict ( core ) <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> form = InventoryForm ( data = data , instance = core ) <EOL> core = form . save ( ) <EOL> self . assertEqual ( core . parent . name , '<STR_LIT>' ) <EOL> class CategoryForm ( forms . ModelForm ) : <EOL> description = forms . CharField ( ) <EOL> class Meta : <EOL> model = Category <EOL> fields = [ '<STR_LIT:description>' , '<STR_LIT:url>' ] <EOL> self . assertEqual ( list ( CategoryForm . base_fields ) , <EOL> [ '<STR_LIT:description>' , '<STR_LIT:url>' ] ) <EOL> self . assertHTMLEqual ( six . text_type ( CategoryForm ( ) ) , '''<STR_LIT>''' ) <EOL> field = forms . ModelMultipleChoiceField ( Inventory . objects . all ( ) , to_field_name = '<STR_LIT>' ) <EOL> self . assertEqual ( tuple ( field . choices ) , ( ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) ) ) <EOL> self . assertQuerysetEqual ( field . clean ( [ <NUM_LIT> ] ) , [ '<STR_LIT>' ] ) <EOL> form = SelectInventoryForm ( { '<STR_LIT>' : [ <NUM_LIT> , <NUM_LIT> ] } ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> self . assertEqual ( len ( form . cleaned_data ) , <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( form . cleaned_data [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_model_field_that_returns_none_to_exclude_itself_with_explicit_fields ( self ) : <EOL> self . assertEqual ( list ( CustomFieldForExclusionForm . base_fields ) , <EOL> [ '<STR_LIT:name>' ] ) <EOL> self . assertHTMLEqual ( six . text_type ( CustomFieldForExclusionForm ( ) ) , <EOL> '''<STR_LIT>''' ) <EOL> def test_iterable_model_m2m ( self ) : <EOL> colour = Colour . objects . create ( name = '<STR_LIT>' ) <EOL> form = ColourfulItemForm ( ) <EOL> self . maxDiff = <NUM_LIT> <EOL> self . assertHTMLEqual ( <EOL> form . as_p ( ) , <EOL> """<STR_LIT>""" <EOL> % { '<STR_LIT>' : colour . pk } ) </s>
