<s> from __future__ import with_statement <EOL> from decimal import Decimal , InvalidOperation <EOL> import time <EOL> from django . core import serializers <EOL> from django . db import models <EOL> from django . db . models import Q <EOL> from django . db . models . signals import post_save <EOL> from django . db . utils import DatabaseError <EOL> from django . dispatch . dispatcher import receiver <EOL> from django . test import TestCase <EOL> from django . utils . unittest import expectedFailure , skip <EOL> from . fields import ListField , SetField , DictField , EmbeddedModelField <EOL> def count_calls ( func ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> wrapper . calls += <NUM_LIT:1> <EOL> return func ( * args , ** kwargs ) <EOL> wrapper . calls = <NUM_LIT:0> <EOL> return wrapper <EOL> class Target ( models . Model ) : <EOL> index = models . IntegerField ( ) <EOL> class Source ( models . Model ) : <EOL> target = models . ForeignKey ( Target ) <EOL> index = models . IntegerField ( ) <EOL> class DecimalModel ( models . Model ) : <EOL> decimal = models . DecimalField ( max_digits = <NUM_LIT:9> , decimal_places = <NUM_LIT:2> ) <EOL> class DecimalKey ( models . Model ) : <EOL> decimal = models . DecimalField ( max_digits = <NUM_LIT:9> , decimal_places = <NUM_LIT:2> , primary_key = True ) <EOL> class DecimalParent ( models . Model ) : <EOL> child = models . ForeignKey ( DecimalKey ) <EOL> class DecimalsList ( models . Model ) : <EOL> decimals = ListField ( models . ForeignKey ( DecimalKey ) ) <EOL> class ListModel ( models . Model ) : <EOL> integer = models . IntegerField ( primary_key = True ) <EOL> floating_point = models . FloatField ( ) <EOL> names = ListField ( models . CharField ) <EOL> names_with_default = ListField ( models . CharField ( max_length = <NUM_LIT> ) , <EOL> default = [ ] ) <EOL> names_nullable = ListField ( models . CharField ( max_length = <NUM_LIT> ) , null = True ) <EOL> class OrderedListModel ( models . Model ) : <EOL> ordered_ints = ListField ( models . IntegerField ( max_length = <NUM_LIT> ) , default = [ ] , <EOL> ordering = count_calls ( lambda x : x ) , null = True ) <EOL> ordered_nullable = ListField ( ordering = lambda x : x , null = True ) <EOL> class SetModel ( models . Model ) : <EOL> setfield = SetField ( models . IntegerField ( ) ) <EOL> class DictModel ( models . Model ) : <EOL> dictfield = DictField ( models . IntegerField ) <EOL> dictfield_nullable = DictField ( null = True ) <EOL> auto_now = DictField ( models . DateTimeField ( auto_now = True ) ) <EOL> class EmbeddedModelFieldModel ( models . Model ) : <EOL> simple = EmbeddedModelField ( '<STR_LIT>' , null = True ) <EOL> simple_untyped = EmbeddedModelField ( null = True ) <EOL> decimal_parent = EmbeddedModelField ( DecimalParent , null = True ) <EOL> typed_list = ListField ( EmbeddedModelField ( '<STR_LIT>' ) ) <EOL> typed_list2 = ListField ( EmbeddedModelField ( '<STR_LIT>' ) ) <EOL> untyped_list = ListField ( EmbeddedModelField ( ) ) <EOL> untyped_dict = DictField ( EmbeddedModelField ( ) ) <EOL> ordered_list = ListField ( EmbeddedModelField ( ) , <EOL> ordering = lambda obj : obj . index ) <EOL> class EmbeddedModel ( models . Model ) : <EOL> some_relation = models . ForeignKey ( DictModel , null = True ) <EOL> someint = models . IntegerField ( db_column = '<STR_LIT>' ) <EOL> auto_now = models . DateTimeField ( auto_now = True ) <EOL> auto_now_add = models . DateTimeField ( auto_now_add = True ) <EOL> class IterableFieldsTest ( TestCase ) : <EOL> floats = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> names = [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] <EOL> unordered_ints = [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:1> ] <EOL> def setUp ( self ) : <EOL> for i , float in zip ( range ( <NUM_LIT:1> , <NUM_LIT:5> ) , IterableFieldsTest . floats ) : <EOL> ListModel ( integer = i , floating_point = float , <EOL> names = IterableFieldsTest . names [ : i ] ) . save ( ) <EOL> def test_startswith ( self ) : <EOL> self . assertEquals ( <EOL> dict ( [ ( entity . pk , entity . names ) for entity in <EOL> ListModel . objects . filter ( names__startswith = '<STR_LIT>' ) ] ) , <EOL> dict ( [ ( <NUM_LIT:3> , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:4> , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , ] ) ) <EOL> def test_options ( self ) : <EOL> self . assertEqual ( [ entity . names_with_default for entity in <EOL> ListModel . objects . filter ( names__startswith = '<STR_LIT>' ) ] , <EOL> [ [ ] , [ ] ] ) <EOL> self . assertEqual ( [ entity . names_nullable for entity in <EOL> ListModel . objects . filter ( names__startswith = '<STR_LIT>' ) ] , <EOL> [ None , None ] ) <EOL> def test_default_value ( self ) : <EOL> ListModel ( ) . names_with_default . append ( <NUM_LIT:2> ) <EOL> self . assertEqual ( ListModel ( ) . names_with_default , [ ] ) <EOL> def test_ordering ( self ) : <EOL> f = OrderedListModel . _meta . fields [ <NUM_LIT:1> ] <EOL> f . ordering . calls = <NUM_LIT:0> <EOL> obj = OrderedListModel ( ) <EOL> obj . ordered_ints = self . unordered_ints <EOL> self . assertEqual ( f . ordering . calls , <NUM_LIT:0> ) <EOL> obj . save ( ) <EOL> self . assertEqual ( OrderedListModel . objects . get ( ) . ordered_ints , <EOL> sorted ( self . unordered_ints ) ) <EOL> self . assertLessEqual ( f . ordering . calls , len ( self . unordered_ints ) ) <EOL> def test_gt ( self ) : <EOL> self . assertEquals ( <EOL> dict ( [ ( entity . pk , entity . names ) for entity in <EOL> ListModel . objects . filter ( names__gt = '<STR_LIT>' ) ] ) , <EOL> dict ( [ ( <NUM_LIT:2> , [ u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:3> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:4> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , ] ) ) <EOL> def test_lt ( self ) : <EOL> self . assertEquals ( <EOL> dict ( [ ( entity . pk , entity . names ) for entity in <EOL> ListModel . objects . filter ( names__lt = '<STR_LIT>' ) ] ) , <EOL> dict ( [ ( <NUM_LIT:1> , [ u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:2> , [ u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:3> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:4> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , ] ) ) <EOL> def test_gte ( self ) : <EOL> self . assertEquals ( <EOL> dict ( [ ( entity . pk , entity . names ) for entity in <EOL> ListModel . objects . filter ( names__gte = '<STR_LIT>' ) ] ) , <EOL> dict ( [ ( <NUM_LIT:3> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:4> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , ] ) ) <EOL> def test_lte ( self ) : <EOL> self . assertEquals ( <EOL> dict ( [ ( entity . pk , entity . names ) for entity in <EOL> ListModel . objects . filter ( names__lte = '<STR_LIT>' ) ] ) , <EOL> dict ( [ ( <NUM_LIT:1> , [ u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:2> , [ u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:3> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:4> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , ] ) ) <EOL> def test_equals ( self ) : <EOL> self . assertEquals ( [ entity . names for entity in <EOL> ListModel . objects . filter ( names = '<STR_LIT>' ) ] , <EOL> [ [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ] ) <EOL> query = ListModel . objects . filter ( names = '<STR_LIT>' ) <EOL> self . assertEquals ( query . get ( pk = query [ <NUM_LIT:0> ] . pk ) . names , <EOL> [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) <EOL> def test_is_null ( self ) : <EOL> self . assertEquals ( ListModel . objects . filter ( <EOL> names__isnull = True ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_exclude ( self ) : <EOL> self . assertEquals ( <EOL> dict ( [ ( entity . pk , entity . names ) for entity in <EOL> ListModel . objects . all ( ) . exclude ( names__lt = '<STR_LIT>' ) ] ) , <EOL> dict ( [ ( <NUM_LIT:3> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , <EOL> ( <NUM_LIT:4> , [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] ) , ] ) ) <EOL> def test_chained_filter ( self ) : <EOL> self . assertEquals ( <EOL> [ entity . names for entity in ListModel . objects <EOL> . filter ( names = '<STR_LIT>' ) . filter ( names = '<STR_LIT>' ) ] , <EOL> [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , ] ) <EOL> self . assertEquals ( <EOL> [ entity . names for entity in ListModel . objects <EOL> . filter ( names__startswith = '<STR_LIT>' ) . filter ( names = '<STR_LIT>' ) ] , <EOL> [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] ) <EOL> self . assertEquals ( <EOL> [ entity . names for entity in ListModel . objects <EOL> . filter ( floating_point = <NUM_LIT> ) . filter ( names__startswith = '<STR_LIT>' ) ] , <EOL> [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , ] ) <EOL> def test_setfield ( self ) : <EOL> setdata = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] <EOL> SetModel ( setfield = map ( str , setdata ) ) . save ( ) <EOL> item = SetModel . objects . filter ( setfield = <NUM_LIT:3> ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( item . setfield , set ( setdata ) ) <EOL> SetModel ( ) . save ( ) <EOL> def test_dictfield ( self ) : <EOL> DictModel ( dictfield = dict ( a = <NUM_LIT:1> , b = '<STR_LIT>' , foo = <NUM_LIT> ) , <EOL> auto_now = { '<STR_LIT:a>' : None } ) . save ( ) <EOL> item = DictModel . objects . get ( ) <EOL> self . assertEqual ( item . dictfield , { u'<STR_LIT:a>' : <NUM_LIT:1> , u'<STR_LIT:b>' : <NUM_LIT> , u'<STR_LIT:foo>' : <NUM_LIT:3> } ) <EOL> dt = item . auto_now [ '<STR_LIT:a>' ] <EOL> self . assertNotEqual ( dt , None ) <EOL> item . save ( ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> self . assertGreater ( DictModel . objects . get ( ) . auto_now [ '<STR_LIT:a>' ] , dt ) <EOL> item . delete ( ) <EOL> DictModel ( ) . save ( ) <EOL> DictModel . add_to_class ( '<STR_LIT>' , DictField ( ) ) <EOL> DictModel . objects . get ( ) <EOL> @ skip ( "<STR_LIT>" ) <EOL> def test_Q_objects ( self ) : <EOL> self . assertEquals ( <EOL> [ entity . names for entity in ListModel . objects <EOL> . exclude ( Q ( names__lt = '<STR_LIT>' ) | Q ( names__gte = '<STR_LIT>' ) ) ] , <EOL> [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] ) <EOL> def test_list_with_foreignkeys ( self ) : <EOL> class ReferenceList ( models . Model ) : <EOL> keys = ListField ( models . ForeignKey ( '<STR_LIT>' ) ) <EOL> class Model ( models . Model ) : <EOL> pass <EOL> model1 = Model . objects . create ( ) <EOL> model2 = Model . objects . create ( ) <EOL> ReferenceList . objects . create ( keys = [ model1 . pk , model2 . pk ] ) <EOL> self . assertEqual ( ReferenceList . objects . get ( ) . keys [ <NUM_LIT:0> ] , model1 . pk ) <EOL> self . assertEqual ( ReferenceList . objects . filter ( keys = model1 . pk ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_list_with_foreign_conversion ( self ) : <EOL> decimal = DecimalKey . objects . create ( decimal = Decimal ( '<STR_LIT>' ) ) <EOL> DecimalsList . objects . create ( decimals = [ decimal . pk ] ) <EOL> @ expectedFailure <EOL> def test_nested_list ( self ) : <EOL> """<STR_LIT>""" <EOL> class UntypedListModel ( models . Model ) : <EOL> untyped_list = ListField ( ) <EOL> UntypedListModel . objects . create ( untyped_list = [ <NUM_LIT:1> , [ <NUM_LIT:2> , <NUM_LIT:3> ] ] ) <EOL> class Child ( models . Model ) : <EOL> pass <EOL> class Parent ( models . Model ) : <EOL> id = models . IntegerField ( primary_key = True ) <EOL> integer_list = ListField ( models . IntegerField ) <EOL> integer_dict = DictField ( models . IntegerField ) <EOL> embedded_list = ListField ( EmbeddedModelField ( Child ) ) <EOL> embedded_dict = DictField ( EmbeddedModelField ( Child ) ) <EOL> class EmbeddedModelFieldTest ( TestCase ) : <EOL> def assertEqualDatetime ( self , d1 , d2 ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( d1 . replace ( microsecond = <NUM_LIT:0> ) , <EOL> d2 . replace ( microsecond = <NUM_LIT:0> ) ) <EOL> def assertNotEqualDatetime ( self , d1 , d2 ) : <EOL> self . assertNotEqual ( d1 . replace ( microsecond = <NUM_LIT:0> ) , <EOL> d2 . replace ( microsecond = <NUM_LIT:0> ) ) <EOL> def _simple_instance ( self ) : <EOL> EmbeddedModelFieldModel . objects . create ( <EOL> simple = EmbeddedModel ( someint = '<STR_LIT:5>' ) ) <EOL> return EmbeddedModelFieldModel . objects . get ( ) <EOL> def test_simple ( self ) : <EOL> instance = self . _simple_instance ( ) <EOL> self . assertIsInstance ( instance . simple , EmbeddedModel ) <EOL> self . assertEqual ( instance . simple . someint , <NUM_LIT:5> ) <EOL> self . assertEqual ( instance . simple . id , None ) <EOL> instance . simple . id = instance . id <EOL> instance . save ( ) <EOL> instance = EmbeddedModelFieldModel . objects . get ( ) <EOL> self . assertEqual ( instance . simple . id , instance . id ) <EOL> def _test_pre_save ( self , instance , get_field ) : <EOL> from time import sleep <EOL> instance . save ( ) <EOL> auto_now = get_field ( instance ) . auto_now <EOL> auto_now_add = get_field ( instance ) . auto_now_add <EOL> self . assertNotEqual ( auto_now , None ) <EOL> self . assertNotEqual ( auto_now_add , None ) <EOL> sleep ( <NUM_LIT:1> ) <EOL> instance . save ( ) <EOL> self . assertNotEqualDatetime ( get_field ( instance ) . auto_now , <EOL> get_field ( instance ) . auto_now_add ) <EOL> instance = EmbeddedModelFieldModel . objects . get ( ) <EOL> instance . save ( ) <EOL> self . assertEqualDatetime ( get_field ( instance ) . auto_now_add , <EOL> auto_now_add ) <EOL> self . assertGreater ( get_field ( instance ) . auto_now , auto_now ) <EOL> def test_pre_save ( self ) : <EOL> obj = EmbeddedModelFieldModel ( simple = EmbeddedModel ( ) ) <EOL> self . _test_pre_save ( obj , lambda instance : instance . simple ) <EOL> def test_pre_save_untyped ( self ) : <EOL> obj = EmbeddedModelFieldModel ( simple_untyped = EmbeddedModel ( ) ) <EOL> self . _test_pre_save ( obj , lambda instance : instance . simple_untyped ) <EOL> def test_pre_save_in_list ( self ) : <EOL> obj = EmbeddedModelFieldModel ( untyped_list = [ EmbeddedModel ( ) ] ) <EOL> self . _test_pre_save ( obj , lambda instance : instance . untyped_list [ <NUM_LIT:0> ] ) <EOL> def test_pre_save_in_dict ( self ) : <EOL> obj = EmbeddedModelFieldModel ( untyped_dict = { '<STR_LIT:a>' : EmbeddedModel ( ) } ) <EOL> self . _test_pre_save ( obj , lambda instance : instance . untyped_dict [ '<STR_LIT:a>' ] ) <EOL> def test_pre_save_list ( self ) : <EOL> EmbeddedModelFieldModel . objects . create ( typed_list2 = [ EmbeddedModel ( ) ] ) <EOL> instance = EmbeddedModelFieldModel . objects . get ( ) <EOL> auto_now = instance . typed_list2 [ <NUM_LIT:0> ] . auto_now <EOL> auto_now_add = instance . typed_list2 [ <NUM_LIT:0> ] . auto_now_add <EOL> self . assertNotEqual ( auto_now , None ) <EOL> self . assertNotEqual ( auto_now_add , None ) <EOL> instance . typed_list2 . append ( EmbeddedModel ( ) ) <EOL> instance . save ( ) <EOL> instance = EmbeddedModelFieldModel . objects . get ( ) <EOL> self . assertEqualDatetime ( instance . typed_list2 [ <NUM_LIT:0> ] . auto_now_add , <EOL> auto_now_add ) <EOL> self . assertGreater ( instance . typed_list2 [ <NUM_LIT:0> ] . auto_now , auto_now ) <EOL> self . assertNotEqual ( instance . typed_list2 [ <NUM_LIT:1> ] . auto_now , None ) <EOL> self . assertNotEqual ( instance . typed_list2 [ <NUM_LIT:1> ] . auto_now_add , None ) <EOL> def test_error_messages ( self ) : <EOL> for kwargs , expected in ( <EOL> ( { '<STR_LIT>' : <NUM_LIT> } , EmbeddedModel ) , <EOL> ( { '<STR_LIT>' : <NUM_LIT> } , models . Model ) , <EOL> ( { '<STR_LIT>' : [ EmbeddedModel ( ) ] } , SetModel ) ) : <EOL> self . assertRaisesRegexp ( <EOL> TypeError , "<STR_LIT>" % expected , <EOL> EmbeddedModelFieldModel ( ** kwargs ) . save ) <EOL> def test_typed_listfield ( self ) : <EOL> EmbeddedModelFieldModel . objects . create ( <EOL> typed_list = [ SetModel ( setfield = range ( <NUM_LIT:3> ) ) , <EOL> SetModel ( setfield = range ( <NUM_LIT:9> ) ) ] , <EOL> ordered_list = [ Target ( index = i ) for i in xrange ( <NUM_LIT:5> , <NUM_LIT:0> , - <NUM_LIT:1> ) ] ) <EOL> obj = EmbeddedModelFieldModel . objects . get ( ) <EOL> self . assertIn ( <NUM_LIT:5> , obj . typed_list [ <NUM_LIT:1> ] . setfield ) <EOL> self . assertEqual ( [ target . index for target in obj . ordered_list ] , <EOL> range ( <NUM_LIT:1> , <NUM_LIT:6> ) ) <EOL> def test_untyped_listfield ( self ) : <EOL> EmbeddedModelFieldModel . objects . create ( untyped_list = [ <EOL> EmbeddedModel ( someint = <NUM_LIT:7> ) , <EOL> OrderedListModel ( ordered_ints = range ( <NUM_LIT:5> , <NUM_LIT:0> , - <NUM_LIT:1> ) ) , <EOL> SetModel ( setfield = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ] ) <EOL> instances = EmbeddedModelFieldModel . objects . get ( ) . untyped_list <EOL> for instance , cls in zip ( instances , <EOL> [ EmbeddedModel , OrderedListModel , SetModel ] ) : <EOL> self . assertIsInstance ( instance , cls ) <EOL> self . assertNotEqual ( instances [ <NUM_LIT:0> ] . auto_now , None ) <EOL> self . assertEqual ( instances [ <NUM_LIT:1> ] . ordered_ints , range ( <NUM_LIT:1> , <NUM_LIT:6> ) ) <EOL> def test_untyped_dict ( self ) : <EOL> EmbeddedModelFieldModel . objects . create ( untyped_dict = { <EOL> '<STR_LIT:a>' : SetModel ( setfield = range ( <NUM_LIT:3> ) ) , <EOL> '<STR_LIT:b>' : DictModel ( dictfield = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } ) , <EOL> '<STR_LIT:c>' : DictModel ( dictfield = { } , auto_now = { '<STR_LIT:y>' : <NUM_LIT:1> } ) } ) <EOL> data = EmbeddedModelFieldModel . objects . get ( ) . untyped_dict <EOL> self . assertIsInstance ( data [ '<STR_LIT:a>' ] , SetModel ) <EOL> self . assertNotEqual ( data [ '<STR_LIT:c>' ] . auto_now [ '<STR_LIT:y>' ] , None ) <EOL> def test_foreignkey_in_embedded_object ( self ) : <EOL> simple = EmbeddedModel ( some_relation = DictModel . objects . create ( ) ) <EOL> obj = EmbeddedModelFieldModel . objects . create ( simple = simple ) <EOL> simple = EmbeddedModelFieldModel . objects . get ( ) . simple <EOL> self . assertNotIn ( '<STR_LIT>' , simple . __dict__ ) <EOL> self . assertIsInstance ( simple . __dict__ [ '<STR_LIT>' ] , <EOL> type ( obj . id ) ) <EOL> self . assertIsInstance ( simple . some_relation , DictModel ) <EOL> def test_embedded_field_with_foreign_conversion ( self ) : <EOL> decimal = DecimalKey . objects . create ( decimal = Decimal ( '<STR_LIT>' ) ) <EOL> decimal_parent = DecimalParent . objects . create ( child = decimal ) <EOL> EmbeddedModelFieldModel . objects . create ( decimal_parent = decimal_parent ) <EOL> def test_update ( self ) : <EOL> """<STR_LIT>""" <EOL> child1 = Child . objects . create ( ) <EOL> child2 = Child . objects . create ( ) <EOL> parent = Parent . objects . create ( pk = <NUM_LIT:1> , <EOL> integer_list = [ <NUM_LIT:1> ] , integer_dict = { '<STR_LIT:a>' : <NUM_LIT:2> } , <EOL> embedded_list = [ child1 ] , embedded_dict = { '<STR_LIT:a>' : child2 } ) <EOL> Parent . objects . filter ( pk = <NUM_LIT:1> ) . update ( <EOL> integer_list = [ '<STR_LIT:3>' ] , integer_dict = { '<STR_LIT:b>' : '<STR_LIT:3>' } , <EOL> embedded_list = [ child2 ] , embedded_dict = { '<STR_LIT:b>' : child1 } ) <EOL> parent = Parent . objects . get ( ) <EOL> self . assertEqual ( parent . integer_list , [ <NUM_LIT:3> ] ) <EOL> self . assertEqual ( parent . integer_dict , { '<STR_LIT:b>' : <NUM_LIT:3> } ) <EOL> self . assertEqual ( parent . embedded_list , [ child2 ] ) <EOL> self . assertEqual ( parent . embedded_dict , { '<STR_LIT:b>' : child1 } ) <EOL> class BaseModel ( models . Model ) : <EOL> pass <EOL> class ExtendedModel ( BaseModel ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:20> ) <EOL> class BaseModelProxy ( BaseModel ) : <EOL> class Meta : <EOL> proxy = True <EOL> class ExtendedModelProxy ( ExtendedModel ) : <EOL> class Meta : <EOL> proxy = True <EOL> class ProxyTest ( TestCase ) : <EOL> def test_proxy ( self ) : <EOL> list ( BaseModelProxy . objects . all ( ) ) <EOL> def test_proxy_with_inheritance ( self ) : <EOL> self . assertRaises ( DatabaseError , <EOL> lambda : list ( ExtendedModelProxy . objects . all ( ) ) ) <EOL> class SignalTest ( TestCase ) : <EOL> def test_post_save ( self ) : <EOL> created = [ ] <EOL> @ receiver ( post_save , sender = SetModel ) <EOL> def handle ( ** kwargs ) : <EOL> created . append ( kwargs [ '<STR_LIT>' ] ) <EOL> SetModel ( ) . save ( ) <EOL> self . assertEqual ( created , [ True ] ) <EOL> SetModel . objects . get ( ) . save ( ) <EOL> self . assertEqual ( created , [ True , False ] ) <EOL> qs = SetModel . objects . all ( ) <EOL> list ( qs ) [ <NUM_LIT:0> ] . save ( ) <EOL> self . assertEqual ( created , [ True , False , False ] ) <EOL> list ( qs ) [ <NUM_LIT:0> ] . save ( ) <EOL> self . assertEqual ( created , [ True , False , False , False ] ) <EOL> list ( qs . select_related ( ) ) [ <NUM_LIT:0> ] . save ( ) <EOL> self . assertEqual ( created , [ True , False , False , False , False ] ) <EOL> class SelectRelatedTest ( TestCase ) : <EOL> def test_select_related ( self ) : <EOL> target = Target ( index = <NUM_LIT:5> ) <EOL> target . save ( ) <EOL> Source ( target = target , index = <NUM_LIT:8> ) . save ( ) <EOL> source = Source . objects . all ( ) . select_related ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( source . target . pk , target . pk ) <EOL> self . assertEqual ( source . target . index , target . index ) <EOL> source = Source . objects . all ( ) . select_related ( '<STR_LIT:target>' ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( source . target . pk , target . pk ) <EOL> self . assertEqual ( source . target . index , target . index ) <EOL> class DBColumn ( models . Model ) : <EOL> a = models . IntegerField ( db_column = '<STR_LIT:b>' ) <EOL> class OrderByTest ( TestCase ) : <EOL> def test_foreign_keys ( self ) : <EOL> target1 = Target . objects . create ( index = <NUM_LIT:1> ) <EOL> target2 = Target . objects . create ( index = <NUM_LIT:2> ) <EOL> source1 = Source . objects . create ( target = target1 , index = <NUM_LIT:3> ) <EOL> source2 = Source . objects . create ( target = target2 , index = <NUM_LIT:4> ) <EOL> self . assertEqual ( list ( Source . objects . all ( ) . order_by ( '<STR_LIT:target>' ) ) , <EOL> [ source1 , source2 ] ) <EOL> self . assertEqual ( list ( Source . objects . all ( ) . order_by ( '<STR_LIT>' ) ) , <EOL> [ source2 , source1 ] ) <EOL> def test_db_column ( self ) : <EOL> model1 = DBColumn . objects . create ( a = <NUM_LIT:1> ) <EOL> model2 = DBColumn . objects . create ( a = <NUM_LIT:2> ) <EOL> self . assertEqual ( list ( DBColumn . objects . all ( ) . order_by ( '<STR_LIT:a>' ) ) , <EOL> [ model1 , model2 ] ) <EOL> self . assertEqual ( list ( DBColumn . objects . all ( ) . order_by ( '<STR_LIT>' ) ) , <EOL> [ model2 , model1 ] ) <EOL> def test_reverse ( self ) : <EOL> model1 = DBColumn . objects . create ( a = <NUM_LIT:1> ) <EOL> model2 = DBColumn . objects . create ( a = <NUM_LIT:2> ) <EOL> self . assertEqual ( list ( DBColumn . objects . all ( ) . order_by ( '<STR_LIT:a>' ) . reverse ( ) ) , <EOL> [ model2 , model1 ] ) <EOL> self . assertEqual ( list ( DBColumn . objects . all ( ) . order_by ( '<STR_LIT>' ) . reverse ( ) ) , <EOL> [ model1 , model2 ] ) <EOL> def test_chain ( self ) : <EOL> model1 = Target . objects . create ( index = <NUM_LIT:1> ) <EOL> model2 = Target . objects . create ( index = <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> list ( Target . objects . all ( ) . order_by ( '<STR_LIT:index>' ) . order_by ( '<STR_LIT>' ) ) , <EOL> [ model2 , model1 ] ) <EOL> class SerializableSetModel ( models . Model ) : <EOL> setfield = SetField ( models . IntegerField ( ) ) <EOL> setcharfield = SetField ( models . CharField ( ) , null = True ) <EOL> class SerializationTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> names = [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_json_listfield ( self ) : <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> ListModel ( integer = i , floating_point = <NUM_LIT:0> , <EOL> names = SerializationTest . names [ : i ] ) . save ( ) <EOL> objects = ListModel . objects . all ( ) <EOL> serialized = serializers . serialize ( '<STR_LIT>' , objects ) <EOL> deserialized = serializers . deserialize ( '<STR_LIT>' , serialized ) <EOL> for m in deserialized : <EOL> integer = m . object . integer <EOL> names = m . object . names <EOL> self . assertEqual ( names , SerializationTest . names [ : integer ] ) <EOL> def test_json_setfield ( self ) : <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> SerializableSetModel ( <EOL> setfield = set ( [ i - <NUM_LIT:1> ] ) , <EOL> setcharfield = set ( SerializationTest . names [ : i ] ) ) . save ( ) <EOL> objects = SerializableSetModel . objects . all ( ) <EOL> serialized = serializers . serialize ( '<STR_LIT>' , objects ) <EOL> deserialized = serializers . deserialize ( '<STR_LIT>' , serialized ) <EOL> for m in deserialized : <EOL> integer = m . object . setfield . pop ( ) <EOL> names = m . object . setcharfield <EOL> self . assertEqual ( names , set ( SerializationTest . names [ : integer + <NUM_LIT:1> ] ) ) <EOL> class String ( models . Model ) : <EOL> s = models . CharField ( max_length = <NUM_LIT:20> ) <EOL> class LazyObjectsTest ( TestCase ) : <EOL> def test_translation ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . utils . translation import ugettext_lazy <EOL> a = String . objects . create ( s = '<STR_LIT:a>' ) <EOL> b = String . objects . create ( s = ugettext_lazy ( '<STR_LIT:b>' ) ) <EOL> self . assertEqual ( String . objects . get ( s = '<STR_LIT:a>' ) , a ) <EOL> self . assertEqual ( list ( String . objects . filter ( s = '<STR_LIT:a>' ) ) , [ a ] ) <EOL> self . assertEqual ( list ( String . objects . filter ( s__lte = '<STR_LIT:a>' ) ) , [ a ] ) <EOL> self . assertEqual ( String . objects . get ( s = ugettext_lazy ( '<STR_LIT:a>' ) ) , a ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__lte = ugettext_lazy ( '<STR_LIT:a>' ) ) ) , [ a ] ) <EOL> self . assertEqual ( String . objects . get ( s = '<STR_LIT:b>' ) , b ) <EOL> self . assertEqual ( list ( String . objects . filter ( s = '<STR_LIT:b>' ) ) , [ b ] ) <EOL> self . assertEqual ( list ( String . objects . filter ( s__gte = '<STR_LIT:b>' ) ) , [ b ] ) <EOL> self . assertEqual ( String . objects . get ( s = ugettext_lazy ( '<STR_LIT:b>' ) ) , b ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__gte = ugettext_lazy ( '<STR_LIT:b>' ) ) ) , [ b ] ) <EOL> def test_marked_strings ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . utils . safestring import mark_safe , mark_for_escaping <EOL> a = String . objects . create ( s = '<STR_LIT:a>' ) <EOL> b = String . objects . create ( s = mark_safe ( '<STR_LIT:b>' ) ) <EOL> c = String . objects . create ( s = mark_for_escaping ( '<STR_LIT:c>' ) ) <EOL> self . assertEqual ( String . objects . get ( s = '<STR_LIT:a>' ) , a ) <EOL> self . assertEqual ( list ( String . objects . filter ( s__startswith = '<STR_LIT:a>' ) ) , [ a ] ) <EOL> self . assertEqual ( String . objects . get ( s = mark_safe ( '<STR_LIT:a>' ) ) , a ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__startswith = mark_safe ( '<STR_LIT:a>' ) ) ) , [ a ] ) <EOL> self . assertEqual ( String . objects . get ( s = mark_for_escaping ( '<STR_LIT:a>' ) ) , a ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__startswith = mark_for_escaping ( '<STR_LIT:a>' ) ) ) , <EOL> [ a ] ) <EOL> self . assertEqual ( String . objects . get ( s = '<STR_LIT:b>' ) , b ) <EOL> self . assertEqual ( list ( String . objects . filter ( s__startswith = '<STR_LIT:b>' ) ) , [ b ] ) <EOL> self . assertEqual ( String . objects . get ( s = mark_safe ( '<STR_LIT:b>' ) ) , b ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__startswith = mark_safe ( '<STR_LIT:b>' ) ) ) , [ b ] ) <EOL> self . assertEqual ( String . objects . get ( s = mark_for_escaping ( '<STR_LIT:b>' ) ) , b ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__startswith = mark_for_escaping ( '<STR_LIT:b>' ) ) ) , <EOL> [ b ] ) <EOL> self . assertEqual ( String . objects . get ( s = '<STR_LIT:c>' ) , c ) <EOL> self . assertEqual ( list ( String . objects . filter ( s__startswith = '<STR_LIT:c>' ) ) , [ c ] ) <EOL> self . assertEqual ( String . objects . get ( s = mark_safe ( '<STR_LIT:c>' ) ) , c ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__startswith = mark_safe ( '<STR_LIT:c>' ) ) ) , [ c ] ) <EOL> self . assertEqual ( String . objects . get ( s = mark_for_escaping ( '<STR_LIT:c>' ) ) , c ) <EOL> self . assertEqual ( <EOL> list ( String . objects . filter ( s__startswith = mark_for_escaping ( '<STR_LIT:c>' ) ) ) , <EOL> [ c ] ) <EOL> class FeaturesTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_subqueries ( self ) : <EOL> """<STR_LIT>""" <EOL> target = Target . objects . create ( index = <NUM_LIT:1> ) <EOL> source = Source . objects . create ( index = <NUM_LIT:2> , target = target ) <EOL> targets = Target . objects . all ( ) <EOL> with self . assertRaises ( DatabaseError ) : <EOL> Source . objects . get ( target__in = targets ) <EOL> self . assertEqual ( <EOL> Source . objects . get ( target__in = list ( targets ) ) , <EOL> source ) <EOL> class DecimalFieldTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> for d in ( Decimal ( '<STR_LIT>' ) , Decimal ( '<STR_LIT:5>' ) , Decimal ( '<STR_LIT>' ) , <EOL> Decimal ( '<STR_LIT>' ) , Decimal ( '<STR_LIT>' ) , ) : <EOL> DecimalModel ( decimal = d ) . save ( ) <EOL> def test_filter ( self ) : <EOL> d = DecimalModel . objects . get ( decimal = Decimal ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( isinstance ( d . decimal , Decimal ) ) <EOL> self . assertEquals ( str ( d . decimal ) , '<STR_LIT>' ) <EOL> d = DecimalModel . objects . get ( decimal = Decimal ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( str ( d . decimal ) , '<STR_LIT>' ) <EOL> d = DecimalModel . objects . get ( decimal = '<STR_LIT>' ) <EOL> self . assertEquals ( str ( d . decimal ) , '<STR_LIT>' ) <EOL> def test_order ( self ) : <EOL> """<STR_LIT>""" <EOL> rows = DecimalModel . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> values = list ( d . decimal for d in rows ) <EOL> self . assertEquals ( values , sorted ( values ) ) <EOL> def test_sign_extend ( self ) : <EOL> DecimalModel ( decimal = Decimal ( '<STR_LIT>' ) ) . save ( ) <EOL> try : <EOL> DecimalModel . objects . filter ( decimal__lt = <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> except InvalidOperation : <EOL> self . assertTrue ( False ) <EOL> class DeleteModel ( models . Model ) : <EOL> key = models . IntegerField ( primary_key = True ) <EOL> deletable = models . BooleanField ( ) <EOL> class BasicDeleteTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> DeleteModel ( key = i , deletable = i % <NUM_LIT:2> == <NUM_LIT:0> ) . save ( ) <EOL> def test_model_delete ( self ) : <EOL> d = DeleteModel . objects . get ( pk = <NUM_LIT:1> ) <EOL> d . delete ( ) <EOL> with self . assertRaises ( DeleteModel . DoesNotExist ) : <EOL> DeleteModel . objects . get ( pk = <NUM_LIT:1> ) <EOL> def test_delete_all ( self ) : <EOL> DeleteModel . objects . all ( ) . delete ( ) <EOL> self . assertEquals ( <NUM_LIT:0> , DeleteModel . objects . all ( ) . count ( ) ) <EOL> def test_delete_filtered ( self ) : <EOL> DeleteModel . objects . filter ( deletable = True ) . delete ( ) <EOL> self . assertEquals ( <NUM_LIT:5> , DeleteModel . objects . all ( ) . count ( ) ) <EOL> class M2MDeleteChildModel ( models . Model ) : <EOL> key = models . IntegerField ( primary_key = True ) <EOL> class M2MDeleteModel ( models . Model ) : <EOL> key = models . IntegerField ( primary_key = True ) <EOL> deletable = models . BooleanField ( ) <EOL> children = models . ManyToManyField ( M2MDeleteChildModel , blank = True ) <EOL> class ManyToManyDeleteTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> M2MDeleteModel ( key = i , deletable = i % <NUM_LIT:2> == <NUM_LIT:0> ) . save ( ) <EOL> def test_model_delete ( self ) : <EOL> d = M2MDeleteModel . objects . get ( pk = <NUM_LIT:1> ) <EOL> d . delete ( ) <EOL> with self . assertRaises ( M2MDeleteModel . DoesNotExist ) : <EOL> M2MDeleteModel . objects . get ( pk = <NUM_LIT:1> ) <EOL> @ expectedFailure <EOL> def test_delete_all ( self ) : <EOL> M2MDeleteModel . objects . all ( ) . delete ( ) <EOL> self . assertEquals ( <NUM_LIT:0> , M2MDeleteModel . objects . all ( ) . count ( ) ) <EOL> @ expectedFailure <EOL> def test_delete_filtered ( self ) : <EOL> M2MDeleteModel . objects . filter ( deletable = True ) . delete ( ) <EOL> self . assertEquals ( <NUM_LIT:5> , M2MDeleteModel . objects . all ( ) . count ( ) ) <EOL> class QuerysetModel ( models . Model ) : <EOL> key = models . IntegerField ( primary_key = True ) <EOL> class QuerysetTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> QuerysetModel . objects . create ( key = i + <NUM_LIT:1> ) <EOL> def test_all ( self ) : <EOL> self . assertEqual ( <NUM_LIT:10> , len ( QuerysetModel . objects . all ( ) ) ) <EOL> def test_none ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , len ( QuerysetModel . objects . none ( ) ) ) </s>
