def test_archive2folder(self): ############################## print "Running unittests for this project: ", project_name print "Running this unittest: ", self._testMethodName ############################## from zipper import zipUtils zip_archive = r"%s%sInvestigation.blah.zip" % (dir_of_the_current_test, delimiter()) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder(zip_archive) print "sub_folder_count=", sub_folder_count assert 0 == sub_folder_count cwd = os.getcwd() # os.chdir(r'%s%sdoesNotExist.blah' % (dir_of_the_current_test,delimiter())) #caused unittest to fail as expected (tried separately) os.chdir(r"%s%sInvestigation.blah" % (dir_of_the_current_test, delimiter())) os.remove("m_1.xls") # os.remove('does_not_exist.xls') #caused unittest to fail as expected (tried separately) os.chdir(dir_of_the_current_test) os.rmdir("Investigation.blah") os.chdir(cwd)
def test_archive2folder(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## from zipper import zipUtils zip_archive = r'%s%sInvestigation.blah.zip' % (dir_of_the_current_test, delimiter()) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder(zip_archive) print 'sub_folder_count=', sub_folder_count assert 0 == sub_folder_count cwd = os.getcwd() #os.chdir(r'%s%sdoesNotExist.blah' % (dir_of_the_current_test,delimiter())) #caused unittest to fail as expected (tried separately) os.chdir(r'%s%sInvestigation.blah' % (dir_of_the_current_test, delimiter())) os.remove('m_1.xls') #os.remove('does_not_exist.xls') #caused unittest to fail as expected (tried separately) os.chdir(dir_of_the_current_test) os.rmdir('Investigation.blah') os.chdir(cwd)
def clean_up_if_all_checks_passed( self, specific_files_or_dirs_not_to_delete, current_test_dir=dir_of_the_current_test): all_files_or_dirs_to_delete = [ r'%s%s%s' % (current_test_dir, delimiter(), file_or_dir_name) for file_or_dir_name in os.listdir(current_test_dir) if not re.search('(.\py$)', file_or_dir_name) and not r'%s%s%s' % (current_test_dir, delimiter(), file_or_dir_name) in specific_files_or_dirs_not_to_delete ] for FILE_OR_DIR_TO_DELETE in all_files_or_dirs_to_delete: if os.path.isfile(FILE_OR_DIR_TO_DELETE): os.remove(FILE_OR_DIR_TO_DELETE) assert not os.path.exists( FILE_OR_DIR_TO_DELETE ), " This still exists: \n %s" % FILE_OR_DIR_TO_DELETE print 'Removed this temporary file: ', FILE_OR_DIR_TO_DELETE else: assert os.path.isdir( FILE_OR_DIR_TO_DELETE ), "FILE_OR_DIR_TO_DELETE=%s; not a directory or a file???" % FILE_OR_DIR_TO_DELETE cwd = os.getcwd() os.chdir(FILE_OR_DIR_TO_DELETE) for file in glob.glob('*'): os.remove(file) del file os.chdir(cwd) del cwd os.rmdir(FILE_OR_DIR_TO_DELETE ) #this will only work on an empty directory!
def test_AtLeastOneInvestigationStudyAssayMaterialRequired_defaultOptions(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## non_problem_inputs = ["TOY.test_11.zip"] non_problem_inputs = [r'%s%s%s' % (dir_of_the_current_test,delimiter(),file) for file in non_problem_inputs] del file problem_inputs = ["TOY.test_11-no.Material.zip","TOY.test_11-no.Investigation.zip","TOY.test_11-no.Study.zip","TOY.test_11-no.Assay.zip"] problem_inputs = [r'%s%s%s' % (dir_of_the_current_test,delimiter(),file) for file in problem_inputs] del file for input in non_problem_inputs+problem_inputs: cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s"' % (project_modules_to_test_dir,delimiter(),input) if not input in problem_inputs: assert input in non_problem_inputs,"input=%s???" % input assert 0 == os.system(cmd) else: assert input in problem_inputs,"input=%s???" % input assert not 0 == os.system(cmd) del cmd del input self.clean_up_if_all_checks_passed(specific_files_or_dirs_not_to_delete=non_problem_inputs+problem_inputs,current_test_dir=dir_of_the_current_test)
def test_checkDuplicatedColumnTitlesAreNotSuspicious_defaultOptions(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## non_problem_inputs = ["TOY.test_11.zip","TOY.test_11-two.Sample.Name.Cols.zip"] non_problem_inputs = [r'%s%s%s' % (dir_of_the_current_test,delimiter(),file) for file in non_problem_inputs] del file problem_inputs = ["TOY.test_11-dup.Factors.zip"] problem_inputs = [r'%s%s%s' % (dir_of_the_current_test,delimiter(),file) for file in problem_inputs] del file for input in non_problem_inputs+problem_inputs: cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s"' % (project_modules_to_test_dir,delimiter(),input) if not input in problem_inputs: assert input in non_problem_inputs,"input=%s???" % input assert not 'WARNING: suspicious duplicates' in self.runCmdToInspectPrintedMsgs(cmd) else: assert input in problem_inputs,"input=%s???" % input assert 'WARNING: suspicious duplicates' in self.runCmdToInspectPrintedMsgs(cmd) del cmd del input self.clean_up_if_all_checks_passed(specific_files_or_dirs_not_to_delete=non_problem_inputs+problem_inputs,current_test_dir=dir_of_the_current_test)
def test_changeXls2txt(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## import itertools from xls2txt import changeXls2txt input_file_extensions = ['xls'] all_input_files = [] for extension in input_file_extensions: extension_regex = re.compile('(\.%s$)' % extension) for input_file in glob.glob( r'%s%s*.%s' % (dir_of_the_current_test, delimiter(), extension)): no_nonblank_sheets = changeXls2txt(input_file, extension_regex) #assert 1 == no_nonblank_sheets, "%d non-blank sheets in %s!" % (no_nonblank_sheets,input_file) all_input_files.append(input_file) del input_file del extension assert 1 == len(all_input_files) all_new_files = list( itertools.chain(*[[ re.sub('(\.%s)' % extension, '.txt', input_file) for input_file in glob.glob(r'%s%s*.%s' % (dir_of_the_current_test, delimiter(), extension)) ] for extension in input_file_extensions])) self.checkStillWorks(all_input_files, all_new_files)
def idInputFiles(xls_folder): instOfzipUtils = zipUtils(delimiter()) input_files = instOfzipUtils.getRelativeFileNames(xls_folder) input_files = [ r'%s%s%s' % (xls_folder, delimiter(), file) for file in input_files if fileNameRegexesDict['input_extension'].search(file) ] del file del instOfzipUtils #non_xls_files = [file_name for file_name in input_files if not fileNameRegexesDict['input_extension'].search(file_name)] #del file_name #assert 0 == len(non_xls_files),"There are %d non-xls files in the folder %s created from the input archive." % (len(non_xls_files),xls_folder) #del non_xls_files fileType2No = {} for fileType in all_file_types: fileType2No[fileType] = len([ file_name for file_name in input_files if fileNameRegexesDict[fileType].match( file_name.split(delimiter())[-1]) ]) assert not 0 == fileType2No[ fileType], "Zero %s input files in the folder created from the input archive!" % fileType print "%d %s input files in the folder created from the input archive!" % ( fileType2No[fileType], fileType) del fileType2No return input_files #,non_xls_files #non_xls_files should just be copied across to the final zip archive without modification -see "def createFlatTxtArchive(xls_folder):"
def test_convertTOYtest11_removeComments(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## ########################## #IMPORTANT: TOY.test_11.zip JUST COPIED FROM test_11 WITHOUT EDITING; EXPECTED OUTPUT [* - Copy.txt] ALSO COPIED, BUT MANUALLY EDITED [WITHOUT CHANGING FILE NAMES!] IN ACCORDANCE WITH EXPECTED CHANGES NOW THAT "-c" FLAG IS BEING USED! ######################### cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s" -c' % (project_modules_to_test_dir,delimiter(),example_input) assert 0 == os.system(cmd) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder(re.sub('(\.zip$)','-txt_opt-c.zip',example_input)) cwd = os.getcwd() os.chdir(re.sub('(\.zip$)','-txt_opt-c',example_input)) comparisons_count = 0 for orig_file in example_output_to_compare_to: new_file = r'%s%s%s' % (os.getcwd(),delimiter(),re.sub('( - Copy\.txt)','.txt',orig_file.split(delimiter())[-1])) self.compareOriginalAndNewFiles(orig_file,new_file) os.remove(new_file) comparisons_count += 1 assert expected_no_output_files == comparisons_count, "comparisons_count=%d;expected_no_output_files=%d" % (comparisons_count,expected_no_output_files) os.chdir(cwd) os.remove(re.sub('(\.zip$)','-txt_opt-c.zip',example_input)) os.rmdir(re.sub('(\.zip$)','-txt_opt-c',example_input))
def main(): global debug global default_sig_tresh global conc_dep_modelling_input global loel_based_output_file print __doc__ ################ opts,args = getopt.getopt(sys.argv[1:],'di:s:',['dir_with_nbi_records=','default_sig_tresh=']) for o,v in opts: if '-d' == o: debug = True if '-i' == o: dir_with_nbi_records = r'%s' % re.sub('"','',v) if '-s' == o: default_sig_tresh = float(v) ################# #################### assert re.search('(\.csv$)',conc_dep_modelling_input),"conc_dep_modelling_input=%s?" % conc_dep_modelling_input conc_dep_modelling_input = re.sub('(\.csv$)','_s%f.csv' % default_sig_tresh,conc_dep_modelling_input) assert re.search('(\.csv$)',loel_based_output_file),"loel_based_output_file=%s?" % loel_based_output_file loel_based_output_file = re.sub('(\.csv$)','_s%f.csv' % default_sig_tresh,loel_based_output_file) #################### print '='*50 print 'Parsing NBI Knowledgebase records found in this directory:',dir_with_nbi_records print 'in order to generate %s' % conc_dep_raw_output_file print '(prior to generating %s and %s)' % (conc_dep_modelling_input,loel_based_output_file) start = time.clock() ok_list = [] rejects_list = [] conc_dep_titles = None for input_file in glob.glob(r'%s%snbi_*.xls' % (dir_with_nbi_records,delimiter())): print '#'*50 anNBIrecord = NBIrecord(input_file) try: anNBIrecord.extractData() conc_dep_titles = processData(anNBIrecord.dataDict,conc_dep_titles,commonTitle2Location,concDepTitle2Location,effect_specific_regex,nmLocation2ID) ok_list.append(input_file.split(delimiter())[-1]) except Exception,e: print '!'*50 print 'Problem parsing this file: ',input_file logging.exception(e) del e rejects_list.append(input_file.split(delimiter())[-1]) print '!'*50 anNBIrecord.cleanUp() del anNBIrecord print '#'*50
def createFlatTxtArchive(xls_folder, mustEditAccessionCodes, mustRemoveComments, mustMakeNanoPUZZLESspecificChanges): flat_txt_archive = xls_folder + "-txt.zip" ########### #Rename the output file if non-default options are used if mustEditAccessionCodes: flat_txt_archive = re.sub('(\.zip$)', '_opt-a.zip', flat_txt_archive) if mustRemoveComments: flat_txt_archive = re.sub('(\.zip$)', '_opt-c.zip', flat_txt_archive) if mustMakeNanoPUZZLESspecificChanges: flat_txt_archive = re.sub('(\.zip$)', '_opt-N.zip', flat_txt_archive) ########### cwd = os.getcwd() os.chdir(xls_folder) for xls_file in glob.glob('*.xls'): os.remove(xls_file) os.chdir(cwd) del cwd, xls_file instOfzipUtils = zipUtils(delimiter_value=delimiter()) instOfzipUtils.filesIntoFlatArchive(folder_name=xls_folder, zip_archive=flat_txt_archive) del instOfzipUtils
def run_all_unit_tests(): import unittest import glob all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests = report_all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests() CWD = os.getcwd() os.chdir(dir_of_this_script) test_dirs = [file_or_dir_name for file_or_dir_name in os.listdir(r'%s' % os.getcwd()) if re.match('(test_[0-9]+$)',file_or_dir_name)]# and 'test_6' == file_or_dir_name] #print 'test_dirs = ', test_dirs #sys.exit(1) ######################################## #The following code was written based on the answer provided by Stephen Cagle[http://stackoverflow.com/users/21317/stephen-cagle]: http://stackoverflow.com/questions/1732438/how-to-run-all-python-unit-tests-in-a-directory test_file_names = [] for TEST_DIR in test_dirs: sys.path.append(r'%s%s%s' % (dir_of_this_script,delimiter(),TEST_DIR)) os.chdir(TEST_DIR) test_file_names += glob.glob('test_*.py') os.chdir("..") del test_dirs del TEST_DIR test_module_names = [name[0:len(name)-3] for name in test_file_names] del name loadedTests = [unittest.defaultTestLoader.loadTestsFromName(name) for name in test_module_names] del name testsReady2Run = unittest.TestSuite(loadedTests) unittest.TextTestRunner().run(testsReady2Run) ######################################## os.chdir(CWD) cleanUp(all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests)
def main(): ################ opts,args = getopt.getopt(sys.argv[1:],'d:',['dir_with_nbi_records=']) for o,v in opts: if '-d' == o: dir_with_nbi_records = r'%s' % re.sub('"','',v) ################# print '='*50 print 'Parsing NBI Knowledgebase records found in this directory:',dir_with_nbi_records start = time.clock() ok_parse_count = 0 bad_parse_count = 0 for input_file in glob.glob(r'%s%snbi_*.xls' % (dir_with_nbi_records,delimiter())): print '#'*50 anNBIrecord = NBIrecord(input_file) try: anNBIrecord.extractData() anNBIrecord.cleanUp() ok_parse_count += 1 except Exception,err_msg: print '!'*50 print 'Problem parsing this file: ',input_file print err_msg bad_parse_count += 1 print '!'*50 del anNBIrecord print '#'*50
def createFlatTxtArchive(xls_folder,mustEditAccessionCodes,mustRemoveComments,mustMakeNanoPUZZLESspecificChanges): flat_txt_archive = xls_folder+"-txt.zip" ########### #Rename the output file if non-default options are used if mustEditAccessionCodes: flat_txt_archive = re.sub('(\.zip$)','_opt-a.zip',flat_txt_archive) if mustRemoveComments: flat_txt_archive = re.sub('(\.zip$)','_opt-c.zip',flat_txt_archive) if mustMakeNanoPUZZLESspecificChanges: flat_txt_archive = re.sub('(\.zip$)','_opt-N.zip',flat_txt_archive) ########### cwd = os.getcwd() os.chdir(xls_folder) for xls_file in glob.glob('*.xls'): os.remove(xls_file) os.chdir(cwd) del cwd,xls_file instOfzipUtils = zipUtils(delimiter_value=delimiter()) instOfzipUtils.filesIntoFlatArchive(folder_name=xls_folder,zip_archive=flat_txt_archive) del instOfzipUtils
def test_filesIntoFlatArchive(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## from zipper import zipUtils folder = r'%s%sInvestigation.blah-TXT' % (dir_of_the_current_test,delimiter()) instOfzipUtils = zipUtils(delimiter_value=delimiter()) instOfzipUtils.filesIntoFlatArchive(folder_name=folder,zip_archive=folder+".zip") del instOfzipUtils os.remove(r'%s%sInvestigation.blah-TXT.zip' % (dir_of_the_current_test,delimiter())) #when this was commented, archive looked as expected (flat archive containing a single text file)
def cleanUp(self): print 'Deleting text files used to read data into instance of NBIrecord() class' for file in self.txt_sheets: assert os.path.isfile(file), r"self.txt_sheets entry %s is not a file???" % file os.remove(file) del file print 'Deleting pyc files in the directory of this module file (os.path.abspath(__file__) will not work once .py compiled to .pyc!)' for file in glob.glob(r'%s%s*pyc' % (dir_of_this_file,delimiter())): os.remove(file)
def extractXlsFolder(xls_archive): instOfzipUtils = zipUtils(delimiter()) sub_folder_count = instOfzipUtils.archive2folder(xls_archive) assert 0 == sub_folder_count return instOfzipUtils.folder_name
def test_NanoPUZZLES_inconsistency_fix_3(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## ########################## #This unit test is designed to check (an aspect of) the "-N" option for this program i.e. the conversion of potentially inconsistent fields in datasets arising from older versions of the NanoPUZZLES ISA-TAB-Nano templates #N.B. The "....TOY.test_11... - Copy.txt" files (which were checked to make sure they were not inconsistent with the expected changes to be made via the "-N" option) were just renamed and the "TOY.test_11.zip" Excel files were updated to make sure they contained (one of) the inconsistencies that the "-N" option is designed to fix, then all such files were renamed to give "TOY.test_17.zip". #[*] In addition, given the changes to the file names, the internal contents of the files (both Excel and expected text output) needed to be edited such that "TOY.test_11" was replaced with "TOY.test_17" for consistency. ######################### ############################## #test_17: inconsistency(ies) fix which is (are) being tested: replace "Characteristics[shape]" fields with "Characteristics [shape {NPO:http://purl.bioontology.org/ontology/npo#NPO_274}]" ############################## cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s" -N' % ( project_modules_to_test_dir, delimiter(), example_input) assert 0 == os.system(cmd) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder( re.sub('(\.zip$)', '-txt_opt-N.zip', example_input)) cwd = os.getcwd() os.chdir(re.sub('(\.zip$)', '-txt_opt-N', example_input)) comparisons_count = 0 #This line would actually be useful in earlier unit tests! for orig_file in example_output_to_compare_to: new_file = r'%s%s%s' % (os.getcwd(), delimiter(), re.sub('( - Copy\.txt)', '.txt', orig_file.split(delimiter())[-1])) self.compareOriginalAndNewFiles(orig_file, new_file) os.remove(new_file) comparisons_count += 1 #This line would actually be useful in earlier unit tests! assert expected_no_output_files == comparisons_count, "comparisons_count=%d;expected_no_output_files=%d" % ( comparisons_count, expected_no_output_files ) #This line would actually be useful in earlier unit tests! os.chdir(cwd) os.remove(re.sub('(\.zip$)', '-txt_opt-N.zip', example_input)) os.rmdir(re.sub('(\.zip$)', '-txt_opt-N', example_input))
def test_convertTOYtest11_defaultOptions(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## ########################## #Observations regarding TOY.test_11.zip files which are relevant to testing capabilities of the code to fix xls errors: #s_TOY.test_11_InVitro.CB.xls => "Factor Value[culture medium]" : should be converted to "Factor Value [culture medium]" in txt version #a_TOY.test_11_PC_size_TEM.xls => "Parameter Value[Instrument Version]" : ditto "Parameter Value [Instrument Version]" #ditto => "Measurement Value[maximum(diameter)]" ditto "Measurement Value [maximum(diameter)]" #m_TiO2_TOY.test_11.xls -> Excel 2010 saved as tab delimited text => "Characteristics [Product impurities found {MEDDRA:\nhttp://purl.bioontology.org/ontology/MDR/10069178}]" i.e. this field contained a line ending inside a field - which should be removed in code created version! #ditto => Characteristics[purity {NPO: http://purl.bioontology.org/ontology/npo#NPO_1345}] : should be converted to "Characteristics [purity {NPO:http://purl.bioontology.org/ontology/npo#NPO_1345}]" ######################### cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s"' % ( project_modules_to_test_dir, delimiter(), example_input) assert 0 == os.system(cmd) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder( re.sub('(\.zip$)', '-txt.zip', example_input)) cwd = os.getcwd() os.chdir(re.sub('(\.zip$)', '-txt', example_input)) comparisons_count = 0 for orig_file in example_output_to_compare_to: new_file = r'%s%s%s' % (os.getcwd(), delimiter(), re.sub('( - Copy\.txt)', '.txt', orig_file.split(delimiter())[-1])) self.compareOriginalAndNewFiles(orig_file, new_file) os.remove(new_file) comparisons_count += 1 assert expected_no_output_files == comparisons_count, "comparisons_count=%d;expected_no_output_files=%d" % ( comparisons_count, expected_no_output_files) os.chdir(cwd) os.remove(re.sub('(\.zip$)', '-txt.zip', example_input)) os.rmdir(re.sub('(\.zip$)', '-txt', example_input))
def test_xls2txtISANANOarchive_entire_workflow(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s%sInvestigation.ID.zip"' % (project_modules_to_test_dir,delimiter(),dir_of_the_current_test,delimiter()) assert 0 == os.system(cmd) os.remove(r'%s%sInvestigation.ID-txt.zip' % (dir_of_the_current_test,delimiter()))#when commented, output files were as expected (no consideration of internal contents of txt files!)
def test_checkDuplicatedColumnTitlesAreNotSuspicious_defaultOptions(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## non_problem_inputs = [ "TOY.test_11.zip", "TOY.test_11-two.Sample.Name.Cols.zip" ] non_problem_inputs = [ r'%s%s%s' % (dir_of_the_current_test, delimiter(), file) for file in non_problem_inputs ] del file problem_inputs = ["TOY.test_11-dup.Factors.zip"] problem_inputs = [ r'%s%s%s' % (dir_of_the_current_test, delimiter(), file) for file in problem_inputs ] del file for input in non_problem_inputs + problem_inputs: cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s"' % ( project_modules_to_test_dir, delimiter(), input) if not input in problem_inputs: assert input in non_problem_inputs, "input=%s???" % input assert not 'WARNING: suspicious duplicates' in self.runCmdToInspectPrintedMsgs( cmd) else: assert input in problem_inputs, "input=%s???" % input assert 'WARNING: suspicious duplicates' in self.runCmdToInspectPrintedMsgs( cmd) del cmd del input self.clean_up_if_all_checks_passed( specific_files_or_dirs_not_to_delete=non_problem_inputs + problem_inputs, current_test_dir=dir_of_the_current_test)
def test_filesIntoFlatArchive(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## from zipper import zipUtils folder = r'%s%sInvestigation.blah-TXT' % (dir_of_the_current_test, delimiter()) instOfzipUtils = zipUtils(delimiter_value=delimiter()) instOfzipUtils.filesIntoFlatArchive(folder_name=folder, zip_archive=folder + ".zip") del instOfzipUtils os.remove( r'%s%sInvestigation.blah-TXT.zip' % (dir_of_the_current_test, delimiter()) ) #when this was commented, archive looked as expected (flat archive containing a single text file)
def test_convertTOYtest11_defaultOptions(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## ########################## #Observations regarding TOY.test_11.zip files which are relevant to testing capabilities of the code to fix xls errors: #s_TOY.test_11_InVitro.CB.xls => "Factor Value[culture medium]" : should be converted to "Factor Value [culture medium]" in txt version #a_TOY.test_11_PC_size_TEM.xls => "Parameter Value[Instrument Version]" : ditto "Parameter Value [Instrument Version]" #ditto => "Measurement Value[maximum(diameter)]" ditto "Measurement Value [maximum(diameter)]" #m_TiO2_TOY.test_11.xls -> Excel 2010 saved as tab delimited text => "Characteristics [Product impurities found {MEDDRA:\nhttp://purl.bioontology.org/ontology/MDR/10069178}]" i.e. this field contained a line ending inside a field - which should be removed in code created version! #ditto => Characteristics[purity {NPO: http://purl.bioontology.org/ontology/npo#NPO_1345}] : should be converted to "Characteristics [purity {NPO:http://purl.bioontology.org/ontology/npo#NPO_1345}]" ######################### cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s"' % (project_modules_to_test_dir,delimiter(),example_input) assert 0 == os.system(cmd) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder(re.sub('(\.zip$)','-txt.zip',example_input)) cwd = os.getcwd() os.chdir(re.sub('(\.zip$)','-txt',example_input)) comparisons_count = 0 for orig_file in example_output_to_compare_to: new_file = r'%s%s%s' % (os.getcwd(),delimiter(),re.sub('( - Copy\.txt)','.txt',orig_file.split(delimiter())[-1])) self.compareOriginalAndNewFiles(orig_file,new_file) os.remove(new_file) comparisons_count += 1 assert expected_no_output_files == comparisons_count, "comparisons_count=%d;expected_no_output_files=%d" % (comparisons_count,expected_no_output_files) os.chdir(cwd) os.remove(re.sub('(\.zip$)','-txt.zip',example_input)) os.rmdir(re.sub('(\.zip$)','-txt',example_input))
def test_NanoPUZZLES_inconsistency_fix_1(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## ########################## #This unit test is designed to check (an aspect of) the "-N" option for this program i.e. the conversion of potentially inconsistent fields in datasets arising from older versions of the NanoPUZZLES ISA-TAB-Nano templates #N.B. The "....TOY.test_11... - Copy.txt" files (which were checked to make sure they were not inconsistent with the expected changes to be made via the "-N" option) were just renamed and the "TOY.test_11.zip" Excel files were updated to make sure they contained (one of) the inconsistencies that the "-N" option is designed to fix, then all such files were renamed to give "TOY.test_15.zip". #[*] In addition, given the changes to the file names, the internal contents of the files (both Excel and expected text output) needed to be edited such that "TOY.test_11" was replaced with "TOY.test_15" for consistency. ######################### ############################## #test_15: inconsistency(ies) fix which is (are) being tested: removal of "Manufacturer supplied " prefixes for Material file "Characteristics [...]" and corresponding "Comment [...:from graph]" fields ############################## #cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s"' % (project_modules_to_test_dir,delimiter(),example_input) #[*] To check no problems with updating names from test_11 files, this test was initially run without making any changes except replacing "TOY.test_11" with "TOY.test_15" in file names and internal contents => OK [test output scrolled through to see that expected comparisons were being made]. Also, a minor error was introduced to check that self.compareOriginalAndNewFiles(orig_file,new_file) would fail as expected for one file => OK [this error was then fixed, this command was ran again with no errors and the files were further updated for the purpose of this test]. cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s" -N' % (project_modules_to_test_dir,delimiter(),example_input) assert 0 == os.system(cmd) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder(re.sub('(\.zip$)','-txt_opt-N.zip',example_input)) cwd = os.getcwd() os.chdir(re.sub('(\.zip$)','-txt_opt-N',example_input)) comparisons_count = 0 #This line would actually be useful in earlier unit tests! for orig_file in example_output_to_compare_to: new_file = r'%s%s%s' % (os.getcwd(),delimiter(),re.sub('( - Copy\.txt)','.txt',orig_file.split(delimiter())[-1])) self.compareOriginalAndNewFiles(orig_file,new_file) os.remove(new_file) comparisons_count += 1 #This line would actually be useful in earlier unit tests! assert expected_no_output_files == comparisons_count, "comparisons_count=%d;expected_no_output_files=%d" % (comparisons_count,expected_no_output_files) #This line would actually be useful in earlier unit tests! os.chdir(cwd) os.remove(re.sub('(\.zip$)','-txt_opt-N.zip',example_input)) os.rmdir(re.sub('(\.zip$)','-txt_opt-N',example_input))
def test_NanoPUZZLES_inconsistency_fix_5(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## ########################## #This unit test is designed to check (an aspect of) the "-N" option for this program i.e. the conversion of potentially inconsistent fields in datasets arising from older versions of the NanoPUZZLES ISA-TAB-Nano templates #N.B. The "....TOY.test_11... - Copy.txt" files (which were checked to make sure they were not inconsistent with the expected changes to be made via the "-N" option) were just renamed and the "TOY.test_11.zip" Excel files were updated to make sure they contained (one of) the inconsistencies that the "-N" option is designed to fix, then all such files were renamed to give "TOY.test_19.zip". #[*] In addition, given the changes to the file names, the internal contents of the files (both Excel and expected text output) needed to be edited such that "TOY.test_11" was replaced with "TOY.test_19" for consistency. ######################### ############################## #test_19: inconsistency(ies) fix which is (are) being tested: replace "nanoparticle sample" with "nanomaterial" and (correspondingly) "http://purl.bioontology.org/ontology/npo#NPO_1404" with "http://purl.bioontology.org/ontology/npo#NPO_199". In practice, this should only affect old biological Study files (name replacements), Material files (name replacements and ontology code replacements) and Investigation files (name replacements and ontology code replacements). ############################## cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s" -N' % (project_modules_to_test_dir,delimiter(),example_input) assert 0 == os.system(cmd) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder(re.sub('(\.zip$)','-txt_opt-N.zip',example_input)) cwd = os.getcwd() os.chdir(re.sub('(\.zip$)','-txt_opt-N',example_input)) comparisons_count = 0 #This line would actually be useful in earlier unit tests! for orig_file in example_output_to_compare_to: new_file = r'%s%s%s' % (os.getcwd(),delimiter(),re.sub('( - Copy\.txt)','.txt',orig_file.split(delimiter())[-1])) self.compareOriginalAndNewFiles(orig_file,new_file) os.remove(new_file) comparisons_count += 1 #This line would actually be useful in earlier unit tests! assert expected_no_output_files == comparisons_count, "comparisons_count=%d;expected_no_output_files=%d" % (comparisons_count,expected_no_output_files) #This line would actually be useful in earlier unit tests! os.chdir(cwd) os.remove(re.sub('(\.zip$)','-txt_opt-N.zip',example_input)) os.rmdir(re.sub('(\.zip$)','-txt_opt-N',example_input))
def test_convertTOYtest11_removeComments(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## ########################## #IMPORTANT: TOY.test_11.zip JUST COPIED FROM test_11 WITHOUT EDITING; EXPECTED OUTPUT [* - Copy.txt] ALSO COPIED, BUT MANUALLY EDITED [WITHOUT CHANGING FILE NAMES!] IN ACCORDANCE WITH EXPECTED CHANGES NOW THAT "-c" FLAG IS BEING USED! ######################### cmd = r'python "%s%sxls2txtISA.NANO.archive.py" -i "%s" -c' % ( project_modules_to_test_dir, delimiter(), example_input) assert 0 == os.system(cmd) instOfzipUtils = zipUtils(delimiter_value=delimiter()) sub_folder_count = instOfzipUtils.archive2folder( re.sub('(\.zip$)', '-txt_opt-c.zip', example_input)) cwd = os.getcwd() os.chdir(re.sub('(\.zip$)', '-txt_opt-c', example_input)) comparisons_count = 0 for orig_file in example_output_to_compare_to: new_file = r'%s%s%s' % (os.getcwd(), delimiter(), re.sub('( - Copy\.txt)', '.txt', orig_file.split(delimiter())[-1])) self.compareOriginalAndNewFiles(orig_file, new_file) os.remove(new_file) comparisons_count += 1 assert expected_no_output_files == comparisons_count, "comparisons_count=%d;expected_no_output_files=%d" % ( comparisons_count, expected_no_output_files) os.chdir(cwd) os.remove(re.sub('(\.zip$)', '-txt_opt-c.zip', example_input)) os.rmdir(re.sub('(\.zip$)', '-txt_opt-c', example_input))
def idInputFiles(xls_folder): instOfzipUtils = zipUtils(delimiter()) input_files = instOfzipUtils.getRelativeFileNames(xls_folder) input_files = [r'%s%s%s' % (xls_folder,delimiter(),file) for file in input_files if fileNameRegexesDict['input_extension'].search(file)] del file del instOfzipUtils #non_xls_files = [file_name for file_name in input_files if not fileNameRegexesDict['input_extension'].search(file_name)] #del file_name #assert 0 == len(non_xls_files),"There are %d non-xls files in the folder %s created from the input archive." % (len(non_xls_files),xls_folder) #del non_xls_files fileType2No = {} for fileType in all_file_types: fileType2No[fileType] = len([file_name for file_name in input_files if fileNameRegexesDict[fileType].match(file_name.split(delimiter())[-1])]) assert not 0 == fileType2No[fileType], "Zero %s input files in the folder created from the input archive!" % fileType print "%d %s input files in the folder created from the input archive!" % (fileType2No[fileType],fileType) del fileType2No return input_files#,non_xls_files #non_xls_files should just be copied across to the final zip archive without modification -see "def createFlatTxtArchive(xls_folder):"
def test_getRows_example_1(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## example_file = '%s%sexample_no_line_endings_in_col_titles.txt' % ( dir_of_the_current_test, delimiter()) expected_col_titles = [ 'A', 'B', 'C', 'Unit', 'D', 'Unit', 'C plus D', 'Unit', 'E' ] expected_data_row = [ 'M1_ID', 'M1', '10', 'nanometers', '20', 'nanometers', '30', 'nanometers', 'Hello' ] assert [expected_col_titles, expected_data_row] == getRows( example_file), "file=%s\nexpected rows=%s\nextracted rows=%s\n" % ( example_file, str([expected_col_titles, expected_data_row ]), str(getRows(example_file)))
def test_getRows_example_2(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## example_file = '%s%sexample_with_line_endings_in_quoted_col_titles.txt' % ( dir_of_the_current_test, delimiter()) expected_col_titles = [ 'A', 'B', 'C', 'Unit', 'D', 'Unit', 'C \nplus D', 'Unit', 'E' ] #It seems that csv.reader(...) converts "\r\n" inside quoted column title to "\n"![using Python 2.7.3] expected_data_row = [ 'M1_ID', 'M1', '10', 'nanometers', '20', 'nanometers', '30', 'nanometers', 'Hello' ] assert [expected_col_titles, expected_data_row] == getRows( example_file), "file=%s\nexpected rows=%s\nextracted rows=%s\n" % ( example_file, str([expected_col_titles, expected_data_row ]), str(getRows(example_file)))
def extractData_from_EMZdata_sheet(self,txt_sheet_name): assert 'NBI-EMZ Data' == self.idTxtSheetType(txt_sheet_name), "%s is NOT a NBI-EMZ Data sheet!" % txt_sheet_name print '-'*50 print 'Extracting data from this NBI-EMZ Data sheet:',txt_sheet_name rows = self.extractRows(txt_sheet_name) assert not 0 == len(rows) main_titles_row_index = self.idEMZdataMainTitlesRow(rows) max_dc_row_index = self.idEMZdataMaxDosageConcRow(main_titles_row_index,rows) hpf_24_col_index,hpf_120_col_index = self.idEMZdataHpfCols(main_titles_row_index,rows) self.dataDict[self.idTxtSheetType(txt_sheet_name)] = self.matchEMZdataRowId2ExperiVarName2Value(max_dc_row_index,main_titles_row_index,hpf_24_col_index,hpf_120_col_index,rows) #<TO DO?>:USE DOSAGE CONCENTRATION VALUES AS KEYS RATHER THAN row IDs if 'nbi_1.xls' == self.nbi_xls_file.split(delimiter())[-1]:#<TO DO?>:ASSUME THE SAME NUMBER OF DOSAGE CONCENTRATIONS ARE ALWAYS TESTED AND ALWAYS APPLY THIS TEST print 'Checking self.dataDict[%s] entry against expected number of evaluated dosage concentrations for nbi_1.xls' % self.idTxtSheetType(txt_sheet_name) assert 8 == len(self.dataDict[self.idTxtSheetType(txt_sheet_name)].keys()), "len(self.dataDict[self.idTxtSheetType(txt_sheet_name)].keys())=%d???" % len(self.dataDict[self.idTxtSheetType(txt_sheet_name)].keys()) print '-'*50
def createAllTxt(xls_folder, mustEditAccessionCodes, mustRemoveComments, mustMakeNanoPUZZLESspecificChanges): abs_name_input_files = idInputFiles(xls_folder) for xls_file in abs_name_input_files: txt_file = changeXls2txt(xls_file, fileNameRegexesDict['input_extension']) applicable_standard_file_types = [ ft for ft in all_file_types if fileNameRegexesDict[ft].match(txt_file.split(delimiter())[-1]) ] del ft assert 1 >= len(applicable_standard_file_types ), "txt_file=%s,applicable_standard_file_types=%s" % ( txt_file, str(applicable_standard_file_types)) if 1 == len(applicable_standard_file_types): current_file_type = applicable_standard_file_types[0] else: assert 0 == len( applicable_standard_file_types ), "txt_file=%s,applicable_standard_file_types=%s" % ( txt_file, str(applicable_standard_file_types)) current_file_type = 'NonStandard' del applicable_standard_file_types fixContents(input_file=txt_file, out_name=None, del_intermediates=True, file_type=current_file_type, shouldEditAccessionCodes=mustEditAccessionCodes, shouldRemoveComments=mustRemoveComments, shouldMakeNanoPUZZLESspecificChanges= mustMakeNanoPUZZLESspecificChanges)
run_tests_levels_below_top_dir = 1 this_test_levels_below_top_dir = 2 ######################### #************************ import sys,re,os,glob cwd = os.getcwd() for level in range(1,(run_tests_levels_below_top_dir+1)): os.chdir('..') #print os.getcwd() del level sys.path.append('utils') from os_ind_delimiter import delimiter from zipper import zipUtils os.chdir(cwd) del cwd project_modules_to_test_dir = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-(this_test_levels_below_top_dir+1)]) print 'project_modules_to_test_dir=',project_modules_to_test_dir sys.path.append(project_modules_to_test_dir) sys.path.append(r'%s%sutils' %(project_modules_to_test_dir,delimiter())) dir_of_the_current_test = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-1]) print 'dir_of_the_current_test=',dir_of_the_current_test import unittest #************************ sys.path.append(r'%s%stests%stest_1' % (project_modules_to_test_dir,delimiter(),delimiter())) from test_1 import test_1 import subprocess class test_8(test_1): def runCmdToInspectPrintedMsgs(self,cmd): #N.B. - see http://stackoverflow.com/questions/2502833/store-output-of-subprocess-popen-call-in-a-string output,errors = subprocess.Popen(cmd,stdout=subprocess.PIPE).communicate()
def createAllTxt(xls_folder,mustEditAccessionCodes,mustRemoveComments,mustMakeNanoPUZZLESspecificChanges): abs_name_input_files = idInputFiles(xls_folder) for xls_file in abs_name_input_files: txt_file = changeXls2txt(xls_file,fileNameRegexesDict['input_extension']) applicable_standard_file_types = [ft for ft in all_file_types if fileNameRegexesDict[ft].match(txt_file.split(delimiter())[-1])] del ft assert 1 >= len(applicable_standard_file_types),"txt_file=%s,applicable_standard_file_types=%s" % (txt_file,str(applicable_standard_file_types)) if 1 == len(applicable_standard_file_types): current_file_type = applicable_standard_file_types[0] else: assert 0 == len(applicable_standard_file_types),"txt_file=%s,applicable_standard_file_types=%s" % (txt_file,str(applicable_standard_file_types)) current_file_type = 'NonStandard' del applicable_standard_file_types fixContents(input_file=txt_file,out_name=None,del_intermediates=True,file_type=current_file_type,shouldEditAccessionCodes=mustEditAccessionCodes,shouldRemoveComments=mustRemoveComments,shouldMakeNanoPUZZLESspecificChanges=mustMakeNanoPUZZLESspecificChanges)
def report_all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests(): return [r'%s%s%s' % (dir_of_this_script,delimiter(),file_or_dir_name) for file_or_dir_name in os.listdir(r'%s' % dir_of_this_script)]
run_tests_levels_below_top_dir = 1 this_test_levels_below_top_dir = 2 ######################### #************************ import sys,re,os,glob cwd = os.getcwd() for level in range(1,(run_tests_levels_below_top_dir+1)): os.chdir('..') #print os.getcwd() del level sys.path.append('utils') from os_ind_delimiter import delimiter from zipper import zipUtils os.chdir(cwd) del cwd project_modules_to_test_dir = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-(this_test_levels_below_top_dir+1)]) print 'project_modules_to_test_dir=',project_modules_to_test_dir sys.path.append(project_modules_to_test_dir) sys.path.append(r'%s%sutils' %(project_modules_to_test_dir,delimiter())) dir_of_the_current_test = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-1]) print 'dir_of_the_current_test=',dir_of_the_current_test import unittest #************************ sys.path.append(r'%s%stests%stest_1' % (project_modules_to_test_dir,delimiter(),delimiter())) from test_1 import test_1 example_input = '%s%sTOY.test_19.zip' % (dir_of_the_current_test,delimiter()) example_output_to_compare_to = glob.glob(r'%s%s* - Copy.txt' % (dir_of_the_current_test,delimiter())) expected_no_output_files = 8 assert expected_no_output_files == len(example_output_to_compare_to) #This line would actually be useful in earlier unit tests! assert expected_no_output_files == len([file for file in example_output_to_compare_to if os.path.exists(file)]), "%s???" % str(example_output_to_compare_to) #This line would actually be useful in earlier unit tests! del file #This line would actually be useful in earlier unit tests!
project_name = 'xls2txtISA.NANO.archive' run_tests_levels_below_top_dir = 1 this_test_levels_below_top_dir = 2 ######################### #************************ import sys,re,os,glob cwd = os.getcwd() for level in range(1,(run_tests_levels_below_top_dir+1)): os.chdir('..') #print os.getcwd() del level sys.path.append('utils') from os_ind_delimiter import delimiter os.chdir(cwd) del cwd project_modules_to_test_dir = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-(this_test_levels_below_top_dir+1)]) print 'project_modules_to_test_dir=',project_modules_to_test_dir sys.path.append(project_modules_to_test_dir) sys.path.append(r'%s%sutils' %(project_modules_to_test_dir,delimiter())) dir_of_the_current_test = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-1]) print 'dir_of_the_current_test=',dir_of_the_current_test import unittest #************************ class test_4(unittest.TestCase): def test_xls2txtISANANOarchive_entire_workflow(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName
def test_getRows_example_1(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## example_file = '%s%sexample_no_line_endings_in_col_titles.txt' % (dir_of_the_current_test,delimiter()) expected_col_titles = ['A','B','C','Unit','D','Unit','C plus D','Unit','E'] expected_data_row = ['M1_ID','M1','10','nanometers','20','nanometers','30','nanometers','Hello'] assert [expected_col_titles,expected_data_row] == getRows(example_file), "file=%s\nexpected rows=%s\nextracted rows=%s\n" % (example_file,str([expected_col_titles,expected_data_row]),str(getRows(example_file)))
''' ######################### run_tests_levels_below_top_dir = 1 ######################### #************************ import sys,re,os,glob,getopt cwd = os.getcwd() for level in range(1,(run_tests_levels_below_top_dir+1)): os.chdir('..') #print os.getcwd() del level sys.path.append('utils') from os_ind_delimiter import delimiter os.chdir(cwd) del cwd dir_of_this_script = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-1]) print 'dir_of_this_script=',dir_of_this_script import unittest #************************ def report_all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests(): return [r'%s%s%s' % (dir_of_this_script,delimiter(),file_or_dir_name) for file_or_dir_name in os.listdir(r'%s' % dir_of_this_script)] def cleanUp(all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests): import glob #some unit tests may actually write intermediate temporary files to dir_of_this_script - hence make sure that these have all been deleted after all unit tests have been carried out! unit_test_temp_files_written_to_dir_of_this_script = [file_name for file_name in glob.glob(r'%s%s*' % (dir_of_this_script,delimiter())) if not file_name in all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests] for UNIT_TEST_TEMP_FILE_WRITTEN_TO_DIR_OF_THIS_SCRIPT in unit_test_temp_files_written_to_dir_of_this_script: print '='*50
run_tests_levels_below_top_dir = 1 this_test_levels_below_top_dir = 2 ######################### #************************ import sys,re,os,glob cwd = os.getcwd() for level in range(1,(run_tests_levels_below_top_dir+1)): os.chdir('..') #print os.getcwd() del level sys.path.append('utils') from os_ind_delimiter import delimiter from zipper import zipUtils os.chdir(cwd) del cwd project_modules_to_test_dir = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-(this_test_levels_below_top_dir+1)]) print 'project_modules_to_test_dir=',project_modules_to_test_dir sys.path.append(project_modules_to_test_dir) sys.path.append(r'%s%sutils' %(project_modules_to_test_dir,delimiter())) dir_of_the_current_test = delimiter().join(os.path.abspath(__file__).split(delimiter())[:-1]) print 'dir_of_the_current_test=',dir_of_the_current_test import unittest #************************ sys.path.append(r'%s%stests%stest_1' % (project_modules_to_test_dir,delimiter(),delimiter())) from test_1 import test_1 example_input = '%s%sTOY.test_15.zip' % (dir_of_the_current_test,delimiter()) example_output_to_compare_to = glob.glob(r'%s%s* - Copy.txt' % (dir_of_the_current_test,delimiter())) expected_no_output_files = 8 assert expected_no_output_files == len(example_output_to_compare_to) #This line would actually be useful in earlier unit tests! assert expected_no_output_files == len([file for file in example_output_to_compare_to if os.path.exists(file)]), "%s???" % str(example_output_to_compare_to) #This line would actually be useful in earlier unit tests! del file #This line would actually be useful in earlier unit tests!
def test_getRows_example_2(self): ############################## print 'Running unittests for this project: ', project_name print 'Running this unittest: ', self._testMethodName ############################## example_file = '%s%sexample_with_line_endings_in_quoted_col_titles.txt' % (dir_of_the_current_test,delimiter()) expected_col_titles = ['A','B','C','Unit','D','Unit','C \nplus D','Unit','E'] #It seems that csv.reader(...) converts "\r\n" inside quoted column title to "\n"![using Python 2.7.3] expected_data_row = ['M1_ID','M1','10','nanometers','20','nanometers','30','nanometers','Hello'] assert [expected_col_titles,expected_data_row] == getRows(example_file), "file=%s\nexpected rows=%s\nextracted rows=%s\n" % (example_file,str([expected_col_titles,expected_data_row]),str(getRows(example_file)))
def cleanUp(all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests): import glob #some unit tests may actually write intermediate temporary files to dir_of_this_script - hence make sure that these have all been deleted after all unit tests have been carried out! unit_test_temp_files_written_to_dir_of_this_script = [file_name for file_name in glob.glob(r'%s%s*' % (dir_of_this_script,delimiter())) if not file_name in all_files_and_dirs_in_dir_of_this_script_prior_to_running_unit_tests] for UNIT_TEST_TEMP_FILE_WRITTEN_TO_DIR_OF_THIS_SCRIPT in unit_test_temp_files_written_to_dir_of_this_script: print '='*50 print 'Removing this temporary file written to the directory of run_tests.py: ', UNIT_TEST_TEMP_FILE_WRITTEN_TO_DIR_OF_THIS_SCRIPT os.remove(UNIT_TEST_TEMP_FILE_WRITTEN_TO_DIR_OF_THIS_SCRIPT) print '='*50