From cba332cde83037cf63780c8eba32fa7f00bfec70 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Tue, 22 Mar 2016 11:41:39 +0100 Subject: [PATCH] Add unit test for csfindobs script. --- cscripts/Makefile.am | 8 ++++++++ test/test_cscaldb.py | 39 ++++++++++++++++++++++++++++++++------- test/test_csfindobs.py | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_cslightcrv.py | 15 +++++++++++++-- test/test_python_cscripts.py | 17 ++++++++++++++++- 5 files changed, 227 insertions(+), 10 deletions(-) create mode 100755 test/test_csfindobs.py diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index e00e546..2f71457 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -27,6 +27,7 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \ testscripts = tests/__init__.py \ ../test/test_python_cscripts.py \ ../test/test_cscaldb.py \ + ../test/test_csfindobs.py \ ../test/test_csobs2caldb.py \ ../test/test_cslightcrv.py @@ -37,6 +38,11 @@ testdata = ../test/data/crab.xml \ ../test/data/crab_cntmap.fits.gz \ ../test/data/exclusion.reg +# Test data to be distributed in $(pyexecdir)/cscripts/tests/iactdata +testiactdata = ../test/iactdata/hdu-index.fits.gz \ + ../test/iactdata/master.json \ + ../test/iactdata/obs-index.fits.gz + # Test IRFs to be distributed in $(pyexecdir)/cscripts/tests/irf testirf = ../test/irf/irf_file.fits.gz @@ -52,6 +58,8 @@ testscriptsdir = $(pyexecdir)/cscripts/tests testscripts_DATA = $(testscripts) testdatadir = $(pyexecdir)/cscripts/tests/data testdata_DATA = $(testdata) +testiactdatadir = $(pyexecdir)/cscripts/tests/iactdata +testiactdata_DATA = $(testiactdata) testirfdir = $(pyexecdir)/cscripts/tests/irf testirf_DATA = $(testirf) diff --git a/test/test_cscaldb.py b/test/test_cscaldb.py index b2d8984..6ca278b 100755 --- a/test/test_cscaldb.py +++ b/test/test_cscaldb.py @@ -54,11 +54,39 @@ class Test(gammalib.GPythonTestSuite): self.name("cscaldb") # Append tests + self.append(self._test_cmd, "Test cscaldb on command line") self.append(self._test_python, "Test cscaldb from Python") # Return return + # Test cscaldb on command line + def _test_cmd(self): + """ + Test cscaldb on the command line. + """ + # Kluge to set the command (installed version has no README file) + if os.path.isfile("README"): + cscaldb = "../cscripts/cscaldb.py" + else: + cscaldb = "csfindobs" + + # Setup cscaldb command + cmd = cscaldb+' logfile="cscaldb_cmd1.log" chatter=1' + + # Execute cscaldb, make sure we catch any exception + try: + rc = os.system(cmd+" >/dev/null 2>&1") + except: + pass + + # Check if execution was successful + self.test_assert(rc == 0, + "Successful cscaldb execution on command line") + + # Return + return + # Test cscaldb from Python def _test_python(self): """ @@ -66,15 +94,12 @@ class Test(gammalib.GPythonTestSuite): """ # Set-up cscaldb caldb = cscripts.cscaldb() + caldb["logfile"] = "cscaldb_py1.log" + caldb["chatter"] = 2 # Run script - self.test_try("Run cscaldb") - try: - caldb.run() - self.test_try_success() - except: - msg = "Exception occured in cscaldb." - self.test_try_failure(msg) + caldb.logFileOpen() # Make sure we get a log file + caldb.run() # Return return diff --git a/test/test_csfindobs.py b/test/test_csfindobs.py new file mode 100755 index 0000000..9e10eed --- /dev/null +++ b/test/test_csfindobs.py @@ -0,0 +1,158 @@ +#! /usr/bin/env python +# ========================================================================== +# This scripts performs unit tests for the csfindobs script. +# +# Copyright (C) 2016 Juergen Knoedlseder +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# ========================================================================== +import os +import gammalib +import cscripts + + +# ================================ # +# Test class for csfindobs script # +# ================================ # +class Test(gammalib.GPythonTestSuite): + """ + Test class for csfindobs script. + + This test class makes unit tests for the csfindobs script by using it + from the command line and from Python. + """ + + # Constructor + def __init__(self): + """ + Constructor. + """ + # Call base class constructor + gammalib.GPythonTestSuite.__init__(self) + + # Return + return + + # Set test functions + def set(self): + """ + Set all test functions. + """ + # Set test name + self.name("csfindobs") + + # Append tests + self.append(self._test_cmd, "Test csfindobs on command line") + self.append(self._test_python, "Test csfindobs from Python") + + # Return + return + + # Test csfindobs on command line + def _test_cmd(self): + """ + Test csfindobs on the command line. + """ + # Kluge to set the command (installed version has no README file) + if os.path.isfile("README"): + csfindobs = "../cscripts/csfindobs.py" + else: + csfindobs = "csfindobs" + + # Setup csfindobs command + cmd = csfindobs+' datapath="iactdata/"'+ \ + ' prodname="unit-test"'+ \ + ' ra=83.63 dec=22.01 rad=1.0'+ \ + ' outfile="runlist_cmd1.dat"'+ \ + ' logfile="csfindobs_cmd1.log" chatter=1' + + # Execute csfindobs, make sure we catch any exception + try: + rc = os.system(cmd+" >/dev/null 2>&1") + except: + pass + + # Check if execution was successful + self.test_assert(rc == 0, + "Successful csfindobs execution on command line") + + # Check run list + self._check_runlist("runlist_cmd1.dat") + + # Setup csfindobs command + cmd = csfindobs+' datapath="path_that_does_not_exist"'+ \ + ' prodname="unit-test"'+ \ + ' ra=83.63 dec=22.01 rad=1.0'+ \ + ' outfile="runlist_cmd2.dat"'+ \ + ' logfile="csfindobs_cmd2.log"' + + # Execute csfindobs, make sure we catch any exception + try: + rc = os.system(cmd+" >/dev/null 2>&1") + except: + pass + + # Check if execution failed + self.test_assert(rc != 0, + "Failure of csfindobs execution on command line") + + # Return + return + + # Test csfindobs from Python + def _test_python(self): + """ + Test csfindobs from Python. + """ + # Set-up csfindobs + findobs = cscripts.csfindobs() + findobs["datapath"] = "iactdata/" + findobs["prodname"] = "unit-test" + findobs["outfile"] = "runlist_py1.dat" + findobs["ra"] = 83.63 + findobs["dec"] = 22.01 + findobs["rad"] = 1.0 + findobs["logfile"] = "csfindobs_py1.log" + findobs["chatter"] = 2 + + # Run csfindobs script and save run list + findobs.logFileOpen() # Make sure we get a log file + findobs.run() + findobs.save() + + # Check run list + self._check_runlist("runlist_py1.dat") + + # Return + return + + # Check run list result file + def _check_runlist(self, filename): + """ + Check run list file. + """ + # Open run list file as CSV file + runlist = gammalib.GCsv(filename) + + # Check dimensions + self.test_value(runlist.ncols(), 1, + 'Check for single column in runlist file') + self.test_value(runlist.nrows(), 1, + 'Check for single row in runlist file') + self.test_assert(runlist[0,0] == '0', + 'Check for value "0" in runlist file') + + # Return + return diff --git a/test/test_cslightcrv.py b/test/test_cslightcrv.py index e66bd55..5b88324 100755 --- a/test/test_cslightcrv.py +++ b/test/test_cslightcrv.py @@ -81,7 +81,8 @@ class Test(gammalib.GPythonTestSuite): ' srcname="Crab" caldb="prod2" irf="South_0.5h"'+ \ ' tbinalg="LIN" tmin=51544.50 tmax=51544.53'+ \ ' tbins=3 enumbins=0 emin=0.1 emax=100.0'+ \ - ' outfile="lightcurve_cmd1.fits"' + ' outfile="lightcurve_cmd1.fits"'+ \ + ' logfile="cslightcrv_cmd1.log" chatter=1' # Execute cslightcrv, make sure we catch any exception try: @@ -102,7 +103,8 @@ class Test(gammalib.GPythonTestSuite): ' srcname="Crab" caldb="prod2" irf="South_0.5h"'+ \ ' tbinalg="LIN" tmin=51544.50 tmax=51544.53'+ \ ' tbins=3 enumbins=0 emin=0.1 emax=100.0'+ \ - ' outfile="lightcurve_cmd1.fits"' + ' outfile="lightcurve_cmd1.fits"'+ \ + ' logfile="cslightcrv_cmd2.log"' # Execute cslightcrv, make sure we catch any exception try: @@ -137,8 +139,11 @@ class Test(gammalib.GPythonTestSuite): lcrv["emin"] = 0.1 lcrv["emax"] = 100.0 lcrv["outfile"] = "lightcurve_py1.fits" + lcrv["logfile"] = "cslightcrv_py1.log" + lcrv["chatter"] = 2 # Run cslightcrv script and save light curve + lcrv.logFileOpen() # Make sure we get a log file lcrv.run() lcrv.save() @@ -169,6 +174,8 @@ class Test(gammalib.GPythonTestSuite): lcrv["emin"] = 0.1 lcrv["emax"] = 100.0 lcrv["outfile"] = "lightcurve_py2.fits" + lcrv["logfile"] = "cslightcrv_py2.log" + lcrv["chatter"] = 3 # Execute cslightcrv script lcrv.execute() @@ -195,6 +202,8 @@ class Test(gammalib.GPythonTestSuite): lcrv["emin"] = 0.1 lcrv["emax"] = 100.0 lcrv["outfile"] = "lightcurve_py3.fits" + lcrv["logfile"] = "cslightcrv_py3.log" + lcrv["chatter"] = 4 # Execute cslightcrv script lcrv.execute() @@ -224,6 +233,8 @@ class Test(gammalib.GPythonTestSuite): lcrv["nypix"] = 20 lcrv["binsz"] = 0.02 lcrv["outfile"] = "lightcurve_py4.fits" + lcrv["logfile"] = "cslightcrv_py4.log" + lcrv["chatter"] = 4 # Execute cslightcrv script lcrv.execute() diff --git a/test/test_python_cscripts.py b/test/test_python_cscripts.py index b824e2a..8c8a566 100755 --- a/test/test_python_cscripts.py +++ b/test/test_python_cscripts.py @@ -24,6 +24,7 @@ import gammalib import cscripts sys.path.append(os.path.dirname(os.path.abspath(__file__))) import test_cscaldb +import test_csfindobs import test_csobs2caldb import test_cslightcrv @@ -73,7 +74,7 @@ def test(installed=False, debug=False): # Allocate test suite container suites = gammalib.GTestSuites("cscripts unit testing") - # Allocate test suites and append them to the container + # Allocate test suites suite_cscaldb = test_cscaldb.Test() suite_csobs2caldb = test_csobs2caldb.Test() suite_cslightcrv = test_cslightcrv.Test() @@ -88,6 +89,20 @@ def test(installed=False, debug=False): suites.append(suite_csobs2caldb) suites.append(suite_cslightcrv) + # Append tests for Python 2.6+ (the IACT cscripts depend on the json + # module which is only available since Python 2.6+ + ver = sys.version.split()[0] + if ver >= '2.6.0': + + # Allocate test suites + suite_csfindobs = test_csfindobs.Test() + + # Setup unit tests + suite_csfindobs.set() + + # Append tests to container + suites.append(suite_csfindobs) + # Run test suite success = suites.run() -- libgit2 0.21.4