Go to the documentation of this file.00001 import os
00002 import sys
00003 import shutil
00004 from __main__ import default
00005 from tasks import *
00006 from taskinit import *
00007 import unittest
00008
00009 import listing
00010
00011 from sdlist import sdlist
00012
00013 class sdlist_test(unittest.TestCase):
00014 """
00015 Unit test for task sdlist.
00016
00017 The list of tests:
00018 test00 --- default parameters (raises an error)
00019 test01 --- valid input filename and save output as a text file
00020 test02 --- test scanaverage=True
00021 test03 --- test overwrite=True
00022 """
00023
00024 datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdlist/'
00025
00026 infile1 = 'OrionS_rawACSmod_cal2123.asap'
00027 infile2 = 'OrionS_rawACSmod'
00028 infiles = [infile1, infile2]
00029 outroot = 'sdlist_test'
00030 refroot = "refsdlist"
00031
00032 def setUp(self):
00033 for infile in self.infiles:
00034 if os.path.exists(infile):
00035 shutil.rmtree(infile)
00036 shutil.copytree(self.datapath+infile, infile)
00037
00038 default(sdlist)
00039
00040
00041 def tearDown(self):
00042 for infile in self.infiles:
00043 if (os.path.exists(infile)):
00044 shutil.rmtree(infile)
00045
00046 def _compareOutFile(self,out,reference):
00047 self.assertTrue(os.path.exists(out))
00048 self.assertTrue(os.path.exists(reference),
00049 msg="Reference file doesn't exist: "+reference)
00050 self.assertTrue(listing.compare(out,reference),
00051 'New and reference files are different. %s != %s. '
00052 %(out,reference))
00053
00054
00055 def test00(self):
00056 """Test 0: Default parameters"""
00057 result=sdlist()
00058 self.assertFalse(result)
00059
00060 def test01(self):
00061 """Test 1: Save output to an ascii file"""
00062 tid = "01"
00063 infile = self.infile1
00064 outfile = self.outroot+tid+".out"
00065 result = sdlist(infile=infile,outfile=outfile)
00066 self.assertEqual(result,None,
00067 msg="The task returned '"+str(result)+"' instead of None")
00068 self._compareOutFile(outfile,self.datapath+self.refroot+tid+".txt")
00069
00070 def test02(self):
00071 """Test 2: Test scanaverage=True"""
00072 tid = "02"
00073 infile = self.infile1
00074 outfile = self.outroot+tid+".out"
00075 result = sdlist(infile=infile,scanaverage=True,outfile=outfile)
00076 self.assertEqual(result,None,
00077 msg="The task returned '"+str(result)+"' instead of None")
00078 self._compareOutFile(outfile,self.datapath+self.refroot+tid+".txt")
00079
00080 def test03(self):
00081 """Test 3: Test overwrite=True"""
00082 tid = "03"
00083 infile = self.infile2
00084 outfile = self.outroot+tid+".out"
00085 result = sdlist(infile=infile,outfile=outfile)
00086 self.assertEqual(result,None,
00087 msg="The task returned '"+str(result)+"' instead of None")
00088 self.assertTrue(os.path.exists(outfile),
00089 msg="Output file doesn't exist after the 1st run.")
00090
00091 infile = self.infile1
00092 outfile = self.outroot+tid+".out"
00093 result = sdlist(infile=infile,outfile=outfile,overwrite=True)
00094 self.assertEqual(result,None,
00095 msg="The task returned '"+str(result)+"' instead of None")
00096 self._compareOutFile(outfile,self.datapath+self.refroot+tid+".txt")
00097
00098 def test04(self):
00099 """Test 4: Test overwrite=False"""
00100 tid = "04"
00101 infile = self.infile2
00102 outfile = self.outroot+tid+".out"
00103 result = sdlist(infile=infile,outfile=outfile)
00104 self.assertEqual(result,None,
00105 msg="The task returned '"+str(result)+"' instead of None")
00106 self.assertTrue(os.path.exists(outfile),
00107 msg="Output file doesn't exist after the 1st run.")
00108
00109 infile = self.infile1
00110 outfile = self.outroot+tid+".out"
00111 try:
00112 result = sdlist(infile=infile,outfile=outfile,overwrite=False)
00113 self.assertTrue(False,
00114 msg='The task must throw exception')
00115 except Exception, e:
00116 pos=str(e).find('Output file \'%s\' exists.'%(outfile))
00117 self.assertNotEqual(pos,-1,
00118 msg='Unexpected exception was thrown: %s'%(str(e)))
00119
00120 def suite():
00121 return [sdlist_test]