Coverage for tests/test_bpsconfig.py : 25%

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 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
23import yaml
24from lsst.daf.butler.core.config import Config
25from lsst.ctrl.bps.bps_config import BpsConfig
28TESTDIR = os.path.abspath(os.path.dirname(__file__))
31class TestBpsConfigConstructor(unittest.TestCase):
33 def setUp(self):
34 self.filename = os.path.join(TESTDIR, "data/config.yaml")
35 with open(self.filename, "r") as f:
36 self.dictionary = yaml.safe_load(f)
38 def tearDown(self):
39 pass
41 def testFromFilename(self):
42 """Test initialization from a file."""
43 config = BpsConfig(self.filename)
44 self.assertIn("foo", config)
46 def testFromDict(self):
47 """Test initialization from a dictionary."""
48 config = BpsConfig(self.dictionary)
49 self.assertIn("bar", config)
51 def testFromConfig(self):
52 """Test initialization from other Config object."""
53 c = Config(self.dictionary)
54 config = BpsConfig(c)
55 self.assertIn("baz", config)
57 def testFromBpsConfig(self):
58 """Test initialization from other BpsConfig object."""
59 c = BpsConfig(self.dictionary)
60 config = BpsConfig(c)
61 self.assertIn("foo", config)
63 def testInvalidArg(self):
64 """Test if exception is raised for an argument of unsupported type."""
65 sequence = ["wibble", "wobble", "wubble", "flob"]
66 with self.assertRaises(RuntimeError):
67 BpsConfig(sequence)
70class TestBpsConfigSearch(unittest.TestCase):
72 def setUp(self):
73 filename = os.path.join(TESTDIR, "data/config.yaml")
74 self.config = BpsConfig(filename, search_order=["baz", "bar", "foo"])
75 os.environ["GARPLY"] = "garply"
77 def tearDown(self):
78 del os.environ["GARPLY"]
80 def testSectionSearchOrder(self):
81 """Test if sections are searched in the prescribed order."""
82 key = "qux"
83 found, value = self.config.search(key)
84 self.assertEqual(found, True)
85 self.assertEqual(value, 2)
87 def testCurrentValues(self):
88 """Test if a current value overrides of the one in configuration."""
89 found, value = self.config.search("qux", opt={"curvals": {"qux": -3}})
90 self.assertEqual(found, True)
91 self.assertEqual(value, -3)
93 def testSearchobjValues(self):
94 """Test if a serachobj value overrides of the one in configuration."""
95 options = {"searchobj": {"qux": 4}}
96 found, value = self.config.search("qux", opt=options)
97 self.assertEqual(found, True)
98 self.assertEqual(value, 4)
100 def testSubsectionSearch(self):
101 options = {"curvals": {"curr_baz": "garply"}}
102 found, value = self.config.search("qux", opt=options)
103 self.assertEqual(found, True)
104 self.assertEqual(value, 3)
106 def testDefault(self):
107 """Test if a default value is properly set."""
108 found, value = self.config.search("plugh", opt={"default": 4})
109 self.assertEqual(found, True)
110 self.assertEqual(value, 4)
112 def testVariables(self):
113 """Test combinations of expandEnvVars, replaceEnvVars,
114 and replaceVars."""
115 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": False}
116 found, value = self.config.search("grault", opt=test_opt)
117 self.assertEqual(found, True)
118 self.assertEqual(value, "${GARPLY}/waldo/{qux:03}")
120 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": True}
121 found, value = self.config.search("grault", opt=test_opt)
122 self.assertEqual(found, True)
123 self.assertEqual(value, "${GARPLY}/waldo/002")
125 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": False}
126 found, value = self.config.search("grault", opt=test_opt)
127 self.assertEqual(found, True)
128 self.assertEqual(value, "<ENV:GARPLY>/waldo/{qux:03}")
130 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": True}
131 found, value = self.config.search("grault", opt=test_opt)
132 self.assertEqual(found, True)
133 self.assertEqual(value, "<ENV:GARPLY>/waldo/002")
135 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": False}
136 found, value = self.config.search("grault", opt=test_opt)
137 self.assertEqual(found, True)
138 self.assertEqual(value, "garply/waldo/{qux:03}")
140 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": True}
141 found, value = self.config.search("grault", opt=test_opt)
142 self.assertEqual(found, True)
143 self.assertEqual(value, "garply/waldo/002")
145 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": False}
146 found, value = self.config.search("grault", opt=test_opt)
147 self.assertEqual(found, True)
148 self.assertEqual(value, "garply/waldo/{qux:03}")
150 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": True}
151 found, value = self.config.search("grault", opt=test_opt)
152 self.assertEqual(found, True)
153 self.assertEqual(found, True)
154 self.assertEqual(value, "garply/waldo/002")
156 def testRequired(self):
157 """Test if exception is raised if a required setting is missing."""
158 with self.assertRaises(KeyError):
159 self.config.search("fred", opt={"required": True})
162if __name__ == "__main__": 162 ↛ 163line 162 didn't jump to line 163, because the condition on line 162 was never true
163 unittest.main()