Coverage for tests/test_makeapdb.py : 33%

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 ap_pipe.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
22import contextlib
23import io
24import shlex
25import sys
26import unittest
28import lsst.utils.tests
29from lsst.ap.pipe.make_apdb import ConfigOnlyParser
32class MakeApdbParserTestSuite(lsst.utils.tests.TestCase):
34 def _parseString(self, commandLine):
35 """Tokenize and parse a command line string.
37 Parameters
38 ----------
39 commandLine : `str`
40 a string containing Unix-style command line arguments, but not the
41 name of the program
43 Returns
44 -------
45 parsed : `argparse.Namespace`
46 The parsed command line.
47 """
48 return self.parser.parse_args(shlex.split(commandLine))
50 def setUp(self):
51 self.parser = ConfigOnlyParser()
53 def testExtras(self):
54 """Verify that a command line containing extra arguments is rejected.
55 """
56 args = '-c db_url="dummy" --id visit=42'
57 with self.assertRaises(SystemExit):
58 self._parseString(args)
60 def testSetValue(self):
61 """Verify that command-line arguments get propagated.
62 """
63 args = '-c db_url="dummy" -c dia_object_index=pix_id_iov'
64 parsed = self._parseString(args)
65 self.assertEqual(parsed.config.db_url, 'dummy')
66 self.assertEqual(parsed.config.dia_object_index, 'pix_id_iov')
68 def testSetValueFile(self):
69 """Verify that config files are handled correctly.
70 """
71 with lsst.utils.tests.getTempFilePath(ext=".py") as configFile:
72 with open(configFile, mode='wt') as config:
73 config.write('config.db_url = "dummy"\n')
74 config.write('config.dia_object_index = "pix_id_iov"\n')
76 args = f"-C {configFile}"
77 parsed = self._parseString(args)
79 self.assertEqual(parsed.config.db_url, 'dummy')
80 self.assertEqual(parsed.config.dia_object_index, 'pix_id_iov')
82 def testDiaPipeDefaults(self):
83 """Verify that DiaPipelineTask's custom APDB settings are included.
84 """
85 args = '-c db_url="dummy"'
86 parsed = self._parseString(args)
87 self.assertIn("ap_association", parsed.config.extra_schema_file)
89 @contextlib.contextmanager
90 def _temporaryBuffer(self):
91 tempStdErr = io.StringIO()
92 savedStdErr = sys.stderr
93 sys.stderr = tempStdErr
94 try:
95 yield tempStdErr
96 finally:
97 sys.stderr = savedStdErr
99 def testOldConfig(self):
100 """Verify that old-style config options are caught.
101 """
102 args = '-c diaPipe.apdb.db_url="dummy"'
103 with self._temporaryBuffer() as buffer:
104 with self.assertRaises(SystemExit):
105 self._parseString(args)
107 output = buffer.getvalue()
108 self.assertIn("try dropping 'diaPipe.apdb'", output)
110 def testOldConfigFile(self):
111 """Verify that old-style config file entries are caught.
112 """
113 with lsst.utils.tests.getTempFilePath(ext=".py") as configFile:
114 with open(configFile, mode='wt') as config:
115 config.write('config.diaPipe.apdb.db_url = "dummy"\n')
117 args = f"-C {configFile}"
118 with self._temporaryBuffer() as buffer:
119 with self.assertRaises(SystemExit):
120 self._parseString(args)
122 output = buffer.getvalue()
123 self.assertIn("try dropping 'diaPipe.apdb'", output)
126class MemoryTester(lsst.utils.tests.MemoryTestCase):
127 pass
130def setup_module(module):
131 lsst.utils.tests.init()
134if __name__ == "__main__": 134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true
135 lsst.utils.tests.init()
136 unittest.main()