Coverage for tests/test_bpsconfig.py : 36%

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 testReplaceOn(self):
113 """Test if environmental variables are replaced, if requested."""
114 found, value = self.config.search("grault", opt={"replaceVars": True})
115 self.assertEqual(found, True)
116 self.assertEqual(value, "garply/waldo")
118 def testReplaceOff(self):
119 """Test if environmental variables are not replaced, if requested."""
120 found, value = self.config.search("grault", opt={"replaceVars": False})
121 self.assertEqual(found, True)
122 self.assertEqual(value, "${GARPLY}/waldo")
124 def testRequired(self):
125 """Test if exception is raised if a required setting is missing."""
126 with self.assertRaises(KeyError):
127 self.config.search("fred", opt={"required": True})
130if __name__ == "__main__": 130 ↛ 131line 130 didn't jump to line 131, because the condition on line 130 was never true
131 unittest.main()