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 

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

25from lsst.ctrl.bps.bps_config import BpsConfig 

26 

27 

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

29 

30 

31class TestBpsConfigConstructor(unittest.TestCase): 

32 

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) 

37 

38 def tearDown(self): 

39 pass 

40 

41 def testFromFilename(self): 

42 """Test initialization from a file.""" 

43 config = BpsConfig(self.filename) 

44 self.assertIn("foo", config) 

45 

46 def testFromDict(self): 

47 """Test initialization from a dictionary.""" 

48 config = BpsConfig(self.dictionary) 

49 self.assertIn("bar", config) 

50 

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) 

56 

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) 

62 

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) 

68 

69 

70class TestBpsConfigSearch(unittest.TestCase): 

71 

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" 

76 

77 def tearDown(self): 

78 del os.environ["GARPLY"] 

79 

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) 

86 

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) 

92 

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) 

99 

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) 

105 

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) 

111 

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}") 

119 

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") 

124 

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}") 

129 

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") 

134 

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}") 

139 

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") 

144 

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}") 

149 

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") 

155 

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}) 

160 

161 

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

163 unittest.main()