Coverage for tests/test_makeapdb.py: 34%
66 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 21:33 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 21:33 +0000
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 # Allow ap_association or AP_ASSOCIATION.
88 self.assertRegex(parsed.config.extra_schema_file, "(?i)ap_association")
90 @contextlib.contextmanager
91 def _temporaryBuffer(self):
92 tempStdErr = io.StringIO()
93 savedStdErr = sys.stderr
94 sys.stderr = tempStdErr
95 try:
96 yield tempStdErr
97 finally:
98 sys.stderr = savedStdErr
100 def testOldConfig(self):
101 """Verify that old-style config options are caught.
102 """
103 args = '-c diaPipe.apdb.db_url="dummy"'
104 with self._temporaryBuffer() as buffer:
105 with self.assertRaises(SystemExit):
106 self._parseString(args)
108 output = buffer.getvalue()
109 self.assertIn("try dropping 'diaPipe.apdb'", output)
111 def testOldConfigFile(self):
112 """Verify that old-style config file entries are caught.
113 """
114 with lsst.utils.tests.getTempFilePath(ext=".py") as configFile:
115 with open(configFile, mode='wt') as config:
116 config.write('config.diaPipe.apdb.db_url = "dummy"\n')
118 args = f"-C {configFile}"
119 with self._temporaryBuffer() as buffer:
120 with self.assertRaises(SystemExit):
121 self._parseString(args)
123 output = buffer.getvalue()
124 self.assertIn("try dropping 'diaPipe.apdb'", output)
127class MemoryTester(lsst.utils.tests.MemoryTestCase):
128 pass
131def setup_module(module):
132 lsst.utils.tests.init()
135if __name__ == "__main__": 135 ↛ 136line 135 didn't jump to line 136, because the condition on line 135 was never true
136 lsst.utils.tests.init()
137 unittest.main()