Hide keyboard shortcuts

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 

24 

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

26from lsst.ctrl.bps import BpsConfig 

27 

28 

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

30 

31 

32class TestBpsConfigConstructor(unittest.TestCase): 

33 

34 def setUp(self): 

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

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

37 self.dictionary = yaml.safe_load(f) 

38 

39 def tearDown(self): 

40 pass 

41 

42 def testFromFilename(self): 

43 """Test initialization from a file.""" 

44 config = BpsConfig(self.filename) 

45 self.assertIn("foo", config) 

46 

47 def testFromDict(self): 

48 """Test initialization from a dictionary.""" 

49 config = BpsConfig(self.dictionary) 

50 self.assertIn("bar", config) 

51 

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) 

57 

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) 

63 

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) 

69 

70 

71class TestBpsConfigSearch(unittest.TestCase): 

72 

73 def setUp(self): 

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

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

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

77 

78 def tearDown(self): 

79 del os.environ["GARPLY"] 

80 

81 def testSectionSearchOrder(self): 

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

83 key = "qux" 

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

85 self.assertEqual(found, True) 

86 self.assertEqual(value, 2) 

87 

88 def testCurrentValues(self): 

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

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

91 self.assertEqual(found, True) 

92 self.assertEqual(value, -3) 

93 

94 def testSearchobjValues(self): 

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

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

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

98 self.assertEqual(found, True) 

99 self.assertEqual(value, 4) 

100 

101 def testSubsectionSearch(self): 

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

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

104 self.assertEqual(found, True) 

105 self.assertEqual(value, 3) 

106 

107 def testDefault(self): 

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

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

110 self.assertEqual(found, True) 

111 self.assertEqual(value, 4) 

112 

113 def testVariables(self): 

114 """Test combinations of expandEnvVars, replaceEnvVars, 

115 and replaceVars.""" 

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

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

118 self.assertEqual(found, True) 

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

120 

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

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

123 self.assertEqual(found, True) 

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

125 

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

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

128 self.assertEqual(found, True) 

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

130 

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

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

133 self.assertEqual(found, True) 

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

135 

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

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

138 self.assertEqual(found, True) 

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

140 

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

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

143 self.assertEqual(found, True) 

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

145 

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

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

148 self.assertEqual(found, True) 

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

150 

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

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

153 self.assertEqual(found, True) 

154 self.assertEqual(found, True) 

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

156 

157 def testRequired(self): 

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

159 with self.assertRaises(KeyError): 

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

161 

162 

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

164 unittest.main()