<s> from django . test import TestCase , Client <EOL> from neurovault . apps . statmaps . models import Collection , User <EOL> import tempfile <EOL> import os <EOL> import shutil <EOL> from django . core . files . uploadedfile import SimpleUploadedFile <EOL> from neurovault . apps . statmaps . forms import NIDMResultsForm <EOL> from neurovault . apps . statmaps . utils import detect_feat_directory , get_traceback <EOL> from nidmfsl . fsl_exporter . fsl_exporter import FSLtoNIDMExporter <EOL> from neurovault . apps . statmaps . nidm_results import NIDMUpload <EOL> import urllib <EOL> import zipfile <EOL> from . utils import clearDB <EOL> class FeatDirectoryTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> testpath = os . path . join ( os . path . abspath ( os . path . dirname ( __file__ ) ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> testdata_repo = '<STR_LIT>' <EOL> self . testfiles = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : [ '<STR_LIT:T>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : [ '<STR_LIT:T>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> } <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> self . user = User . objects . create_user ( '<STR_LIT>' ) <EOL> self . user . save ( ) <EOL> self . client = Client ( ) <EOL> self . client . login ( username = self . user ) <EOL> self . coll = Collection ( owner = self . user , name = "<STR_LIT>" ) <EOL> self . coll . save ( ) <EOL> for fname , info in self . testfiles . items ( ) : <EOL> self . testfiles [ fname ] [ '<STR_LIT:file>' ] = os . path . join ( testpath , fname ) <EOL> if not os . path . exists ( self . testfiles [ fname ] [ '<STR_LIT:file>' ] ) : <EOL> print '<STR_LIT>' . format ( fname ) <EOL> furl = '<STR_LIT>' . format ( testdata_repo , info [ '<STR_LIT>' ] ) <EOL> try : <EOL> urllib . urlretrieve ( furl , self . testfiles [ fname ] [ '<STR_LIT:file>' ] ) <EOL> except : <EOL> os . remove ( os . path . join ( testpath , fname ) ) <EOL> raise <EOL> self . testfiles [ fname ] [ '<STR_LIT>' ] = self . testfiles [ fname ] [ '<STR_LIT:file>' ] [ : - <NUM_LIT:4> ] <EOL> self . testfiles [ fname ] [ '<STR_LIT>' ] = os . path . join ( self . tmpdir , fname [ : - <NUM_LIT:4> ] ) <EOL> if not os . path . exists ( self . testfiles [ fname ] [ '<STR_LIT>' ] ) : <EOL> fh = open ( os . path . join ( testpath , fname ) , '<STR_LIT:rb>' ) <EOL> z = zipfile . ZipFile ( fh ) <EOL> for name in [ v for v in z . namelist ( ) if not v . startswith ( '<STR_LIT:.>' ) and <EOL> '<STR_LIT>' not in v ] : <EOL> outpath = self . testfiles [ fname ] [ '<STR_LIT>' ] <EOL> z . extract ( name , outpath ) <EOL> fh . close ( ) <EOL> shutil . copytree ( self . testfiles [ fname ] [ '<STR_LIT>' ] , self . testfiles [ fname ] [ '<STR_LIT>' ] ) <EOL> def tearDown ( self ) : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> clearDB ( ) <EOL> def testFEAT_NIDM ( self ) : <EOL> for fname , info in self . testfiles . items ( ) : <EOL> info [ '<STR_LIT>' ] = False <EOL> for root , dirs , files in os . walk ( info [ '<STR_LIT>' ] ) : <EOL> if detect_feat_directory ( root ) : <EOL> print '<STR_LIT>' . format ( root ) <EOL> info [ '<STR_LIT>' ] = True <EOL> try : <EOL> fslnidm = FSLtoNIDMExporter ( feat_dir = root , version = "<STR_LIT>" ) <EOL> fslnidm . parse ( ) <EOL> export_dir = fslnidm . export ( ) <EOL> ttl_file = os . path . join ( export_dir , '<STR_LIT>' ) <EOL> except : <EOL> print ( "<STR_LIT>" . format ( get_traceback ( ) ) ) <EOL> self . assertTrue ( os . path . exists ( export_dir ) ) <EOL> self . assertEquals ( os . path . join ( info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] ) , export_dir ) <EOL> self . assertGreaterEqual ( os . path . getsize ( ttl_file ) , info [ '<STR_LIT>' ] ) <EOL> for fname , info in self . testfiles . items ( ) : <EOL> nidm_zpath = os . path . join ( self . tmpdir , '<STR_LIT>' . format ( fname . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> nidm_zip = zipfile . ZipFile ( nidm_zpath , '<STR_LIT:w>' ) <EOL> for root , dirs , files in os . walk ( os . path . join ( info [ '<STR_LIT>' ] , info [ '<STR_LIT>' ] ) ) : <EOL> for nfile in files : <EOL> nidm_zip . write ( os . path . join ( root , nfile ) ) <EOL> nidm_zip . close ( ) <EOL> zname = os . path . basename ( nidm_zip . filename ) <EOL> post_dict = { <EOL> '<STR_LIT:name>' : zname , <EOL> '<STR_LIT:description>' : '<STR_LIT>' . format ( zname ) , <EOL> '<STR_LIT>' : self . coll . pk , <EOL> } <EOL> file_dict = { '<STR_LIT>' : SimpleUploadedFile ( zname , open ( nidm_zpath , '<STR_LIT:r>' ) . read ( ) ) } <EOL> form = NIDMResultsForm ( post_dict , file_dict ) <EOL> self . assertEqual ( form . errors , { } ) <EOL> nidm = form . save ( ) <EOL> statmaps = nidm . nidmresultstatisticmap_set . all ( ) <EOL> self . assertEquals ( len ( statmaps ) , info [ '<STR_LIT>' ] ) <EOL> map_types = [ v . map_type for v in statmaps ] <EOL> self . assertEquals ( sorted ( map_types ) , sorted ( info [ '<STR_LIT>' ] ) ) <EOL> names = [ v . name for v in statmaps ] <EOL> self . assertEquals ( sorted ( names ) , sorted ( info [ '<STR_LIT>' ] ) ) </s>
