<s> import glob <EOL> import os <EOL> from django . test import TestCase <EOL> from django . contrib . auth . models import User <EOL> from onadata . apps . logger . models import XForm , Instance <EOL> from onadata . apps . main . models import UserProfile <EOL> def open_all_files ( path ) : <EOL> file_paths = glob . glob ( os . path . join ( path , "<STR_LIT:*>" ) ) <EOL> result = { } <EOL> for file_path in file_paths : <EOL> if file_path . endswith ( "<STR_LIT>" ) : <EOL> result [ file_path ] = open ( file_path , "<STR_LIT:rb>" ) <EOL> else : <EOL> result [ file_path ] = open ( file_path ) <EOL> return result <EOL> def create_post_data ( path ) : <EOL> xml_files = glob . glob ( os . path . join ( path , "<STR_LIT>" ) ) <EOL> if len ( xml_files ) != <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> xml_file = open ( xml_files [ <NUM_LIT:0> ] ) <EOL> post_data = { "<STR_LIT>" : xml_file } <EOL> for jpg in glob . glob ( os . path . join ( path , "<STR_LIT>" ) ) : <EOL> image_file = open ( jpg , "<STR_LIT:rb>" ) <EOL> post_data [ jpg ] = image_file <EOL> return post_data <EOL> def get_absolute_path ( subdirectory ) : <EOL> return os . path . join ( <EOL> os . path . dirname ( os . path . abspath ( __file__ ) ) , subdirectory ) <EOL> class TestInstanceCreation ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = User . objects . create ( username = "<STR_LIT>" ) <EOL> profile , c = UserProfile . objects . get_or_create ( user = self . user ) <EOL> profile . require_auth = False <EOL> profile . save ( ) <EOL> absolute_path = get_absolute_path ( "<STR_LIT>" ) <EOL> open_forms = open_all_files ( absolute_path ) <EOL> self . json = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> for path , open_file in open_forms . items ( ) : <EOL> XForm . objects . create ( <EOL> xml = open_file . read ( ) , user = self . user , json = self . json , <EOL> require_auth = False ) <EOL> open_file . close ( ) <EOL> self . _create_water_translated_form ( ) <EOL> def _create_water_translated_form ( self ) : <EOL> f = open ( os . path . join ( <EOL> os . path . dirname ( os . path . abspath ( __file__ ) ) , <EOL> "<STR_LIT>" <EOL> ) ) <EOL> xml = f . read ( ) <EOL> f . close ( ) <EOL> self . xform = XForm . objects . create ( <EOL> xml = xml , user = self . user , json = self . json ) <EOL> def test_form_submission ( self ) : <EOL> f = open ( os . path . join ( <EOL> os . path . dirname ( os . path . abspath ( __file__ ) ) , <EOL> "<STR_LIT>" ) ) <EOL> xml = f . read ( ) <EOL> f . close ( ) <EOL> Instance . objects . create ( xml = xml , user = self . user , xform = self . xform ) <EOL> def test_data_submission ( self ) : <EOL> subdirectories = [ "<STR_LIT>" ] <EOL> for subdirectory in subdirectories : <EOL> path = get_absolute_path ( subdirectory ) <EOL> postdata = create_post_data ( path ) <EOL> response = self . client . post ( '<STR_LIT>' , postdata ) <EOL> self . failUnlessEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_submission_for_missing_form ( self ) : <EOL> xml_file = open ( os . path . join ( <EOL> os . path . dirname ( os . path . abspath ( __file__ ) ) , <EOL> "<STR_LIT>" <EOL> ) ) <EOL> postdata = { "<STR_LIT>" : xml_file } <EOL> response = self . client . post ( '<STR_LIT>' , postdata ) <EOL> self . failUnlessEqual ( response . status_code , <NUM_LIT> ) </s>
