Coverage for tests/test_constraints.py: 13%

93 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-23 09:30 +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 def setUp(self): 

32 self.id = 0 

33 

34 # Create DatasetRefs to test against constraints model 

35 self.universe = DimensionUniverse() 

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

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

38 self.calexpA = self.makeDatasetRef( 

39 "calexp", 

40 dimensions, 

41 sc, 

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

43 ) 

44 

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

46 self.pviA = self.makeDatasetRef( 

47 "pvi", 

48 dimensions, 

49 sc, 

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

51 ) 

52 self.pviB = self.makeDatasetRef( 

53 "pvi", 

54 dimensions, 

55 sc, 

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

57 ) 

58 

59 def testSimpleAccept(self): 

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

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

62 

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

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

65 

66 # Dimension accept 

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

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

69 

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

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

72 

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

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

75 

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

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

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

79 

80 # Only accept instrument A pvi 

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

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

83 

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

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

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

87 

88 # Accept PVI for instrument B but not instrument A 

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

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

91 

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

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

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

95 

96 def testSimpleReject(self): 

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

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

99 

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

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

102 

103 def testAcceptReject(self): 

104 # Reject everything except calexp 

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

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

107 

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

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

110 

111 # Accept everything except calexp 

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

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

114 

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

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

117 

118 # Reject pvi but explicitly accept pvi for instrument A 

119 # Reject all instrument A but accept everything else 

120 # The reject here is superfluous 

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

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

123 

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

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

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

127 

128 # Accept everything except pvi from other than instrument A 

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

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

131 

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

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

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

135 

136 def testWildcardReject(self): 

137 # Reject everything 

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

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

140 

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

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

143 

144 # Reject all instrument A but accept everything else 

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

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

147 

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

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

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

151 

152 def testWildcardAccept(self): 

153 # Accept everything 

154 config = ConstraintsConfig({}) 

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

156 

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

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

159 

160 # Accept everything 

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

162 

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

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

165 

166 # Accept everything explicitly 

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

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

169 

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

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

172 

173 # Accept all instrument A but reject everything else 

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

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

176 

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

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

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

180 

181 def testEdgeCases(self): 

182 # Accept everything and reject everything 

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

184 with self.assertRaises(ValidationError): 

185 Constraints(config, universe=self.universe) 

186 

187 

188if __name__ == "__main__": 

189 unittest.main()