Coverage for tests/test_bpsconfig.py: 27%
113 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-23 10:45 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-23 10:45 +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 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 <https://www.gnu.org/licenses/>.
21import os
22import unittest
24import yaml
25from lsst.ctrl.bps import BpsConfig
26from lsst.daf.butler.core.config import Config
28TESTDIR = os.path.abspath(os.path.dirname(__file__))
31class TestBpsConfigConstructor(unittest.TestCase):
32 """Test BpsConfig construction."""
34 def setUp(self):
35 self.filename = os.path.join(TESTDIR, "data/config.yaml")
36 with open(self.filename) as f:
37 self.dictionary = yaml.safe_load(f)
39 def tearDown(self):
40 pass
42 def testFromFilename(self):
43 """Test initialization from a file."""
44 config = BpsConfig(self.filename)
45 self.assertIn("foo", config)
47 def testFromDict(self):
48 """Test initialization from a dictionary."""
49 config = BpsConfig(self.dictionary)
50 self.assertIn("bar", config)
52 def testFromConfig(self):
53 """Test initialization from other Config object."""
54 c = Config(self.dictionary)
55 config = BpsConfig(c)
56 self.assertIn("baz", config)
58 def testFromBpsConfig(self):
59 """Test initialization from other BpsConfig object."""
60 c = BpsConfig(self.dictionary)
61 config = BpsConfig(c)
62 self.assertIn("foo", config)
64 def testInvalidArg(self):
65 """Test if exception is raised for an argument of unsupported type."""
66 sequence = ["wibble", "wobble", "wubble", "flob"]
67 with self.assertRaises(RuntimeError):
68 BpsConfig(sequence)
71class TestBpsConfigGet(unittest.TestCase):
72 """Test retrieval of items from BpsConfig."""
74 def setUp(self):
75 self.config = BpsConfig({"foo": "bar"})
77 def tearDown(self):
78 pass
80 def testKeyExistsNoDefault(self):
81 """Test if the value is returned when the key is in the dictionary."""
82 self.assertEqual("bar", self.config.get("foo"))
84 def testKeyExistsDefaultProvided(self):
85 """Test if the value is returned when the key is in the dictionary."""
86 self.assertEqual("bar", self.config.get("foo", "qux"))
88 def testKeyMissingNoDefault(self):
89 """Test if the provided default is returned if the key is missing."""
90 self.assertEqual("", self.config.get("baz"))
92 def testKeyMissingDefaultProvided(self):
93 """Test if the custom default is returned if the key is missing."""
94 self.assertEqual("qux", self.config.get("baz", "qux"))
97class TestBpsConfigSearch(unittest.TestCase):
98 """Test searching of BpsConfig."""
100 def setUp(self):
101 filename = os.path.join(TESTDIR, "data/config.yaml")
102 self.config = BpsConfig(filename, search_order=["baz", "bar", "foo"])
103 os.environ["GARPLY"] = "garply"
105 def tearDown(self):
106 del os.environ["GARPLY"]
108 def testSectionSearchOrder(self):
109 """Test if sections are searched in the prescribed order."""
110 key = "qux"
111 found, value = self.config.search(key)
112 self.assertEqual(found, True)
113 self.assertEqual(value, 2)
115 def testCurrentValues(self):
116 """Test if a current value overrides of the one in configuration."""
117 found, value = self.config.search("qux", opt={"curvals": {"qux": -3}})
118 self.assertEqual(found, True)
119 self.assertEqual(value, -3)
121 def testSearchobjValues(self):
122 """Test if a serachobj value overrides of the one in configuration."""
123 options = {"searchobj": {"qux": 4}}
124 found, value = self.config.search("qux", opt=options)
125 self.assertEqual(found, True)
126 self.assertEqual(value, 4)
128 def testSubsectionSearch(self):
129 options = {"curvals": {"curr_baz": "garply"}}
130 found, value = self.config.search("qux", opt=options)
131 self.assertEqual(found, True)
132 self.assertEqual(value, 3)
134 def testDefault(self):
135 """Test if a default value is properly set."""
136 found, value = self.config.search("plugh", opt={"default": 4})
137 self.assertEqual(found, True)
138 self.assertEqual(value, 4)
140 def testVariables(self):
141 """Test combinations of expandEnvVars, replaceEnvVars,
142 and replaceVars.
143 """
144 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": False}
145 found, value = self.config.search("grault", opt=test_opt)
146 self.assertEqual(found, True)
147 self.assertEqual(value, "${GARPLY}/waldo/{qux:03}")
149 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": True}
150 found, value = self.config.search("grault", opt=test_opt)
151 self.assertEqual(found, True)
152 self.assertEqual(value, "${GARPLY}/waldo/002")
154 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": False}
155 found, value = self.config.search("grault", opt=test_opt)
156 self.assertEqual(found, True)
157 self.assertEqual(value, "<ENV:GARPLY>/waldo/{qux:03}")
159 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": True}
160 found, value = self.config.search("grault", opt=test_opt)
161 self.assertEqual(found, True)
162 self.assertEqual(value, "<ENV:GARPLY>/waldo/002")
164 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": False}
165 found, value = self.config.search("grault", opt=test_opt)
166 self.assertEqual(found, True)
167 self.assertEqual(value, "garply/waldo/{qux:03}")
169 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": True}
170 found, value = self.config.search("grault", opt=test_opt)
171 self.assertEqual(found, True)
172 self.assertEqual(value, "garply/waldo/002")
174 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": False}
175 found, value = self.config.search("grault", opt=test_opt)
176 self.assertEqual(found, True)
177 self.assertEqual(value, "garply/waldo/{qux:03}")
179 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": True}
180 found, value = self.config.search("grault", opt=test_opt)
181 self.assertEqual(found, True)
182 self.assertEqual(found, True)
183 self.assertEqual(value, "garply/waldo/002")
185 def testRequired(self):
186 """Test if exception is raised if a required setting is missing."""
187 with self.assertRaises(KeyError):
188 self.config.search("fred", opt={"required": True})
191if __name__ == "__main__": 191 ↛ 192line 191 didn't jump to line 192, because the condition on line 191 was never true
192 unittest.main()