<s> import os , os . path , unittest <EOL> from django . contrib . gis . gdal import DataSource , Envelope , OGRException , OGRIndexError <EOL> from django . contrib . gis . gdal . field import OFTReal , OFTInteger , OFTString <EOL> data_path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:data>' ) <EOL> def get_ds_file ( name , ext ) : <EOL> return os . sep . join ( [ data_path , name , name + '<STR_LIT>' % ext ] ) <EOL> class TestDS : <EOL> def __init__ ( self , name , ** kwargs ) : <EOL> ext = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . ds = get_ds_file ( name , ext ) <EOL> for key , value in kwargs . items ( ) : <EOL> setattr ( self , key , value ) <EOL> ds_list = ( TestDS ( '<STR_LIT>' , nfeat = <NUM_LIT:5> , nfld = <NUM_LIT:3> , geom = '<STR_LIT>' , gtype = <NUM_LIT:1> , driver = '<STR_LIT>' , <EOL> fields = { '<STR_LIT>' : OFTReal , '<STR_LIT:int>' : OFTInteger , '<STR_LIT:str>' : OFTString , } , <EOL> extent = ( - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) , <EOL> srs_wkt = '<STR_LIT>' , <EOL> field_values = { '<STR_LIT>' : [ float ( i ) for i in range ( <NUM_LIT:1> , <NUM_LIT:6> ) ] , '<STR_LIT:int>' : range ( <NUM_LIT:1> , <NUM_LIT:6> ) , '<STR_LIT:str>' : [ str ( i ) for i in range ( <NUM_LIT:1> , <NUM_LIT:6> ) ] } , <EOL> fids = range ( <NUM_LIT:5> ) ) , <EOL> TestDS ( '<STR_LIT>' , ext = '<STR_LIT>' , nfeat = <NUM_LIT:3> , nfld = <NUM_LIT:3> , geom = '<STR_LIT>' , gtype = <NUM_LIT:1> , driver = '<STR_LIT>' , <EOL> fields = { '<STR_LIT>' : OFTString , '<STR_LIT>' : OFTString , '<STR_LIT>' : OFTString } , <EOL> extent = ( <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> field_values = { '<STR_LIT>' : [ '<STR_LIT:1.0>' , '<STR_LIT>' , '<STR_LIT>' ] , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , '<STR_LIT>' : [ '<STR_LIT:5>' , '<STR_LIT>' , '<STR_LIT>' ] } , <EOL> fids = range ( <NUM_LIT:1> , <NUM_LIT:4> ) ) , <EOL> TestDS ( '<STR_LIT>' , nfeat = <NUM_LIT:3> , nfld = <NUM_LIT:3> , geom = '<STR_LIT>' , gtype = <NUM_LIT:3> , <EOL> driver = '<STR_LIT>' , <EOL> fields = { '<STR_LIT:float>' : OFTReal , '<STR_LIT:int>' : OFTInteger , '<STR_LIT:str>' : OFTString , } , <EOL> extent = ( - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> srs_wkt = '<STR_LIT>' ) , <EOL> ) <EOL> bad_ds = ( TestDS ( '<STR_LIT:foo>' ) , <EOL> ) <EOL> class DataSourceTest ( unittest . TestCase ) : <EOL> def test01_valid_shp ( self ) : <EOL> "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( ds ) ) <EOL> self . assertEqual ( source . ds , ds . name ) <EOL> self . assertEqual ( source . driver , str ( ds . driver ) ) <EOL> try : <EOL> ds [ len ( ds ) ] <EOL> except OGRIndexError : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test02_invalid_shp ( self ) : <EOL> "<STR_LIT>" <EOL> for source in bad_ds : <EOL> self . assertRaises ( OGRException , DataSource , source . ds ) <EOL> def test03a_layers ( self ) : <EOL> "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> for layer in ds : <EOL> self . assertEqual ( len ( layer ) , source . nfeat ) <EOL> self . assertEqual ( source . nfld , layer . num_fields ) <EOL> self . assertEqual ( source . nfld , len ( layer . fields ) ) <EOL> self . assertEqual ( True , isinstance ( layer . extent , Envelope ) ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:0> ] , layer . extent . min_x , <NUM_LIT:5> ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:1> ] , layer . extent . min_y , <NUM_LIT:5> ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:2> ] , layer . extent . max_x , <NUM_LIT:5> ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:3> ] , layer . extent . max_y , <NUM_LIT:5> ) <EOL> flds = layer . fields <EOL> for f in flds : self . assertEqual ( True , f in source . fields ) <EOL> self . assertRaises ( OGRIndexError , layer . __getitem__ , - <NUM_LIT:1> ) <EOL> self . assertRaises ( OGRIndexError , layer . __getitem__ , <NUM_LIT> ) <EOL> if hasattr ( source , '<STR_LIT>' ) : <EOL> fld_names = source . field_values . keys ( ) <EOL> for fld_name in fld_names : <EOL> self . assertEqual ( source . field_values [ fld_name ] , layer . get_fields ( fld_name ) ) <EOL> for i , fid in enumerate ( source . fids ) : <EOL> feat = layer [ fid ] <EOL> self . assertEqual ( fid , feat . fid ) <EOL> for fld_name in fld_names : <EOL> self . assertEqual ( source . field_values [ fld_name ] [ i ] , feat . get ( fld_name ) ) <EOL> print "<STR_LIT>" <EOL> def test03b_layer_slice ( self ) : <EOL> "<STR_LIT>" <EOL> source = ds_list [ <NUM_LIT:0> ] <EOL> ds = DataSource ( source . ds ) <EOL> sl = slice ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> feats = ds [ <NUM_LIT:0> ] [ sl ] <EOL> for fld_name in ds [ <NUM_LIT:0> ] . fields : <EOL> test_vals = [ feat . get ( fld_name ) for feat in feats ] <EOL> control_vals = source . field_values [ fld_name ] [ sl ] <EOL> self . assertEqual ( control_vals , test_vals ) <EOL> def test04_features ( self ) : <EOL> "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> for layer in ds : <EOL> for feat in layer : <EOL> self . assertEqual ( source . nfld , len ( list ( feat ) ) ) <EOL> self . assertEqual ( source . gtype , feat . geom_type ) <EOL> for k , v in source . fields . items ( ) : <EOL> self . assertEqual ( True , isinstance ( feat [ k ] , v ) ) <EOL> for fld in feat : self . assertEqual ( True , fld . name in source . fields . keys ( ) ) <EOL> def test05_geometries ( self ) : <EOL> "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> for layer in ds : <EOL> for feat in layer : <EOL> g = feat . geom <EOL> self . assertEqual ( source . geom , g . geom_name ) <EOL> self . assertEqual ( source . gtype , g . geom_type ) <EOL> if hasattr ( source , '<STR_LIT>' ) : <EOL> self . assertEqual ( source . srs_wkt , g . srs . wkt ) <EOL> def suite ( ) : <EOL> s = unittest . TestSuite ( ) <EOL> s . addTest ( unittest . makeSuite ( DataSourceTest ) ) <EOL> return s <EOL> def run ( verbosity = <NUM_LIT:2> ) : <EOL> unittest . TextTestRunner ( verbosity = verbosity ) . run ( suite ( ) ) </s>
