Coverage for tests/test_makeapdb.py: 30%
62 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-01 10:02 +0000
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-01 10:02 +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 @contextlib.contextmanager
83 def _temporaryBuffer(self):
84 tempStdErr = io.StringIO()
85 savedStdErr = sys.stderr
86 sys.stderr = tempStdErr
87 try:
88 yield tempStdErr
89 finally:
90 sys.stderr = savedStdErr
92 def testOldConfig(self):
93 """Verify that old-style config options are caught.
94 """
95 args = '-c diaPipe.apdb.db_url="dummy"'
96 with self._temporaryBuffer() as buffer:
97 with self.assertRaises(SystemExit):
98 self._parseString(args)
100 output = buffer.getvalue()
101 self.assertIn("try dropping 'diaPipe.apdb'", output)
103 def testOldConfigFile(self):
104 """Verify that old-style config file entries are caught.
105 """
106 with lsst.utils.tests.getTempFilePath(ext=".py") as configFile:
107 with open(configFile, mode='wt') as config:
108 config.write('config.diaPipe.apdb.db_url = "dummy"\n')
110 args = f"-C {configFile}"
111 with self._temporaryBuffer() as buffer:
112 with self.assertRaises(SystemExit):
113 self._parseString(args)
115 output = buffer.getvalue()
116 self.assertIn("try dropping 'diaPipe.apdb'", output)
119class MemoryTester(lsst.utils.tests.MemoryTestCase):
120 pass
123def setup_module(module):
124 lsst.utils.tests.init()
127if __name__ == "__main__": 127 ↛ 128line 127 didn't jump to line 128, because the condition on line 127 was never true
128 lsst.utils.tests.init()
129 unittest.main()