Coverage for tests/test_convertReferenceCatalog.py: 16%

108 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-30 02:15 -0700

1# This file is part of meas_algorithms. 

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 os.path 

23import sys 

24import unittest 

25import unittest.mock 

26import tempfile 

27 

28from lsst.pex.config import FieldValidationError 

29from lsst.meas.algorithms import convertReferenceCatalog 

30import lsst.utils 

31 

32from convertReferenceCatalogTestBase import makeConvertConfig 

33 

34 

35class TestMain(lsst.utils.tests.TestCase): 

36 """Test mocking commandline arguments and calling 

37 ``convertReferenceCatalog.main()``. 

38 """ 

39 def setUp(self): 

40 self.inpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/mockrefcat/") 

41 self.expected_files = [os.path.join(self.inpath, "123.fits"), 

42 os.path.join(self.inpath, "124.fits"), 

43 os.path.join(self.inpath, "125.fits")] 

44 

45 def test_main_args(self): 

46 """Test that main configures the task and calls run() with the correct 

47 file list. 

48 """ 

49 outdir = tempfile.TemporaryDirectory() 

50 outpath = outdir.name 

51 args = ["convertReferenceCatalog", 

52 outpath, 

53 os.path.join(self.inpath, "mock_config.py"), 

54 os.path.join(self.inpath, "*.fits")] 

55 with unittest.mock.patch.object(convertReferenceCatalog.ConvertReferenceCatalogTask, "run") as run, \ 

56 unittest.mock.patch.object(sys, "argv", args): 

57 convertReferenceCatalog.main() 

58 # Test with sets because the glob can come out in any order. 

59 self.assertEqual(set(run.call_args.args[0]), set(self.expected_files)) 

60 # This is necessary to avoid a ResourceWarning. 

61 outdir.cleanup() 

62 

63 def test_main_args_bad_config(self): 

64 """Test that a bad config file produces a useful error, i.e. that 

65 main() validates the config. 

66 """ 

67 outdir = tempfile.TemporaryDirectory() 

68 outpath = outdir.name 

69 args = ["convertReferenceCatalog", 

70 outpath, 

71 os.path.join(self.inpath, "bad_config.py"), 

72 os.path.join(self.inpath, "*.fits")] 

73 with self.assertRaisesRegex(FieldValidationError, "Field 'ra_name' failed validation"), \ 

74 unittest.mock.patch.object(sys, "argv", args): 

75 convertReferenceCatalog.main() 

76 # This is necessary to avoid a ResourceWarning. 

77 outdir.cleanup() 

78 

79 def test_main_args_expanded_glob(self): 

80 """Test that an un-quoted glob (i.e. list of files) fails with a 

81 useful error. 

82 """ 

83 outdir = tempfile.TemporaryDirectory() 

84 outpath = outdir.name 

85 args = ["convertReferenceCatalog", 

86 outpath, 

87 os.path.join(self.inpath, "mock_config.py"), 

88 # an un-quoted glob will be shell-expanded to a list of files. 

89 "file1", "file2", "file3"] 

90 msg = "Final argument must be a quoted file glob, not a shell-expanded list of files." 

91 with self.assertRaisesRegex(RuntimeError, msg), \ 

92 unittest.mock.patch.object(sys, "argv", args): 

93 convertReferenceCatalog.main() 

94 # This is necessary to avoid a ResourceWarning. 

95 outdir.cleanup() 

96 

97 

98class ConvertReferenceCatalogConfigValidateTestCase(lsst.utils.tests.TestCase): 

99 """Test validation of ConvertReferenceCatalogConfig.""" 

100 def testValidateRaDecMag(self): 

101 config = makeConvertConfig() 

102 config.validate() 

103 

104 for name in ("ra_name", "dec_name", "mag_column_list"): 

105 with self.subTest(name=name): 

106 config = makeConvertConfig() 

107 setattr(config, name, None) 

108 with self.assertRaises(ValueError): 

109 config.validate() 

110 

111 def testValidateRaDecErr(self): 

112 # check that a basic config validates 

113 config = makeConvertConfig(withRaDecErr=True) 

114 config.validate() 

115 

116 # check that a config with any of these fields missing does not validate 

117 for name in ("ra_err_name", "dec_err_name", "coord_err_unit"): 

118 with self.subTest(name=name): 

119 config = makeConvertConfig(withRaDecErr=True) 

120 setattr(config, name, None) 

121 with self.assertRaises(ValueError): 

122 config.validate() 

123 

124 # check that coord_err_unit must be an astropy unit 

125 config = makeConvertConfig(withRaDecErr=True) 

126 config.coord_err_unit = "nonsense unit" 

127 with self.assertRaisesRegex(ValueError, "is not a valid astropy unit string"): 

128 config.validate() 

129 

130 def testValidateMagErr(self): 

131 config = makeConvertConfig(withMagErr=True) 

132 config.validate() 

133 

134 # test for missing names 

135 for name in config.mag_column_list: 

136 with self.subTest(name=name): 

137 config = makeConvertConfig(withMagErr=True) 

138 del config.mag_err_column_map[name] 

139 with self.assertRaises(ValueError): 

140 config.validate() 

141 

142 # test for incorrect names 

143 for name in config.mag_column_list: 

144 with self.subTest(name=name): 

145 config = makeConvertConfig(withMagErr=True) 

146 config.mag_err_column_map["badName"] = config.mag_err_column_map[name] 

147 del config.mag_err_column_map[name] 

148 with self.assertRaises(ValueError): 

149 config.validate() 

150 

151 def testValidatePm(self): 

152 basicNames = ["pm_ra_name", "pm_dec_name", "epoch_name", "epoch_format", "epoch_scale"] 

153 

154 for withPmErr in (False, True): 

155 config = makeConvertConfig(withPm=True, withPmErr=withPmErr) 

156 config.validate() 

157 del config 

158 

159 if withPmErr: 

160 names = basicNames + ["pm_ra_err_name", "pm_dec_err_name"] 

161 else: 

162 names = basicNames 

163 for name in names: 

164 with self.subTest(name=name, withPmErr=withPmErr): 

165 config = makeConvertConfig(withPm=True, withPmErr=withPmErr) 

166 setattr(config, name, None) 

167 with self.assertRaises(ValueError): 

168 config.validate() 

169 

170 def testValidateParallax(self): 

171 """Validation should fail if any parallax-related fields are missing. 

172 """ 

173 names = ["parallax_name", "epoch_name", "epoch_format", "epoch_scale", "parallax_err_name"] 

174 

175 config = makeConvertConfig(withParallax=True) 

176 config.validate() 

177 del config 

178 

179 for name in names: 

180 with self.subTest(name=name): 

181 config = makeConvertConfig(withParallax=True) 

182 setattr(config, name, None) 

183 with self.assertRaises(ValueError, msg=name): 

184 config.validate() 

185 

186 

187class TestMemory(lsst.utils.tests.MemoryTestCase): 

188 pass 

189 

190 

191def setup_module(module): 

192 lsst.utils.tests.init() 

193 

194 

195if __name__ == "__main__": 195 ↛ 196line 195 didn't jump to line 196, because the condition on line 195 was never true

196 lsst.utils.tests.init() 

197 unittest.main()