Coverage for tests/test_constraints.py: 13%

93 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-10-02 08:00 +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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

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

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

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

18# (at your option) any later version. 

19# 

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

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

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

23# GNU General Public License for more details. 

24# 

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

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

27 

28"""Test the constraints model.""" 

29 

30import unittest 

31 

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

33from lsst.daf.butler.tests import DatasetTestHelper 

34 

35 

36class ConstraintsTestCase(unittest.TestCase, DatasetTestHelper): 

37 """Test constraints system.""" 

38 

39 def setUp(self): 

40 self.id = 0 

41 

42 # Create DatasetRefs to test against constraints model 

43 self.universe = DimensionUniverse() 

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

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

46 self.calexpA = self.makeDatasetRef( 

47 "calexp", 

48 dimensions, 

49 sc, 

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

51 ) 

52 

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

54 self.pviA = self.makeDatasetRef( 

55 "pvi", 

56 dimensions, 

57 sc, 

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

59 ) 

60 self.pviB = self.makeDatasetRef( 

61 "pvi", 

62 dimensions, 

63 sc, 

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

65 ) 

66 

67 def testSimpleAccept(self): 

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

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

70 

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

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

73 

74 # Dimension accept 

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

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

77 

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

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

80 

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

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

83 

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

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

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

87 

88 # Only accept instrument A pvi 

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

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

91 

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

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

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

95 

96 # Accept PVI for instrument B but not instrument A 

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

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

99 

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

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

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

103 

104 def testSimpleReject(self): 

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

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

107 

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

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

110 

111 def testAcceptReject(self): 

112 # Reject everything except calexp 

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

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

115 

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

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

118 

119 # Accept everything except calexp 

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

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

122 

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

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

125 

126 # Reject pvi but explicitly accept pvi for instrument A 

127 # Reject all instrument A but accept everything else 

128 # The reject here is superfluous 

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

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

131 

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

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

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

135 

136 # Accept everything except pvi from other than instrument A 

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

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

139 

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

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

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

143 

144 def testWildcardReject(self): 

145 # Reject everything 

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

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

148 

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

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

151 

152 # Reject all instrument A but accept everything else 

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

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

155 

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

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

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

159 

160 def testWildcardAccept(self): 

161 # Accept everything 

162 config = ConstraintsConfig({}) 

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

164 

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

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

167 

168 # Accept everything 

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

170 

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

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

173 

174 # Accept everything explicitly 

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

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

177 

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

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

180 

181 # Accept all instrument A but reject everything else 

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

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

184 

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

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

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

188 

189 def testEdgeCases(self): 

190 # Accept everything and reject everything 

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

192 with self.assertRaises(ValidationError): 

193 Constraints(config, universe=self.universe) 

194 

195 

196if __name__ == "__main__": 

197 unittest.main()