Commit 816acf9e83d81aea66fa254e2c2d2ff0f470011a

Authored by Jürgen Knödlseder
1 parent 49470333

Add csobsinfo script unit test.

cscripts/Makefile.am
@@ -32,6 +32,7 @@ testscripts = tests/__init__.py \ @@ -32,6 +32,7 @@ testscripts = tests/__init__.py \
32 ../test/test_csmodelmerge.py \ 32 ../test/test_csmodelmerge.py \
33 ../test/test_csobs2caldb.py \ 33 ../test/test_csobs2caldb.py \
34 ../test/test_csobsdef.py \ 34 ../test/test_csobsdef.py \
  35 + ../test/test_csobsinfo.py \
35 ../test/test_csfindobs.py \ 36 ../test/test_csfindobs.py \
36 ../test/test_csiactcopy.py \ 37 ../test/test_csiactcopy.py \
37 ../test/test_csiactdata.py \ 38 ../test/test_csiactdata.py \
test/test_csmodelinfo.py
@@ -90,7 +90,7 @@ class Test(gammalib.GPythonTestSuite): @@ -90,7 +90,7 @@ class Test(gammalib.GPythonTestSuite):
90 "Successful csmodelinfo execution on command line") 90 "Successful csmodelinfo execution on command line")
91 91
92 # Check region file 92 # Check region file
93 - self._check_region_file("model_cmd1.reg") 93 + self._check_ds9_file("model_cmd1.reg")
94 94
95 # Setup csmodelinfo command 95 # Setup csmodelinfo command
96 cmd = csmodelinfo+' inmodel="model_that_does_not_exist.xml"'+ \ 96 cmd = csmodelinfo+' inmodel="model_that_does_not_exist.xml"'+ \
@@ -118,7 +118,7 @@ class Test(gammalib.GPythonTestSuite): @@ -118,7 +118,7 @@ class Test(gammalib.GPythonTestSuite):
118 # Set-up csmodelinfo 118 # Set-up csmodelinfo
119 modelinfo = cscripts.csmodelinfo() 119 modelinfo = cscripts.csmodelinfo()
120 modelinfo["inmodel"] = self._model_name 120 modelinfo["inmodel"] = self._model_name
121 - modelinfo["ds9file"] = "model_py2.reg" 121 + modelinfo["ds9file"] = "csmodelinfo_py1.reg"
122 modelinfo["logfile"] = "csmodelinfo_py1.log" 122 modelinfo["logfile"] = "csmodelinfo_py1.log"
123 modelinfo["chatter"] = 2 123 modelinfo["chatter"] = 2
124 124
@@ -128,13 +128,26 @@ class Test(gammalib.GPythonTestSuite): @@ -128,13 +128,26 @@ class Test(gammalib.GPythonTestSuite):
128 modelinfo.save() 128 modelinfo.save()
129 129
130 # Check region file 130 # Check region file
131 - self._check_region_file("model_py2.reg") 131 + self._check_ds9_file("csmodelinfo_py1.reg")
  132 +
  133 + # Set-up csmodelinfo
  134 + modelinfo = cscripts.csmodelinfo()
  135 + modelinfo["inmodel"] = self._model_name
  136 + modelinfo["ds9file"] = "csmodelinfo_py2.reg"
  137 + modelinfo["logfile"] = "csmodelinfo_py2.log"
  138 + modelinfo["chatter"] = 3
  139 +
  140 + # Execute csmodelinfo script
  141 + modelinfo.execute()
  142 +
  143 + # Check region file
  144 + self._check_ds9_file("csmodelinfo_py2.reg")
132 145
133 # Return 146 # Return
134 return 147 return
135 148
136 # Check region file 149 # Check region file
137 - def _check_region_file(self, filename): 150 + def _check_ds9_file(self, filename):
138 """ 151 """
139 Check region file. 152 Check region file.
140 """ 153 """
test/test_csobsinfo.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the csobsinfo script.
  4 +#
  5 +# Copyright (C) 2016 Juergen Knoedlseder
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
  21 +import os
  22 +import gammalib
  23 +import cscripts
  24 +
  25 +
  26 +# =============================== #
  27 +# Test class for csobsinfo script #
  28 +# =============================== #
  29 +class Test(gammalib.GPythonTestSuite):
  30 + """
  31 + Test class for csobsinfo script.
  32 +
  33 + This test class makes unit tests for the csobsinfo script by using it
  34 + from the command line and from Python.
  35 + """
  36 +
  37 + # Constructor
  38 + def __init__(self):
  39 + """
  40 + Constructor.
  41 + """
  42 + # Call base class constructor
  43 + gammalib.GPythonTestSuite.__init__(self)
  44 +
  45 + # Return
  46 + return
  47 +
  48 + # Set test functions
  49 + def set(self):
  50 + """
  51 + Set all test functions.
  52 + """
  53 + # Set test name
  54 + self.name("csobsinfo")
  55 +
  56 + # Append tests
  57 + self.append(self._test_cmd, "Test csobsinfo on command line")
  58 + self.append(self._test_python, "Test csobsinfo from Python")
  59 +
  60 + # Return
  61 + return
  62 +
  63 + # Test csobsinfo on command line
  64 + def _test_cmd(self):
  65 + """
  66 + Test csobsinfo on the command line.
  67 + """
  68 + # Kluge to set the command (installed version has no README file)
  69 + if os.path.isfile("README"):
  70 + csobsinfo = "../cscripts/csobsinfo.py"
  71 + else:
  72 + csobsinfo = "csobsinfo"
  73 +
  74 + # Setup csobsinfo command
  75 + cmd = csobsinfo+' inobs="data/obs_unbinned.xml"'+ \
  76 + ' ds9file="csobsinfo_cmd1.reg"'+ \
  77 + ' offset=yes ra=83.63 dec=22.01'+ \
  78 + ' logfile="csobsinfo_cmd1.log" chatter=1'
  79 +
  80 + # Execute csobsinfo, make sure we catch any exception
  81 + try:
  82 + rc = os.system(cmd+" >/dev/null 2>&1")
  83 + except:
  84 + pass
  85 +
  86 + # Check if execution was successful
  87 + self.test_assert(rc == 0,
  88 + "Successful csobsinfo execution on command line")
  89 +
  90 + # Check DS9 file
  91 + self._check_ds9_file("csobsinfo_cmd1.reg")
  92 +
  93 + # Setup csobsinfo command
  94 + cmd = csobsinfo+' inobs="obs_definition_that_does_not_exist.xml"'+ \
  95 + ' ds9file="csobsinfo_cmd2.reg"'+ \
  96 + ' offset=yes ra=83.63 dec=22.01'+ \
  97 + ' logfile="csobsinfo_cmd2.log"'
  98 +
  99 + # Execute csobsinfo, make sure we catch any exception
  100 + try:
  101 + rc = os.system(cmd+" >/dev/null 2>&1")
  102 + except:
  103 + pass
  104 +
  105 + # Check if execution failed
  106 + self.test_assert(rc != 0,
  107 + "Failure of csobsinfo execution on command line")
  108 +
  109 + # Return
  110 + return
  111 +
  112 + # Test csobsinfo from Python
  113 + def _test_python(self):
  114 + """
  115 + Test csobsinfo from Python.
  116 + """
  117 + # Set-up csobsinfo
  118 + obsinfo = cscripts.csobsinfo()
  119 + obsinfo["inobs"] = "data/obs_unbinned.xml"
  120 + obsinfo["ds9file"] = "csobsinfo_py1.reg"
  121 + obsinfo["offset"] = True
  122 + obsinfo["ra"] = 83.63
  123 + obsinfo["dec"] = 22.01
  124 + obsinfo["logfile"] = "csobsinfo_py1.log"
  125 + obsinfo["chatter"] = 2
  126 +
  127 + # Run csobsinfo script and save XML file
  128 + obsinfo.logFileOpen() # Make sure we get a log file
  129 + obsinfo.run()
  130 + obsinfo.save()
  131 +
  132 + # Check DS9 file
  133 + self._check_ds9_file("csobsinfo_py1.reg")
  134 +
  135 + # Set-up csobsinfo
  136 + obsinfo = cscripts.csobsinfo()
  137 + obsinfo["inobs"] = "data/obs_unbinned.xml"
  138 + obsinfo["ds9file"] = "csobsinfo_py2.reg"
  139 + obsinfo["offset"] = True
  140 + obsinfo["ra"] = 83.63
  141 + obsinfo["dec"] = 22.01
  142 + obsinfo["logfile"] = "csobsinfo_py2.log"
  143 + obsinfo["chatter"] = 3
  144 +
  145 + # Execute csobsinfo script
  146 + obsinfo.execute()
  147 +
  148 + # Check DS9 file
  149 + self._check_ds9_file("csobsinfo_py2.reg")
  150 +
  151 + # Return
  152 + return
  153 +
  154 + # Check DS9 file
  155 + def _check_ds9_file(self, filename):
  156 + """
  157 + Check DS9 file.
  158 + """
  159 + # Open file
  160 + f = open(filename,"r")
  161 +
  162 + # Expect "fk5" in first line
  163 + line = f.readline().strip('\n')
  164 + self.test_assert(line == 'fk5',
  165 + 'Check for "fk5" in first line')
  166 +
  167 + # Expect "fk5" in first line
  168 + line = f.readline().strip('\n')
  169 + self.test_assert(line[0:6] == 'point(',
  170 + 'Check for "point(" in second line')
  171 +
  172 + # Close file
  173 + f.close()
  174 +
  175 + # Return
  176 + return
test/test_python_cscripts.py
@@ -29,6 +29,7 @@ import test_csmodelinfo @@ -29,6 +29,7 @@ import test_csmodelinfo
29 import test_csmodelmerge 29 import test_csmodelmerge
30 import test_csobs2caldb 30 import test_csobs2caldb
31 import test_csobsdef 31 import test_csobsdef
  32 +import test_csobsinfo
32 import test_csfindobs 33 import test_csfindobs
33 import test_csiactcopy 34 import test_csiactcopy
34 import test_csiactdata 35 import test_csiactdata
@@ -87,6 +88,7 @@ def test(installed=False, debug=False): @@ -87,6 +88,7 @@ def test(installed=False, debug=False):
87 suite_csmodelmerge = test_csmodelmerge.Test() 88 suite_csmodelmerge = test_csmodelmerge.Test()
88 suite_csobs2caldb = test_csobs2caldb.Test() 89 suite_csobs2caldb = test_csobs2caldb.Test()
89 suite_csobsdef = test_csobsdef.Test() 90 suite_csobsdef = test_csobsdef.Test()
  91 + suite_csobsinfo = test_csobsinfo.Test()
90 92
91 # Setup unit tests 93 # Setup unit tests
92 suite_cscaldb.set() 94 suite_cscaldb.set()
@@ -95,6 +97,7 @@ def test(installed=False, debug=False): @@ -95,6 +97,7 @@ def test(installed=False, debug=False):
95 suite_csmodelmerge.set() 97 suite_csmodelmerge.set()
96 suite_csobs2caldb.set() 98 suite_csobs2caldb.set()
97 suite_csobsdef.set() 99 suite_csobsdef.set()
  100 + suite_csobsinfo.set()
98 101
99 # Append tests to container 102 # Append tests to container
100 suites.append(suite_cscaldb) 103 suites.append(suite_cscaldb)
@@ -103,6 +106,7 @@ def test(installed=False, debug=False): @@ -103,6 +106,7 @@ def test(installed=False, debug=False):
103 suites.append(suite_csmodelmerge) 106 suites.append(suite_csmodelmerge)
104 suites.append(suite_csobs2caldb) 107 suites.append(suite_csobs2caldb)
105 suites.append(suite_csobsdef) 108 suites.append(suite_csobsdef)
  109 + suites.append(suite_csobsinfo)
106 110
107 # Append tests for Python 2.6+ (the IACT cscripts depend on the json 111 # Append tests for Python 2.6+ (the IACT cscripts depend on the json
108 # module which is only available since Python 2.6+ 112 # module which is only available since Python 2.6+