<s> from django . test import TestCase <EOL> from django . contrib . gis . geos import GEOSGeometry , Point , MultiPoint <EOL> from django . contrib . gis . db . models import Collect , Count , Extent , F , Union <EOL> from django . contrib . gis . geometry . backend import Geometry <EOL> from django . contrib . gis . tests . utils import mysql , oracle , no_mysql , no_oracle , no_spatialite <EOL> from models import City , Location , DirectoryEntry , Parcel , Book , Author , Article <EOL> class RelatedGeoModelTest ( TestCase ) : <EOL> def test02_select_related ( self ) : <EOL> "<STR_LIT>" <EOL> qs1 = City . objects . all ( ) <EOL> qs2 = City . objects . select_related ( ) <EOL> qs3 = City . objects . select_related ( '<STR_LIT:location>' ) <EOL> cities = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , - <NUM_LIT> , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , - <NUM_LIT> , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , - <NUM_LIT> , <NUM_LIT> ) , <EOL> ) <EOL> for qs in ( qs1 , qs2 , qs3 ) : <EOL> for ref , c in zip ( cities , qs ) : <EOL> nm , st , lon , lat = ref <EOL> self . assertEqual ( nm , c . name ) <EOL> self . assertEqual ( st , c . state ) <EOL> self . assertEqual ( Point ( lon , lat ) , c . location . point ) <EOL> @ no_mysql <EOL> def test03_transform_related ( self ) : <EOL> "<STR_LIT>" <EOL> tol = <NUM_LIT:0> <EOL> def check_pnt ( ref , pnt ) : <EOL> self . assertAlmostEqual ( ref . x , pnt . x , tol ) <EOL> self . assertAlmostEqual ( ref . y , pnt . y , tol ) <EOL> self . assertEqual ( ref . srid , pnt . srid ) <EOL> transformed = ( ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' ) , <EOL> ) <EOL> for name , srid , wkt in transformed : <EOL> qs = list ( City . objects . filter ( name = name ) . transform ( srid , field_name = '<STR_LIT>' ) ) <EOL> check_pnt ( GEOSGeometry ( wkt , srid ) , qs [ <NUM_LIT:0> ] . location . point ) <EOL> @ no_mysql <EOL> @ no_spatialite <EOL> def test04a_related_extent_aggregate ( self ) : <EOL> "<STR_LIT>" <EOL> aggs = City . objects . aggregate ( Extent ( '<STR_LIT>' ) ) <EOL> all_extent = ( - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) <EOL> txpa_extent = ( - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) <EOL> e1 = City . objects . extent ( field_name = '<STR_LIT>' ) <EOL> e2 = City . objects . exclude ( state = '<STR_LIT>' ) . extent ( field_name = '<STR_LIT>' ) <EOL> e3 = aggs [ '<STR_LIT>' ] <EOL> tol = <NUM_LIT:4> <EOL> for ref , e in [ ( all_extent , e1 ) , ( txpa_extent , e2 ) , ( all_extent , e3 ) ] : <EOL> for ref_val , e_val in zip ( ref , e ) : self . assertAlmostEqual ( ref_val , e_val , tol ) <EOL> @ no_mysql <EOL> def test04b_related_union_aggregate ( self ) : <EOL> "<STR_LIT>" <EOL> aggs = City . objects . aggregate ( Union ( '<STR_LIT>' ) ) <EOL> p1 = Point ( - <NUM_LIT> , <NUM_LIT> ) <EOL> p2 = Point ( - <NUM_LIT> , <NUM_LIT> ) <EOL> p3 = Point ( - <NUM_LIT> , <NUM_LIT> ) <EOL> p4 = Point ( - <NUM_LIT> , <NUM_LIT> ) <EOL> p5 = Point ( - <NUM_LIT> , <NUM_LIT> ) <EOL> if oracle : <EOL> ref_u1 = MultiPoint ( p4 , p5 , p3 , p1 , p2 , srid = <NUM_LIT> ) <EOL> ref_u2 = MultiPoint ( p3 , p2 , srid = <NUM_LIT> ) <EOL> else : <EOL> ref_u1 = MultiPoint ( p1 , p2 , p4 , p5 , p3 , srid = <NUM_LIT> ) <EOL> ref_u2 = MultiPoint ( p2 , p3 , srid = <NUM_LIT> ) <EOL> u1 = City . objects . unionagg ( field_name = '<STR_LIT>' ) <EOL> u2 = City . objects . exclude ( name__in = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) . unionagg ( field_name = '<STR_LIT>' ) <EOL> u3 = aggs [ '<STR_LIT>' ] <EOL> self . assertEqual ( ref_u1 , u1 ) <EOL> self . assertEqual ( ref_u2 , u2 ) <EOL> self . assertEqual ( ref_u1 , u3 ) <EOL> def test05_select_related_fk_to_subclass ( self ) : <EOL> "<STR_LIT>" <EOL> l = list ( DirectoryEntry . objects . all ( ) . select_related ( ) ) <EOL> def test06_f_expressions ( self ) : <EOL> "<STR_LIT>" <EOL> b1 = GEOSGeometry ( '<STR_LIT>' , srid = <NUM_LIT> ) <EOL> pcity = City . objects . get ( name = '<STR_LIT>' ) <EOL> c1 = pcity . location . point <EOL> c2 = c1 . transform ( <NUM_LIT> , clone = True ) <EOL> b2 = c2 . buffer ( <NUM_LIT:100> ) <EOL> p1 = Parcel . objects . create ( name = '<STR_LIT>' , city = pcity , center1 = c1 , center2 = c2 , border1 = b1 , border2 = b2 ) <EOL> c1 = b1 . centroid <EOL> c2 = c1 . transform ( <NUM_LIT> , clone = True ) <EOL> p2 = Parcel . objects . create ( name = '<STR_LIT>' , city = pcity , center1 = c1 , center2 = c2 , border1 = b1 , border2 = b1 ) <EOL> qs = Parcel . objects . filter ( center1__within = F ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( qs ) ) <EOL> self . assertEqual ( '<STR_LIT>' , qs [ <NUM_LIT:0> ] . name ) <EOL> if not mysql : <EOL> qs = Parcel . objects . filter ( center2__within = F ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( qs ) ) <EOL> self . assertEqual ( '<STR_LIT>' , qs [ <NUM_LIT:0> ] . name ) <EOL> qs = Parcel . objects . filter ( center1 = F ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( qs ) ) <EOL> self . assertEqual ( '<STR_LIT>' , qs [ <NUM_LIT:0> ] . name ) <EOL> if not mysql : <EOL> qs = Parcel . objects . filter ( border2__contains = F ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( qs ) ) <EOL> self . assertEqual ( '<STR_LIT>' , qs [ <NUM_LIT:0> ] . name ) <EOL> def test07_values ( self ) : <EOL> "<STR_LIT>" <EOL> gqs = Location . objects . all ( ) <EOL> gvqs = Location . objects . values ( ) <EOL> gvlqs = Location . objects . values_list ( ) <EOL> for m , d , t in zip ( gqs , gvqs , gvlqs ) : <EOL> self . failUnless ( isinstance ( d [ '<STR_LIT>' ] , Geometry ) ) <EOL> self . failUnless ( isinstance ( t [ <NUM_LIT:1> ] , Geometry ) ) <EOL> self . assertEqual ( m . point , d [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( m . point , t [ <NUM_LIT:1> ] ) <EOL> def test08_defer_only ( self ) : <EOL> "<STR_LIT>" <EOL> qs = Location . objects . all ( ) <EOL> def_qs = Location . objects . defer ( '<STR_LIT>' ) <EOL> for loc , def_loc in zip ( qs , def_qs ) : <EOL> self . assertEqual ( loc . point , def_loc . point ) <EOL> def test09_pk_relations ( self ) : <EOL> "<STR_LIT>" <EOL> city_ids = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> loc_ids = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> ids_qs = City . objects . order_by ( '<STR_LIT:id>' ) . values ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> for val_dict , c_id , l_id in zip ( ids_qs , city_ids , loc_ids ) : <EOL> self . assertEqual ( val_dict [ '<STR_LIT:id>' ] , c_id ) <EOL> self . assertEqual ( val_dict [ '<STR_LIT>' ] , l_id ) <EOL> def test10_combine ( self ) : <EOL> "<STR_LIT>" <EOL> buf1 = City . objects . get ( name = '<STR_LIT>' ) . location . point . buffer ( <NUM_LIT:0.1> ) <EOL> buf2 = City . objects . get ( name = '<STR_LIT>' ) . location . point . buffer ( <NUM_LIT:0.1> ) <EOL> qs1 = City . objects . filter ( location__point__within = buf1 ) <EOL> qs2 = City . objects . filter ( location__point__within = buf2 ) <EOL> combined = qs1 | qs2 <EOL> names = [ c . name for c in combined ] <EOL> self . assertEqual ( <NUM_LIT:2> , len ( names ) ) <EOL> self . failUnless ( '<STR_LIT>' in names ) <EOL> self . failUnless ( '<STR_LIT>' in names ) <EOL> def test11_geoquery_pickle ( self ) : <EOL> "<STR_LIT>" <EOL> import pickle <EOL> from django . contrib . gis . db . models . sql import GeoQuery <EOL> qs = City . objects . all ( ) <EOL> q_str = pickle . dumps ( qs . query ) <EOL> q = pickle . loads ( q_str ) <EOL> self . assertEqual ( GeoQuery , q . __class__ ) <EOL> @ no_oracle <EOL> def test12a_count ( self ) : <EOL> "<STR_LIT>" <EOL> dallas = City . objects . get ( name = '<STR_LIT>' ) <EOL> loc = Location . objects . annotate ( num_cities = Count ( '<STR_LIT>' ) ) . get ( id = dallas . location . id ) <EOL> self . assertEqual ( <NUM_LIT:2> , loc . num_cities ) <EOL> def test12b_count ( self ) : <EOL> "<STR_LIT>" <EOL> qs = Author . objects . annotate ( num_books = Count ( '<STR_LIT>' ) ) . filter ( num_books__gt = <NUM_LIT:1> ) <EOL> vqs = Author . objects . values ( '<STR_LIT:name>' ) . annotate ( num_books = Count ( '<STR_LIT>' ) ) . filter ( num_books__gt = <NUM_LIT:1> ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( qs ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , qs [ <NUM_LIT:0> ] . num_books ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( vqs ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , vqs [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> @ no_oracle <EOL> def test13_select_related_null_fk ( self ) : <EOL> "<STR_LIT>" <EOL> no_author = Book . objects . create ( title = '<STR_LIT>' ) <EOL> b = Book . objects . select_related ( '<STR_LIT>' ) . get ( title = '<STR_LIT>' ) <EOL> self . assertEqual ( None , b . author ) <EOL> @ no_mysql <EOL> @ no_oracle <EOL> @ no_spatialite <EOL> def test14_collect ( self ) : <EOL> "<STR_LIT>" <EOL> ref_geom = GEOSGeometry ( '<STR_LIT>' ) <EOL> c1 = City . objects . filter ( state = '<STR_LIT>' ) . collect ( field_name = '<STR_LIT>' ) <EOL> c2 = City . objects . filter ( state = '<STR_LIT>' ) . aggregate ( Collect ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> for coll in ( c1 , c2 ) : <EOL> self . assertEqual ( <NUM_LIT:4> , len ( coll ) ) <EOL> self . assertEqual ( ref_geom , coll ) <EOL> def test15_invalid_select_related ( self ) : <EOL> "<STR_LIT>" <EOL> qs = Article . objects . select_related ( '<STR_LIT>' ) <EOL> sql = str ( qs . query ) </s>
