<s> import nibabel as nb <EOL> import os <EOL> from django . core . files . uploadedfile import SimpleUploadedFile <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase , Client <EOL> from neurovault . apps . statmaps . forms import StatisticMapForm <EOL> from neurovault . apps . statmaps . models import Collection , User , StatisticMap <EOL> from neurovault . apps . statmaps . utils import detect_4D , split_4D_to_3D <EOL> from . utils import clearDB <EOL> class AddStatmapsTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = User . objects . create_user ( '<STR_LIT>' , password = "<STR_LIT>" ) <EOL> self . user . save ( ) <EOL> self . client = Client ( ) <EOL> self . client . login ( username = self . user , password = "<STR_LIT>" ) <EOL> self . coll = Collection ( owner = self . user , name = "<STR_LIT>" ) <EOL> self . coll . save ( ) <EOL> def tearDown ( self ) : <EOL> clearDB ( ) <EOL> def testaddNiiGz ( self ) : <EOL> post_dict = { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:T>' , <EOL> '<STR_LIT>' : self . coll . pk , <EOL> } <EOL> testpath = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> fname = os . path . join ( testpath , '<STR_LIT>' ) <EOL> file_dict = { '<STR_LIT:file>' : SimpleUploadedFile ( fname , open ( fname ) . read ( ) ) } <EOL> form = StatisticMapForm ( post_dict , file_dict ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> self . assertEqual ( StatisticMap . objects . filter ( collection = self . coll . pk ) [ <NUM_LIT:0> ] . name , "<STR_LIT>" ) <EOL> def testaddAFNI ( self ) : <EOL> post_dict = { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:T>' , <EOL> '<STR_LIT>' : self . coll . pk , <EOL> } <EOL> testpath = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> fname = os . path . join ( testpath , '<STR_LIT>' ) <EOL> nii = nb . load ( fname ) <EOL> self . assertTrue ( detect_4D ( nii ) ) <EOL> self . assertTrue ( len ( split_4D_to_3D ( nii ) ) > <NUM_LIT:0> ) <EOL> file_dict = { '<STR_LIT:file>' : SimpleUploadedFile ( fname , open ( fname ) . read ( ) ) } <EOL> form = StatisticMapForm ( post_dict , file_dict ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> self . assertEqual ( StatisticMap . objects . filter ( collection = self . coll . pk ) . count ( ) , <NUM_LIT:2> ) <EOL> def testaddImgHdr ( self ) : <EOL> post_dict = { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:T>' , <EOL> '<STR_LIT>' : self . coll . pk , <EOL> } <EOL> testpath = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> fname_img = os . path . join ( testpath , '<STR_LIT>' ) <EOL> fname_hdr = os . path . join ( testpath , '<STR_LIT>' ) <EOL> file_dict = { '<STR_LIT:file>' : SimpleUploadedFile ( fname_img , open ( fname_img ) . read ( ) ) , <EOL> '<STR_LIT>' : SimpleUploadedFile ( fname_hdr , open ( fname_hdr ) . read ( ) ) } <EOL> form = StatisticMapForm ( post_dict , file_dict ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertTrue ( "<STR_LIT>" in form . errors [ "<STR_LIT:file>" ] [ <NUM_LIT:0> ] ) <EOL> post_dict = { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:T>' , <EOL> '<STR_LIT>' : self . coll . pk , <EOL> '<STR_LIT>' : True <EOL> } <EOL> testpath = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> fname_img = os . path . join ( testpath , '<STR_LIT>' ) <EOL> fname_hdr = os . path . join ( testpath , '<STR_LIT>' ) <EOL> file_dict = { '<STR_LIT:file>' : SimpleUploadedFile ( fname_img , open ( fname_img ) . read ( ) ) , <EOL> '<STR_LIT>' : SimpleUploadedFile ( fname_hdr , open ( fname_hdr ) . read ( ) ) } <EOL> form = StatisticMapForm ( post_dict , file_dict ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> self . assertEqual ( StatisticMap . objects . filter ( collection = self . coll . pk ) [ <NUM_LIT:0> ] . name , "<STR_LIT>" ) <EOL> def test_add_image_non_owner ( self ) : <EOL> non_owner = User . objects . create_user ( '<STR_LIT>' , password = "<STR_LIT>" ) <EOL> non_owner . save ( ) <EOL> client = Client ( ) <EOL> client . login ( username = non_owner , password = "<STR_LIT>" ) <EOL> testpath = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> fname_img = os . path . join ( testpath , '<STR_LIT>' ) <EOL> fname_hdr = os . path . join ( testpath , '<STR_LIT>' ) <EOL> post_dict = { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:T>' , <EOL> '<STR_LIT>' : self . coll . pk , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:file>' : SimpleUploadedFile ( fname_img , open ( fname_img ) . read ( ) ) , <EOL> '<STR_LIT>' : SimpleUploadedFile ( fname_hdr , open ( fname_hdr ) . read ( ) ) <EOL> } <EOL> cid = self . coll . pk <EOL> response = client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : cid } ) , post_dict ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_add_image_owner ( self ) : <EOL> testpath = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> fname_img = os . path . join ( testpath , '<STR_LIT>' ) <EOL> fname_hdr = os . path . join ( testpath , '<STR_LIT>' ) <EOL> post_dict = { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:T>' , <EOL> '<STR_LIT>' : self . coll . pk , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:file>' : SimpleUploadedFile ( fname_img , open ( fname_img ) . read ( ) ) , <EOL> '<STR_LIT>' : SimpleUploadedFile ( fname_hdr , open ( fname_hdr ) . read ( ) ) <EOL> } <EOL> cid = self . coll . pk <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : cid } ) , post_dict ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> image_pk = StatisticMap . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . pk <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : image_pk } ) ) </s>
