Coverage for tests/test_constraints.py : 14%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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, StorageClass, ValidationError, DimensionUniverse
27from lsst.daf.butler.tests import DatasetTestHelper
30class ConstraintsTestCase(unittest.TestCase, DatasetTestHelper):
32 def setUp(self):
33 self.id = 0
35 # Create DatasetRefs to test against constraints model
36 self.universe = DimensionUniverse()
37 dimensions = self.universe.extract(("visit", "physical_filter", "instrument"))
38 sc = StorageClass("DummySC", dict, None)
39 self.calexpA = self.makeDatasetRef("calexp", dimensions, sc,
40 {"instrument": "A", "physical_filter": "u"},
41 conform=False)
43 dimensions = self.universe.extract(("visit", "detector", "instrument"))
44 self.pviA = self.makeDatasetRef("pvi", dimensions, sc, {"instrument": "A", "visit": 1},
45 conform=False)
46 self.pviB = self.makeDatasetRef("pvi", dimensions, sc, {"instrument": "B", "visit": 2},
47 conform=False)
49 def testSimpleAccept(self):
50 config = ConstraintsConfig({"accept": ["calexp", "ExposureF"]})
51 constraints = Constraints(config, universe=self.universe)
53 self.assertTrue(constraints.isAcceptable(self.calexpA))
54 self.assertFalse(constraints.isAcceptable(self.pviA))
56 # Dimension accept
57 config = ConstraintsConfig({"accept": ["visit+physical_filter+instrument", "ExposureF"]})
58 constraints = Constraints(config, universe=self.universe)
60 self.assertTrue(constraints.isAcceptable(self.calexpA))
61 self.assertFalse(constraints.isAcceptable(self.pviA))
63 config = ConstraintsConfig({"accept": ["visit+detector+instrument", "ExposureF"]})
64 constraints = Constraints(config, universe=self.universe)
66 self.assertFalse(constraints.isAcceptable(self.calexpA))
67 self.assertTrue(constraints.isAcceptable(self.pviA))
68 self.assertTrue(constraints.isAcceptable(self.pviA))
70 # Only accept instrument A pvi
71 config = ConstraintsConfig({"accept": [{"instrument<A>": ["pvi"]}]})
72 constraints = Constraints(config, universe=self.universe)
74 self.assertFalse(constraints.isAcceptable(self.calexpA))
75 self.assertTrue(constraints.isAcceptable(self.pviA))
76 self.assertFalse(constraints.isAcceptable(self.pviB))
78 # Accept PVI for instrument B but not instrument A
79 config = ConstraintsConfig({"accept": ["calexp",
80 {"instrument<B>": ["pvi"]}]})
81 constraints = Constraints(config, universe=self.universe)
83 self.assertTrue(constraints.isAcceptable(self.calexpA))
84 self.assertFalse(constraints.isAcceptable(self.pviA))
85 self.assertTrue(constraints.isAcceptable(self.pviB))
87 def testSimpleReject(self):
88 config = ConstraintsConfig({"reject": ["calexp", "ExposureF"]})
89 constraints = Constraints(config, universe=self.universe)
91 self.assertFalse(constraints.isAcceptable(self.calexpA))
92 self.assertTrue(constraints.isAcceptable(self.pviA))
94 def testAcceptReject(self):
95 # Reject everything except calexp
96 config = ConstraintsConfig({"accept": ["calexp"], "reject": ["all"]})
97 constraints = Constraints(config, universe=self.universe)
99 self.assertTrue(constraints.isAcceptable(self.calexpA))
100 self.assertFalse(constraints.isAcceptable(self.pviA))
102 # Accept everything except calexp
103 config = ConstraintsConfig({"reject": ["calexp"], "accept": ["all"]})
104 constraints = Constraints(config, universe=self.universe)
106 self.assertFalse(constraints.isAcceptable(self.calexpA))
107 self.assertTrue(constraints.isAcceptable(self.pviA))
109 # Reject pvi but explicitly accept pvi for instrument A
110 # Reject all instrument A but accept everything else
111 # The reject here is superfluous
112 config = ConstraintsConfig({"accept": [{"instrument<A>": ["pvi"]}], "reject": ["pvi"]})
113 constraints = Constraints(config, universe=self.universe)
115 self.assertFalse(constraints.isAcceptable(self.calexpA))
116 self.assertTrue(constraints.isAcceptable(self.pviA))
117 self.assertFalse(constraints.isAcceptable(self.pviB))
119 # Accept everything except pvi from other than instrument A
120 config = ConstraintsConfig({"accept": ["all", {"instrument<A>": ["pvi"]}], "reject": ["pvi"]})
121 constraints = Constraints(config, universe=self.universe)
123 self.assertTrue(constraints.isAcceptable(self.calexpA))
124 self.assertTrue(constraints.isAcceptable(self.pviA))
125 self.assertFalse(constraints.isAcceptable(self.pviB))
127 def testWildcardReject(self):
128 # Reject everything
129 config = ConstraintsConfig({"reject": ["all"]})
130 constraints = Constraints(config, universe=self.universe)
132 self.assertFalse(constraints.isAcceptable(self.calexpA))
133 self.assertFalse(constraints.isAcceptable(self.pviA))
135 # Reject all instrument A but accept everything else
136 config = ConstraintsConfig({"reject": [{"instrument<A>": ["all"]}]})
137 constraints = Constraints(config, universe=self.universe)
139 self.assertFalse(constraints.isAcceptable(self.calexpA))
140 self.assertFalse(constraints.isAcceptable(self.pviA))
141 self.assertTrue(constraints.isAcceptable(self.pviB))
143 def testWildcardAccept(self):
144 # Accept everything
145 config = ConstraintsConfig({})
146 constraints = Constraints(config, universe=self.universe)
148 self.assertTrue(constraints.isAcceptable(self.calexpA))
149 self.assertTrue(constraints.isAcceptable(self.pviA))
151 # Accept everything
152 constraints = Constraints(None, universe=self.universe)
154 self.assertTrue(constraints.isAcceptable(self.calexpA))
155 self.assertTrue(constraints.isAcceptable(self.pviA))
157 # Accept everything explicitly
158 config = ConstraintsConfig({"accept": ["all"]})
159 constraints = Constraints(config, universe=self.universe)
161 self.assertTrue(constraints.isAcceptable(self.calexpA))
162 self.assertTrue(constraints.isAcceptable(self.pviA))
164 # Accept all instrument A but reject everything else
165 config = ConstraintsConfig({"accept": [{"instrument<A>": ["all"]}]})
166 constraints = Constraints(config, universe=self.universe)
168 self.assertTrue(constraints.isAcceptable(self.calexpA))
169 self.assertTrue(constraints.isAcceptable(self.pviA))
170 self.assertFalse(constraints.isAcceptable(self.pviB))
172 def testEdgeCases(self):
173 # Accept everything and reject everything
174 config = ConstraintsConfig({"accept": ["all"], "reject": ["all"]})
175 with self.assertRaises(ValidationError):
176 Constraints(config, universe=self.universe)
179if __name__ == "__main__": 179 ↛ 180line 179 didn't jump to line 180, because the condition on line 179 was never true
180 unittest.main()