casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_plotms.py
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 import sha
00009 import time
00010 
00011 # Path for data
00012 datapath = os.environ.get('CASAPATH').split()[0] + "/data/regression/unittest/plotms/"
00013 
00014 # Pick up alternative data directory to run tests on MMSs
00015 testmms = False
00016 if os.environ.has_key('TEST_DATADIR'):   
00017     DATADIR = str(os.environ.get('TEST_DATADIR'))+'/plotms/'
00018     if os.path.isdir(DATADIR):
00019         testmms = True
00020         datapath = DATADIR
00021 
00022 print 'plotms tests will use data from '+datapath         
00023 
00024 class test_base(unittest.TestCase):
00025 
00026     ms = "pm_ngc5921.ms"
00027     if testmms:
00028         ms = "pm_ngc5921.mms"
00029     plotfile_jpg = "myplot.jpg"
00030     display = os.environ.get("DISPLAY")
00031     plotfile_hash = ""
00032     plotfile_minsize = 120000
00033 
00034     def _cleanUp(self):
00035         if os.path.exists(self.ms):
00036             shutil.rmtree(self.ms)
00037         if os.path.exists(self.plotfile_jpg):
00038             os.remove(self.plotfile_jpg)
00039     
00040     def setUpdata(self):
00041         if not self.display.startswith(':'):
00042             self.res = None
00043             default(plotms)
00044             self._cleanUp()
00045             shutil.copytree(datapath+self.ms, self.ms, symlinks=True)            
00046 
00047     def tearDowndata(self):
00048         if not self.display.startswith(':'):
00049             self._cleanUp()
00050             pm.setPlotMSFilename("")
00051 
00052     def _checkPlotFile(self):
00053         self.assertTrue(os.path.isfile(self.plotfile_jpg))
00054         self.assertTrue(os.path.getsize(self.plotfile_jpg) > self.plotfile_minsize)
00055         if(self.plotfile_hash):
00056             self.assertEqual(
00057                 sha.new(open(self.plotfile_jpg, 'r').read()).hexdigest(),
00058                 self.plotfile_hash
00059             )
00060         else:
00061             # store to check against following test results
00062             self.plotfile_hash = sha.new(open(self.plotfile_jpg, 'r').read()).hexdigest()
00063             
00064     def _waitForFile(self, file, seconds):
00065         for i in range(0,seconds):
00066             if (os.path.isfile(file)):
00067                 return
00068             time.sleep(1)
00069         
00070 class plotms_test1(test_base):
00071 
00072     def setUp(self):
00073         self.setUpdata()
00074         
00075     def tearDown(self):
00076         self.tearDowndata()
00077         
00078     # This test does not work. It seems that the pm tool 
00079     # does not draw anything on the GUI inside this script.
00080     # It works manually inside casapy. ??????????
00081 #    def test000(self):
00082 #        '''Plotms 0: Write a jpg file using the pm tool'''
00083 #        self.assertFalse(self.display.startswith(':'),'DISPLAY not set, cannot run test')
00084 #        pm.setPlotMSFilename(self.ms)
00085 #        time.sleep(5)
00086 #        pm.show()
00087 #        time.sleep(5)
00088 #        while (pm.isDrawing()):
00089 #            time.sleep(0.5)
00090 #        pm.save(self.plotfile_jpg, format='jpg')
00091 #        self._waitForFile(self.plotfile_jpg, 10)
00092 #        self._checkPlotFile()
00093         
00094     def test001(self):
00095         '''Plotms 1: Write a jpg file using the plotms task'''
00096         self.assertFalse(self.display.startswith(':'),'DISPLAY not set, cannot run test')
00097         time.sleep(5)
00098         self.res = plotms(vis=self.ms, plotfile=self.plotfile_jpg, format='jpg')
00099         self.assertTrue(self.res)
00100         self._waitForFile(self.plotfile_jpg, 10)
00101         self.assertTrue(os.path.exists(self.plotfile_jpg), 'Plot was not created')
00102         self._checkPlotFile()
00103         
00104     def test002(self):
00105         '''Plotms 2: Check overwrite=False functionality works'''
00106         self.assertFalse(self.display.startswith(':'),'DISPLAY not set, cannot run test')
00107         time.sleep(5)
00108         self.res = plotms(vis=self.ms, plotfile=self.plotfile_jpg, format='jpg')
00109         self.assertTrue(self.res)
00110         self._waitForFile(self.plotfile_jpg, 10)
00111         self._checkPlotFile()
00112 
00113         self.res = plotms(vis=self.ms, plotfile=self.plotfile_jpg, format='jpg')
00114         self.assertFalse(self.res)
00115 
00116 
00117     def test003(self):
00118         '''Plotms 3: Plot using data selection'''
00119         self.assertFalse(self.display.startswith(':'),'DISPLAY not set, cannot run test')
00120         time.sleep(5)
00121         self.res = plotms(vis=self.ms, scan='2,4', plotfile=self.plotfile_jpg, format='jpg')
00122         self.assertTrue(self.res)
00123         self._waitForFile(self.plotfile_jpg, 5)
00124         self.assertTrue(os.path.isfile(self.plotfile_jpg))
00125         self.assertTrue(os.path.getsize(self.plotfile_jpg) > 50000)
00126         
00127         
00128 # This test has been separated into another class so that
00129 # it runs as the last test. If it runs before the other tests,
00130 # an exception from plotms such as:
00131 # terminate called after throwing an instance of 'casa::AipsError'
00132 #  what():  CountedPtr: null dereference error
00133 # will kill the GUI and will prevent the other files from working. 
00134 # To be fixed in plotms. ?????
00135 class plotms_test2(test_base):
00136 
00137     def setUp(self):
00138         self.setUpdata()
00139         
00140     def tearDown(self):
00141         self.tearDowndata()
00142 
00143     def test004(self):
00144         '''Plotms 4: Check overwrite=True functionality works'''
00145         self.assertFalse(self.display.startswith(':'),'DISPLAY not set, cannot run test')
00146         time.sleep(5)
00147         self.res = plotms(vis=self.ms, plotfile=self.plotfile_jpg, format='jpg')
00148         self.assertTrue(self.res)
00149         self._waitForFile(self.plotfile_jpg, 10)
00150         self._checkPlotFile()
00151 
00152         time.sleep(5)
00153         self.res = plotms(vis=self.ms, plotfile=self.plotfile_jpg, format='jpg', overwrite=True)
00154         self.assertTrue(self.res)
00155         self._waitForFile(self.plotfile_jpg, 10)
00156         self._checkPlotFile()
00157 
00158 
00159 def suite():
00160     return [plotms_test1, plotms_test2]
00161