Commits
48 48 | # |
49 49 | # <synopsis> |
50 50 | # Test the slsearch task and the sl.search() method upon which it is built. |
51 51 | # </synopsis> |
52 52 | # |
53 53 | # <example> |
54 54 | # |
55 55 | # This test runs as part of the CASA python unit test suite and can be run from |
56 56 | # the command line via eg |
57 57 | # |
58 - | # `echo $CASAPATH/bin/casa | sed -e 's$ $/$'` --nologger --log2term -c `echo $CASAPATH | awk '{print $1}'`/code/xmlcasa/scripts/regressions/admin/runUnitTest.py test_slsearch[test1,test2,...] |
58 + | # casa --nogui --log2term -c runUnitTest.py test_slsearch |
59 59 | # |
60 60 | # </example> |
61 61 | # |
62 62 | # <motivation> |
63 63 | # To provide a test standard for the slsearch task to ensure |
64 64 | # coding changes do not break the associated bits |
65 65 | # </motivation> |
66 66 | # |
67 67 | |
68 68 | ########################################################################### |
69 69 | from __future__ import absolute_import |
70 70 | import os |
71 71 | import shutil |
72 72 | import unittest |
73 73 | |
74 74 | from casatasks.private.casa_transition import is_CASA6 |
75 75 | if is_CASA6: |
76 76 | from casatools import ctsys, spectralline, table |
77 - | from casatasks import slsearch |
77 + | from casatasks import slsearch, casalog |
78 78 | else: |
79 79 | import casac |
80 80 | from tasks import * |
81 81 | from taskinit import * |
82 82 | from taskinit import sltool as spectralline |
83 83 | from taskinit import tbtool as table |
84 84 | from __main__ import * |
85 85 | |
86 86 | good_table = "biglist.tbl" |
87 87 | |
104 104 | chemnames=chemnames, qns=qns, |
105 105 | intensity=intensity, smu2=smu2, loga=loga, |
106 106 | el=el, eu=eu, rrlinclude=rrlinclude, |
107 107 | rrlonly=rrlonly, verbose=verbose, logfile=logfile, |
108 108 | append=append |
109 109 | ) |
110 110 | except: |
111 111 | raise |
112 112 | finally: |
113 113 | mysl.done() |
114 - | |
115 114 | |
116 115 | def run_slsearch( |
117 116 | tab, outfile, freqrange, species, reconly, |
118 117 | chemnames, qns, intensity, smu2, loga, el, |
119 118 | eu, rrlinclude, rrlonly, verbose, logfile, |
120 119 | append |
121 120 | ): |
122 121 | if not is_CASA6: |
123 122 | default(slsearch) |
124 123 | return slsearch( |
182 181 | else: |
183 182 | datapath=os.path.join(os.environ.get('CASAPATH').split()[0],'data/regression/unittest/slsearch') |
184 183 | shutil.copytree(os.path.join(datapath,good_table), good_table) |
185 184 | |
186 185 | def tearDown(self): |
187 186 | shutil.rmtree(good_table) |
188 187 | self.assertTrue(len(_tb.showcache()) == 0) |
189 188 | |
190 189 | def test_exceptions(self): |
191 190 | """slsearch: Test various exception cases""" |
191 + | |
192 + | # check_search is used when run_search is expected to fail |
193 + | # includes closing the returned spectralline tool in |
194 + | # case run_search does not throw an exception |
195 + | def check_search( |
196 + | tab, outfile, freqrange, species, reconly, |
197 + | chemnames, qns, intensity, smu2, loga, el, |
198 + | eu, rrlinclude, rrlonly, verbose, logfile, |
199 + | append |
200 + | ): |
201 + | mysl = run_search(tab, outfile, freqrange, species, reconly, |
202 + | chemnames, qns, intensity, smu2, loga, el, |
203 + | eu, rrlinclude, rrlonly, verbose, logfile, |
204 + | append) |
205 + | mysl.done() |
206 + | |
207 + | |
192 208 | def testit( |
193 209 | tab, outfile, freqrange, species, reconly, |
194 210 | chemnames, qns, intensity, smu2, loga, el, |
195 211 | eu, rrlinclude, rrlonly, verbose, logfile, |
196 212 | append |
197 213 | ): |
198 - | for i in [0, 1]: |
214 + | for i in [0,1]: |
199 215 | if (i==0): |
200 216 | self.assertRaises( |
201 - | Exception, run_search, tab, outfile, |
217 + | Exception, check_search, tab, outfile, |
202 218 | freqrange, species, reconly, chemnames, |
203 219 | qns, intensity, smu2, loga, el, eu, |
204 220 | rrlinclude, rrlonly, verbose, logfile, |
205 221 | append |
206 222 | ) |
207 223 | self.assertTrue(len(_tb.showcache()) == 0) |
208 224 | else: |
209 - | self.assertEqual( |
210 - | run_slsearch( |
225 + | # CASA6 slsearch raises an exception, CASA5 returns None |
226 + | if is_CASA6: |
227 + | self.assertRaises( |
228 + | Exception, run_slsearch, |
211 229 | tab, outfile, freqrange, species, |
212 230 | reconly, chemnames, qns, intensity, |
213 231 | smu2, loga, el, eu, rrlinclude, rrlonly, |
214 232 | verbose, logfile, append |
215 - | ), None |
216 - | ) |
233 + | ) |
234 + | else: |
235 + | self.assertEqual( |
236 + | run_slsearch( |
237 + | tab, outfile, freqrange, species, |
238 + | reconly, chemnames, qns, intensity, |
239 + | smu2, loga, el, eu, rrlinclude, rrlonly, |
240 + | verbose, logfile, append |
241 + | ), None |
242 + | ) |
243 + | # either way, no tables should be open |
217 244 | self.assertTrue(len(_tb.showcache()) == 0) |
218 245 | |
219 246 | # bogus input table name |
220 - | # CASA6 throws an exception, 5 does not, this captures both cases |
247 + | # the version of testit used here throws an exception if the |
248 + | # expected exceptions or return values did not happen |
221 249 | try: |
222 - | OK = False |
223 250 | testit( |
224 251 | tab="fred.tbl", outfile="x", freqrange=[0, 100], species=[], |
225 252 | reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1], |
226 253 | loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=True, |
227 254 | verbose=True, logfile="", append=True |
228 255 | ) |
229 - | # this can only be OK if it's not CASA6 |
230 - | if not is_CASA6: |
231 - | # bad output name |
232 - | self.assertTrue(len(_tb.showcache()) == 0) |
233 - | OK = True |
234 256 | except: |
235 - | # this can only be OK if it is CASA6 |
236 - | if is_CASA6: |
237 - | self.assertTrue(len(_tb.showcache()) == 0) |
238 - | OK = True |
239 - | |
240 - | self.assertTrue(OK) |
257 + | casalog.post("Failure in test_exceptions testing bogus input table name", 'SEVERE') |
258 + | raise |
241 259 | |
242 260 | # bad output name |
243 261 | try: |
244 - | OK = False |
245 262 | testit( |
246 - | tab=good_table, outfile="/x", freqrange=[0, 100], species=[], |
263 + | tab=good_table, outfile="foo/bar/bad", freqrange=[0, 100], species=[], |
247 264 | reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1], |
248 265 | loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=True, |
249 266 | verbose=True, logfile="", append=True |
250 267 | ) |
251 - | if not is_CASA6: |
252 - | OK = True |
253 268 | except: |
254 - | if is_CASA6: |
255 - | OK = True |
256 - | |
257 - | self.assertTrue(OK) |
258 - | |
269 + | casalog.post("Failure in test_exceptions testing bad output name", 'SEVERE') |
270 + | raise |
271 + | |
259 272 | def test_table(self): |
260 273 | """ test various settings of the table parameter""" |
261 274 | |
262 275 | # no table name works because it defaults to the system spectral line table |
263 276 | self._testit( |
264 277 | tab="", outfile="", freqrange=[0, 100], species=[], |
265 278 | reconly=True, chemnames=[], qns=[], intensity=[-1], smu2=[-1], |
266 279 | loga=[-1], el=[-1], eu=[-1], rrlinclude=True, rrlonly=False, |
267 280 | verbose=False, logfile="", append=True, nrows=59998 |
268 281 | ) |
479 492 | tab=good_table, outfile="", freqrange=[0, 100], species=[], |
480 493 | reconly=True, chemnames=[], qns=[], |
481 494 | intensity=[], smu2=[], |
482 495 | loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True, |
483 496 | verbose=False, logfile="", append=True, nrows=5821 |
484 497 | ) |
485 498 | |
486 499 | def test_logfile(self): |
487 500 | """ test various settings of the logfile and append parameters""" |
488 501 | |
502 + | def count_lines(txtfile): |
503 + | count = 0 |
504 + | with open(txtfile,'r') as f: |
505 + | for count,l in enumerate(f,1): |
506 + | pass |
507 + | return count |
508 + | |
489 509 | logfile = "xx.log" |
490 510 | # verbose = False so no logfile should be written |
491 511 | self._testit( |
492 512 | tab=good_table, outfile="", freqrange=[0, 100], species=[], |
493 513 | reconly=True, chemnames=[], qns=[], |
494 514 | intensity=[], smu2=[], |
495 515 | loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True, |
496 516 | verbose=False, logfile=logfile, append=False, nrows=5821 |
497 517 | ) |
498 518 | self.assertFalse(os.path.exists(logfile)) |
499 519 | # verbose and overwrite |
500 520 | self._testit( |
501 521 | tab=good_table, outfile="", freqrange=[0, 100], species=[], |
502 522 | reconly=True, chemnames=[], qns=[], |
503 523 | intensity=[], smu2=[], |
504 524 | loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True, |
505 525 | verbose=True, logfile=logfile, append=False, nrows=5821 |
506 526 | ) |
507 527 | self.assertTrue(os.path.exists(logfile)) |
508 528 | |
509 - | num_lines = sum(1 for line in open(logfile)) |
529 + | num_lines = count_lines(logfile) |
530 + | self.assertEqual(num_lines, 5822) |
510 531 | # append (twice) |
511 - | self.assertEquals(num_lines, 5822) |
512 532 | self._testit( |
513 533 | tab=good_table, outfile="", freqrange=[0, 100], species=[], |
514 534 | reconly=True, chemnames=[], qns=[], |
515 535 | intensity=[], smu2=[], |
516 536 | loga=[], el=[], eu=[], rrlinclude=True, rrlonly=True, |
517 537 | verbose=True, logfile=logfile, append=True, nrows=5821 |
518 538 | ) |
519 539 | self.assertTrue(os.path.exists(logfile)) |
520 - | num_lines = sum(1 for line in open(logfile)) |
521 - | self.assertEquals(num_lines, 3*5822) |
540 + | num_lines = count_lines(logfile) |
541 + | self.assertEqual(num_lines, 3*5822) |
522 542 | os.remove(logfile) |
523 543 | |
524 544 | |
525 545 | def suite(): |
526 546 | return [slsearch_test] |
527 547 | |
528 548 | if is_CASA6: |
529 549 | if __name__ == '__main__': |
530 550 | unittest.main() |