Coverage for tests/test_testUtils.py: 9%

105 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-13 16:09 -0700

1# 

2# Developed for the LSST Data Management System. 

3# This product includes software developed by the LSST Project 

4# (https://www.lsst.org). 

5# See the COPYRIGHT file at the top-level directory of this distribution 

6# for details of code ownership. 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the GNU General Public License 

19# along with this program. If not, see <https://www.gnu.org/licenses/>. 

20# 

21 

22import math 

23import unittest 

24 

25# importing lsst.geom adds assert methods to lsst.utils.tests.TestCase, 

26# and these are most or all of what this module tests 

27import lsst.geom 

28import lsst.utils.tests 

29 

30 

31class TestTestUtils(lsst.utils.tests.TestCase): 

32 """Test test methods added to lsst.utils.tests.TestCase 

33 """ 

34 def testAssertAnglesAlmostEqual(self): 

35 """Test assertAnglesAlmostEqual""" 

36 for angDeg in (0, 45, -75): 

37 ang0 = angDeg*lsst.geom.degrees 

38 self.assertAnglesAlmostEqual( 

39 ang0, 

40 ang0 + 0.01*lsst.geom.arcseconds, 

41 maxDiff=0.010001*lsst.geom.arcseconds, 

42 ) 

43 with self.assertRaises(AssertionError): 

44 self.assertAnglesAlmostEqual( 

45 ang0, 

46 ang0 + 0.01*lsst.geom.arcseconds, 

47 maxDiff=0.009999*lsst.geom.arcseconds, 

48 ) 

49 

50 self.assertAnglesAlmostEqual( 

51 ang0, 

52 ang0 - 0.01*lsst.geom.arcseconds, 

53 maxDiff=0.010001*lsst.geom.arcseconds, 

54 ) 

55 with self.assertRaises(AssertionError): 

56 self.assertAnglesAlmostEqual( 

57 ang0, 

58 ang0 - 0.01*lsst.geom.arcseconds, 

59 maxDiff=0.009999*lsst.geom.arcseconds, 

60 ) 

61 

62 self.assertAnglesAlmostEqual( 

63 ang0 - 720*lsst.geom.degrees, 

64 ang0 + 0.01*lsst.geom.arcseconds, 

65 maxDiff=0.010001*lsst.geom.arcseconds, 

66 ) 

67 with self.assertRaises(AssertionError): 

68 self.assertAnglesAlmostEqual( 

69 ang0 - 720*lsst.geom.degrees, 

70 ang0 + 0.01*lsst.geom.arcseconds, 

71 ignoreWrap=False, 

72 maxDiff=0.010001*lsst.geom.arcseconds, 

73 ) 

74 with self.assertRaises(AssertionError): 

75 self.assertAnglesAlmostEqual( 

76 ang0 - 720*lsst.geom.degrees, 

77 ang0 + 0.01*lsst.geom.arcseconds, 

78 maxDiff=0.009999*lsst.geom.arcseconds, 

79 ) 

80 

81 self.assertAnglesAlmostEqual( 

82 ang0, 

83 ang0 + 360*lsst.geom.degrees + 0.01*lsst.geom.arcseconds, 

84 maxDiff=0.010001*lsst.geom.arcseconds, 

85 ) 

86 with self.assertRaises(AssertionError): 

87 self.assertAnglesAlmostEqual( 

88 ang0, 

89 ang0 + 360*lsst.geom.degrees + 0.01*lsst.geom.arcseconds, 

90 ignoreWrap=False, 

91 maxDiff=0.010001*lsst.geom.arcseconds, 

92 ) 

93 with self.assertRaises(AssertionError): 

94 self.assertAnglesAlmostEqual( 

95 ang0, 

96 ang0 + 360*lsst.geom.degrees + 0.01*lsst.geom.arcseconds, 

97 maxDiff=0.009999*lsst.geom.arcseconds, 

98 ) 

99 

100 # Compare with NaN 

101 ang0 = float("NaN")*lsst.geom.degrees 

102 ang1 = 1.*lsst.geom.degrees 

103 with self.assertRaises(AssertionError): 

104 self.assertAnglesAlmostEqual(ang0, ang1) 

105 with self.assertRaises(AssertionError): 

106 self.assertAnglesAlmostEqual(ang1, ang0) 

107 

108 def testAssertBoxesAlmostEqual(self): 

109 """Test assertBoxesAlmostEqual""" 

110 for min0 in ((0, 0), (-1000.5, 5000.1)): 

111 min0 = lsst.geom.Point2D(*min0) 

112 for extent0 in ((2.01, 3.01), (5432, 2342)): 

113 extent0 = lsst.geom.Extent2D(*extent0) 

114 box0 = lsst.geom.Box2D(min0, extent0) 

115 self.assertBoxesAlmostEqual(box0, box0, maxDiff=1e-7) 

116 for deltaExtent in ((0.001, -0.001), (2, -3)): 

117 deltaExtent = lsst.geom.Extent2D(*deltaExtent) 

118 box1 = lsst.geom.Box2D( 

119 box0.getMin() + deltaExtent, box0.getMax()) 

120 radDiff = math.hypot(*deltaExtent) 

121 self.assertBoxesAlmostEqual( 

122 box0, box1, maxDiff=radDiff*1.00001) 

123 with self.assertRaises(AssertionError): 

124 self.assertBoxesAlmostEqual( 

125 box0, box1, maxDiff=radDiff*0.99999) 

126 

127 box2 = lsst.geom.Box2D( 

128 box0.getMin() - deltaExtent, box0.getMax()) 

129 self.assertBoxesAlmostEqual( 

130 box0, box2, maxDiff=radDiff*1.00001) 

131 with self.assertRaises(AssertionError): 

132 self.assertBoxesAlmostEqual( 

133 box0, box2, maxDiff=radDiff*0.999999) 

134 

135 box3 = lsst.geom.Box2D( 

136 box0.getMin(), box0.getMax() + deltaExtent) 

137 self.assertBoxesAlmostEqual( 

138 box0, box3, maxDiff=radDiff*1.00001) 

139 with self.assertRaises(AssertionError): 

140 self.assertBoxesAlmostEqual( 

141 box0, box3, maxDiff=radDiff*0.999999) 

142 

143 def testAssertSpherePointsAlmostEqual(self): 

144 """Test assertSpherePointsAlmostEqual""" 

145 for raDecDeg in ((45, 45), (-70, 89), (130, -89.5)): 

146 raDecDeg = [val*lsst.geom.degrees for val in raDecDeg] 

147 sp0 = lsst.geom.SpherePoint(*raDecDeg) 

148 self.assertSpherePointsAlmostEqual( 

149 sp0, sp0, maxSep=1e-7*lsst.geom.arcseconds) 

150 # make sure specifying msg is acceptable 

151 self.assertSpherePointsAlmostEqual( 

152 sp0, sp0, maxSep=1e-7*lsst.geom.arcseconds, msg="any") 

153 

154 for offAng in (0, 45, 90): 

155 offAng = offAng*lsst.geom.degrees 

156 for offDist in (0.001, 0.1): 

157 offDist = offDist*lsst.geom.arcseconds 

158 sp1 = sp0.offset(bearing=offAng, amount=offDist) 

159 self.assertSpherePointsAlmostEqual( 

160 sp0, sp1, maxSep=offDist*1.00001) 

161 with self.assertRaises(AssertionError): 

162 self.assertSpherePointsAlmostEqual( 

163 sp0, sp1, maxSep=offDist*0.99999) 

164 

165 # make sure msg is appended 

166 try: 

167 self.assertSpherePointsAlmostEqual( 

168 sp0, sp1, maxSep=offDist*0.99999, msg="boo") 

169 self.fail("Sphere point lists should be unequal") 

170 except AssertionError as e: 

171 errMsg = e.args[0] 

172 self.assertTrue(errMsg.endswith("boo")) 

173 

174 # test wraparound in RA 

175 sp2 = lsst.geom.SpherePoint( 

176 raDecDeg[0] + 360*lsst.geom.degrees, raDecDeg[1]) 

177 self.assertSpherePointsAlmostEqual( 

178 sp0, sp2, maxSep=1e-7*lsst.geom.arcseconds) 

179 

180 def testAssertSpherePointListsAlmostEqual(self): 

181 """Test assertSpherePointListsAlmostEqual 

182 """ 

183 splist0 = [lsst.geom.SpherePoint(val[0]*lsst.geom.degrees, val[1]*lsst.geom.degrees) 

184 for val in ((45, 45), (-70, 89), (130, -89.5))] 

185 self.assertSpherePointListsAlmostEqual(splist0, splist0) 

186 

187 offDist = 1.1 * lsst.geom.arcseconds 

188 splist1 = [sp0.offset(bearing=bearDeg*lsst.geom.degrees, amount=offDist) 

189 for sp0, bearDeg in zip(splist0, (-10, 78, 123))] 

190 self.assertSpherePointListsAlmostEqual( 

191 splist0, splist1, maxSep=offDist*1.00001) 

192 with self.assertRaises(AssertionError): 

193 self.assertSpherePointListsAlmostEqual( 

194 splist0, splist1, maxSep=offDist*0.99999) 

195 

196 # make sure msg is appended 

197 try: 

198 self.assertSpherePointListsAlmostEqual( 

199 splist0, splist1, maxSep=offDist*0.99999, msg="boo") 

200 self.fail("Sphere point lists should be unequal") 

201 except AssertionError as e: 

202 errMsg = e.args[0] 

203 self.assertTrue(errMsg.endswith("boo")) 

204 

205 def testAssertPairsAlmostEqual(self): 

206 """Test assertPairsAlmostEqual""" 

207 for pair0 in ((-5, 4), (-5, 0.001), (0, 0), (49, 0.1)): 

208 self.assertPairsAlmostEqual(pair0, pair0, maxDiff=1e-7) 

209 self.assertPairsAlmostEqual(lsst.geom.Point2D(*pair0), 

210 lsst.geom.Extent2D(*pair0), maxDiff=1e-7) 

211 for diff in ((0.001, 0), (-0.01, 0.03)): 

212 pair1 = [pair0[i] + diff[i] for i in range(2)] 

213 radialDiff = math.hypot(*diff) 

214 self.assertPairsAlmostEqual( 

215 pair0, pair1, maxDiff=radialDiff+1e-7) 

216 with self.assertRaises(AssertionError): 

217 self.assertPairsAlmostEqual( 

218 pair0, pair1, maxDiff=radialDiff-1e-7) 

219 

220 

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

222 pass 

223 

224 

225def setup_module(module): 

226 lsst.utils.tests.init() 

227 

228 

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

230 lsst.utils.tests.init() 

231 unittest.main()