From a3e46f279f0164004c0471970e4db7d4d978d31a Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Tue, 22 Mar 2016 20:49:41 +0100 Subject: [PATCH] Add csiactdata script unit test. --- cscripts/Makefile.am | 3 ++- cscripts/cscaldb.py | 10 +++++----- cscripts/csfindobs.py | 12 ++++++------ cscripts/csiactdata.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------- test/test_csiactdata.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_python_cscripts.py | 4 ++++ 6 files changed, 200 insertions(+), 71 deletions(-) create mode 100755 test/test_csiactdata.py diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index c49e314..5ccff4d 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -30,7 +30,8 @@ testscripts = tests/__init__.py \ ../test/test_csobs2caldb.py \ ../test/test_cslightcrv.py \ ../test/test_csfindobs.py \ - ../test/test_csiactcopy.py + ../test/test_csiactcopy.py \ + ../test/test_csiactdata.py # Test data to be distributed in $(pyexecdir)/cscripts/tests/data testdata = ../test/data/crab.xml \ diff --git a/cscripts/cscaldb.py b/cscripts/cscaldb.py index db2e9e7..35ac959 100755 --- a/cscripts/cscaldb.py +++ b/cscripts/cscaldb.py @@ -59,6 +59,11 @@ class cscaldb(ctools.cscript): """ Get parameters from parfile. """ + # Write input parameters into logger + if self._logTerse(): + self._log_parameters() + self._log("\n") + # Return return @@ -160,11 +165,6 @@ class cscaldb(ctools.cscript): # Get parameters self._get_parameters() - # Write input parameters into logger - if self._logTerse(): - self._log_parameters() - self._log("\n") - # Get the calibration database caldb = gammalib.GCaldb() diff --git a/cscripts/csfindobs.py b/cscripts/csfindobs.py index 9881acf..977069f 100755 --- a/cscripts/csfindobs.py +++ b/cscripts/csfindobs.py @@ -151,6 +151,11 @@ class csfindobs(ctools.cscript): 'file or run csiactdata to get a list of '+ 'available storage names.') + # Write input parameters into logger + if self._logTerse(): + self._log_parameters() + self._log("\n") + # Return return @@ -166,12 +171,7 @@ class csfindobs(ctools.cscript): # Get parameters self._get_parameters() - - # Write input parameters into logger - if self._logTerse(): - self._log_parameters() - self._log("\n") - + # Initialise run list self._runs = [] diff --git a/cscripts/csiactdata.py b/cscripts/csiactdata.py index 1c81d8d..81d05ca 100755 --- a/cscripts/csiactdata.py +++ b/cscripts/csiactdata.py @@ -60,7 +60,7 @@ class csiactdata(ctools.cscript): # Private methods - def get_parameters(self): + def _get_parameters(self): """ Get parameters from parfile and setup the observation. """ @@ -82,33 +82,64 @@ class csiactdata(ctools.cscript): '" not found. Use hidden parameter '+ '"master_indx" to specifiy a different '+ 'filename.') + + # Write input parameters into logger + if self._logTerse(): + self._log_parameters() + self._log("\n") # Return return + # Log configuration + def _log_configuration(self, config): + """ + Log configuration. + """ + # Log information + if self._logTerse(): + self._log.header3(str(config["name"])) + self._log.parformat("Name") + self._log(str(config["name"])) + self._log("\n") + self._log.parformat("Observation index") + self._log(str(config["obsindx"])) + self._log("\n") + self._log.parformat("HDU index") + self._log(str(config["hduindx"])) + self._log("\n") + + # Print additional information + for key in config: + if key in ["name","hduindx","obsindx"]: + continue + self._log.parformat(str(key)) + self._log(str(config[key])) + self._log("\n") + + # Return + return + + # Public methods def run(self): """ Run the script. """ - # Switch screen logging on - self._log.cout(True) + # Switch screen logging on in debug mode + if self._logDebug(): + self._log.cout(True) # Get parameters self._get_parameters() - # Write input parameters into logger + # Write header into logger if self._logTerse(): - self._log_parameters() self._log("\n") - - self._log("\n") - self._log.header1("Data storage entries") - self._log("\n") - self._log.parformat("Master index file") - self._log(self._master_file) - self._log("\n") - self._log("\n") + self._log.header1("Data storage entries") + self._log.parformat("Master index file") + self._log(self._master_file) + self._log("\n") # Open and load JSON file json_data = open(self._master_file).read() @@ -117,24 +148,8 @@ class csiactdata(ctools.cscript): # Initialise array for available names self._prodnames = [] - - # Loop over configs and display unavailable storage first - for config in configs: - - # Create hdu and obs index files - hdu = os.path.join(self._datapath, config["hduindx"]) - obs = os.path.join(self._datapath, config["obsindx"]) - - filename_hdu = gammalib.GFilename(str(hdu)+"[HDU_INDEX]") - filename_obs = gammalib.GFilename(str(obs)+"[OBS_INDEX]") - - # Check if index files are available - if not (filename_hdu.is_fits() and filename_obs.is_fits()): - self._log.parformat(str(config["name"])) - self._log("Not available") - self._log("\n") - - + + # Write header into logger self._log("\n") self._log.header2("Available data configs") @@ -142,40 +157,26 @@ class csiactdata(ctools.cscript): for config in configs: # Create hdu and obs index files - hdu = os.path.join(self._datapath, config["hduindx"]) - obs = os.path.join(self._datapath, config["obsindx"]) - + hdu = os.path.join(self._datapath, config["hduindx"]) + obs = os.path.join(self._datapath, config["obsindx"]) filename_hdu = gammalib.GFilename(str(hdu)+"[HDU_INDEX]") filename_obs = gammalib.GFilename(str(obs)+"[OBS_INDEX]") - # Check if index files are available + # If index files are available then log configuration + # information ... if (filename_hdu.is_fits() and filename_obs.is_fits()): - - # Log data information - self._log("\n") - self._log.header3(str(config["name"])) - - # Print important informations first - self._log.parformat("Name") - self._log(str(config["name"])) - self._log("\n") - self._log.parformat("Observation index") - self._log(str(config["obsindx"])) - self._log("\n") - self._log.parformat("HDU index") - self._log(str(config["hduindx"])) - self._log("\n") - + # Append to available names self._prodnames.append(str(config["name"])) - # Print additional information - for key in config: - if key in ["name","hduindx","obsindx"]: - continue - self._log.parformat(str(key)) - self._log(str(config[key])) - self._log("\n") + # Log information + self._log_configuration(config) + + # ... otherwise log that the configuration is not available + else: + if self._logTerse(): + self._log.header3(str(config["name"])) + self._log(" Not available\n") # Return return @@ -193,10 +194,12 @@ class csiactdata(ctools.cscript): # Return return - def names(self): """ Return available FITS production names + + Returns: + List of available FITS production names. """ # Return diff --git a/test/test_csiactdata.py b/test/test_csiactdata.py new file mode 100755 index 0000000..cb10874 --- /dev/null +++ b/test/test_csiactdata.py @@ -0,0 +1,121 @@ +#! /usr/bin/env python +# ========================================================================== +# This scripts performs unit tests for the csiactdata 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 csiactdata script # +# ================================ # +class Test(gammalib.GPythonTestSuite): + """ + Test class for csiactdata script. + + This test class makes unit tests for the csiactdata 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("csiactdata") + + # Append tests + self.append(self._test_cmd, "Test csiactdata on command line") + self.append(self._test_python, "Test csiactdata from Python") + + # Return + return + + # Test csiactdata on command line + def _test_cmd(self): + """ + Test csiactdata on the command line. + """ + # Kluge to set the command (installed version has no README file) + if os.path.isfile("README"): + csiactdata = "../cscripts/csiactdata.py" + else: + csiactdata = "csiactdata" + + # Setup csiactdata command + cmd = csiactdata+' datapath="iactdata"'+ \ + ' logfile="csiactdata_cmd1.log" chatter=1' + + # Execute csiactdata, 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 csiactdata execution on command line") + + # Setup csiactdata command + cmd = csiactdata+' datapath="data_path_that_does_not_exist"'+ \ + ' logfile="csiactdata_cmd2.log"' + + # Execute csiactdata, 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 csiactdata execution on command line") + + # Return + return + + # Test csiactdata from Python + def _test_python(self): + """ + Test csiactdata from Python. + """ + # Set-up csiactdata + iactdata = cscripts.csiactdata() + iactdata["datapath"] = "iactdata" + iactdata["logfile"] = "csiactdata_py1.log" + iactdata["chatter"] = 2 + + # Run csiactdata script and save run list + iactdata.logFileOpen() # Make sure we get a log file + iactdata.run() + + # Return + return diff --git a/test/test_python_cscripts.py b/test/test_python_cscripts.py index 86b411d..3b64691 100755 --- a/test/test_python_cscripts.py +++ b/test/test_python_cscripts.py @@ -28,6 +28,7 @@ import test_csobs2caldb import test_cslightcrv import test_csfindobs import test_csiactcopy +import test_csiactdata # ================== # @@ -98,14 +99,17 @@ def test(installed=False, debug=False): # Allocate test suites suite_csfindobs = test_csfindobs.Test() suite_csiactcopy = test_csiactcopy.Test() + suite_csiactdata = test_csiactdata.Test() # Setup unit tests suite_csfindobs.set() suite_csiactcopy.set() + suite_csiactdata.set() # Append tests to container suites.append(suite_csfindobs) suites.append(suite_csiactcopy) + suites.append(suite_csiactdata) # Run test suite success = suites.run() -- libgit2 0.21.4