Coverage for tests/test_constraints.py: 13%

93 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-25 15:14 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22"""Test the constraints model.""" 

23 

24import unittest 

25 

26from lsst.daf.butler import Constraints, ConstraintsConfig, DimensionUniverse, StorageClass, ValidationError 

27from lsst.daf.butler.tests import DatasetTestHelper 

28 

29 

30class ConstraintsTestCase(unittest.TestCase, DatasetTestHelper): 

31 """Test constraints system.""" 

32 

33 def setUp(self): 

34 self.id = 0 

35 

36 # Create DatasetRefs to test against constraints model 

37 self.universe = DimensionUniverse() 

38 dimensions = self.universe.extract(("visit", "physical_filter", "instrument")) 

39 sc = StorageClass("DummySC", dict, None) 

40 self.calexpA = self.makeDatasetRef( 

41 "calexp", 

42 dimensions, 

43 sc, 

44 {"instrument": "A", "physical_filter": "u", "visit": 3}, 

45 ) 

46 

47 dimensions = self.universe.extract(("visit", "detector", "instrument")) 

48 self.pviA = self.makeDatasetRef( 

49 "pvi", 

50 dimensions, 

51 sc, 

52 {"instrument": "A", "visit": 1, "detector": 0}, 

53 ) 

54 self.pviB = self.makeDatasetRef( 

55 "pvi", 

56 dimensions, 

57 sc, 

58 {"instrument": "B", "visit": 2, "detector": 0}, 

59 ) 

60 

61 def testSimpleAccept(self): 

62 config = ConstraintsConfig({"accept": ["calexp", "ExposureF"]}) 

63 constraints = Constraints(config, universe=self.universe) 

64 

65 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

66 self.assertFalse(constraints.isAcceptable(self.pviA)) 

67 

68 # Dimension accept 

69 config = ConstraintsConfig({"accept": ["visit+physical_filter+instrument", "ExposureF"]}) 

70 constraints = Constraints(config, universe=self.universe) 

71 

72 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

73 self.assertFalse(constraints.isAcceptable(self.pviA)) 

74 

75 config = ConstraintsConfig({"accept": ["visit+detector+instrument", "ExposureF"]}) 

76 constraints = Constraints(config, universe=self.universe) 

77 

78 self.assertFalse(constraints.isAcceptable(self.calexpA)) 

79 self.assertTrue(constraints.isAcceptable(self.pviA)) 

80 self.assertTrue(constraints.isAcceptable(self.pviA)) 

81 

82 # Only accept instrument A pvi 

83 config = ConstraintsConfig({"accept": [{"instrument<A>": ["pvi"]}]}) 

84 constraints = Constraints(config, universe=self.universe) 

85 

86 self.assertFalse(constraints.isAcceptable(self.calexpA)) 

87 self.assertTrue(constraints.isAcceptable(self.pviA)) 

88 self.assertFalse(constraints.isAcceptable(self.pviB)) 

89 

90 # Accept PVI for instrument B but not instrument A 

91 config = ConstraintsConfig({"accept": ["calexp", {"instrument<B>": ["pvi"]}]}) 

92 constraints = Constraints(config, universe=self.universe) 

93 

94 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

95 self.assertFalse(constraints.isAcceptable(self.pviA)) 

96 self.assertTrue(constraints.isAcceptable(self.pviB)) 

97 

98 def testSimpleReject(self): 

99 config = ConstraintsConfig({"reject": ["calexp", "ExposureF"]}) 

100 constraints = Constraints(config, universe=self.universe) 

101 

102 self.assertFalse(constraints.isAcceptable(self.calexpA)) 

103 self.assertTrue(constraints.isAcceptable(self.pviA)) 

104 

105 def testAcceptReject(self): 

106 # Reject everything except calexp 

107 config = ConstraintsConfig({"accept": ["calexp"], "reject": ["all"]}) 

108 constraints = Constraints(config, universe=self.universe) 

109 

110 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

111 self.assertFalse(constraints.isAcceptable(self.pviA)) 

112 

113 # Accept everything except calexp 

114 config = ConstraintsConfig({"reject": ["calexp"], "accept": ["all"]}) 

115 constraints = Constraints(config, universe=self.universe) 

116 

117 self.assertFalse(constraints.isAcceptable(self.calexpA)) 

118 self.assertTrue(constraints.isAcceptable(self.pviA)) 

119 

120 # Reject pvi but explicitly accept pvi for instrument A 

121 # Reject all instrument A but accept everything else 

122 # The reject here is superfluous 

123 config = ConstraintsConfig({"accept": [{"instrument<A>": ["pvi"]}], "reject": ["pvi"]}) 

124 constraints = Constraints(config, universe=self.universe) 

125 

126 self.assertFalse(constraints.isAcceptable(self.calexpA)) 

127 self.assertTrue(constraints.isAcceptable(self.pviA)) 

128 self.assertFalse(constraints.isAcceptable(self.pviB)) 

129 

130 # Accept everything except pvi from other than instrument A 

131 config = ConstraintsConfig({"accept": ["all", {"instrument<A>": ["pvi"]}], "reject": ["pvi"]}) 

132 constraints = Constraints(config, universe=self.universe) 

133 

134 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

135 self.assertTrue(constraints.isAcceptable(self.pviA)) 

136 self.assertFalse(constraints.isAcceptable(self.pviB)) 

137 

138 def testWildcardReject(self): 

139 # Reject everything 

140 config = ConstraintsConfig({"reject": ["all"]}) 

141 constraints = Constraints(config, universe=self.universe) 

142 

143 self.assertFalse(constraints.isAcceptable(self.calexpA)) 

144 self.assertFalse(constraints.isAcceptable(self.pviA)) 

145 

146 # Reject all instrument A but accept everything else 

147 config = ConstraintsConfig({"reject": [{"instrument<A>": ["all"]}]}) 

148 constraints = Constraints(config, universe=self.universe) 

149 

150 self.assertFalse(constraints.isAcceptable(self.calexpA)) 

151 self.assertFalse(constraints.isAcceptable(self.pviA)) 

152 self.assertTrue(constraints.isAcceptable(self.pviB)) 

153 

154 def testWildcardAccept(self): 

155 # Accept everything 

156 config = ConstraintsConfig({}) 

157 constraints = Constraints(config, universe=self.universe) 

158 

159 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

160 self.assertTrue(constraints.isAcceptable(self.pviA)) 

161 

162 # Accept everything 

163 constraints = Constraints(None, universe=self.universe) 

164 

165 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

166 self.assertTrue(constraints.isAcceptable(self.pviA)) 

167 

168 # Accept everything explicitly 

169 config = ConstraintsConfig({"accept": ["all"]}) 

170 constraints = Constraints(config, universe=self.universe) 

171 

172 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

173 self.assertTrue(constraints.isAcceptable(self.pviA)) 

174 

175 # Accept all instrument A but reject everything else 

176 config = ConstraintsConfig({"accept": [{"instrument<A>": ["all"]}]}) 

177 constraints = Constraints(config, universe=self.universe) 

178 

179 self.assertTrue(constraints.isAcceptable(self.calexpA)) 

180 self.assertTrue(constraints.isAcceptable(self.pviA)) 

181 self.assertFalse(constraints.isAcceptable(self.pviB)) 

182 

183 def testEdgeCases(self): 

184 # Accept everything and reject everything 

185 config = ConstraintsConfig({"accept": ["all"], "reject": ["all"]}) 

186 with self.assertRaises(ValidationError): 

187 Constraints(config, universe=self.universe) 

188 

189 

190if __name__ == "__main__": 

191 unittest.main()