Commit 6bb547d9a5bb15c1a247346eaa72e9ad1d6923f4

Authored by Jürgen Knödlseder
1 parent aa5dede9

Add csmodelinfo script unit test.

.gitignore
... ... @@ -75,12 +75,11 @@ test/iactdata_*
75 75 test/*.fits
76 76 test/*.dat
77 77 test/*.xml
  78 +test/*.reg
78 79 test/*.trs
79 80 test/test_cscripts.sh
80 81 test/reports/*.xml
81 82 test/butterfly.txt
82   -test/model.reg
83   -test/obs.reg
84 83 test/.coverage
85 84 test/test_python_ctools.sh
86 85 test/test_python_cscripts.sh
... ...
cscripts/Makefile.am
... ... @@ -29,6 +29,7 @@ testscripts = tests/__init__.py \
29 29 ../test/test_cscaldb.py \
30 30 ../test/test_csobs2caldb.py \
31 31 ../test/test_cslightcrv.py \
  32 + ../test/test_csmodelinfo.py \
32 33 ../test/test_csfindobs.py \
33 34 ../test/test_csiactcopy.py \
34 35 ../test/test_csiactdata.py \
... ...
test/test_csmodelinfo.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This scripts performs unit tests for the csmodelinfo 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 csmodelinfo script #
  28 +# ================================= #
  29 +class Test(gammalib.GPythonTestSuite):
  30 + """
  31 + Test class for csmodelinfo script.
  32 +
  33 + This test class makes unit tests for the csmodelinfo 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 + # Set members
  46 + self._model_name = "data/crab.xml"
  47 +
  48 + # Return
  49 + return
  50 +
  51 + # Set test functions
  52 + def set(self):
  53 + """
  54 + Set all test functions.
  55 + """
  56 + # Set test name
  57 + self.name("csmodelinfo")
  58 +
  59 + # Append tests
  60 + self.append(self._test_cmd, "Test csmodelinfo on command line")
  61 + self.append(self._test_python, "Test csmodelinfo from Python")
  62 +
  63 + # Return
  64 + return
  65 +
  66 + # Test csmodelinfo on command line
  67 + def _test_cmd(self):
  68 + """
  69 + Test csmodelinfo on the command line.
  70 + """
  71 + # Kluge to set the command (installed version has no README file)
  72 + if os.path.isfile("README"):
  73 + csmodelinfo = "../cscripts/csmodelinfo.py"
  74 + else:
  75 + csmodelinfo = "csmodelinfo"
  76 +
  77 + # Setup csmodelinfo command
  78 + cmd = csmodelinfo+' inmodel="'+self._model_name+'"'+ \
  79 + ' ds9file="model_cmd1.reg"'+ \
  80 + ' logfile="csmodelinfo_cmd1.log" chatter=1'
  81 +
  82 + # Execute csmodelinfo, make sure we catch any exception
  83 + try:
  84 + rc = os.system(cmd+" >/dev/null 2>&1")
  85 + except:
  86 + pass
  87 +
  88 + # Check if execution was successful
  89 + self.test_assert(rc == 0,
  90 + "Successful csmodelinfo execution on command line")
  91 +
  92 + # Check region file
  93 + self._check_region_file("model_cmd1.reg")
  94 +
  95 + # Setup csmodelinfo command
  96 + cmd = csmodelinfo+' inmodel="model_that_does_not_exist.xml"'+ \
  97 + ' ds9file="model_cmd2.reg"'+ \
  98 + ' logfile="csmodelinfo_cmd2.log"'
  99 +
  100 + # Execute csmodelinfo, make sure we catch any exception
  101 + try:
  102 + rc = os.system(cmd+" >/dev/null 2>&1")
  103 + except:
  104 + pass
  105 +
  106 + # Check if execution failed
  107 + self.test_assert(rc != 0,
  108 + "Failure of csmodelinfo execution on command line")
  109 +
  110 + # Return
  111 + return
  112 +
  113 + # Test csmodelinfo from Python
  114 + def _test_python(self):
  115 + """
  116 + Test csmodelinfo from Python.
  117 + """
  118 + # Set-up csmodelinfo
  119 + modelinfo = cscripts.csmodelinfo()
  120 + modelinfo["inmodel"] = self._model_name
  121 + modelinfo["ds9file"] = "model_py2.reg"
  122 + modelinfo["logfile"] = "csmodelinfo_py1.log"
  123 + modelinfo["chatter"] = 2
  124 +
  125 + # Run csmodelinfo script and save light curve
  126 + modelinfo.logFileOpen() # Make sure we get a log file
  127 + modelinfo.run()
  128 + modelinfo.save()
  129 +
  130 + # Check region file
  131 + self._check_region_file("model_py2.reg")
  132 +
  133 + # Return
  134 + return
  135 +
  136 + # Check region file
  137 + def _check_region_file(self, filename):
  138 + """
  139 + Check region file.
  140 + """
  141 + # Open file
  142 + f = open(filename,"r")
  143 +
  144 + # Expect "fk5" in first line
  145 + line = f.readline().strip('\n')
  146 + self.test_assert(line == 'fk5',
  147 + 'Check for "fk5" in first line')
  148 +
  149 + # Expect "fk5" in first line
  150 + line = f.readline().strip('\n')
  151 + self.test_assert(line[0:6] == 'point(',
  152 + 'Check for "point(" in second line')
  153 +
  154 + # Close file
  155 + f.close()
  156 +
  157 + # Return
  158 + return
... ...
test/test_python_cscripts.py
... ... @@ -26,6 +26,7 @@ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
26 26 import test_cscaldb
27 27 import test_csobs2caldb
28 28 import test_cslightcrv
  29 +import test_csmodelinfo
29 30 import test_csfindobs
30 31 import test_csiactcopy
31 32 import test_csiactdata
... ... @@ -81,16 +82,19 @@ def test(installed=False, debug=False):
81 82 suite_cscaldb = test_cscaldb.Test()
82 83 suite_csobs2caldb = test_csobs2caldb.Test()
83 84 suite_cslightcrv = test_cslightcrv.Test()
  85 + suite_csmodelinfo = test_csmodelinfo.Test()
84 86  
85 87 # Setup unit tests
86 88 suite_cscaldb.set()
87 89 suite_csobs2caldb.set()
88 90 suite_cslightcrv.set()
  91 + suite_csmodelinfo.set()
89 92  
90 93 # Append tests to container
91 94 suites.append(suite_cscaldb)
92 95 suites.append(suite_csobs2caldb)
93 96 suites.append(suite_cslightcrv)
  97 + suites.append(suite_csmodelinfo)
94 98  
95 99 # Append tests for Python 2.6+ (the IACT cscripts depend on the json
96 100 # module which is only available since Python 2.6+
... ...