Commit 72cdceaa3e84772bdae4cb1c18ebec87f6d71f8d
1 parent
2139cad1
Add csiactcopy script unit test.
Showing
4 changed files
with
171 additions
and
5 deletions
.gitignore
| @@ -71,7 +71,7 @@ src/ctulimit/ctulimit | @@ -71,7 +71,7 @@ src/ctulimit/ctulimit | ||
| 71 | src/cterror/cterror | 71 | src/cterror/cterror |
| 72 | test/pfiles/ | 72 | test/pfiles/ |
| 73 | test/caldb/ | 73 | test/caldb/ |
| 74 | -test/iactdata_test | 74 | +test/iactdata_* |
| 75 | test/*.fits | 75 | test/*.fits |
| 76 | test/*.dat | 76 | test/*.dat |
| 77 | test/*.xml | 77 | test/*.xml |
cscripts/Makefile.am
| @@ -27,9 +27,10 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \ | @@ -27,9 +27,10 @@ linkscripts = cspull cssens cstsdist cscaldb csresmap \ | ||
| 27 | testscripts = tests/__init__.py \ | 27 | testscripts = tests/__init__.py \ |
| 28 | ../test/test_python_cscripts.py \ | 28 | ../test/test_python_cscripts.py \ |
| 29 | ../test/test_cscaldb.py \ | 29 | ../test/test_cscaldb.py \ |
| 30 | - ../test/test_csfindobs.py \ | ||
| 31 | ../test/test_csobs2caldb.py \ | 30 | ../test/test_csobs2caldb.py \ |
| 32 | - ../test/test_cslightcrv.py | 31 | + ../test/test_cslightcrv.py \ |
| 32 | + ../test/test_csfindobs.py \ | ||
| 33 | + ../test/test_csiactcopy.py | ||
| 33 | 34 | ||
| 34 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data | 35 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data |
| 35 | testdata = ../test/data/crab.xml \ | 36 | testdata = ../test/data/crab.xml \ |
test/test_csiactcopy.py
0 → 100755
| 1 | +#! /usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# This scripts performs unit tests for the csiactcopy 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 csiactcopy script # | ||
| 28 | +# ================================ # | ||
| 29 | +class Test(gammalib.GPythonTestSuite): | ||
| 30 | + """ | ||
| 31 | + Test class for csiactcopy script. | ||
| 32 | + | ||
| 33 | + This test class makes unit tests for the csiactcopy 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("csiactcopy") | ||
| 55 | + | ||
| 56 | + # Append tests | ||
| 57 | + self.append(self._test_cmd, "Test csiactcopy on command line") | ||
| 58 | + self.append(self._test_python, "Test csiactcopy from Python") | ||
| 59 | + | ||
| 60 | + # Return | ||
| 61 | + return | ||
| 62 | + | ||
| 63 | + # Test csiactcopy on command line | ||
| 64 | + def _test_cmd(self): | ||
| 65 | + """ | ||
| 66 | + Test csiactcopy on the command line. | ||
| 67 | + """ | ||
| 68 | + # Kluge to set the command (installed version has no README file) | ||
| 69 | + if os.path.isfile("README"): | ||
| 70 | + csiactcopy = "../cscripts/csiactcopy.py" | ||
| 71 | + else: | ||
| 72 | + csiactcopy = "csiactcopy" | ||
| 73 | + | ||
| 74 | + # Setup csiactcopy command | ||
| 75 | + cmd = csiactcopy+' remote_master="iactdata/master.json"'+ \ | ||
| 76 | + ' prodname="unit-test"'+ \ | ||
| 77 | + ' outpath="iactdata_cmd1"'+ \ | ||
| 78 | + ' logfile="csiactcopy_cmd1.log" chatter=1' | ||
| 79 | + | ||
| 80 | + # Execute csiactcopy, make sure we catch any exception | ||
| 81 | + try: | ||
| 82 | + rc = os.system(cmd+" >/dev/null 2>&1") | ||
| 83 | + except: | ||
| 84 | + pass | ||
| 85 | + | ||
| 86 | + # Check if execution was successful | ||
| 87 | + self.test_assert(rc == 0, | ||
| 88 | + "Successful csiactcopy execution on command line") | ||
| 89 | + | ||
| 90 | + # Check copy | ||
| 91 | + self._check_copy("iactdata_cmd1") | ||
| 92 | + | ||
| 93 | + # Setup csiactcopy command | ||
| 94 | + cmd = csiactcopy+' remote_master="master_that_does_not_exist"'+ \ | ||
| 95 | + ' prodname="unit-test"'+ \ | ||
| 96 | + ' outpath="iactdata_test"'+ \ | ||
| 97 | + ' logfile="csiactcopy_cmd2.log"' | ||
| 98 | + | ||
| 99 | + # Execute csiactcopy, make sure we catch any exception | ||
| 100 | + try: | ||
| 101 | + rc = os.system(cmd+" >/dev/null 2>&1") | ||
| 102 | + except: | ||
| 103 | + pass | ||
| 104 | + | ||
| 105 | + # Check if execution failed | ||
| 106 | + self.test_assert(rc != 0, | ||
| 107 | + "Failure of csiactcopy execution on command line") | ||
| 108 | + | ||
| 109 | + # Return | ||
| 110 | + return | ||
| 111 | + | ||
| 112 | + # Test csiactcopy from Python | ||
| 113 | + def _test_python(self): | ||
| 114 | + """ | ||
| 115 | + Test csiactcopy from Python. | ||
| 116 | + """ | ||
| 117 | + # Set-up csiactcopy | ||
| 118 | + findobs = cscripts.csiactcopy() | ||
| 119 | + findobs["remote_master"] = "iactdata/master.json" | ||
| 120 | + findobs["prodname"] = "unit-test" | ||
| 121 | + findobs["outpath"] = "iactdata_py1" | ||
| 122 | + findobs["logfile"] = "csiactcopy_py1.log" | ||
| 123 | + findobs["chatter"] = 2 | ||
| 124 | + | ||
| 125 | + # Run csiactcopy script and save run list | ||
| 126 | + findobs.logFileOpen() # Make sure we get a log file | ||
| 127 | + findobs.run() | ||
| 128 | + findobs.save() | ||
| 129 | + | ||
| 130 | + # Check copy | ||
| 131 | + self._check_copy("iactdata_py1") | ||
| 132 | + | ||
| 133 | + # Return | ||
| 134 | + return | ||
| 135 | + | ||
| 136 | + # Check copy | ||
| 137 | + def _check_copy(self, pathname): | ||
| 138 | + """ | ||
| 139 | + Check copy. | ||
| 140 | + """ | ||
| 141 | + # Set file names | ||
| 142 | + hdu_index_name = gammalib.GFilename(pathname+"/hdu-index.fits") | ||
| 143 | + obs_index_name = gammalib.GFilename(pathname+"/obs-index.fits") | ||
| 144 | + master_name = gammalib.GFilename(pathname+"/master.json") | ||
| 145 | + | ||
| 146 | + # Check for existence of files | ||
| 147 | + self.test_assert(hdu_index_name.exists(), | ||
| 148 | + 'Check is file "hdu-index.fits" exists.') | ||
| 149 | + self.test_assert(obs_index_name.exists(), | ||
| 150 | + 'Check is file "obs-index.fits" exists.') | ||
| 151 | + self.test_assert(master_name.exists(), | ||
| 152 | + 'Check is file "master.json" exists.') | ||
| 153 | + | ||
| 154 | + # Check if index files are FITS file | ||
| 155 | + self.test_assert(hdu_index_name.is_fits(), | ||
| 156 | + 'Check is file "hdu-index.fits" is FITS file.') | ||
| 157 | + self.test_assert(obs_index_name.is_fits(), | ||
| 158 | + 'Check is file "obs-index.fits" is FITS file.') | ||
| 159 | + | ||
| 160 | + # Return | ||
| 161 | + return |
test/test_python_cscripts.py
| @@ -24,9 +24,10 @@ import gammalib | @@ -24,9 +24,10 @@ import gammalib | ||
| 24 | import cscripts | 24 | import cscripts |
| 25 | sys.path.append(os.path.dirname(os.path.abspath(__file__))) | 25 | sys.path.append(os.path.dirname(os.path.abspath(__file__))) |
| 26 | import test_cscaldb | 26 | import test_cscaldb |
| 27 | -import test_csfindobs | ||
| 28 | import test_csobs2caldb | 27 | import test_csobs2caldb |
| 29 | import test_cslightcrv | 28 | import test_cslightcrv |
| 29 | +import test_csfindobs | ||
| 30 | +import test_csiactcopy | ||
| 30 | 31 | ||
| 31 | 32 | ||
| 32 | # ================== # | 33 | # ================== # |
| @@ -95,13 +96,16 @@ def test(installed=False, debug=False): | @@ -95,13 +96,16 @@ def test(installed=False, debug=False): | ||
| 95 | if ver >= '2.6.0': | 96 | if ver >= '2.6.0': |
| 96 | 97 | ||
| 97 | # Allocate test suites | 98 | # Allocate test suites |
| 98 | - suite_csfindobs = test_csfindobs.Test() | 99 | + suite_csfindobs = test_csfindobs.Test() |
| 100 | + suite_csiactcopy = test_csiactcopy.Test() | ||
| 99 | 101 | ||
| 100 | # Setup unit tests | 102 | # Setup unit tests |
| 101 | suite_csfindobs.set() | 103 | suite_csfindobs.set() |
| 104 | + suite_csiactcopy.set() | ||
| 102 | 105 | ||
| 103 | # Append tests to container | 106 | # Append tests to container |
| 104 | suites.append(suite_csfindobs) | 107 | suites.append(suite_csfindobs) |
| 108 | + suites.append(suite_csiactcopy) | ||
| 105 | 109 | ||
| 106 | # Run test suite | 110 | # Run test suite |
| 107 | success = suites.run() | 111 | success = suites.run() |