Coverage for tests/test_pho2rafts.py : 36%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of obs_lsst.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22"""Test the phosimToRafts.py script in bin.src."""
24import unittest
25import yaml
26import os
27import shutil
28from tempfile import mkdtemp
30import lsst.log
31import lsst.utils
32import lsst.utils.tests
34from lsst.obs.lsst.script.phosimToRafts import processPhosimData
36TESTDIR = os.path.abspath(os.path.dirname(__file__))
37EXECUTABLEDIR = os.path.normpath(os.path.join(TESTDIR, os.path.pardir, 'bin'))
38DATADIR = os.path.normpath(os.path.join(TESTDIR, os.path.pardir, 'data', 'input', 'phosim'))
39EXPYAMLDIR = os.path.normpath(os.path.join(TESTDIR, os.path.pardir, 'policy', 'phosim'))
42class PhosimToRaftsTestCase(lsst.utils.tests.ExecutablesTestCase):
43 """Test the phosimToRafts.py utility script."""
45 def setUp(self):
46 self.testdir = mkdtemp(dir=TESTDIR)
48 def tearDown(self):
49 shutil.rmtree(self.testdir, ignore_errors=True)
51 def testPhosimToRaftsExecutable(self):
52 """Test phosimToRafts.py"""
53 self.assertExecutable("phosimToRafts.py",
54 root_dir=EXECUTABLEDIR,
55 args=[DATADIR, "--visit", "204595", "--output_dir", self.testdir],
56 msg="phosimToRafts.py failed")
57 self.assertRaftsEqual()
59 def testPhosimToRaftsWithId(self):
60 """Test just with an ID"""
61 # Redirect lsst logger to Python to hide irrelevant log messages
62 with lsst.log.UsePythonLogging():
63 # Check that we get log messages but we do not need to test
64 # all combinations
65 with self.assertLogs("lsst.obs.lsst.script.phosimToRafts", level="INFO") as cm:
66 processPhosimData("visit=204595", None, DATADIR, self.testdir)
68 self.assertIn("DataId = {'expId': 204595, 'run': '204595', 'snap': 0}", cm.output[0])
69 self.assertIn("Processing data from detector R11_S20", cm.output[1])
71 self.assertRaftsEqual()
73 def testPhosimToRaftsWithNone(self):
74 """Test that no visits are needed"""
75 with lsst.log.UsePythonLogging():
76 processPhosimData(None, None, DATADIR, self.testdir)
77 self.assertRaftsEqual()
79 def testPhosimToRaftsWithBoth(self):
80 """Test visit and id can be specified if they match"""
81 with lsst.log.UsePythonLogging():
82 processPhosimData("visit=204595", 204595, DATADIR, self.testdir)
83 self.assertRaftsEqual()
85 def testPhosimToRaftsWithVisit(self):
86 """Specify just a visit"""
87 with lsst.log.UsePythonLogging():
88 processPhosimData(None, 204595, DATADIR, self.testdir)
89 self.assertRaftsEqual()
91 def testPhosimToRaftsFail(self):
92 """Test some failure modes"""
93 with self.assertRaises(RuntimeError):
94 processPhosimData(None, None, None, None)
95 with self.assertRaises(RuntimeError):
96 processPhosimData("visit=1234", 4321, None, None)
97 with self.assertRaises(RuntimeError):
98 processPhosimData("visit=b1234", None, None, None)
100 # A visit that does not exist
101 with self.assertRaises(RuntimeError):
102 processPhosimData(None, 1234, DATADIR, self.testdir)
104 def assertRaftsEqual(self):
105 # Read file produced by script above
106 with open(os.path.join(self.testdir, 'R11.yaml')) as fh:
107 doc = yaml.load(fh, Loader=yaml.CSafeLoader)
108 # Read file with expected outputs
109 with open(os.path.join(EXPYAMLDIR, 'R11.yaml')) as fh:
110 exp_doc = yaml.load(fh, Loader=yaml.CSafeLoader)
111 # The test data only have the S20 chip
112 # Note: as of python 3 the iterator on dicts is not a list, so we need
113 # to manually create one since we are deleting entries from the dict.
114 for key in list(exp_doc['R11']['amplifiers']):
115 if key != 'S20':
116 del exp_doc['R11']['amplifiers'][key]
117 # Can't compare since the serial depends on how many rafts are in the
118 # input repository. The test repository only has 1.
119 for d in (doc, exp_doc):
120 del d['R11']['raftSerial']
121 self.assertEqual(doc, exp_doc)
124if __name__ == "__main__": 124 ↛ 125line 124 didn't jump to line 125, because the condition on line 124 was never true
125 lsst.utils.tests.init()
126 unittest.main()