Commit a3e46f279f0164004c0471970e4db7d4d978d31a
1 parent
72cdceaa
Add csiactdata script unit test.
Showing
6 changed files
with
200 additions
and
71 deletions
cscripts/Makefile.am
| @@ -30,7 +30,8 @@ testscripts = tests/__init__.py \ | @@ -30,7 +30,8 @@ testscripts = tests/__init__.py \ | ||
| 30 | ../test/test_csobs2caldb.py \ | 30 | ../test/test_csobs2caldb.py \ |
| 31 | ../test/test_cslightcrv.py \ | 31 | ../test/test_cslightcrv.py \ |
| 32 | ../test/test_csfindobs.py \ | 32 | ../test/test_csfindobs.py \ |
| 33 | - ../test/test_csiactcopy.py | 33 | + ../test/test_csiactcopy.py \ |
| 34 | + ../test/test_csiactdata.py | ||
| 34 | 35 | ||
| 35 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data | 36 | # Test data to be distributed in $(pyexecdir)/cscripts/tests/data |
| 36 | testdata = ../test/data/crab.xml \ | 37 | testdata = ../test/data/crab.xml \ |
cscripts/cscaldb.py
| @@ -59,6 +59,11 @@ class cscaldb(ctools.cscript): | @@ -59,6 +59,11 @@ class cscaldb(ctools.cscript): | ||
| 59 | """ | 59 | """ |
| 60 | Get parameters from parfile. | 60 | Get parameters from parfile. |
| 61 | """ | 61 | """ |
| 62 | + # Write input parameters into logger | ||
| 63 | + if self._logTerse(): | ||
| 64 | + self._log_parameters() | ||
| 65 | + self._log("\n") | ||
| 66 | + | ||
| 62 | # Return | 67 | # Return |
| 63 | return | 68 | return |
| 64 | 69 | ||
| @@ -160,11 +165,6 @@ class cscaldb(ctools.cscript): | @@ -160,11 +165,6 @@ class cscaldb(ctools.cscript): | ||
| 160 | # Get parameters | 165 | # Get parameters |
| 161 | self._get_parameters() | 166 | self._get_parameters() |
| 162 | 167 | ||
| 163 | - # Write input parameters into logger | ||
| 164 | - if self._logTerse(): | ||
| 165 | - self._log_parameters() | ||
| 166 | - self._log("\n") | ||
| 167 | - | ||
| 168 | # Get the calibration database | 168 | # Get the calibration database |
| 169 | caldb = gammalib.GCaldb() | 169 | caldb = gammalib.GCaldb() |
| 170 | 170 |
cscripts/csfindobs.py
| @@ -151,6 +151,11 @@ class csfindobs(ctools.cscript): | @@ -151,6 +151,11 @@ class csfindobs(ctools.cscript): | ||
| 151 | 'file or run csiactdata to get a list of '+ | 151 | 'file or run csiactdata to get a list of '+ |
| 152 | 'available storage names.') | 152 | 'available storage names.') |
| 153 | 153 | ||
| 154 | + # Write input parameters into logger | ||
| 155 | + if self._logTerse(): | ||
| 156 | + self._log_parameters() | ||
| 157 | + self._log("\n") | ||
| 158 | + | ||
| 154 | # Return | 159 | # Return |
| 155 | return | 160 | return |
| 156 | 161 | ||
| @@ -166,12 +171,7 @@ class csfindobs(ctools.cscript): | @@ -166,12 +171,7 @@ class csfindobs(ctools.cscript): | ||
| 166 | 171 | ||
| 167 | # Get parameters | 172 | # Get parameters |
| 168 | self._get_parameters() | 173 | self._get_parameters() |
| 169 | - | ||
| 170 | - # Write input parameters into logger | ||
| 171 | - if self._logTerse(): | ||
| 172 | - self._log_parameters() | ||
| 173 | - self._log("\n") | ||
| 174 | - | 174 | + |
| 175 | # Initialise run list | 175 | # Initialise run list |
| 176 | self._runs = [] | 176 | self._runs = [] |
| 177 | 177 |
cscripts/csiactdata.py
| @@ -60,7 +60,7 @@ class csiactdata(ctools.cscript): | @@ -60,7 +60,7 @@ class csiactdata(ctools.cscript): | ||
| 60 | 60 | ||
| 61 | 61 | ||
| 62 | # Private methods | 62 | # Private methods |
| 63 | - def get_parameters(self): | 63 | + def _get_parameters(self): |
| 64 | """ | 64 | """ |
| 65 | Get parameters from parfile and setup the observation. | 65 | Get parameters from parfile and setup the observation. |
| 66 | """ | 66 | """ |
| @@ -82,33 +82,64 @@ class csiactdata(ctools.cscript): | @@ -82,33 +82,64 @@ class csiactdata(ctools.cscript): | ||
| 82 | '" not found. Use hidden parameter '+ | 82 | '" not found. Use hidden parameter '+ |
| 83 | '"master_indx" to specifiy a different '+ | 83 | '"master_indx" to specifiy a different '+ |
| 84 | 'filename.') | 84 | 'filename.') |
| 85 | + | ||
| 86 | + # Write input parameters into logger | ||
| 87 | + if self._logTerse(): | ||
| 88 | + self._log_parameters() | ||
| 89 | + self._log("\n") | ||
| 85 | 90 | ||
| 86 | # Return | 91 | # Return |
| 87 | return | 92 | return |
| 88 | 93 | ||
| 94 | + # Log configuration | ||
| 95 | + def _log_configuration(self, config): | ||
| 96 | + """ | ||
| 97 | + Log configuration. | ||
| 98 | + """ | ||
| 99 | + # Log information | ||
| 100 | + if self._logTerse(): | ||
| 101 | + self._log.header3(str(config["name"])) | ||
| 102 | + self._log.parformat("Name") | ||
| 103 | + self._log(str(config["name"])) | ||
| 104 | + self._log("\n") | ||
| 105 | + self._log.parformat("Observation index") | ||
| 106 | + self._log(str(config["obsindx"])) | ||
| 107 | + self._log("\n") | ||
| 108 | + self._log.parformat("HDU index") | ||
| 109 | + self._log(str(config["hduindx"])) | ||
| 110 | + self._log("\n") | ||
| 111 | + | ||
| 112 | + # Print additional information | ||
| 113 | + for key in config: | ||
| 114 | + if key in ["name","hduindx","obsindx"]: | ||
| 115 | + continue | ||
| 116 | + self._log.parformat(str(key)) | ||
| 117 | + self._log(str(config[key])) | ||
| 118 | + self._log("\n") | ||
| 119 | + | ||
| 120 | + # Return | ||
| 121 | + return | ||
| 122 | + | ||
| 123 | + | ||
| 89 | # Public methods | 124 | # Public methods |
| 90 | def run(self): | 125 | def run(self): |
| 91 | """ | 126 | """ |
| 92 | Run the script. | 127 | Run the script. |
| 93 | """ | 128 | """ |
| 94 | - # Switch screen logging on | ||
| 95 | - self._log.cout(True) | 129 | + # Switch screen logging on in debug mode |
| 130 | + if self._logDebug(): | ||
| 131 | + self._log.cout(True) | ||
| 96 | 132 | ||
| 97 | # Get parameters | 133 | # Get parameters |
| 98 | self._get_parameters() | 134 | self._get_parameters() |
| 99 | 135 | ||
| 100 | - # Write input parameters into logger | 136 | + # Write header into logger |
| 101 | if self._logTerse(): | 137 | if self._logTerse(): |
| 102 | - self._log_parameters() | ||
| 103 | self._log("\n") | 138 | self._log("\n") |
| 104 | - | ||
| 105 | - self._log("\n") | ||
| 106 | - self._log.header1("Data storage entries") | ||
| 107 | - self._log("\n") | ||
| 108 | - self._log.parformat("Master index file") | ||
| 109 | - self._log(self._master_file) | ||
| 110 | - self._log("\n") | ||
| 111 | - self._log("\n") | 139 | + self._log.header1("Data storage entries") |
| 140 | + self._log.parformat("Master index file") | ||
| 141 | + self._log(self._master_file) | ||
| 142 | + self._log("\n") | ||
| 112 | 143 | ||
| 113 | # Open and load JSON file | 144 | # Open and load JSON file |
| 114 | json_data = open(self._master_file).read() | 145 | json_data = open(self._master_file).read() |
| @@ -117,24 +148,8 @@ class csiactdata(ctools.cscript): | @@ -117,24 +148,8 @@ class csiactdata(ctools.cscript): | ||
| 117 | 148 | ||
| 118 | # Initialise array for available names | 149 | # Initialise array for available names |
| 119 | self._prodnames = [] | 150 | self._prodnames = [] |
| 120 | - | ||
| 121 | - # Loop over configs and display unavailable storage first | ||
| 122 | - for config in configs: | ||
| 123 | - | ||
| 124 | - # Create hdu and obs index files | ||
| 125 | - hdu = os.path.join(self._datapath, config["hduindx"]) | ||
| 126 | - obs = os.path.join(self._datapath, config["obsindx"]) | ||
| 127 | - | ||
| 128 | - filename_hdu = gammalib.GFilename(str(hdu)+"[HDU_INDEX]") | ||
| 129 | - filename_obs = gammalib.GFilename(str(obs)+"[OBS_INDEX]") | ||
| 130 | - | ||
| 131 | - # Check if index files are available | ||
| 132 | - if not (filename_hdu.is_fits() and filename_obs.is_fits()): | ||
| 133 | - self._log.parformat(str(config["name"])) | ||
| 134 | - self._log("Not available") | ||
| 135 | - self._log("\n") | ||
| 136 | - | ||
| 137 | - | 151 | + |
| 152 | + # Write header into logger | ||
| 138 | self._log("\n") | 153 | self._log("\n") |
| 139 | self._log.header2("Available data configs") | 154 | self._log.header2("Available data configs") |
| 140 | 155 | ||
| @@ -142,40 +157,26 @@ class csiactdata(ctools.cscript): | @@ -142,40 +157,26 @@ class csiactdata(ctools.cscript): | ||
| 142 | for config in configs: | 157 | for config in configs: |
| 143 | 158 | ||
| 144 | # Create hdu and obs index files | 159 | # Create hdu and obs index files |
| 145 | - hdu = os.path.join(self._datapath, config["hduindx"]) | ||
| 146 | - obs = os.path.join(self._datapath, config["obsindx"]) | ||
| 147 | - | 160 | + hdu = os.path.join(self._datapath, config["hduindx"]) |
| 161 | + obs = os.path.join(self._datapath, config["obsindx"]) | ||
| 148 | filename_hdu = gammalib.GFilename(str(hdu)+"[HDU_INDEX]") | 162 | filename_hdu = gammalib.GFilename(str(hdu)+"[HDU_INDEX]") |
| 149 | filename_obs = gammalib.GFilename(str(obs)+"[OBS_INDEX]") | 163 | filename_obs = gammalib.GFilename(str(obs)+"[OBS_INDEX]") |
| 150 | 164 | ||
| 151 | - # Check if index files are available | 165 | + # If index files are available then log configuration |
| 166 | + # information ... | ||
| 152 | if (filename_hdu.is_fits() and filename_obs.is_fits()): | 167 | if (filename_hdu.is_fits() and filename_obs.is_fits()): |
| 153 | - | ||
| 154 | - # Log data information | ||
| 155 | - self._log("\n") | ||
| 156 | - self._log.header3(str(config["name"])) | ||
| 157 | - | ||
| 158 | - # Print important informations first | ||
| 159 | - self._log.parformat("Name") | ||
| 160 | - self._log(str(config["name"])) | ||
| 161 | - self._log("\n") | ||
| 162 | - self._log.parformat("Observation index") | ||
| 163 | - self._log(str(config["obsindx"])) | ||
| 164 | - self._log("\n") | ||
| 165 | - self._log.parformat("HDU index") | ||
| 166 | - self._log(str(config["hduindx"])) | ||
| 167 | - self._log("\n") | ||
| 168 | - | 168 | + |
| 169 | # Append to available names | 169 | # Append to available names |
| 170 | self._prodnames.append(str(config["name"])) | 170 | self._prodnames.append(str(config["name"])) |
| 171 | 171 | ||
| 172 | - # Print additional information | ||
| 173 | - for key in config: | ||
| 174 | - if key in ["name","hduindx","obsindx"]: | ||
| 175 | - continue | ||
| 176 | - self._log.parformat(str(key)) | ||
| 177 | - self._log(str(config[key])) | ||
| 178 | - self._log("\n") | 172 | + # Log information |
| 173 | + self._log_configuration(config) | ||
| 174 | + | ||
| 175 | + # ... otherwise log that the configuration is not available | ||
| 176 | + else: | ||
| 177 | + if self._logTerse(): | ||
| 178 | + self._log.header3(str(config["name"])) | ||
| 179 | + self._log(" Not available\n") | ||
| 179 | 180 | ||
| 180 | # Return | 181 | # Return |
| 181 | return | 182 | return |
| @@ -193,10 +194,12 @@ class csiactdata(ctools.cscript): | @@ -193,10 +194,12 @@ class csiactdata(ctools.cscript): | ||
| 193 | # Return | 194 | # Return |
| 194 | return | 195 | return |
| 195 | 196 | ||
| 196 | - | ||
| 197 | def names(self): | 197 | def names(self): |
| 198 | """ | 198 | """ |
| 199 | Return available FITS production names | 199 | Return available FITS production names |
| 200 | + | ||
| 201 | + Returns: | ||
| 202 | + List of available FITS production names. | ||
| 200 | """ | 203 | """ |
| 201 | 204 | ||
| 202 | # Return | 205 | # Return |
test/test_csiactdata.py
0 → 100755
| 1 | +#! /usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# This scripts performs unit tests for the csiactdata 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 csiactdata script # | ||
| 28 | +# ================================ # | ||
| 29 | +class Test(gammalib.GPythonTestSuite): | ||
| 30 | + """ | ||
| 31 | + Test class for csiactdata script. | ||
| 32 | + | ||
| 33 | + This test class makes unit tests for the csiactdata 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("csiactdata") | ||
| 55 | + | ||
| 56 | + # Append tests | ||
| 57 | + self.append(self._test_cmd, "Test csiactdata on command line") | ||
| 58 | + self.append(self._test_python, "Test csiactdata from Python") | ||
| 59 | + | ||
| 60 | + # Return | ||
| 61 | + return | ||
| 62 | + | ||
| 63 | + # Test csiactdata on command line | ||
| 64 | + def _test_cmd(self): | ||
| 65 | + """ | ||
| 66 | + Test csiactdata on the command line. | ||
| 67 | + """ | ||
| 68 | + # Kluge to set the command (installed version has no README file) | ||
| 69 | + if os.path.isfile("README"): | ||
| 70 | + csiactdata = "../cscripts/csiactdata.py" | ||
| 71 | + else: | ||
| 72 | + csiactdata = "csiactdata" | ||
| 73 | + | ||
| 74 | + # Setup csiactdata command | ||
| 75 | + cmd = csiactdata+' datapath="iactdata"'+ \ | ||
| 76 | + ' logfile="csiactdata_cmd1.log" chatter=1' | ||
| 77 | + | ||
| 78 | + # Execute csiactdata, make sure we catch any exception | ||
| 79 | + try: | ||
| 80 | + rc = os.system(cmd+" >/dev/null 2>&1") | ||
| 81 | + except: | ||
| 82 | + pass | ||
| 83 | + | ||
| 84 | + # Check if execution was successful | ||
| 85 | + self.test_assert(rc == 0, | ||
| 86 | + "Successful csiactdata execution on command line") | ||
| 87 | + | ||
| 88 | + # Setup csiactdata command | ||
| 89 | + cmd = csiactdata+' datapath="data_path_that_does_not_exist"'+ \ | ||
| 90 | + ' logfile="csiactdata_cmd2.log"' | ||
| 91 | + | ||
| 92 | + # Execute csiactdata, make sure we catch any exception | ||
| 93 | + try: | ||
| 94 | + rc = os.system(cmd+" >/dev/null 2>&1") | ||
| 95 | + except: | ||
| 96 | + pass | ||
| 97 | + | ||
| 98 | + # Check if execution failed | ||
| 99 | + self.test_assert(rc != 0, | ||
| 100 | + "Failure of csiactdata execution on command line") | ||
| 101 | + | ||
| 102 | + # Return | ||
| 103 | + return | ||
| 104 | + | ||
| 105 | + # Test csiactdata from Python | ||
| 106 | + def _test_python(self): | ||
| 107 | + """ | ||
| 108 | + Test csiactdata from Python. | ||
| 109 | + """ | ||
| 110 | + # Set-up csiactdata | ||
| 111 | + iactdata = cscripts.csiactdata() | ||
| 112 | + iactdata["datapath"] = "iactdata" | ||
| 113 | + iactdata["logfile"] = "csiactdata_py1.log" | ||
| 114 | + iactdata["chatter"] = 2 | ||
| 115 | + | ||
| 116 | + # Run csiactdata script and save run list | ||
| 117 | + iactdata.logFileOpen() # Make sure we get a log file | ||
| 118 | + iactdata.run() | ||
| 119 | + | ||
| 120 | + # Return | ||
| 121 | + return |
test/test_python_cscripts.py
| @@ -28,6 +28,7 @@ import test_csobs2caldb | @@ -28,6 +28,7 @@ import test_csobs2caldb | ||
| 28 | import test_cslightcrv | 28 | import test_cslightcrv |
| 29 | import test_csfindobs | 29 | import test_csfindobs |
| 30 | import test_csiactcopy | 30 | import test_csiactcopy |
| 31 | +import test_csiactdata | ||
| 31 | 32 | ||
| 32 | 33 | ||
| 33 | # ================== # | 34 | # ================== # |
| @@ -98,14 +99,17 @@ def test(installed=False, debug=False): | @@ -98,14 +99,17 @@ def test(installed=False, debug=False): | ||
| 98 | # Allocate test suites | 99 | # Allocate test suites |
| 99 | suite_csfindobs = test_csfindobs.Test() | 100 | suite_csfindobs = test_csfindobs.Test() |
| 100 | suite_csiactcopy = test_csiactcopy.Test() | 101 | suite_csiactcopy = test_csiactcopy.Test() |
| 102 | + suite_csiactdata = test_csiactdata.Test() | ||
| 101 | 103 | ||
| 102 | # Setup unit tests | 104 | # Setup unit tests |
| 103 | suite_csfindobs.set() | 105 | suite_csfindobs.set() |
| 104 | suite_csiactcopy.set() | 106 | suite_csiactcopy.set() |
| 107 | + suite_csiactdata.set() | ||
| 105 | 108 | ||
| 106 | # Append tests to container | 109 | # Append tests to container |
| 107 | suites.append(suite_csfindobs) | 110 | suites.append(suite_csfindobs) |
| 108 | suites.append(suite_csiactcopy) | 111 | suites.append(suite_csiactcopy) |
| 112 | + suites.append(suite_csiactdata) | ||
| 109 | 113 | ||
| 110 | # Run test suite | 114 | # Run test suite |
| 111 | success = suites.run() | 115 | success = suites.run() |