From 816acf9e83d81aea66fa254e2c2d2ff0f470011a Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 23 Mar 2016 21:51:34 +0100 Subject: [PATCH] Add csobsinfo script unit test. --- cscripts/Makefile.am | 1 + test/test_csmodelinfo.py | 21 +++++++++++++++++---- test/test_csobsinfo.py | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_python_cscripts.py | 4 ++++ 4 files changed, 198 insertions(+), 4 deletions(-) create mode 100755 test/test_csobsinfo.py diff --git a/cscripts/Makefile.am b/cscripts/Makefile.am index 5e1bed8..2a0d4f8 100644 --- a/cscripts/Makefile.am +++ b/cscripts/Makefile.am @@ -32,6 +32,7 @@ testscripts = tests/__init__.py \ ../test/test_csmodelmerge.py \ ../test/test_csobs2caldb.py \ ../test/test_csobsdef.py \ + ../test/test_csobsinfo.py \ ../test/test_csfindobs.py \ ../test/test_csiactcopy.py \ ../test/test_csiactdata.py \ diff --git a/test/test_csmodelinfo.py b/test/test_csmodelinfo.py index e92f935..bb1ec1f 100755 --- a/test/test_csmodelinfo.py +++ b/test/test_csmodelinfo.py @@ -90,7 +90,7 @@ class Test(gammalib.GPythonTestSuite): "Successful csmodelinfo execution on command line") # Check region file - self._check_region_file("model_cmd1.reg") + self._check_ds9_file("model_cmd1.reg") # Setup csmodelinfo command cmd = csmodelinfo+' inmodel="model_that_does_not_exist.xml"'+ \ @@ -118,7 +118,7 @@ class Test(gammalib.GPythonTestSuite): # Set-up csmodelinfo modelinfo = cscripts.csmodelinfo() modelinfo["inmodel"] = self._model_name - modelinfo["ds9file"] = "model_py2.reg" + modelinfo["ds9file"] = "csmodelinfo_py1.reg" modelinfo["logfile"] = "csmodelinfo_py1.log" modelinfo["chatter"] = 2 @@ -128,13 +128,26 @@ class Test(gammalib.GPythonTestSuite): modelinfo.save() # Check region file - self._check_region_file("model_py2.reg") + self._check_ds9_file("csmodelinfo_py1.reg") + + # Set-up csmodelinfo + modelinfo = cscripts.csmodelinfo() + modelinfo["inmodel"] = self._model_name + modelinfo["ds9file"] = "csmodelinfo_py2.reg" + modelinfo["logfile"] = "csmodelinfo_py2.log" + modelinfo["chatter"] = 3 + + # Execute csmodelinfo script + modelinfo.execute() + + # Check region file + self._check_ds9_file("csmodelinfo_py2.reg") # Return return # Check region file - def _check_region_file(self, filename): + def _check_ds9_file(self, filename): """ Check region file. """ diff --git a/test/test_csobsinfo.py b/test/test_csobsinfo.py new file mode 100755 index 0000000..2e151a9 --- /dev/null +++ b/test/test_csobsinfo.py @@ -0,0 +1,176 @@ +#! /usr/bin/env python +# ========================================================================== +# This scripts performs unit tests for the csobsinfo 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 csobsinfo script # +# =============================== # +class Test(gammalib.GPythonTestSuite): + """ + Test class for csobsinfo script. + + This test class makes unit tests for the csobsinfo 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("csobsinfo") + + # Append tests + self.append(self._test_cmd, "Test csobsinfo on command line") + self.append(self._test_python, "Test csobsinfo from Python") + + # Return + return + + # Test csobsinfo on command line + def _test_cmd(self): + """ + Test csobsinfo on the command line. + """ + # Kluge to set the command (installed version has no README file) + if os.path.isfile("README"): + csobsinfo = "../cscripts/csobsinfo.py" + else: + csobsinfo = "csobsinfo" + + # Setup csobsinfo command + cmd = csobsinfo+' inobs="data/obs_unbinned.xml"'+ \ + ' ds9file="csobsinfo_cmd1.reg"'+ \ + ' offset=yes ra=83.63 dec=22.01'+ \ + ' logfile="csobsinfo_cmd1.log" chatter=1' + + # Execute csobsinfo, 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 csobsinfo execution on command line") + + # Check DS9 file + self._check_ds9_file("csobsinfo_cmd1.reg") + + # Setup csobsinfo command + cmd = csobsinfo+' inobs="obs_definition_that_does_not_exist.xml"'+ \ + ' ds9file="csobsinfo_cmd2.reg"'+ \ + ' offset=yes ra=83.63 dec=22.01'+ \ + ' logfile="csobsinfo_cmd2.log"' + + # Execute csobsinfo, 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 csobsinfo execution on command line") + + # Return + return + + # Test csobsinfo from Python + def _test_python(self): + """ + Test csobsinfo from Python. + """ + # Set-up csobsinfo + obsinfo = cscripts.csobsinfo() + obsinfo["inobs"] = "data/obs_unbinned.xml" + obsinfo["ds9file"] = "csobsinfo_py1.reg" + obsinfo["offset"] = True + obsinfo["ra"] = 83.63 + obsinfo["dec"] = 22.01 + obsinfo["logfile"] = "csobsinfo_py1.log" + obsinfo["chatter"] = 2 + + # Run csobsinfo script and save XML file + obsinfo.logFileOpen() # Make sure we get a log file + obsinfo.run() + obsinfo.save() + + # Check DS9 file + self._check_ds9_file("csobsinfo_py1.reg") + + # Set-up csobsinfo + obsinfo = cscripts.csobsinfo() + obsinfo["inobs"] = "data/obs_unbinned.xml" + obsinfo["ds9file"] = "csobsinfo_py2.reg" + obsinfo["offset"] = True + obsinfo["ra"] = 83.63 + obsinfo["dec"] = 22.01 + obsinfo["logfile"] = "csobsinfo_py2.log" + obsinfo["chatter"] = 3 + + # Execute csobsinfo script + obsinfo.execute() + + # Check DS9 file + self._check_ds9_file("csobsinfo_py2.reg") + + # Return + return + + # Check DS9 file + def _check_ds9_file(self, filename): + """ + Check DS9 file. + """ + # Open file + f = open(filename,"r") + + # Expect "fk5" in first line + line = f.readline().strip('\n') + self.test_assert(line == 'fk5', + 'Check for "fk5" in first line') + + # Expect "fk5" in first line + line = f.readline().strip('\n') + self.test_assert(line[0:6] == 'point(', + 'Check for "point(" in second line') + + # Close file + f.close() + + # Return + return diff --git a/test/test_python_cscripts.py b/test/test_python_cscripts.py index 08ed09a..1e3d3de 100755 --- a/test/test_python_cscripts.py +++ b/test/test_python_cscripts.py @@ -29,6 +29,7 @@ import test_csmodelinfo import test_csmodelmerge import test_csobs2caldb import test_csobsdef +import test_csobsinfo import test_csfindobs import test_csiactcopy import test_csiactdata @@ -87,6 +88,7 @@ def test(installed=False, debug=False): suite_csmodelmerge = test_csmodelmerge.Test() suite_csobs2caldb = test_csobs2caldb.Test() suite_csobsdef = test_csobsdef.Test() + suite_csobsinfo = test_csobsinfo.Test() # Setup unit tests suite_cscaldb.set() @@ -95,6 +97,7 @@ def test(installed=False, debug=False): suite_csmodelmerge.set() suite_csobs2caldb.set() suite_csobsdef.set() + suite_csobsinfo.set() # Append tests to container suites.append(suite_cscaldb) @@ -103,6 +106,7 @@ def test(installed=False, debug=False): suites.append(suite_csmodelmerge) suites.append(suite_csobs2caldb) suites.append(suite_csobsdef) + suites.append(suite_csobsinfo) # Append tests for Python 2.6+ (the IACT cscripts depend on the json # module which is only available since Python 2.6+ -- libgit2 0.21.4