Coverage for tests/test_taskmetadata.py: 9%

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

163 statements  

1# This file is part of pipe_base. 

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 json 

23import unittest 

24 

25try: 

26 import numpy 

27except ImportError: 

28 numpy = None 

29 

30from lsst.pipe.base import TaskMetadata 

31 

32 

33class TaskMetadataTestCase(unittest.TestCase): 

34 def testTaskMetadata(self): 

35 """Full test of TaskMetadata API.""" 

36 meta = TaskMetadata() 

37 meta["test"] = 42 

38 self.assertEqual(meta["test"], 42) 

39 meta.add("test", 55) 

40 self.assertEqual(meta["test"], 55) 

41 meta.add("test", [1, 2]) 

42 self.assertEqual(meta.getScalar("test"), 2) 

43 self.assertEqual(meta.getArray("test"), [42, 55, 1, 2]) 

44 meta["new.int"] = 30 

45 self.assertEqual(meta["new.int"], 30) 

46 self.assertEqual(meta["new"]["int"], 30) 

47 self.assertEqual(meta.getArray("new.int"), [30]) 

48 self.assertEqual(meta.getScalar("new.int"), 30) 

49 self.assertIsInstance(meta["new"], TaskMetadata) 

50 self.assertIsInstance(meta.getScalar("new"), TaskMetadata) 

51 self.assertIsInstance(meta.getArray("new")[0], TaskMetadata) 

52 meta.add("new.int", 24) 

53 self.assertEqual(meta["new.int"], 24) 

54 meta["new.str"] = "str" 

55 self.assertEqual(meta["new.str"], "str") 

56 

57 meta["test"] = "string" 

58 self.assertEqual(meta["test"], "string") 

59 

60 self.assertIn("test", meta) 

61 self.assertIn("new", meta) 

62 self.assertIn("new.int", meta) 

63 self.assertNotIn("new2.int", meta) 

64 self.assertNotIn("test2", meta) 

65 

66 self.assertEqual(meta.paramNames(topLevelOnly=False), {"test", "new.int", "new.str"}) 

67 self.assertEqual(meta.paramNames(topLevelOnly=True), {"test"}) 

68 self.assertEqual(meta.names(topLevelOnly=False), {"test", "new", "new.int", "new.str"}) 

69 self.assertEqual(meta.keys(), ("test", "new")) 

70 self.assertEqual(len(meta), 2) 

71 self.assertEqual(len(meta["new"]), 2) 

72 

73 meta["new_array"] = ("a", "b") 

74 self.assertEqual(meta["new_array"], "b") 

75 self.assertEqual(meta.getArray("new_array"), ["a", "b"]) 

76 meta.add("new_array", "c") 

77 self.assertEqual(meta["new_array"], "c") 

78 self.assertEqual(meta.getArray("new_array"), ["a", "b", "c"]) 

79 meta["new_array"] = [1, 2, 3] 

80 self.assertEqual(meta.getArray("new_array"), [1, 2, 3]) 

81 

82 meta["meta"] = 5 

83 meta["meta"] = TaskMetadata() 

84 self.assertIsInstance(meta["meta"], TaskMetadata) 

85 meta["meta.a.b"] = "deep" 

86 self.assertEqual(meta["meta.a.b"], "deep") 

87 self.assertIsInstance(meta["meta.a"], TaskMetadata) 

88 

89 meta.add("via_scalar", 22) 

90 self.assertEqual(meta["via_scalar"], 22) 

91 

92 del meta["test"] 

93 self.assertNotIn("test", meta) 

94 del meta["new.int"] 

95 self.assertNotIn("new.int", meta) 

96 self.assertIn("new", meta) 

97 with self.assertRaises(KeyError): 

98 del meta["test2"] 

99 with self.assertRaises(KeyError) as cm: 

100 # Check that deleting a hierarchy that is not present also 

101 # reports the correct key. 

102 del meta["new.a.b.c"] 

103 self.assertIn("new.a.b.c", str(cm.exception)) 

104 

105 with self.assertRaises(KeyError) as cm: 

106 # Something that doesn't exist at all. 

107 meta["something.a.b"] 

108 # Ensure that the full key hierarchy is reported in the error message. 

109 self.assertIn("something.a.b", str(cm.exception)) 

110 

111 with self.assertRaises(KeyError) as cm: 

112 # Something that does exist at level 2 but not further down. 

113 meta["new.str.a"] 

114 # Ensure that the full key hierarchy is reported in the error message. 

115 self.assertIn("new.str.a", str(cm.exception)) 

116 

117 with self.assertRaises(KeyError) as cm: 

118 # Something that only exists at level 1. 

119 meta["new.str3"] 

120 # Ensure that the full key hierarchy is reported in the error message. 

121 self.assertIn("new.str3", str(cm.exception)) 

122 

123 with self.assertRaises(KeyError) as cm: 

124 # Something that only exists at level 1 but as an array. 

125 meta.getArray("new.str3") 

126 # Ensure that the full key hierarchy is reported in the error message. 

127 self.assertIn("new.str3", str(cm.exception)) 

128 

129 with self.assertRaises(ValueError): 

130 meta.add("new", 1) 

131 

132 with self.assertRaises(KeyError): 

133 meta[42] 

134 

135 with self.assertRaises(KeyError): 

136 meta["not.present"] 

137 

138 with self.assertRaises(KeyError): 

139 meta["not_present"] 

140 

141 with self.assertRaises(KeyError): 

142 meta.getScalar("not_present") 

143 

144 with self.assertRaises(KeyError): 

145 meta.getArray("not_present") 

146 

147 def testValidation(self): 

148 """Test that validation works.""" 

149 meta = TaskMetadata() 

150 

151 class BadThing: 

152 pass 

153 

154 with self.assertRaises(ValueError): 

155 meta["bad"] = BadThing() 

156 

157 with self.assertRaises(ValueError): 

158 meta["bad_list"] = [BadThing()] 

159 

160 meta.add("int", 4) 

161 with self.assertRaises(ValueError): 

162 meta.add("int", "string") 

163 

164 with self.assertRaises(ValueError): 

165 meta.add("mapping", {}) 

166 

167 with self.assertRaises(ValueError): 

168 meta.add("int", ["string", "array"]) 

169 

170 with self.assertRaises(ValueError): 

171 meta["mixed"] = [1, "one"] 

172 

173 def testDict(self): 

174 """Construct a TaskMetadata from a dictionary.""" 

175 d = {"a": "b", "c": 1, "d": [1, 2], "e": {"f": "g", "h": {"i": [3, 4]}}} 

176 

177 meta = TaskMetadata.from_dict(d) 

178 self.assertEqual(meta["a"], "b") 

179 self.assertEqual(meta["e.f"], "g") 

180 self.assertEqual(meta.getArray("d"), [1, 2]) 

181 self.assertEqual(meta["e.h.i"], 4) 

182 

183 j = meta.json() 

184 meta2 = TaskMetadata.parse_obj(json.loads(j)) 

185 self.assertEqual(meta2, meta) 

186 

187 # Round trip. 

188 meta3 = TaskMetadata.from_metadata(meta) 

189 self.assertEqual(meta3, meta) 

190 

191 # Add a new element that would be a single-element array. 

192 # This will not equate as equal because from_metadata will move 

193 # the item to the scalar part of the model and pydantic does not 

194 # see them as equal. 

195 meta3.add("e.new", 5) 

196 meta4 = TaskMetadata.from_metadata(meta3) 

197 self.assertNotEqual(meta4, meta3) 

198 self.assertEqual(meta4["e.new"], meta3["e.new"]) 

199 del meta4["e.new"] 

200 del meta3["e.new"] 

201 self.assertEqual(meta4, meta3) 

202 

203 def testDeprecated(self): 

204 """Test the deprecated interface issues warnings.""" 

205 meta = TaskMetadata.from_dict({"a": 1, "b": 2}) 

206 

207 with self.assertWarns(FutureWarning): 

208 meta.set("c", 3) 

209 self.assertEqual(meta["c"], 3) 

210 with self.assertWarns(FutureWarning): 

211 self.assertEqual(meta.getAsDouble("c"), 3.0) 

212 

213 with self.assertWarns(FutureWarning): 

214 meta.remove("c") 

215 self.assertNotIn("c", meta) 

216 with self.assertWarns(FutureWarning): 

217 meta.remove("d") 

218 

219 with self.assertWarns(FutureWarning): 

220 self.assertEqual(meta.names(topLevelOnly=True), set(meta.keys())) 

221 

222 @unittest.skipIf(not numpy, "Numpy is required for this test.") 

223 def testNumpy(self): 

224 meta = TaskMetadata() 

225 meta["int"] = numpy.int64(42) 

226 self.assertEqual(meta["int"], 42) 

227 self.assertEqual(type(meta["int"]), int) 

228 

229 meta["float"] = numpy.float64(3.14) 

230 self.assertEqual(meta["float"], 3.14) 

231 self.assertEqual(type(meta["float"]), float) 

232 

233 meta.add("floatArray", [numpy.float64(1.5), numpy.float64(3.0)]) 

234 self.assertEqual(meta.getArray("floatArray"), [1.5, 3.0]) 

235 self.assertEqual(type(meta["floatArray"]), float) 

236 

237 meta.add("intArray", [numpy.int64(1), numpy.int64(3)]) 

238 self.assertEqual(meta.getArray("intArray"), [1, 3]) 

239 self.assertEqual(type(meta["intArray"]), int) 

240 

241 with self.assertRaises(ValueError): 

242 meta.add("mixed", [1.5, numpy.float64(4.5)]) 

243 

244 with self.assertRaises(ValueError): 

245 meta["numpy"] = numpy.zeros(5) 

246 

247 

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

249 unittest.main()