Coverage for tests/test_bpsconfig.py: 27%
113 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-04 09:56 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-04 09:56 +0000
1# This file is part of ctrl_bps.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
27import os
28import unittest
30import yaml
31from lsst.ctrl.bps import BpsConfig
32from lsst.daf.butler import Config
34TESTDIR = os.path.abspath(os.path.dirname(__file__))
37class TestBpsConfigConstructor(unittest.TestCase):
38 """Test BpsConfig construction."""
40 def setUp(self):
41 self.filename = os.path.join(TESTDIR, "data/config.yaml")
42 with open(self.filename) as f:
43 self.dictionary = yaml.safe_load(f)
45 def tearDown(self):
46 pass
48 def testFromFilename(self):
49 """Test initialization from a file."""
50 config = BpsConfig(self.filename)
51 self.assertIn("foo", config)
53 def testFromDict(self):
54 """Test initialization from a dictionary."""
55 config = BpsConfig(self.dictionary)
56 self.assertIn("bar", config)
58 def testFromConfig(self):
59 """Test initialization from other Config object."""
60 c = Config(self.dictionary)
61 config = BpsConfig(c)
62 self.assertIn("baz", config)
64 def testFromBpsConfig(self):
65 """Test initialization from other BpsConfig object."""
66 c = BpsConfig(self.dictionary)
67 config = BpsConfig(c)
68 self.assertIn("foo", config)
70 def testInvalidArg(self):
71 """Test if exception is raised for an argument of unsupported type."""
72 sequence = ["wibble", "wobble", "wubble", "flob"]
73 with self.assertRaises(RuntimeError):
74 BpsConfig(sequence)
77class TestBpsConfigGet(unittest.TestCase):
78 """Test retrieval of items from BpsConfig."""
80 def setUp(self):
81 self.config = BpsConfig({"foo": "bar"})
83 def tearDown(self):
84 pass
86 def testKeyExistsNoDefault(self):
87 """Test if the value is returned when the key is in the dictionary."""
88 self.assertEqual("bar", self.config.get("foo"))
90 def testKeyExistsDefaultProvided(self):
91 """Test if the value is returned when the key is in the dictionary."""
92 self.assertEqual("bar", self.config.get("foo", "qux"))
94 def testKeyMissingNoDefault(self):
95 """Test if the provided default is returned if the key is missing."""
96 self.assertEqual("", self.config.get("baz"))
98 def testKeyMissingDefaultProvided(self):
99 """Test if the custom default is returned if the key is missing."""
100 self.assertEqual("qux", self.config.get("baz", "qux"))
103class TestBpsConfigSearch(unittest.TestCase):
104 """Test searching of BpsConfig."""
106 def setUp(self):
107 filename = os.path.join(TESTDIR, "data/config.yaml")
108 self.config = BpsConfig(filename, search_order=["baz", "bar", "foo"])
109 os.environ["GARPLY"] = "garply"
111 def tearDown(self):
112 del os.environ["GARPLY"]
114 def testSectionSearchOrder(self):
115 """Test if sections are searched in the prescribed order."""
116 key = "qux"
117 found, value = self.config.search(key)
118 self.assertEqual(found, True)
119 self.assertEqual(value, 2)
121 def testCurrentValues(self):
122 """Test if a current value overrides of the one in configuration."""
123 found, value = self.config.search("qux", opt={"curvals": {"qux": -3}})
124 self.assertEqual(found, True)
125 self.assertEqual(value, -3)
127 def testSearchobjValues(self):
128 """Test if a serachobj value overrides of the one in configuration."""
129 options = {"searchobj": {"qux": 4}}
130 found, value = self.config.search("qux", opt=options)
131 self.assertEqual(found, True)
132 self.assertEqual(value, 4)
134 def testSubsectionSearch(self):
135 options = {"curvals": {"curr_baz": "garply"}}
136 found, value = self.config.search("qux", opt=options)
137 self.assertEqual(found, True)
138 self.assertEqual(value, 3)
140 def testDefault(self):
141 """Test if a default value is properly set."""
142 found, value = self.config.search("plugh", opt={"default": 4})
143 self.assertEqual(found, True)
144 self.assertEqual(value, 4)
146 def testVariables(self):
147 """Test combinations of expandEnvVars, replaceEnvVars,
148 and replaceVars.
149 """
150 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": False}
151 found, value = self.config.search("grault", opt=test_opt)
152 self.assertEqual(found, True)
153 self.assertEqual(value, "${GARPLY}/waldo/{qux:03}")
155 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": True}
156 found, value = self.config.search("grault", opt=test_opt)
157 self.assertEqual(found, True)
158 self.assertEqual(value, "${GARPLY}/waldo/002")
160 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": False}
161 found, value = self.config.search("grault", opt=test_opt)
162 self.assertEqual(found, True)
163 self.assertEqual(value, "<ENV:GARPLY>/waldo/{qux:03}")
165 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": True}
166 found, value = self.config.search("grault", opt=test_opt)
167 self.assertEqual(found, True)
168 self.assertEqual(value, "<ENV:GARPLY>/waldo/002")
170 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": False}
171 found, value = self.config.search("grault", opt=test_opt)
172 self.assertEqual(found, True)
173 self.assertEqual(value, "garply/waldo/{qux:03}")
175 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": True}
176 found, value = self.config.search("grault", opt=test_opt)
177 self.assertEqual(found, True)
178 self.assertEqual(value, "garply/waldo/002")
180 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": False}
181 found, value = self.config.search("grault", opt=test_opt)
182 self.assertEqual(found, True)
183 self.assertEqual(value, "garply/waldo/{qux:03}")
185 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": True}
186 found, value = self.config.search("grault", opt=test_opt)
187 self.assertEqual(found, True)
188 self.assertEqual(found, True)
189 self.assertEqual(value, "garply/waldo/002")
191 def testRequired(self):
192 """Test if exception is raised if a required setting is missing."""
193 with self.assertRaises(KeyError):
194 self.config.search("fred", opt={"required": True})
197if __name__ == "__main__": 197 ↛ 198line 197 didn't jump to line 198, because the condition on line 197 was never true
198 unittest.main()