testing.py.in
8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# ==========================================================================
# Python base class for unit tests
#
# Copyright (C) 2016-2018 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 <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import os
import sys
import pickle
import gammalib
# ================================ #
# Python base class for unit tests #
# ================================ #
class test(gammalib.GPythonTestSuite):
"""
Python base class for unit tests
"""
# Constructor
def __init__(self):
"""
Constructor
"""
# Call base class constructor
gammalib.GPythonTestSuite.__init__(self)
# Set test data directory
self._datadir = os.environ['TEST_DATA']
# Set some standard test data (additional test data will be set in
# the derived classes as needed)
self._events = self._datadir + '/crab_events.fits'
self._cntcube = self._datadir + '/crab_cntmap.fits'
self._model = self._datadir + '/crab.xml'
self._caldb = 'prod2'
self._irf = 'South_0.5h'
# Return
return
# Set script name
def _script(self, name):
"""
Set script name
Parameters
----------
name : str
Script name
Returns
-------
script : str
Script name including path
"""
# Set script name dependent on whether the software is installed or
# not. If the software is not installed the 'TEST_BUILDDIR' environment
# variable will exist
if 'TEST_BUILDDIR' in os.environ:
script = '@PYTHON_COVERAGE@' + os.environ['TEST_BUILDDIR'] + \
'/cscripts/'+name+'.py'
else:
script = name
# Return script name
return script
# Set tool name
def _tool(self, name):
"""
Set tool name
Parameters
----------
name : str
Tool name
Returns
-------
tool : str
Tool name including path
"""
# Set tool name dependent on whether the software is installed or
# not. If the software is not installed the 'TEST_BUILDDIR' environment
# variable will exist
if 'TEST_BUILDDIR' in os.environ:
tool = os.environ['TEST_BUILDDIR'] + '/src/'+name+'/'+name
else:
tool = name
# Return tool name
return tool
# Set comscript name
def _comscript(self, name):
"""
Set comscript name
Parameters
----------
name : str
COMPTEL script name
Returns
-------
script : str
COMPTEL script name including path
"""
# Set script name dependent on whether the software is installed or
# not. If the software is not installed the 'TEST_BUILDDIR' environment
# variable will exist
if 'TEST_BUILDDIR' in os.environ:
script = '@PYTHON_COVERAGE@' + os.environ['TEST_BUILDDIR'] + \
'/modules/comscripts/'+name+'.py'
else:
script = name
# Return COMPTEL script name
return script
# Execute command and catch any exception
def _execute(self, cmd, success=True):
"""
Execute command
Parameters
----------
cmd : str
Command string to be executed
success : bool
True if success is expected, False otherwise
Returns
-------
rc : int
Return code
"""
# Execute command, make sure we catch any exception
try:
rc = os.system(cmd+' > ctools_test_output.dmp 2>&1')
except:
rc = -1
# If an exception occured then put output on screen
if success and rc != 0 or not success and rc == 0:
print('\n*** Unexpected exception (%d) for command %s:' % (rc, cmd))
f = open('ctools_test_output.dmp', 'r')
for line in f:
print(line.rstrip('\n'))
f.close()
# Remove test output
os.remove('ctools_test_output.dmp')
# Return return code
return rc
# Return observation container with 3 identical event lists
def _obs_events(self):
"""
Return observation container with 3 identical event lists
Returns
-------
obs : `~gammalib.GObservations`
Observations container
"""
# Load CTA event list
cta = gammalib.GCTAObservation(self._events)
# Allocate observation container
obs = gammalib.GObservations()
# Append CTA observation with ID "0001"
cta.id('0001')
obs.append(cta)
# Append CTA observation with ID "0002"
cta.id('0002')
obs.append(cta)
# Append CTA observation with ID "0003"
cta.id('0003')
obs.append(cta)
# Return observation container
return obs
# Return observation container with event list, counts cube and LAT
# observation
def _obs_mixed(self):
"""
Return observation container with event list, counts cube and LAT
observation
Returns
-------
obs : `~gammalib.GObservations`
Observations container
"""
# Load CTA event list
obs1 = gammalib.GCTAObservation(self._events)
obs1.id('0001')
# Load CTA counts cube
obs2 = gammalib.GCTAObservation(self._cntcube)
obs2.id('0002')
# Create empty LAT observation
obs3 = gammalib.GLATObservation()
obs3.id('0001')
obs3.events(gammalib.GLATEventList())
# Allocate observation container
obs = gammalib.GObservations()
# Append observations to container
obs.append(obs1)
obs.append(obs2)
obs.append(obs3)
# Return observation container
return obs
# Check
def _check_help(self, tool):
"""
Check for help text
Parameters
----------
name : str
Tool name
"""
# Setup tool --help option
cmd = tool+' --help'
# Check if execution was successful in case that the CTOOLS
# environment variable was set or failed otherwise
if 'CTOOLS' in os.environ:
self.test_value(self._execute(cmd), 0,
'Check successful execution with --help option')
else:
self.test_assert(self._execute(cmd, success=False) != 0,
'Check execution failure with --help option')
# Return
return
# Test pickeling
def _pickeling(self, object):
"""
Test class pickeling
Parameters
----------
object : `~gammalib.GBase`
Class instance
"""
# Get class name
name = object.__class__.__name__
# Start exception catching
self.test_try('Test pickeling of "%s" class.' % name)
# Test if pickling results in an exception
try:
dump = pickle.dumps(object)
obj = pickle.loads(dump)
dump_again = pickle.dumps(obj)
self.test_assert(dump == dump_again,
'Check pickled dump of "%s\n%s\n%s"' %
(name, str(object), str(obj)))
self.test_try_success()
except:
_, e, _ = sys.exc_info()
self.test_try_failure('Error in pickeling "%s" (%s).' %
(name, str(e)))
# Return
return obj