Coverage for tests/test_makeapdb.py: 29%

62 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 12:09 +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/>. 

21 

22import contextlib 

23import io 

24import shlex 

25import sys 

26import unittest 

27 

28import lsst.utils.tests 

29from lsst.ap.pipe.make_apdb import ConfigOnlyParser 

30 

31 

32class MakeApdbParserTestSuite(lsst.utils.tests.TestCase): 

33 

34 def _parseString(self, commandLine): 

35 """Tokenize and parse a command line string. 

36 

37 Parameters 

38 ---------- 

39 commandLine : `str` 

40 a string containing Unix-style command line arguments, but not the 

41 name of the program 

42 

43 Returns 

44 ------- 

45 parsed : `argparse.Namespace` 

46 The parsed command line. 

47 """ 

48 return self.parser.parse_args(shlex.split(commandLine)) 

49 

50 def setUp(self): 

51 self.parser = ConfigOnlyParser() 

52 

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) 

59 

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') 

67 

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') 

75 

76 args = f"-C {configFile}" 

77 parsed = self._parseString(args) 

78 

79 self.assertEqual(parsed.config.db_url, 'dummy') 

80 self.assertEqual(parsed.config.dia_object_index, 'pix_id_iov') 

81 

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 

91 

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) 

99 

100 output = buffer.getvalue() 

101 self.assertIn("try dropping 'diaPipe.apdb'", output) 

102 

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') 

109 

110 args = f"-C {configFile}" 

111 with self._temporaryBuffer() as buffer: 

112 with self.assertRaises(SystemExit): 

113 self._parseString(args) 

114 

115 output = buffer.getvalue() 

116 self.assertIn("try dropping 'diaPipe.apdb'", output) 

117 

118 

119class MemoryTester(lsst.utils.tests.MemoryTestCase): 

120 pass 

121 

122 

123def setup_module(module): 

124 lsst.utils.tests.init() 

125 

126 

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()