Coverage for tests/test_makeapdb.py: 38%

Shortcuts 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

66 statements  

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

89 

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 

99 

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) 

107 

108 output = buffer.getvalue() 

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

110 

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

117 

118 args = f"-C {configFile}" 

119 with self._temporaryBuffer() as buffer: 

120 with self.assertRaises(SystemExit): 

121 self._parseString(args) 

122 

123 output = buffer.getvalue() 

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

125 

126 

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

128 pass 

129 

130 

131def setup_module(module): 

132 lsst.utils.tests.init() 

133 

134 

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