<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . exceptions import ValidationError <EOL> from django . test . testcases import TestCase <EOL> from django . utils . translation import ugettext <EOL> from mutant . forms import FieldDefinitionTypeField <EOL> from mutant . models . field import FieldDefinition <EOL> from . models import CustomFieldDefinition , FieldDefinitionModel <EOL> class FieldDefinitionTypeFieldTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . field_definition_ct = FieldDefinition . get_content_type ( ) <EOL> self . custom_field_ct = CustomFieldDefinition . get_content_type ( ) <EOL> self . content_type_ct = ContentType . objects . get_for_model ( ContentType ) <EOL> self . field_types = ContentType . objects . filter ( <EOL> ** FieldDefinition . subclasses_lookup ( '<STR_LIT>' ) <EOL> ) <EOL> ContentType . objects . clear_cache ( ) <EOL> def test_invalid_field_definitions ( self ) : <EOL> with self . assertRaisesMessage ( TypeError , '<STR_LIT>' ) : <EOL> FieldDefinitionTypeField ( <EOL> self . field_types , field_definitions = [ FieldDefinitionTypeField ] <EOL> ) <EOL> def test_valid_value ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> field = FieldDefinitionTypeField ( self . field_types ) <EOL> self . assertEqual ( <EOL> field . to_python ( self . field_definition_ct . pk ) , <EOL> self . field_definition_ct <EOL> ) <EOL> self . assertEqual ( <EOL> field . to_python ( self . custom_field_ct . pk ) , <EOL> self . custom_field_ct <EOL> ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> field . to_python ( self . content_type_ct . pk ) <EOL> def test_field_definitions_valid_value ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> field = FieldDefinitionTypeField ( <EOL> self . field_types , field_definitions = [ CustomFieldDefinition ] <EOL> ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> field . to_python ( self . field_definition_ct . pk ) <EOL> self . assertEqual ( <EOL> field . to_python ( self . custom_field_ct . pk ) , <EOL> self . custom_field_ct <EOL> ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> field . to_python ( self . content_type_ct . pk ) <EOL> def test_form_validation ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> class CustomModelForm ( forms . Form ) : <EOL> field_type = FieldDefinitionTypeField ( self . field_types ) <EOL> custom_field_ct = CustomFieldDefinition . get_content_type ( ) <EOL> form = CustomModelForm ( { '<STR_LIT>' : self . custom_field_ct . pk } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> self . assertEqual ( form . cleaned_data [ '<STR_LIT>' ] , custom_field_ct ) <EOL> def test_model_form_validation ( self ) : <EOL> form_cls = forms . models . modelform_factory ( <EOL> FieldDefinitionModel , fields = [ '<STR_LIT>' ] <EOL> ) <EOL> form = form_cls ( { '<STR_LIT>' : self . field_definition_ct . pk } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> self . assertEqual ( <EOL> form . cleaned_data [ '<STR_LIT>' ] , self . field_definition_ct <EOL> ) <EOL> form = form_cls ( { '<STR_LIT>' : self . custom_field_ct . pk } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> self . assertEqual ( form . cleaned_data [ '<STR_LIT>' ] , self . custom_field_ct ) <EOL> form = form_cls ( { '<STR_LIT>' : self . content_type_ct . pk } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> def test_choices ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> field = FieldDefinitionTypeField ( <EOL> ContentType . objects . filter ( pk__in = [ <EOL> self . field_definition_ct . pk , self . custom_field_ct . pk <EOL> ] ) . order_by ( '<STR_LIT>' ) , group_by_category = False , empty_label = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( <EOL> list ( field . choices ) , [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] + sorted ( [ <EOL> ( self . field_definition_ct . pk , '<STR_LIT:None>' ) , <EOL> ( self . custom_field_ct . pk , ugettext ( '<STR_LIT>' ) ) <EOL> ] ) <EOL> ) <EOL> def test_group_by_category_choices ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> field = FieldDefinitionTypeField ( <EOL> ContentType . objects . filter ( pk__in = [ <EOL> self . field_definition_ct . pk , self . custom_field_ct . pk <EOL> ] ) . order_by ( '<STR_LIT>' ) , group_by_category = True , empty_label = None <EOL> ) <EOL> self . assertEqual ( <EOL> list ( field . choices ) , [ <EOL> ( self . field_definition_ct . pk , '<STR_LIT:None>' ) , <EOL> ( ugettext ( '<STR_LIT>' ) , ( <EOL> ( self . custom_field_ct . pk , ugettext ( '<STR_LIT>' ) ) , <EOL> ) ) <EOL> ] <EOL> ) </s>
