Coverage for tests/test_constraints.py: 14%
95 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 02:51 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 02:51 -0800
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/>.
22"""Test the constraints model."""
24import unittest
26from lsst.daf.butler import Constraints, ConstraintsConfig, DimensionUniverse, StorageClass, ValidationError
27from lsst.daf.butler.tests import DatasetTestHelper
30class ConstraintsTestCase(unittest.TestCase, DatasetTestHelper):
31 def setUp(self):
32 self.id = 0
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", dimensions, sc, {"instrument": "A", "physical_filter": "u"}, conform=False
40 )
42 dimensions = self.universe.extract(("visit", "detector", "instrument"))
43 self.pviA = self.makeDatasetRef("pvi", dimensions, sc, {"instrument": "A", "visit": 1}, conform=False)
44 self.pviB = self.makeDatasetRef("pvi", dimensions, sc, {"instrument": "B", "visit": 2}, conform=False)
46 def testSimpleAccept(self):
47 config = ConstraintsConfig({"accept": ["calexp", "ExposureF"]})
48 constraints = Constraints(config, universe=self.universe)
50 self.assertTrue(constraints.isAcceptable(self.calexpA))
51 self.assertFalse(constraints.isAcceptable(self.pviA))
53 # Dimension accept
54 config = ConstraintsConfig({"accept": ["visit+physical_filter+instrument", "ExposureF"]})
55 constraints = Constraints(config, universe=self.universe)
57 self.assertTrue(constraints.isAcceptable(self.calexpA))
58 self.assertFalse(constraints.isAcceptable(self.pviA))
60 config = ConstraintsConfig({"accept": ["visit+detector+instrument", "ExposureF"]})
61 constraints = Constraints(config, universe=self.universe)
63 self.assertFalse(constraints.isAcceptable(self.calexpA))
64 self.assertTrue(constraints.isAcceptable(self.pviA))
65 self.assertTrue(constraints.isAcceptable(self.pviA))
67 # Only accept instrument A pvi
68 config = ConstraintsConfig({"accept": [{"instrument<A>": ["pvi"]}]})
69 constraints = Constraints(config, universe=self.universe)
71 self.assertFalse(constraints.isAcceptable(self.calexpA))
72 self.assertTrue(constraints.isAcceptable(self.pviA))
73 self.assertFalse(constraints.isAcceptable(self.pviB))
75 # Accept PVI for instrument B but not instrument A
76 config = ConstraintsConfig({"accept": ["calexp", {"instrument<B>": ["pvi"]}]})
77 constraints = Constraints(config, universe=self.universe)
79 self.assertTrue(constraints.isAcceptable(self.calexpA))
80 self.assertFalse(constraints.isAcceptable(self.pviA))
81 self.assertTrue(constraints.isAcceptable(self.pviB))
83 def testSimpleReject(self):
84 config = ConstraintsConfig({"reject": ["calexp", "ExposureF"]})
85 constraints = Constraints(config, universe=self.universe)
87 self.assertFalse(constraints.isAcceptable(self.calexpA))
88 self.assertTrue(constraints.isAcceptable(self.pviA))
90 def testAcceptReject(self):
91 # Reject everything except calexp
92 config = ConstraintsConfig({"accept": ["calexp"], "reject": ["all"]})
93 constraints = Constraints(config, universe=self.universe)
95 self.assertTrue(constraints.isAcceptable(self.calexpA))
96 self.assertFalse(constraints.isAcceptable(self.pviA))
98 # Accept everything except calexp
99 config = ConstraintsConfig({"reject": ["calexp"], "accept": ["all"]})
100 constraints = Constraints(config, universe=self.universe)
102 self.assertFalse(constraints.isAcceptable(self.calexpA))
103 self.assertTrue(constraints.isAcceptable(self.pviA))
105 # Reject pvi but explicitly accept pvi for instrument A
106 # Reject all instrument A but accept everything else
107 # The reject here is superfluous
108 config = ConstraintsConfig({"accept": [{"instrument<A>": ["pvi"]}], "reject": ["pvi"]})
109 constraints = Constraints(config, universe=self.universe)
111 self.assertFalse(constraints.isAcceptable(self.calexpA))
112 self.assertTrue(constraints.isAcceptable(self.pviA))
113 self.assertFalse(constraints.isAcceptable(self.pviB))
115 # Accept everything except pvi from other than instrument A
116 config = ConstraintsConfig({"accept": ["all", {"instrument<A>": ["pvi"]}], "reject": ["pvi"]})
117 constraints = Constraints(config, universe=self.universe)
119 self.assertTrue(constraints.isAcceptable(self.calexpA))
120 self.assertTrue(constraints.isAcceptable(self.pviA))
121 self.assertFalse(constraints.isAcceptable(self.pviB))
123 def testWildcardReject(self):
124 # Reject everything
125 config = ConstraintsConfig({"reject": ["all"]})
126 constraints = Constraints(config, universe=self.universe)
128 self.assertFalse(constraints.isAcceptable(self.calexpA))
129 self.assertFalse(constraints.isAcceptable(self.pviA))
131 # Reject all instrument A but accept everything else
132 config = ConstraintsConfig({"reject": [{"instrument<A>": ["all"]}]})
133 constraints = Constraints(config, universe=self.universe)
135 self.assertFalse(constraints.isAcceptable(self.calexpA))
136 self.assertFalse(constraints.isAcceptable(self.pviA))
137 self.assertTrue(constraints.isAcceptable(self.pviB))
139 def testWildcardAccept(self):
140 # Accept everything
141 config = ConstraintsConfig({})
142 constraints = Constraints(config, universe=self.universe)
144 self.assertTrue(constraints.isAcceptable(self.calexpA))
145 self.assertTrue(constraints.isAcceptable(self.pviA))
147 # Accept everything
148 constraints = Constraints(None, universe=self.universe)
150 self.assertTrue(constraints.isAcceptable(self.calexpA))
151 self.assertTrue(constraints.isAcceptable(self.pviA))
153 # Accept everything explicitly
154 config = ConstraintsConfig({"accept": ["all"]})
155 constraints = Constraints(config, universe=self.universe)
157 self.assertTrue(constraints.isAcceptable(self.calexpA))
158 self.assertTrue(constraints.isAcceptable(self.pviA))
160 # Accept all instrument A but reject everything else
161 config = ConstraintsConfig({"accept": [{"instrument<A>": ["all"]}]})
162 constraints = Constraints(config, universe=self.universe)
164 self.assertTrue(constraints.isAcceptable(self.calexpA))
165 self.assertTrue(constraints.isAcceptable(self.pviA))
166 self.assertFalse(constraints.isAcceptable(self.pviB))
168 def testEdgeCases(self):
169 # Accept everything and reject everything
170 config = ConstraintsConfig({"accept": ["all"], "reject": ["all"]})
171 with self.assertRaises(ValidationError):
172 Constraints(config, universe=self.universe)
175if __name__ == "__main__": 175 ↛ 176line 175 didn't jump to line 176, because the condition on line 175 was never true
176 unittest.main()