Commit cba332cde83037cf63780c8eba32fa7f00bfec70

Authored by Jürgen Knödlseder
1 parent 5cd517cf

Add unit test for csfindobs script.

cscripts/Makefile.am
... ... @@ -27,6 +27,7 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \
27 27 testscripts = tests/__init__.py \
28 28 ../test/test_python_cscripts.py \
29 29 ../test/test_cscaldb.py \
  30 + ../test/test_csfindobs.py \
30 31 ../test/test_csobs2caldb.py \
31 32 ../test/test_cslightcrv.py
32 33  
... ... @@ -37,6 +38,11 @@ testdata = ../test/data/crab.xml \
37 38 ../test/data/crab_cntmap.fits.gz \
38 39 ../test/data/exclusion.reg
39 40  
  41 +# Test data to be distributed in $(pyexecdir)/cscripts/tests/iactdata
  42 +testiactdata = ../test/iactdata/hdu-index.fits.gz \
  43 + ../test/iactdata/master.json \
  44 + ../test/iactdata/obs-index.fits.gz
  45 +
40 46 # Test IRFs to be distributed in $(pyexecdir)/cscripts/tests/irf
41 47 testirf = ../test/irf/irf_file.fits.gz
42 48  
... ... @@ -52,6 +58,8 @@ testscriptsdir = $(pyexecdir)/cscripts/tests
52 58 testscripts_DATA = $(testscripts)
53 59 testdatadir = $(pyexecdir)/cscripts/tests/data
54 60 testdata_DATA = $(testdata)
  61 +testiactdatadir = $(pyexecdir)/cscripts/tests/iactdata
  62 +testiactdata_DATA = $(testiactdata)
55 63 testirfdir = $(pyexecdir)/cscripts/tests/irf
56 64 testirf_DATA = $(testirf)
57 65  
... ...
test/test_cscaldb.py
... ... @@ -54,11 +54,39 @@ class Test(gammalib.GPythonTestSuite):
54 54 self.name("cscaldb")
55 55  
56 56 # Append tests
  57 + self.append(self._test_cmd, "Test cscaldb on command line")
57 58 self.append(self._test_python, "Test cscaldb from Python")
58 59  
59 60 # Return
60 61 return
61 62  
  63 + # Test cscaldb on command line
  64 + def _test_cmd(self):
  65 + """
  66 + Test cscaldb on the command line.
  67 + """
  68 + # Kluge to set the command (installed version has no README file)
  69 + if os.path.isfile("README"):
  70 + cscaldb = "../cscripts/cscaldb.py"
  71 + else:
  72 + cscaldb = "csfindobs"
  73 +
  74 + # Setup cscaldb command
  75 + cmd = cscaldb+' logfile="cscaldb_cmd1.log" chatter=1'
  76 +
  77 + # Execute cscaldb, make sure we catch any exception
  78 + try:
  79 + rc = os.system(cmd+" >/dev/null 2>&1")
  80 + except:
  81 + pass
  82 +
  83 + # Check if execution was successful
  84 + self.test_assert(rc == 0,
  85 + "Successful cscaldb execution on command line")
  86 +
  87 + # Return
  88 + return
  89 +
62 90 # Test cscaldb from Python
63 91 def _test_python(self):
64 92 """
... ... @@ -66,15 +94,12 @@ class Test(gammalib.GPythonTestSuite):
66 94 """
67 95 # Set-up cscaldb
68 96 caldb = cscripts.cscaldb()
  97 + caldb["logfile"] = "cscaldb_py1.log"
  98 + caldb["chatter"] = 2
69 99  
70 100 # Run script
71   - self.test_try("Run cscaldb")
72   - try:
73   - caldb.run()
74   - self.test_try_success()
75   - except:
76   - msg = "Exception occured in cscaldb."
77   - self.test_try_failure(msg)
  101 + caldb.logFileOpen() # Make sure we get a log file
  102 + caldb.run()
78 103  
79 104 # Return
80 105 return
... ...
test/test_csfindobs.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the csfindobs 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 csfindobs script #
  28 +# ================================ #
  29 +class Test(gammalib.GPythonTestSuite):
  30 + """
  31 + Test class for csfindobs script.
  32 +
  33 + This test class makes unit tests for the csfindobs 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("csfindobs")
  55 +
  56 + # Append tests
  57 + self.append(self._test_cmd, "Test csfindobs on command line")
  58 + self.append(self._test_python, "Test csfindobs from Python")
  59 +
  60 + # Return
  61 + return
  62 +
  63 + # Test csfindobs on command line
  64 + def _test_cmd(self):
  65 + """
  66 + Test csfindobs on the command line.
  67 + """
  68 + # Kluge to set the command (installed version has no README file)
  69 + if os.path.isfile("README"):
  70 + csfindobs = "../cscripts/csfindobs.py"
  71 + else:
  72 + csfindobs = "csfindobs"
  73 +
  74 + # Setup csfindobs command
  75 + cmd = csfindobs+' datapath="iactdata/"'+ \
  76 + ' prodname="unit-test"'+ \
  77 + ' ra=83.63 dec=22.01 rad=1.0'+ \
  78 + ' outfile="runlist_cmd1.dat"'+ \
  79 + ' logfile="csfindobs_cmd1.log" chatter=1'
  80 +
  81 + # Execute csfindobs, make sure we catch any exception
  82 + try:
  83 + rc = os.system(cmd+" >/dev/null 2>&1")
  84 + except:
  85 + pass
  86 +
  87 + # Check if execution was successful
  88 + self.test_assert(rc == 0,
  89 + "Successful csfindobs execution on command line")
  90 +
  91 + # Check run list
  92 + self._check_runlist("runlist_cmd1.dat")
  93 +
  94 + # Setup csfindobs command
  95 + cmd = csfindobs+' datapath="path_that_does_not_exist"'+ \
  96 + ' prodname="unit-test"'+ \
  97 + ' ra=83.63 dec=22.01 rad=1.0'+ \
  98 + ' outfile="runlist_cmd2.dat"'+ \
  99 + ' logfile="csfindobs_cmd2.log"'
  100 +
  101 + # Execute csfindobs, make sure we catch any exception
  102 + try:
  103 + rc = os.system(cmd+" >/dev/null 2>&1")
  104 + except:
  105 + pass
  106 +
  107 + # Check if execution failed
  108 + self.test_assert(rc != 0,
  109 + "Failure of csfindobs execution on command line")
  110 +
  111 + # Return
  112 + return
  113 +
  114 + # Test csfindobs from Python
  115 + def _test_python(self):
  116 + """
  117 + Test csfindobs from Python.
  118 + """
  119 + # Set-up csfindobs
  120 + findobs = cscripts.csfindobs()
  121 + findobs["datapath"] = "iactdata/"
  122 + findobs["prodname"] = "unit-test"
  123 + findobs["outfile"] = "runlist_py1.dat"
  124 + findobs["ra"] = 83.63
  125 + findobs["dec"] = 22.01
  126 + findobs["rad"] = 1.0
  127 + findobs["logfile"] = "csfindobs_py1.log"
  128 + findobs["chatter"] = 2
  129 +
  130 + # Run csfindobs script and save run list
  131 + findobs.logFileOpen() # Make sure we get a log file
  132 + findobs.run()
  133 + findobs.save()
  134 +
  135 + # Check run list
  136 + self._check_runlist("runlist_py1.dat")
  137 +
  138 + # Return
  139 + return
  140 +
  141 + # Check run list result file
  142 + def _check_runlist(self, filename):
  143 + """
  144 + Check run list file.
  145 + """
  146 + # Open run list file as CSV file
  147 + runlist = gammalib.GCsv(filename)
  148 +
  149 + # Check dimensions
  150 + self.test_value(runlist.ncols(), 1,
  151 + 'Check for single column in runlist file')
  152 + self.test_value(runlist.nrows(), 1,
  153 + 'Check for single row in runlist file')
  154 + self.test_assert(runlist[0,0] == '0',
  155 + 'Check for value "0" in runlist file')
  156 +
  157 + # Return
  158 + return
... ...
test/test_cslightcrv.py
... ... @@ -81,7 +81,8 @@ class Test(gammalib.GPythonTestSuite):
81 81 ' srcname="Crab" caldb="prod2" irf="South_0.5h"'+ \
82 82 ' tbinalg="LIN" tmin=51544.50 tmax=51544.53'+ \
83 83 ' tbins=3 enumbins=0 emin=0.1 emax=100.0'+ \
84   - ' outfile="lightcurve_cmd1.fits"'
  84 + ' outfile="lightcurve_cmd1.fits"'+ \
  85 + ' logfile="cslightcrv_cmd1.log" chatter=1'
85 86  
86 87 # Execute cslightcrv, make sure we catch any exception
87 88 try:
... ... @@ -102,7 +103,8 @@ class Test(gammalib.GPythonTestSuite):
102 103 ' srcname="Crab" caldb="prod2" irf="South_0.5h"'+ \
103 104 ' tbinalg="LIN" tmin=51544.50 tmax=51544.53'+ \
104 105 ' tbins=3 enumbins=0 emin=0.1 emax=100.0'+ \
105   - ' outfile="lightcurve_cmd1.fits"'
  106 + ' outfile="lightcurve_cmd1.fits"'+ \
  107 + ' logfile="cslightcrv_cmd2.log"'
106 108  
107 109 # Execute cslightcrv, make sure we catch any exception
108 110 try:
... ... @@ -137,8 +139,11 @@ class Test(gammalib.GPythonTestSuite):
137 139 lcrv["emin"] = 0.1
138 140 lcrv["emax"] = 100.0
139 141 lcrv["outfile"] = "lightcurve_py1.fits"
  142 + lcrv["logfile"] = "cslightcrv_py1.log"
  143 + lcrv["chatter"] = 2
140 144  
141 145 # Run cslightcrv script and save light curve
  146 + lcrv.logFileOpen() # Make sure we get a log file
142 147 lcrv.run()
143 148 lcrv.save()
144 149  
... ... @@ -169,6 +174,8 @@ class Test(gammalib.GPythonTestSuite):
169 174 lcrv["emin"] = 0.1
170 175 lcrv["emax"] = 100.0
171 176 lcrv["outfile"] = "lightcurve_py2.fits"
  177 + lcrv["logfile"] = "cslightcrv_py2.log"
  178 + lcrv["chatter"] = 3
172 179  
173 180 # Execute cslightcrv script
174 181 lcrv.execute()
... ... @@ -195,6 +202,8 @@ class Test(gammalib.GPythonTestSuite):
195 202 lcrv["emin"] = 0.1
196 203 lcrv["emax"] = 100.0
197 204 lcrv["outfile"] = "lightcurve_py3.fits"
  205 + lcrv["logfile"] = "cslightcrv_py3.log"
  206 + lcrv["chatter"] = 4
198 207  
199 208 # Execute cslightcrv script
200 209 lcrv.execute()
... ... @@ -224,6 +233,8 @@ class Test(gammalib.GPythonTestSuite):
224 233 lcrv["nypix"] = 20
225 234 lcrv["binsz"] = 0.02
226 235 lcrv["outfile"] = "lightcurve_py4.fits"
  236 + lcrv["logfile"] = "cslightcrv_py4.log"
  237 + lcrv["chatter"] = 4
227 238  
228 239 # Execute cslightcrv script
229 240 lcrv.execute()
... ...
test/test_python_cscripts.py
... ... @@ -24,6 +24,7 @@ import gammalib
24 24 import cscripts
25 25 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
26 26 import test_cscaldb
  27 +import test_csfindobs
27 28 import test_csobs2caldb
28 29 import test_cslightcrv
29 30  
... ... @@ -73,7 +74,7 @@ def test(installed=False, debug=False):
73 74 # Allocate test suite container
74 75 suites = gammalib.GTestSuites("cscripts unit testing")
75 76  
76   - # Allocate test suites and append them to the container
  77 + # Allocate test suites
77 78 suite_cscaldb = test_cscaldb.Test()
78 79 suite_csobs2caldb = test_csobs2caldb.Test()
79 80 suite_cslightcrv = test_cslightcrv.Test()
... ... @@ -88,6 +89,20 @@ def test(installed=False, debug=False):
88 89 suites.append(suite_csobs2caldb)
89 90 suites.append(suite_cslightcrv)
90 91  
  92 + # Append tests for Python 2.6+ (the IACT cscripts depend on the json
  93 + # module which is only available since Python 2.6+
  94 + ver = sys.version.split()[0]
  95 + if ver >= '2.6.0':
  96 +
  97 + # Allocate test suites
  98 + suite_csfindobs = test_csfindobs.Test()
  99 +
  100 + # Setup unit tests
  101 + suite_csfindobs.set()
  102 +
  103 + # Append tests to container
  104 + suites.append(suite_csfindobs)
  105 +
91 106 # Run test suite
92 107 success = suites.run()
93 108  
... ...