Coverage for tests/test_bpsconfig.py: 27%

113 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-06 16:01 -0700

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 

23 

24import yaml 

25from lsst.ctrl.bps import BpsConfig 

26from lsst.daf.butler.core.config import Config 

27 

28TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

29 

30 

31class TestBpsConfigConstructor(unittest.TestCase): 

32 def setUp(self): 

33 self.filename = os.path.join(TESTDIR, "data/config.yaml") 

34 with open(self.filename, "r") as f: 

35 self.dictionary = yaml.safe_load(f) 

36 

37 def tearDown(self): 

38 pass 

39 

40 def testFromFilename(self): 

41 """Test initialization from a file.""" 

42 config = BpsConfig(self.filename) 

43 self.assertIn("foo", config) 

44 

45 def testFromDict(self): 

46 """Test initialization from a dictionary.""" 

47 config = BpsConfig(self.dictionary) 

48 self.assertIn("bar", config) 

49 

50 def testFromConfig(self): 

51 """Test initialization from other Config object.""" 

52 c = Config(self.dictionary) 

53 config = BpsConfig(c) 

54 self.assertIn("baz", config) 

55 

56 def testFromBpsConfig(self): 

57 """Test initialization from other BpsConfig object.""" 

58 c = BpsConfig(self.dictionary) 

59 config = BpsConfig(c) 

60 self.assertIn("foo", config) 

61 

62 def testInvalidArg(self): 

63 """Test if exception is raised for an argument of unsupported type.""" 

64 sequence = ["wibble", "wobble", "wubble", "flob"] 

65 with self.assertRaises(RuntimeError): 

66 BpsConfig(sequence) 

67 

68 

69class TestBpsConfigGet(unittest.TestCase): 

70 def setUp(self): 

71 self.config = BpsConfig({"foo": "bar"}) 

72 

73 def tearDown(self): 

74 pass 

75 

76 def testKeyExistsNoDefault(self): 

77 """Test if the value is returned when the key is in the dictionary.""" 

78 self.assertEqual("bar", self.config.get("foo")) 

79 

80 def testKeyExistsDefaultProvided(self): 

81 """Test if the value is returned when the key is in the dictionary.""" 

82 self.assertEqual("bar", self.config.get("foo", "qux")) 

83 

84 def testKeyMissingNoDefault(self): 

85 """Test if the provided default is returned if the key is missing.""" 

86 self.assertEqual("", self.config.get("baz")) 

87 

88 def testKeyMissingDefaultProvided(self): 

89 """Test if the custom default is returned if the key is missing.""" 

90 self.assertEqual("qux", self.config.get("baz", "qux")) 

91 

92 

93class TestBpsConfigSearch(unittest.TestCase): 

94 def setUp(self): 

95 filename = os.path.join(TESTDIR, "data/config.yaml") 

96 self.config = BpsConfig(filename, search_order=["baz", "bar", "foo"]) 

97 os.environ["GARPLY"] = "garply" 

98 

99 def tearDown(self): 

100 del os.environ["GARPLY"] 

101 

102 def testSectionSearchOrder(self): 

103 """Test if sections are searched in the prescribed order.""" 

104 key = "qux" 

105 found, value = self.config.search(key) 

106 self.assertEqual(found, True) 

107 self.assertEqual(value, 2) 

108 

109 def testCurrentValues(self): 

110 """Test if a current value overrides of the one in configuration.""" 

111 found, value = self.config.search("qux", opt={"curvals": {"qux": -3}}) 

112 self.assertEqual(found, True) 

113 self.assertEqual(value, -3) 

114 

115 def testSearchobjValues(self): 

116 """Test if a serachobj value overrides of the one in configuration.""" 

117 options = {"searchobj": {"qux": 4}} 

118 found, value = self.config.search("qux", opt=options) 

119 self.assertEqual(found, True) 

120 self.assertEqual(value, 4) 

121 

122 def testSubsectionSearch(self): 

123 options = {"curvals": {"curr_baz": "garply"}} 

124 found, value = self.config.search("qux", opt=options) 

125 self.assertEqual(found, True) 

126 self.assertEqual(value, 3) 

127 

128 def testDefault(self): 

129 """Test if a default value is properly set.""" 

130 found, value = self.config.search("plugh", opt={"default": 4}) 

131 self.assertEqual(found, True) 

132 self.assertEqual(value, 4) 

133 

134 def testVariables(self): 

135 """Test combinations of expandEnvVars, replaceEnvVars, 

136 and replaceVars.""" 

137 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": False} 

138 found, value = self.config.search("grault", opt=test_opt) 

139 self.assertEqual(found, True) 

140 self.assertEqual(value, "${GARPLY}/waldo/{qux:03}") 

141 

142 test_opt = {"expandEnvVars": False, "replaceEnvVars": False, "replaceVars": True} 

143 found, value = self.config.search("grault", opt=test_opt) 

144 self.assertEqual(found, True) 

145 self.assertEqual(value, "${GARPLY}/waldo/002") 

146 

147 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": False} 

148 found, value = self.config.search("grault", opt=test_opt) 

149 self.assertEqual(found, True) 

150 self.assertEqual(value, "<ENV:GARPLY>/waldo/{qux:03}") 

151 

152 test_opt = {"expandEnvVars": False, "replaceEnvVars": True, "replaceVars": True} 

153 found, value = self.config.search("grault", opt=test_opt) 

154 self.assertEqual(found, True) 

155 self.assertEqual(value, "<ENV:GARPLY>/waldo/002") 

156 

157 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": False} 

158 found, value = self.config.search("grault", opt=test_opt) 

159 self.assertEqual(found, True) 

160 self.assertEqual(value, "garply/waldo/{qux:03}") 

161 

162 test_opt = {"expandEnvVars": True, "replaceEnvVars": False, "replaceVars": True} 

163 found, value = self.config.search("grault", opt=test_opt) 

164 self.assertEqual(found, True) 

165 self.assertEqual(value, "garply/waldo/002") 

166 

167 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": False} 

168 found, value = self.config.search("grault", opt=test_opt) 

169 self.assertEqual(found, True) 

170 self.assertEqual(value, "garply/waldo/{qux:03}") 

171 

172 test_opt = {"expandEnvVars": True, "replaceEnvVars": True, "replaceVars": True} 

173 found, value = self.config.search("grault", opt=test_opt) 

174 self.assertEqual(found, True) 

175 self.assertEqual(found, True) 

176 self.assertEqual(value, "garply/waldo/002") 

177 

178 def testRequired(self): 

179 """Test if exception is raised if a required setting is missing.""" 

180 with self.assertRaises(KeyError): 

181 self.config.search("fred", opt={"required": True}) 

182 

183 

184if __name__ == "__main__": 184 ↛ 185line 184 didn't jump to line 185, because the condition on line 184 was never true

185 unittest.main()