<s> from dockit . schema import fields <EOL> from dockit . schema . common import DotPathList , DotPathSet , DotPathDict <EOL> from django . utils import unittest <EOL> from django . contrib . contenttypes . models import ContentType <EOL> import datetime <EOL> from decimal import Decimal <EOL> from common import SimpleSchema , SimpleDocument <EOL> class BaseFieldTestCase ( unittest . TestCase ) : <EOL> EXAMPLE_VALUES = [ ] <EOL> EXAMPLE_PRIMITIVE_VALUES = [ ] <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = [ ] <EOL> NULL_VALUE = None <EOL> field_class = fields . BaseField <EOL> def get_example_values ( self ) : <EOL> return self . EXAMPLE_VALUES <EOL> def get_example_primitive_values ( self ) : <EOL> return self . EXAMPLE_PRIMITIVE_VALUES <EOL> def get_example_portable_primitive_values ( self ) : <EOL> return self . EXAMPLE_PORTABLE_PRIMITIVE_VALUES <EOL> def get_field_kwargs ( self ) : <EOL> return { } <EOL> def get_field ( self , ** kwargs ) : <EOL> params = self . get_field_kwargs ( ) <EOL> params . update ( kwargs ) <EOL> return self . field_class ( ** params ) <EOL> def compare_py_val_to_primitive ( self , py_val , primitive ) : <EOL> self . assertEqual ( py_val , primitive ) <EOL> def compare_primitives ( self , prim1 , prim2 ) : <EOL> self . assertEqual ( prim1 , prim2 ) <EOL> def compare_portable_primitives ( self , prim1 , prim2 ) : <EOL> return self . compare_primitives ( prim1 , prim2 ) <EOL> def test_handles_null_value ( self ) : <EOL> field = self . get_field ( null = True ) <EOL> val = field . to_python ( None ) <EOL> self . assertEqual ( val , self . NULL_VALUE ) <EOL> def test_to_python_to_primitive ( self ) : <EOL> field = self . get_field ( ) <EOL> for val in self . get_example_values ( ) : <EOL> primitive = field . to_primitive ( val ) <EOL> py_val = field . to_python ( primitive ) <EOL> self . compare_py_val_to_primitive ( py_val , val ) <EOL> for val in self . get_example_primitive_values ( ) : <EOL> py_val = field . to_python ( val ) <EOL> primitive = field . to_primitive ( py_val ) <EOL> self . compare_primitives ( val , primitive ) <EOL> for val in self . get_example_portable_primitive_values ( ) : <EOL> py_val = field . normalize_portable_primitives ( val ) <EOL> primitive = field . to_portable_primitive ( py_val ) <EOL> self . compare_portable_primitives ( val , primitive ) <EOL> def test_form_field ( self ) : <EOL> field = self . get_field ( ) <EOL> field . formfield ( ) <EOL> def test_get_choices ( self ) : <EOL> field = self . get_field ( ) <EOL> field . get_choices ( ) <EOL> def test_json_serializable ( self ) : <EOL> field = self . get_field ( ) <EOL> for val in self . get_example_values ( ) : <EOL> primitive = field . to_primitive ( val ) <EOL> def test_traverse_dotpath ( self ) : <EOL> pass <EOL> class CharFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ "<STR_LIT:abc>" , u"<STR_LIT>" ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . CharField <EOL> class IntegerFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ <NUM_LIT:1> , <NUM_LIT> ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . IntegerField <EOL> class BigIntegerFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:2> ** <NUM_LIT:32> + <NUM_LIT:1> ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . BigIntegerField <EOL> class BooleanFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ True , False ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . BooleanField <EOL> class DateFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . DateField <EOL> class DateTimeFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . DateTimeField <EOL> class DecimalFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ <NUM_LIT:1> , <NUM_LIT:2> , Decimal ( '<STR_LIT>' ) ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . DecimalField <EOL> def compare_primitives ( self , prim1 , prim2 ) : <EOL> self . assertEqual ( str ( prim1 ) , str ( prim2 ) ) <EOL> class EmailFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ '<STR_LIT>' ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . EmailField <EOL> class IPAddressFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ '<STR_LIT:127.0.0.1>' ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . IPAddressField <EOL> class SlugFieldTestCase ( BaseFieldTestCase ) : <EOL> EXAMPLE_VALUES = [ '<STR_LIT>' ] <EOL> EXAMPLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> EXAMPLE_PORTABLE_PRIMITIVE_VALUES = EXAMPLE_VALUES <EOL> field_class = fields . SlugField <EOL> class TimeFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . TimeField <EOL> class SchemaTypeFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . SchemaTypeField <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : dict ( ) } <EOL> class SchemaFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . SchemaField <EOL> EXAMPLE_VALUES = [ SimpleSchema ( charfield = '<STR_LIT>' ) ] <EOL> EXAMPLE_PRIMITIVE_VALUES = [ { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> NULL_VALUE = None <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : SimpleSchema } <EOL> class TypedSchemaFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . TypedSchemaField <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : dict ( ) } <EOL> class ListFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . ListField <EOL> EXAMPLE_VALUES = [ DotPathList ( [ ] ) , DotPathList ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) ] <EOL> EXAMPLE_PRIMITIVE_VALUES = [ [ ] , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ] <EOL> NULL_VALUE = [ ] <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : fields . CharField ( ) } <EOL> class SetFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . SetField <EOL> EXAMPLE_VALUES = [ DotPathSet ( [ ] ) , DotPathSet ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) ] <EOL> EXAMPLE_PRIMITIVE_VALUES = [ [ ] , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ] <EOL> NULL_VALUE = set ( ) <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : fields . CharField ( ) } <EOL> def compare_py_val_to_primitive ( self , py_val , primitive ) : <EOL> self . assertTrue ( isinstance ( py_val , DotPathSet ) ) <EOL> self . assertEqual ( py_val , set ( primitive ) ) <EOL> def compare_primitives ( self , prim1 , prim2 ) : <EOL> self . assertEqual ( set ( prim1 ) , set ( prim2 ) ) <EOL> class DictFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . DictField <EOL> EXAMPLE_VALUES = [ DotPathDict ( { } ) , DotPathDict ( { '<STR_LIT:a>' : <NUM_LIT:1> } ) ] <EOL> EXAMPLE_PRIMITIVE_VALUES = [ { } , { '<STR_LIT:a>' : <NUM_LIT:1> } ] <EOL> NULL_VALUE = dict ( ) <EOL> def get_field_kwargs ( self ) : <EOL> return { } <EOL> class ReferenceFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . ReferenceField <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : SimpleDocument } <EOL> class DocumentSetFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . DocumentSetField <EOL> NULL_VALUE = set ( ) <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : SimpleDocument } <EOL> class ModelReferenceFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . ModelReferenceField <EOL> def get_example_values ( self ) : <EOL> return [ ContentType . objects . all ( ) [ <NUM_LIT:0> ] ] <EOL> def get_example_primitive_values ( self ) : <EOL> return [ ContentType . objects . all ( ) [ <NUM_LIT:0> ] . pk ] <EOL> def compare_py_val_to_primitive ( self , py_val , primitive ) : <EOL> self . assertTrue ( isinstance ( py_val , ContentType ) , "<STR_LIT>" % ( type ( py_val ) , ContentType ) ) <EOL> super ( ModelReferenceFieldTestCase , self ) . compare_py_val_to_primitive ( py_val , primitive ) <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : ContentType } <EOL> class ModelSetFieldTestCase ( BaseFieldTestCase ) : <EOL> field_class = fields . ModelSetField <EOL> NULL_VALUE = set ( ) <EOL> def get_example_values ( self ) : <EOL> return [ set ( [ ContentType . objects . all ( ) [ <NUM_LIT:0> ] ] ) ] <EOL> def get_example_primitive_values ( self ) : <EOL> return [ [ ContentType . objects . all ( ) [ <NUM_LIT:0> ] . pk ] ] <EOL> def compare_py_val_to_primitive ( self , py_val , primitive ) : <EOL> if len ( py_val ) : <EOL> val = list ( py_val ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( val , ContentType ) , "<STR_LIT>" % ( type ( val ) , ContentType ) ) <EOL> super ( ModelSetFieldTestCase , self ) . compare_py_val_to_primitive ( py_val , primitive ) <EOL> def get_field_kwargs ( self ) : <EOL> return { '<STR_LIT>' : ContentType } </s>
